### Question:
EnvironmentBaseNetworkConverter implements EnvironmentNetworkConverter { @Override public Network convertToLegacyNetwork(EnvironmentNetworkResponse source, String availabilityZone) { Network result = new Network(); result.setName(missingResourceNameGenerator.generateName(APIResourceType.NETWORK)); result.setSubnetCIDR(null); result.setOutboundInternetTraffic(source.getOutboundInternetTraffic()); result.setNetworkCidrs(source.getNetworkCidrs()); Map<String, Object> attributes = new HashMap<>(); Optional<CloudSubnet> cloudSubnet; if (StringUtils.isNotBlank(source.getPreferedSubnetId())) { LOGGER.debug("Choosing subnet by prefered subnet Id {}", source.getPreferedSubnetId()); CloudSubnet cloudSubnetById = source.getSubnetMetas().get(source.getPreferedSubnetId()); if (cloudSubnetById == null) { cloudSubnetById = source.getSubnetMetas().values() .stream() .filter(e -> e.getId().equals(source.getPreferedSubnetId())) .findFirst().orElse(null); } cloudSubnet = Optional.of(cloudSubnetById); } else if (StringUtils.isNotEmpty(availabilityZone)) { LOGGER.debug("Choosing subnet by availability zone {}", availabilityZone); cloudSubnet = source.getSubnetMetas().values().stream() .filter(s -> StringUtils.isNotEmpty(s.getAvailabilityZone()) && s.getAvailabilityZone().equals(availabilityZone)) .findFirst(); } else { LOGGER.debug("Fallback to choose random subnet"); cloudSubnet = source.getSubnetMetas().values().stream().findFirst(); } if (cloudSubnet.isEmpty()) { throw new BadRequestException("No subnet for the given availability zone: " + availabilityZone); } LOGGER.debug("Chosen subnet: {}", cloudSubnet.get()); attributes.put("subnetId", cloudSubnet.get().getId()); attributes.put("cloudPlatform", getCloudPlatform().name()); attributes.putAll(getAttributesForLegacyNetwork(source)); try { result.setAttributes(new Json(attributes)); } catch (IllegalArgumentException e) { LOGGER.debug("Environment's network could not be converted to network.", e); } return result; }  @Override Network convertToLegacyNetwork(EnvironmentNetworkResponse source, String availabilityZone);  }### Answer:
@Test public void testConvertToLegacyNetworkWhenSubnetNotFound() { EnvironmentNetworkResponse source = new EnvironmentNetworkResponse(); source.setSubnetMetas(Map.of("key", getCloudSubnet("any"))); BadRequestException badRequestException = assertThrows(BadRequestException.class, () -> underTest.convertToLegacyNetwork(source, "eu-west-1a")); assertEquals(badRequestException.getMessage(), "No subnet for the given availability zone: eu-west-1a"); }
@Test public void testConvertToLegacyNetworkWhenSubnetFound() { EnvironmentNetworkResponse source = new EnvironmentNetworkResponse(); Set<String> networkCidrs = Set.of("1.2.3.4/32", "0.0.0.0/0"); source.setNetworkCidrs(networkCidrs); source.setOutboundInternetTraffic(OutboundInternetTraffic.DISABLED); source.setSubnetMetas(Map.of("key", getCloudSubnet("eu-west-1a"))); Network network = underTest.convertToLegacyNetwork(source, "eu-west-1a"); assertEquals(network.getAttributes().getValue("subnetId"), "eu-west-1"); assertTrue(network.getNetworkCidrs().containsAll(networkCidrs)); assertEquals(source.getOutboundInternetTraffic(), network.getOutboundInternetTraffic()); }