EmrHelperServiceImpl implements EmrHelperService { void updateEmrClusterDefinitionWithValidInstanceFleetSubnets(EmrClusterAlternateKeyDto emrClusterAlternateKeyDto, EmrClusterDefinition emrClusterDefinition, AwsParamsDto awsParamsDto) { Integer instanceFleetMinimumIpAvailableFilter = emrClusterDefinition.getInstanceFleetMinimumIpAvailableFilter(); if (instanceFleetMinimumIpAvailableFilter == null || instanceFleetMinimumIpAvailableFilter == 0) { return; } List<Subnet> subnets = emrPricingHelper.getSubnets(emrClusterDefinition, awsParamsDto); List<String> validSubnetIds = subnets.stream().filter(subnet -> subnet.getAvailableIpAddressCount() >= instanceFleetMinimumIpAvailableFilter).map(Subnet::getSubnetId) .collect(Collectors.toList()); String contextInfo = String.format("namespace=\"%s\" emrClusterDefinitionName=\"%s\" emrClusterName=\"%s\" " + "instanceFleetMinimumIpAvailableFilter=%s subnetAvailableIpAddressCounts=%s validSubnetIds=%s", emrClusterAlternateKeyDto.getNamespace(), emrClusterAlternateKeyDto.getEmrClusterDefinitionName(), emrClusterAlternateKeyDto.getEmrClusterName(), instanceFleetMinimumIpAvailableFilter, jsonHelper.objectToJson(subnets.stream().collect(Collectors.toMap(Subnet::getSubnetId, Subnet::getAvailableIpAddressCount))), validSubnetIds.toString()); LOGGER.info("Current IP availability: {}", contextInfo); if (validSubnetIds.isEmpty()) { throw new IllegalArgumentException( "Specified subnets do not have enough available IP addresses required for the instance fleet. " + "Current IP availability: " + contextInfo); } emrClusterDefinition.setSubnetId(String.join(",", validSubnetIds)); }  @Override @Transactional(propagation = Propagation.NOT_SUPPORTED) EmrClusterCreateDto emrCreateClusterAwsSpecificSteps(EmrClusterCreateRequest emrClusterCreateRequest, EmrClusterDefinition emrClusterDefinition,
        EmrClusterAlternateKeyDto emrClusterAlternateKeyDto, EmrParamsDto emrParamsDto); @Override @Transactional(propagation = Propagation.REQUIRES_NEW) EmrClusterPreCreateDto emrPreCreateClusterSteps(EmrClusterAlternateKeyDto emrClusterAlternateKeyDto, EmrClusterCreateRequest emrClusterCreateRequest); @Override @Transactional(propagation = Propagation.REQUIRES_NEW) void logEmrClusterCreation(EmrClusterAlternateKeyDto emrClusterAlternateKeyDto, EmrClusterDefinition emrClusterDefinition, String clusterId);  }
@Test public void testUpdateEmrClusterDefinitionWithNullFilterValue() { AwsParamsDto awsParamsDto = new AwsParamsDto(); EmrClusterAlternateKeyDto emrClusterAlternateKeyDto = new EmrClusterAlternateKeyDto(NAMESPACE, EMR_CLUSTER_DEFINITION_NAME, EMR_CLUSTER_NAME); EmrClusterDefinition emrClusterDefinition = new EmrClusterDefinition(); emrClusterDefinition.setInstanceFleetMinimumIpAvailableFilter(null); emrClusterDefinition.setSubnetId("Test_Subnet_1,Test_Subnet_2"); emrHelperServiceImpl.updateEmrClusterDefinitionWithValidInstanceFleetSubnets(emrClusterAlternateKeyDto, emrClusterDefinition, awsParamsDto); assertEquals("Test_Subnet_1,Test_Subnet_2", emrClusterDefinition.getSubnetId()); verifyNoMoreInteractionsHelper(); }
@Test public void testUpdateEmrClusterDefinitionWithZeroFilterValue() { AwsParamsDto awsParamsDto = new AwsParamsDto(); EmrClusterAlternateKeyDto emrClusterAlternateKeyDto = new EmrClusterAlternateKeyDto(NAMESPACE, EMR_CLUSTER_DEFINITION_NAME, EMR_CLUSTER_NAME); EmrClusterDefinition emrClusterDefinition = new EmrClusterDefinition(); emrClusterDefinition.setInstanceFleetMinimumIpAvailableFilter(0); emrClusterDefinition.setSubnetId("Test_Subnet_1,Test_Subnet_2"); emrHelperServiceImpl.updateEmrClusterDefinitionWithValidInstanceFleetSubnets(emrClusterAlternateKeyDto, emrClusterDefinition, awsParamsDto); assertEquals("Test_Subnet_1,Test_Subnet_2", emrClusterDefinition.getSubnetId()); verifyNoMoreInteractionsHelper(); }
@Test public void testUpdateEmrClusterDefinitionWithAllValidInstanceFleetSubnets() { AwsParamsDto awsParamsDto = new AwsParamsDto(); EmrClusterAlternateKeyDto emrClusterAlternateKeyDto = new EmrClusterAlternateKeyDto(NAMESPACE, EMR_CLUSTER_DEFINITION_NAME, EMR_CLUSTER_NAME); EmrClusterDefinition emrClusterDefinition = new EmrClusterDefinition(); emrClusterDefinition.setInstanceFleetMinimumIpAvailableFilter(1); emrClusterDefinition.setSubnetId("SUBNET_1,SUBNET_2,SUBNET_3"); List<Subnet> subnets = initializeTestSubnets(3); when(emrPricingHelper.getSubnets(emrClusterDefinition, awsParamsDto)).thenReturn(subnets); when(jsonHelper.objectToJson(Mockito.any())).thenReturn("{jsonFormattedSubnetsAvailability}"); emrHelperServiceImpl.updateEmrClusterDefinitionWithValidInstanceFleetSubnets(emrClusterAlternateKeyDto, emrClusterDefinition, awsParamsDto); assertEquals("SUBNET_1,SUBNET_2,SUBNET_3", emrClusterDefinition.getSubnetId()); verify(emrPricingHelper).getSubnets(emrClusterDefinition, awsParamsDto); verify(jsonHelper).objectToJson(Mockito.any()); verifyNoMoreInteractionsHelper(); }
@Test public void testUpdateEmrClusterDefinitionWithOneValidInstanceFleetSubnet() { AwsParamsDto awsParamsDto = new AwsParamsDto(); EmrClusterAlternateKeyDto emrClusterAlternateKeyDto = new EmrClusterAlternateKeyDto(NAMESPACE, EMR_CLUSTER_DEFINITION_NAME, EMR_CLUSTER_NAME); EmrClusterDefinition emrClusterDefinition = new EmrClusterDefinition(); emrClusterDefinition.setInstanceFleetMinimumIpAvailableFilter(25); emrClusterDefinition.setSubnetId("SUBNET_1,SUBNET_2,SUBNET_3"); List<Subnet> subnets = initializeTestSubnets(3); when(emrPricingHelper.getSubnets(emrClusterDefinition, awsParamsDto)).thenReturn(subnets); when(jsonHelper.objectToJson(Mockito.any())).thenReturn("{jsonFormattedSubnetsAvailability}"); emrHelperServiceImpl.updateEmrClusterDefinitionWithValidInstanceFleetSubnets(emrClusterAlternateKeyDto, emrClusterDefinition, awsParamsDto); assertEquals("SUBNET_3", emrClusterDefinition.getSubnetId()); verify(emrPricingHelper).getSubnets(emrClusterDefinition, awsParamsDto); verify(jsonHelper).objectToJson(Mockito.any()); verifyNoMoreInteractionsHelper(); }
@Test public void testUpdateEmrClusterDefinitionWithNonValidInstanceFleetSubnets() { AwsParamsDto awsParamsDto = new AwsParamsDto(); EmrClusterAlternateKeyDto emrClusterAlternateKeyDto = new EmrClusterAlternateKeyDto(NAMESPACE, EMR_CLUSTER_DEFINITION_NAME, EMR_CLUSTER_NAME); EmrClusterDefinition emrClusterDefinition = new EmrClusterDefinition(); emrClusterDefinition.setInstanceFleetMinimumIpAvailableFilter(50); List<Subnet> subnets = initializeTestSubnets(3); when(emrPricingHelper.getSubnets(emrClusterDefinition, awsParamsDto)).thenReturn(subnets); when(jsonHelper.objectToJson(Mockito.any())).thenReturn("{jsonFormattedSubnetsAvailability}"); try { emrHelperServiceImpl.updateEmrClusterDefinitionWithValidInstanceFleetSubnets(emrClusterAlternateKeyDto, emrClusterDefinition, awsParamsDto); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException ex) { assertTrue(ex.getMessage().startsWith("Specified subnets do not have enough available IP addresses required for the instance fleet.")); assertTrue(ex.getMessage().contains(NAMESPACE)); assertTrue(ex.getMessage().contains(EMR_CLUSTER_DEFINITION_NAME)); assertTrue(ex.getMessage().contains(EMR_CLUSTER_NAME)); assertTrue(ex.getMessage().contains("{jsonFormattedSubnetsAvailability}")); } verify(emrPricingHelper).getSubnets(emrClusterDefinition, awsParamsDto); verify(jsonHelper).objectToJson(Mockito.any()); verifyNoMoreInteractionsHelper(); }