### Question:
EnvironmentNetworkService { public BaseNetwork createCloudNetwork(EnvironmentDto environment, BaseNetwork baseNetwork) { NetworkConnector networkConnector = getNetworkConnector(environment.getCloudPlatform()) .orElseThrow(() -> new BadRequestException("No network connector for cloud platform: " + environment.getCloudPlatform())); NetworkCreationRequest networkCreationRequest = networkCreationRequestFactory.create(environment); EnvironmentNetworkConverter converter = environmentNetworkConverterMap.get(getCloudPlatform(environment)); CreatedCloudNetwork createdCloudNetwork = networkConnector.createNetworkWithSubnets(networkCreationRequest); return converter.setCreatedCloudNetwork(baseNetwork, createdCloudNetwork); } EnvironmentNetworkService(
            CloudPlatformConnectors cloudPlatformConnectors,
            NetworkCreationRequestFactory networkCreationRequestFactory,
            Map<CloudPlatform, EnvironmentNetworkConverter> environmentNetworkConverterMap,
            CredentialToCloudCredentialConverter credentialToCloudCredentialConverter); BaseNetwork createCloudNetwork(EnvironmentDto environment, BaseNetwork baseNetwork); NetworkCidr getNetworkCidr(Network network, String cloudPlatform, Credential credential); void deleteNetwork(EnvironmentDto environment);  }### Answer:
@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); }
@Test @SuppressWarnings("unchecked") void testCreateNetworkIfUnableToObtainNetworkConnectorThenBadRequestExceptionComes() { EnvironmentDto environmentDto = EnvironmentDto.builder().withCloudPlatform(CLOUD_PLATFORM).build(); CloudConnector<Object> cloudConnector = mock(CloudConnector.class); when(cloudPlatformConnectors.get(any(CloudPlatformVariant.class))).thenReturn(cloudConnector); when(cloudConnector.networkConnector()).thenReturn(null); Assertions.assertThrows(BadRequestException.class, () -> underTest.createCloudNetwork(environmentDto, new AzureNetwork())); verify(cloudPlatformConnectors, times(1)).get(any()); }