File size: 9,766 Bytes
442299c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.dalab.discovery.domain.model;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.dalab.discovery.catalog.persistence.CloudHierarchyRegistry;
import com.dalab.discovery.common.config.CloudHierarchyProperties;
import com.dalab.discovery.common.config.CloudHierarchyProperties.ProviderConfig;
import com.dalab.discovery.common.config.CloudHierarchyProperties.ResourceTypeConfig;
import com.dalab.discovery.common.config.CloudHierarchyProperties.ServiceConfig;
import com.dalab.discovery.common.model.CloudService;
import com.dalab.discovery.common.model.ResourceType;
import com.dalab.discovery.common.model.enums.CloudProvider;
import com.dalab.discovery.crawler.model.gcp.ComputeResource;
import com.dalab.discovery.crawler.model.gcp.GcpResource;

/**
 * This test demonstrates how to use CloudHierarchyRegistry to load ResourceType
 * and
 * CloudService instances from configuration, rather than manually creating
 * them.
 */
class CloudHierarchyRegistryLoadingTest {

	private CloudHierarchyRegistry registry;

	@Mock
	private CloudHierarchyProperties mockProperties;

	// Test data
	private List<ProviderConfig> mockProviders;

	@BeforeEach
	void setUp() {
		MockitoAnnotations.openMocks(this);
		setupMockConfiguration();

		// Create and initialize the registry with our mock configuration
		registry = new CloudHierarchyRegistry(mockProperties);
		when(mockProperties.getProviders()).thenReturn(mockProviders);
		registry.initialize();
	}

	@Test
	void testLoadingAndUsingGCPComputeResourceType() {
		// Get resource type from registry instead of manually creating it
		ResourceType computeInstanceType = registry.getResourceType("gcp_compute_instance");

		// Verify the resource type was loaded correctly
		assertNotNull(computeInstanceType);
		assertEquals("gcp_compute_instance", computeInstanceType.id());
		assertEquals("Compute Instance", computeInstanceType.displayName());

		// Verify the service and provider hierarchy
		CloudService service = computeInstanceType.service();
		assertNotNull(service);
		assertEquals("gcp_compute", service.id());
		assertEquals("Compute Engine", service.displayName());
		assertEquals(CloudProvider.GCP, service.provider());

		// Now use the loaded resource type to create a resource
		GcpResource resource = new GcpResource(computeInstanceType, "instance-1", "Test Instance");

		// Verify the resource was created with the correct type
		assertEquals(computeInstanceType, resource.getResourceType());
		assertEquals("instance-1", resource.getResourceId());
		assertEquals("Test Instance", resource.getName());
		assertEquals(CloudProvider.GCP, resource.getResourceType().service().provider());
	}

	@Test
	void testLoadingAndUsingAWSS3ResourceType() {
		// Get resource type from registry
		ResourceType s3BucketType = registry.getResourceType("aws_s3_bucket");

		// Verify the resource type was loaded correctly
		assertNotNull(s3BucketType);
		assertEquals("aws_s3_bucket", s3BucketType.id());
		assertEquals("S3 Bucket", s3BucketType.displayName());

		// Verify the service and provider hierarchy
		CloudService service = s3BucketType.service();
		assertNotNull(service);
		assertEquals("aws_s3", service.id());
		assertEquals("Amazon S3", service.displayName());
		assertEquals(CloudProvider.AWS, service.provider());
	}

	@Test
	void testCreatingComputeResourceWithRegistryType() {
		// Get resource type from registry
		ResourceType computeInstanceType = registry.getResourceType("gcp_compute_instance");
		assertNotNull(computeInstanceType);

		// Create a compute-specific resource using the registry type
		ComputeResource computeResource = new ComputeResource(computeInstanceType, "instance-1", "Test Instance");

		// Set compute-specific properties
		computeResource.setMachineType("n1-standard-1");
		computeResource.setCpuCount(2);
		computeResource.setMemoryMb(4096);

		// Verify the properties
		assertEquals(computeInstanceType, computeResource.getResourceType());
		assertEquals("n1-standard-1", computeResource.getMachineType());
		assertEquals(2, computeResource.getCpuCount());
		assertEquals(4096, computeResource.getMemoryMb());

		// Also verify resource ID and name
		assertEquals("instance-1", computeResource.getResourceId());
		assertEquals("Test Instance", computeResource.getName());
	}

	@Test
	void testAccessingMultipleResourceTypesForService() {
		// Get the cloud service for BigQuery
		CloudService bigQueryService = registry.getService("gcp_bigquery");
		assertNotNull(bigQueryService);

		// Get all resource types for that service
		List<ResourceType> bigQueryTypes = registry.getResourceTypes(bigQueryService);

		// Verify we have the expected types
		assertEquals(2, bigQueryTypes.size());

		// Verify each type
		ResourceType datasetType = null;
		ResourceType tableType = null;

		for (ResourceType type : bigQueryTypes) {
			if (type.id().equals("gcp_bigquery_dataset")) {
				datasetType = type;
			} else if (type.id().equals("gcp_bigquery_table")) {
				tableType = type;
			}
		}

		assertNotNull(datasetType, "BigQuery Dataset type should be found");
		assertNotNull(tableType, "BigQuery Table type should be found");

		// Verify their properties
		assertEquals("BigQuery Dataset", datasetType.displayName());
		assertEquals("BigQuery Table", tableType.displayName());

		// Verify both types point to the same service
		assertSame(bigQueryService, datasetType.service());
		assertSame(bigQueryService, tableType.service());
	}

	@Test
	void testFindingAllResourceTypesForProvider() {
		// Get all GCP resource types
		List<ResourceType> gcpTypes = registry.getResourceTypes(CloudProvider.GCP);

		// There should be 4 types for GCP (2 for Compute, 2 for BigQuery)
		assertEquals(4, gcpTypes.size());

		// Verify we have the expected types by ID
		assertTrue(gcpTypes.stream().anyMatch(rt -> rt.id().equals("gcp_compute_instance")));
		assertTrue(gcpTypes.stream().anyMatch(rt -> rt.id().equals("gcp_compute_disk")));
		assertTrue(gcpTypes.stream().anyMatch(rt -> rt.id().equals("gcp_bigquery_dataset")));
		assertTrue(gcpTypes.stream().anyMatch(rt -> rt.id().equals("gcp_bigquery_table")));

		// Also verify all have the same provider
		for (ResourceType type : gcpTypes) {
			assertEquals(CloudProvider.GCP, type.service().provider());
		}
	}

	/**
	 * Sets up the mock configuration to mimic what would be in application.yml
	 */
	private void setupMockConfiguration() {
		mockProviders = new ArrayList<>();

		// ======== GCP Provider Configuration =========
		ProviderConfig gcpConfig = new ProviderConfig();
		gcpConfig.setProvider(CloudProvider.GCP);

		List<ServiceConfig> gcpServices = new ArrayList<>();

		// --- GCP Compute Service ---
		ServiceConfig gcpComputeService = new ServiceConfig();
		gcpComputeService.setId("gcp_compute");
		gcpComputeService.setDisplayName("Compute Engine");

		List<ResourceTypeConfig> gcpComputeTypes = new ArrayList<>();

		ResourceTypeConfig computeInstanceType = new ResourceTypeConfig();
		computeInstanceType.setId("gcp_compute_instance");
		computeInstanceType.setDisplayName("Compute Instance");
		gcpComputeTypes.add(computeInstanceType);

		ResourceTypeConfig computeDiskType = new ResourceTypeConfig();
		computeDiskType.setId("gcp_compute_disk");
		computeDiskType.setDisplayName("Compute Disk");
		gcpComputeTypes.add(computeDiskType);

		gcpComputeService.setResourceTypes(gcpComputeTypes);
		gcpServices.add(gcpComputeService);

		// --- GCP BigQuery Service ---
		ServiceConfig gcpBigQueryService = new ServiceConfig();
		gcpBigQueryService.setId("gcp_bigquery");
		gcpBigQueryService.setDisplayName("BigQuery");

		List<ResourceTypeConfig> gcpBigQueryTypes = new ArrayList<>();

		ResourceTypeConfig bigQueryDatasetType = new ResourceTypeConfig();
		bigQueryDatasetType.setId("gcp_bigquery_dataset");
		bigQueryDatasetType.setDisplayName("BigQuery Dataset");
		gcpBigQueryTypes.add(bigQueryDatasetType);

		ResourceTypeConfig bigQueryTableType = new ResourceTypeConfig();
		bigQueryTableType.setId("gcp_bigquery_table");
		bigQueryTableType.setDisplayName("BigQuery Table");
		gcpBigQueryTypes.add(bigQueryTableType);

		gcpBigQueryService.setResourceTypes(gcpBigQueryTypes);
		gcpServices.add(gcpBigQueryService);

		gcpConfig.setServices(gcpServices);
		mockProviders.add(gcpConfig);

		// ======== AWS Provider Configuration =========
		ProviderConfig awsConfig = new ProviderConfig();
		awsConfig.setProvider(CloudProvider.AWS);

		List<ServiceConfig> awsServices = new ArrayList<>();

		// --- AWS EC2 Service ---
		ServiceConfig awsEc2Service = new ServiceConfig();
		awsEc2Service.setId("aws_ec2");
		awsEc2Service.setDisplayName("Amazon EC2");

		List<ResourceTypeConfig> awsEc2Types = new ArrayList<>();

		ResourceTypeConfig ec2InstanceType = new ResourceTypeConfig();
		ec2InstanceType.setId("aws_ec2_instance");
		ec2InstanceType.setDisplayName("EC2 Instance");
		awsEc2Types.add(ec2InstanceType);

		awsEc2Service.setResourceTypes(awsEc2Types);
		awsServices.add(awsEc2Service);

		// --- AWS S3 Service ---
		ServiceConfig awsS3Service = new ServiceConfig();
		awsS3Service.setId("aws_s3");
		awsS3Service.setDisplayName("Amazon S3");

		List<ResourceTypeConfig> awsS3Types = new ArrayList<>();

		ResourceTypeConfig s3BucketType = new ResourceTypeConfig();
		s3BucketType.setId("aws_s3_bucket");
		s3BucketType.setDisplayName("S3 Bucket");
		awsS3Types.add(s3BucketType);

		awsS3Service.setResourceTypes(awsS3Types);
		awsServices.add(awsS3Service);

		awsConfig.setServices(awsServices);
		mockProviders.add(awsConfig);
	}
}