Search in sources :

Example 11 with AwsNetwork

use of com.sequenceiq.environment.network.dao.domain.AwsNetwork in project cloudbreak by hortonworks.

the class EnvironmentNetworkServiceTest method testCreateNetworkShouldReturnWithANewNetwork.

@Test
void testCreateNetworkShouldReturnWithANewNetwork() {
    EnvironmentDto environmentDto = EnvironmentDto.builder().withCloudPlatform(CLOUD_PLATFORM).withCreator(USER_CRN).build();
    BaseNetwork baseNetwork = new AwsNetwork();
    NetworkCreationRequest networkCreationRequest = new NetworkCreationRequest.Builder().build();
    CreatedCloudNetwork createdCloudNetwork = new CreatedCloudNetwork();
    AwsEnvironmentNetworkConverter networkConverter = Mockito.mock(AwsEnvironmentNetworkConverter.class);
    when(cloudConnector.networkConnector()).thenReturn(networkConnector);
    when(networkCreationRequestFactory.create(environmentDto)).thenReturn(networkCreationRequest);
    when(networkConnector.createNetworkWithSubnets(networkCreationRequest)).thenReturn(createdCloudNetwork);
    when(environmentNetworkConverterMap.get(CloudPlatform.valueOf(CLOUD_PLATFORM))).thenReturn(networkConverter);
    when(networkConverter.setCreatedCloudNetwork(baseNetwork, createdCloudNetwork)).thenReturn(baseNetwork);
    BaseNetwork actual = underTest.createCloudNetwork(environmentDto, baseNetwork);
    verify(cloudConnector).networkConnector();
    verify(cloudPlatformConnectors).get(any(CloudPlatformVariant.class));
    verify(networkCreationRequestFactory).create(environmentDto);
    verify(networkConnector).createNetworkWithSubnets(networkCreationRequest);
    verify(environmentNetworkConverterMap).get(CloudPlatform.valueOf(CLOUD_PLATFORM));
    verify(networkConverter).setCreatedCloudNetwork(baseNetwork, createdCloudNetwork);
    assertEquals(baseNetwork, actual);
}
Also used : AwsEnvironmentNetworkConverter(com.sequenceiq.environment.network.v1.converter.AwsEnvironmentNetworkConverter) BaseNetwork(com.sequenceiq.environment.network.dao.domain.BaseNetwork) EnvironmentDto(com.sequenceiq.environment.environment.dto.EnvironmentDto) AwsNetwork(com.sequenceiq.environment.network.dao.domain.AwsNetwork) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CloudPlatformVariant(com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with AwsNetwork

use of com.sequenceiq.environment.network.dao.domain.AwsNetwork in project cloudbreak by hortonworks.

the class NetworkServiceTest method testMergeNetworkDtoWithNetworkIfNetworkCreateNew.

// TODO: 2020. 01. 31. move these tests to the new CloudNetworkServiceTest class
/*@Test
    public void testRetrieveSubnetMetadataIfNetworkNull() {
        Map<String, CloudSubnet> actual = underTest.retrieveSubnetMetadata(null, null);
        Assertions.assertTrue(actual.isEmpty());
    }

    @Test
    public void testRetrieveSubnetMetadataWhenAwsAndNoExistedSubnetOnProvider() {
        Environment environment = new Environment();
        environment.setCloudPlatform("AWS");
        Region region = new Region();
        region.setName("region1");
        environment.setRegions(Set.of(region));
        NetworkDto networkDto = NetworkDto.builder().withSubnetMetas(Map.of("sub", new CloudSubnet())).build();
        when(cloudNetworkService.getCloudNetworks(any(PlatformResourceRequest.class))).thenReturn(new CloudNetworks(Map.of("region1", Set.of())));
        Map<String, CloudSubnet> actual = underTest.retrieveSubnetMetadata(environment, networkDto);
        Assertions.assertTrue(actual.isEmpty());
    }

    @Test
    public void testRetrieveSubnetMetadataWhenAwsAndHasSubnetOnProviderAndMatch() {
        Environment environment = new Environment();
        environment.setCloudPlatform("AWS");
        Region region = new Region();
        region.setName("region1");
        environment.setRegions(Set.of(region));
        CloudSubnet editSubnet = new CloudSubnet();
        NetworkDto networkDto = NetworkDto.builder().withSubnetMetas(Map.of("sub", editSubnet)).build();
        CloudSubnet providerSubnet = new CloudSubnet();
        providerSubnet.setId("sub");
        CloudNetwork cloudNetwork = new CloudNetwork("network", "network", Set.of(providerSubnet), Collections.emptyMap());
        when(cloudNetworkService.getCloudNetworks(any(PlatformResourceRequest.class))).thenReturn(new CloudNetworks(Map.of("region1",
                Set.of(cloudNetwork))));
        Map<String, CloudSubnet> actual = underTest.retrieveSubnetMetadata(environment, networkDto);
        Assertions.assertEquals(1, actual.size());
        Assertions.assertEquals(actual.get("sub"), providerSubnet);
    }

    @Test
    public void testRetrieveSubnetMetadataWhenAwsAndHasSubnetOnProviderAndNotMatch() {
        Environment environment = new Environment();
        environment.setCloudPlatform("AWS");
        Region region = new Region();
        region.setName("region1");
        environment.setRegions(Set.of(region));
        CloudSubnet editSubnet = new CloudSubnet();
        NetworkDto networkDto = NetworkDto.builder().withSubnetMetas(Map.of("sub", editSubnet)).build();
        CloudSubnet providerSubnet = new CloudSubnet();
        providerSubnet.setId("diff-sub");
        CloudNetwork cloudNetwork = new CloudNetwork("network", "network", Set.of(providerSubnet), Collections.emptyMap());
        when(cloudNetworkService.getCloudNetworks(any(PlatformResourceRequest.class))).thenReturn(new CloudNetworks(Map.of("region1",
                Set.of(cloudNetwork))));
        Map<String, CloudSubnet> actual = underTest.retrieveSubnetMetadata(environment, networkDto);
        Assertions.assertEquals(0, actual.size());
    }*/
@ParameterizedTest
@EnumSource(CloudPlatform.class)
public void testMergeNetworkDtoWithNetworkIfNetworkCreateNew(CloudPlatform cloudPlatform) {
    AwsNetwork baseNetwork = new AwsNetwork();
    baseNetwork.setSubnetMetas(Collections.emptyMap());
    baseNetwork.setRegistrationType(RegistrationType.CREATE_NEW);
    Environment environment = new Environment();
    environment.setCloudPlatform(cloudPlatform.name());
    environment.setNetwork(baseNetwork);
    NetworkDto networkDto = NetworkDto.builder().withRegistrationType(RegistrationType.CREATE_NEW).build();
    EnvironmentEditDto environmentEditDto = EnvironmentEditDto.builder().withNetwork(networkDto).build();
    when(environmentNetworkConverterMap.get(cloudPlatform)).thenReturn(environmentNetworkConverter);
    when(environmentNetworkConverter.convertToDto(baseNetwork)).thenReturn(networkDto);
    when(networkCreationValidator.validateNetworkEdit(eq(environment), any(NetworkDto.class))).thenReturn(new ValidationResult.ValidationResultBuilder());
    BadRequestException exception = Assertions.assertThrows(BadRequestException.class, () -> underTest.validate(baseNetwork, environmentEditDto, environment));
    String baseMessage = "Subnets of this environment could not be modified, because its network has been created by Cloudera. " + "You need to re-install the environment into an existing VPC/VNet.";
    String providerSpecificLink = "";
    switch(cloudPlatform) {
        case AWS:
            providerSpecificLink = " Refer to Cloudera documentation at " + "https://docs.cloudera.com/management-console/cloud/environments/topics/mc-subnet-adding-azure.html for more information.";
            break;
        case AZURE:
            providerSpecificLink = " Refer to Cloudera documentation at " + "https://docs.cloudera.com/management-console/cloud/environments-azure/topics/mc-subnet-adding-azure.html for more information.";
            break;
        default:
            break;
    }
    Assertions.assertEquals(String.format("%s%s", baseMessage, providerSpecificLink), exception.getMessage());
}
Also used : NetworkDto(com.sequenceiq.environment.network.dto.NetworkDto) AwsNetwork(com.sequenceiq.environment.network.dao.domain.AwsNetwork) Environment(com.sequenceiq.environment.environment.domain.Environment) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) EnvironmentEditDto(com.sequenceiq.environment.environment.dto.EnvironmentEditDto) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with AwsNetwork

use of com.sequenceiq.environment.network.dao.domain.AwsNetwork in project cloudbreak by hortonworks.

the class AwsEnvironmentNetworkConverterTest method testConvertToDtoShouldConvertABaseNetworkIntoANetworkDto.

@Test
void testConvertToDtoShouldConvertABaseNetworkIntoANetworkDto() {
    AwsNetwork awsNetwork = createAwsNetwork();
    NetworkDto actual = underTest.convertToDto(awsNetwork);
    assertEquals(awsNetwork.getId(), actual.getId());
    assertEquals(awsNetwork.getName(), actual.getNetworkName());
    assertEquals(SUBNET_IDS, actual.getSubnetIds());
    assertTrue(actual.getSubnetMetas().containsKey(SUBNET_1));
    assertTrue(actual.getSubnetMetas().containsKey(SUBNET_2));
    assertTrue(actual.getSubnetMetas().containsKey(SUBNET_3));
    assertNull(actual.getPublicEndpointAccessGateway());
    assertEquals(0, actual.getEndpointGatewaySubnetIds().size());
    assertEquals(awsNetwork.getNetworkCidr(), actual.getNetworkCidr());
    assertEquals(awsNetwork.getResourceCrn(), actual.getResourceCrn());
    assertEquals(awsNetwork.getVpcId(), actual.getAws().getVpcId());
}
Also used : NetworkDto(com.sequenceiq.environment.network.dto.NetworkDto) AwsNetwork(com.sequenceiq.environment.network.dao.domain.AwsNetwork) Test(org.junit.jupiter.api.Test)

Example 14 with AwsNetwork

use of com.sequenceiq.environment.network.dao.domain.AwsNetwork in project cloudbreak by hortonworks.

the class AwsEnvironmentNetworkConverterTest method testSetProviderSpecificNetworkShouldPopulateTheExistingNetworkWithTheNewNetworkData.

@Test
void testSetProviderSpecificNetworkShouldPopulateTheExistingNetworkWithTheNewNetworkData() {
    BaseNetwork awsNetwork = new AwsNetwork();
    Set<CreatedSubnet> createdSubnets = createCreatedSubnets();
    CreatedCloudNetwork createdCloudNetwork = new CreatedCloudNetwork("network-1", VPC_ID, createdSubnets);
    AwsNetwork actual = (AwsNetwork) underTest.setCreatedCloudNetwork(awsNetwork, createdCloudNetwork);
    assertEquals(createdCloudNetwork.getStackName(), actual.getName());
    assertEquals(VPC_ID, actual.getVpcId());
    assertTrue(SUBNET_IDS.containsAll(actual.getSubnetMetas().keySet()));
    assertEquals(SUBNET_1, awsNetwork.getSubnetMetas().get(SUBNET_1).getId());
    assertEquals(SUBNET_1, awsNetwork.getSubnetMetas().get(SUBNET_1).getName());
    assertEquals(AZ_1, awsNetwork.getSubnetMetas().get(SUBNET_1).getAvailabilityZone());
    assertEquals(SUBNET_CIDR_1, awsNetwork.getSubnetMetas().get(SUBNET_1).getCidr());
    assertFalse(awsNetwork.getSubnetMetas().get(SUBNET_1).isPrivateSubnet());
    assertEquals(SUBNET_2, awsNetwork.getSubnetMetas().get(SUBNET_2).getId());
    assertEquals(SUBNET_2, awsNetwork.getSubnetMetas().get(SUBNET_2).getName());
    assertEquals(AZ_2, awsNetwork.getSubnetMetas().get(SUBNET_2).getAvailabilityZone());
    assertEquals(SUBNET_CIDR_2, awsNetwork.getSubnetMetas().get(SUBNET_2).getCidr());
    assertFalse(awsNetwork.getSubnetMetas().get(SUBNET_2).isPrivateSubnet());
    assertEquals(SUBNET_3, awsNetwork.getSubnetMetas().get(SUBNET_3).getId());
    assertEquals(SUBNET_3, awsNetwork.getSubnetMetas().get(SUBNET_3).getName());
    assertEquals(AZ_3, awsNetwork.getSubnetMetas().get(SUBNET_3).getAvailabilityZone());
    assertEquals(SUBNET_CIDR_3, awsNetwork.getSubnetMetas().get(SUBNET_3).getCidr());
    assertFalse(awsNetwork.getSubnetMetas().get(SUBNET_3).isPrivateSubnet());
}
Also used : BaseNetwork(com.sequenceiq.environment.network.dao.domain.BaseNetwork) AwsNetwork(com.sequenceiq.environment.network.dao.domain.AwsNetwork) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) Test(org.junit.jupiter.api.Test)

Example 15 with AwsNetwork

use of com.sequenceiq.environment.network.dao.domain.AwsNetwork in project cloudbreak by hortonworks.

the class AwsEnvironmentNetworkConverterTest method testConvertToDtoShouldConvertABaseNetworkWithEndpointSubnetsIntoANetworkDto.

@Test
void testConvertToDtoShouldConvertABaseNetworkWithEndpointSubnetsIntoANetworkDto() {
    AwsNetwork awsNetwork = createAwsNetwork();
    awsNetwork.setPublicEndpointAccessGateway(PublicEndpointAccessGateway.ENABLED);
    awsNetwork.setEndpointGatewaySubnetMetas(createEndpointSubnetMetas());
    NetworkDto actual = underTest.convertToDto(awsNetwork);
    assertEquals(awsNetwork.getId(), actual.getId());
    assertEquals(awsNetwork.getName(), actual.getNetworkName());
    assertEquals(SUBNET_IDS, actual.getSubnetIds());
    assertTrue(actual.getSubnetMetas().containsKey(SUBNET_1));
    assertTrue(actual.getSubnetMetas().containsKey(SUBNET_2));
    assertTrue(actual.getSubnetMetas().containsKey(SUBNET_3));
    assertEquals(PublicEndpointAccessGateway.ENABLED, actual.getPublicEndpointAccessGateway());
    assertEquals(PUBLIC_SUBNET_IDS, actual.getEndpointGatewaySubnetIds());
    assertTrue(actual.getEndpointGatewaySubnetMetas().containsKey(PUBLIC_SUBNET_1));
    assertTrue(actual.getEndpointGatewaySubnetMetas().containsKey(PUBLIC_SUBNET_2));
    assertTrue(actual.getEndpointGatewaySubnetMetas().containsKey(PUBLIC_SUBNET_3));
    assertEquals(awsNetwork.getNetworkCidr(), actual.getNetworkCidr());
    assertEquals(awsNetwork.getResourceCrn(), actual.getResourceCrn());
    assertEquals(awsNetwork.getVpcId(), actual.getAws().getVpcId());
}
Also used : NetworkDto(com.sequenceiq.environment.network.dto.NetworkDto) AwsNetwork(com.sequenceiq.environment.network.dao.domain.AwsNetwork) Test(org.junit.jupiter.api.Test)

Aggregations

AwsNetwork (com.sequenceiq.environment.network.dao.domain.AwsNetwork)22 Test (org.junit.jupiter.api.Test)15 Environment (com.sequenceiq.environment.environment.domain.Environment)12 NetworkDto (com.sequenceiq.environment.network.dto.NetworkDto)9 BaseNetwork (com.sequenceiq.environment.network.dao.domain.BaseNetwork)8 CloudSubnet (com.sequenceiq.cloudbreak.cloud.model.CloudSubnet)5 Network (com.sequenceiq.cloudbreak.cloud.model.Network)4 CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)4 EnvironmentDto (com.sequenceiq.environment.environment.dto.EnvironmentDto)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Credential (com.sequenceiq.environment.credential.domain.Credential)3 EnvironmentEditDto (com.sequenceiq.environment.environment.dto.EnvironmentEditDto)3 NetworkTest (com.sequenceiq.environment.environment.service.network.NetworkTest)3 CreatedSubnet (com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet)2 Region (com.sequenceiq.environment.environment.domain.Region)2 GcpNetwork (com.sequenceiq.environment.network.dao.domain.GcpNetwork)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 CloudPlatformVariant (com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant)1 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)1 NetworkCreationRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest)1