Spaces:
Build error
Build error
File size: 8,853 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 |
package com.dalab.discovery.common.model;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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.enums.CloudProvider;
import com.dalab.discovery.crawler.model.gcp.ComputeResource;
import com.dalab.discovery.crawler.model.gcp.GcpResource;
class CloudResourceIntegrationTest {
private CloudHierarchyRegistry registry;
@Mock
private CloudHierarchyProperties mockProperties;
// Test data
private List<ProviderConfig> mockProviders;
private ResourceType computeInstanceType;
private ResourceType computeDiskType;
@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();
// Get the resource types from the registry
computeInstanceType = registry.getResourceType("gcp_compute_instance");
computeDiskType = registry.getResourceType("gcp_compute_disk");
assertNotNull(computeInstanceType, "Compute instance type should be available in registry");
assertNotNull(computeDiskType, "Compute disk type should be available in registry");
}
@Test
void testCreateGCPResourceWithResourceType() {
// Create a GCP resource with our resource type from registry
GcpResource resource = new GcpResource(computeInstanceType, "instance-1", "Test Instance");
// Verify that the resource was created with the correct resource type
assertEquals(computeInstanceType, resource.getResourceType());
assertEquals("instance-1", resource.getResourceId());
assertEquals("Test Instance", resource.getName());
// Verify the provider is accessible through the resource type
assertEquals(CloudProvider.GCP, resource.getResourceType().service().provider());
}
@Test
void testCreateComputeResourceWithResourceType() {
// Create a compute resource with our resource type from registry
ComputeResource computeResource = new ComputeResource(computeInstanceType, "instance-1", "Test Instance");
// Verify the resource was created with the correct resource type
assertEquals(computeInstanceType, computeResource.getResourceType());
assertEquals("instance-1", computeResource.getResourceId());
assertEquals("Test Instance", computeResource.getName());
// Set some compute-specific properties
computeResource.setMachineType("n1-standard-1");
computeResource.setCpuCount(1);
computeResource.setMemoryMb(3840);
// Verify the properties were set
assertEquals("n1-standard-1", computeResource.getMachineType());
assertEquals(1, computeResource.getCpuCount());
assertEquals(3840, computeResource.getMemoryMb());
// Also verify the provider info is accessible
assertEquals(CloudProvider.GCP, computeResource.getResourceType().service().provider());
}
@Test
void testResourceTags() {
// Create a resource with resource type from registry
GcpResource resource = new GcpResource(computeInstanceType, "instance-1", "Test Instance");
// Add tags using the Map interface
Map<String, String> tags = resource.getTags();
tags.put("environment", "production");
tags.put("owner", "data-team");
// Verify tags
assertTrue(tags.containsKey("environment"));
assertTrue(tags.containsKey("owner"));
assertEquals("production", tags.get("environment"));
assertEquals("data-team", tags.get("owner"));
assertEquals(2, tags.size());
}
@Test
void testResourceProperties() {
// Create a resource with resource type from registry
GcpResource resource = new GcpResource(computeInstanceType, "instance-1", "Test Instance");
// Add properties using the Map interface
Map<String, Object> properties = resource.getProperties();
properties.put("ip_address", "10.0.0.1");
properties.put("boot_disk_size_gb", 10);
// Verify properties
assertTrue(properties.containsKey("ip_address"));
assertTrue(properties.containsKey("boot_disk_size_gb"));
assertEquals("10.0.0.1", properties.get("ip_address"));
assertEquals(10, properties.get("boot_disk_size_gb"));
assertEquals(2, properties.size());
}
@Test
void testResourceMetadata() {
// Create a resource with resource type from registry
GcpResource resource = new GcpResource(computeInstanceType, "instance-1", "Test Instance");
// Set metadata
resource.setRegion("us-central1");
resource.setZone("us-central1-a");
resource.setProjectId("test-project");
resource.setCreatedAt(Instant.parse("2023-01-01T00:00:00Z"));
resource.setLastDiscoveredAt(Instant.now());
// Verify metadata
assertEquals("us-central1", resource.getRegion());
assertEquals("us-central1-a", resource.getZone());
assertEquals("test-project", resource.getProjectId());
assertEquals(Instant.parse("2023-01-01T00:00:00Z"), resource.getCreatedAt());
assertNotNull(resource.getLastDiscoveredAt());
}
@Test
void testValidationForCorrectResourceType() {
// Attempting to create a GCP resource with a GCP resource type should work
assertDoesNotThrow(() -> {
new GcpResource(computeInstanceType, "instance-1", "Test Instance");
});
}
@Test
void testValidationForIncorrectResourceType() {
// Get an AWS resource type from the registry
ResourceType awsEc2InstanceType = registry.getResourceType("aws_ec2_instance");
assertNotNull(awsEc2InstanceType, "AWS EC2 instance type should be available in registry");
// Attempting to create a GCP resource with an AWS resource type should throw
assertThrows(IllegalArgumentException.class, () -> {
new GcpResource(awsEc2InstanceType, "instance-1", "Test Instance");
});
}
@Test
void testCloudProviderAccessThroughResourceType() {
// Create a resource with resource type from registry
GcpResource resource = new GcpResource(computeInstanceType, "instance-1", "Test Instance");
// Access cloud provider through resource type
CloudProvider provider = resource.getResourceType().service().provider();
// Verify it's the correct provider
assertEquals(CloudProvider.GCP, provider);
assertEquals("Google Cloud Platform", provider.getDisplayName());
}
/**
* 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 computeInstanceTypeConfig = new ResourceTypeConfig();
computeInstanceTypeConfig.setId("gcp_compute_instance");
computeInstanceTypeConfig.setDisplayName("Compute Instance");
gcpComputeTypes.add(computeInstanceTypeConfig);
ResourceTypeConfig computeDiskTypeConfig = new ResourceTypeConfig();
computeDiskTypeConfig.setId("gcp_compute_disk");
computeDiskTypeConfig.setDisplayName("Compute Disk");
gcpComputeTypes.add(computeDiskTypeConfig);
gcpComputeService.setResourceTypes(gcpComputeTypes);
gcpServices.add(gcpComputeService);
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 ec2InstanceTypeConfig = new ResourceTypeConfig();
ec2InstanceTypeConfig.setId("aws_ec2_instance");
ec2InstanceTypeConfig.setDisplayName("EC2 Instance");
awsEc2Types.add(ec2InstanceTypeConfig);
awsEc2Service.setResourceTypes(awsEc2Types);
awsServices.add(awsEc2Service);
awsConfig.setServices(awsServices);
mockProviders.add(awsConfig);
}
} |