PricingClientImpl implements PricingClient { @Override public List<String> populateInstanceTypesForSelectedRegion() throws AWSPricingException, IOException { List<String> productDescriptions = getProductDescriptions(); if ( productDescriptions == null || productDescriptions.size() == 0 ) { return instanceTypes; } List<InstanceType> tmpInstanceTypes = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); String instanceTypeName; String instanceFamily; for ( String description : productDescriptions ) { instanceTypeName = mapper.readTree( description ).path( "product" ).get( "attributes" ).get( "instanceType" ).asText(); instanceFamily = mapper.readTree( description ).path( "product" ).get( "attributes" ).get( "instanceFamily" ).asText(); tmpInstanceTypes.add( new InstanceType( instanceTypeName, instanceFamily ) ); } instanceTypes = InstanceType.sortInstanceTypes( tmpInstanceTypes ); return instanceTypes; } PricingClientImpl( AWSPricing pricing, String humanReadableRegion ); @Override List<String> populateInstanceTypesForSelectedRegion();  }
@Test public void testPopulateInstanceTypesForSelectedRegion_withValidValues() throws Exception { List<String> expectedInstanceTypes = new ArrayList<>(); expectedInstanceTypes.add( "c4.2xlarge" ); expectedInstanceTypes.add( "c4.4xlarge" ); List<String> productDescriptions = new ArrayList<>(); String jsonDescriptionC2xlarge = "{" + "\"product\": {" + "\"productFamily\": \"Elastic Map Reduce Instance\"," + "\"attributes\": {" + "\"servicecode\": \"ElasticMapReduce\"," + "\"softwareType\": \"Hunk\"," + "\"instanceType\": \"c4.2xlarge\"," + "\"usagetype\": \"HunkBoxUsage:c4.2xlarge\"," + "\"locationType\": \"AWS Region\"," + "\"location\": \"US East (N. Virginia)\"," + "\"servicename\": \"Amazon Elastic MapReduce\"," + "\"instanceFamily\": \"Compute optimized\"," + "\"operation\": \"\"" + "}," + "\"sku\": \"226GQ2CAZYZ8D7MG\"" + "}}"; String jsonDescriptionC4xlarge = "{" + "\"product\": {" + "\"productFamily\": \"Elastic Map Reduce Instance\"," + "\"attributes\": {" + "\"servicecode\": \"ElasticMapReduce\"," + "\"softwareType\": \"Hunk\"," + "\"instanceType\": \"c4.4xlarge\"," + "\"usagetype\": \"HunkBoxUsage:c4.4xlarge\"," + "\"locationType\": \"AWS Region\"," + "\"location\": \"US East (N. Virginia)\"," + "\"servicename\": \"Amazon Elastic MapReduce\"," + "\"instanceFamily\": \"Compute optimized\"," + "\"operation\": \"\"" + "}," + "\"sku\": \"226GQ2CAZYZ8D7MG\"" + "}}"; productDescriptions.add( jsonDescriptionC2xlarge ); productDescriptions.add( jsonDescriptionC4xlarge ); PowerMockito.doReturn( productDescriptions ).when( pricingClient, "getProductDescriptions" ); List<String> instanceTypes = pricingClient.populateInstanceTypesForSelectedRegion(); Assert.assertEquals( expectedInstanceTypes, instanceTypes ); }
@Test public void testPopulateInstanceTypesForSelectedRegion_whenDescriptionIsNull() throws Exception { List<String> productDescriptions = null; PowerMockito.doReturn( productDescriptions ).when( pricingClient, "getProductDescriptions" ); List<String> instanceTypes = pricingClient.populateInstanceTypesForSelectedRegion(); Assert.assertNull( instanceTypes ); }
@Test public void testPopulateInstanceTypesForSelectedRegion_whenDescriptionIsEmpty() throws Exception { List<String> productDescriptions = new ArrayList<>(); PowerMockito.doReturn( productDescriptions ).when( pricingClient, "getProductDescriptions" ); List<String> instanceTypes = pricingClient.populateInstanceTypesForSelectedRegion(); Assert.assertNull( instanceTypes ); }