/** * Returns a mapping of instance types to on-demand prices for the given AZ and instance types. The on-demand prices are retrieved from database * configurations. The on-demand prices are looked up by the AZ's region name. This method also validates that the given instance types are real instance * types supported by AWS. * * @param availabilityZone the availability zone of the on-demand instances * @param instanceTypes the sizes of the on-demand instances * * @return the map of instance type to on-demand price * @throws ObjectNotFoundException when any of the instance type was not found in the given region */ private Map<String, BigDecimal> getInstanceTypeOnDemandPrices(AvailabilityZone availabilityZone, Set<String> instanceTypes) { Map<String, BigDecimal> instanceTypeOnDemandPrices = new HashMap<>(); for (String instanceType : instanceTypes) { Ec2OnDemandPricingEntity onDemandPrice = ec2OnDemandPricingDao.getEc2OnDemandPricing(availabilityZone.getRegionName(), instanceType); if (onDemandPrice == null) { throw new ObjectNotFoundException( "On-demand price for region '" + availabilityZone.getRegionName() + "' and instance type '" + instanceType + "' not found."); } instanceTypeOnDemandPrices.put(instanceType, onDemandPrice.getHourlyPrice()); } return instanceTypeOnDemandPrices; }
List<Ec2OnDemandPricingEntity> ec2OnDemandPricingEntities = ec2OnDemandPricingDao.getEc2OnDemandPricingEntities(); ec2OnDemandPricingDao.saveAndRefresh(ec2OnDemandPricingEntity); ec2OnDemandPricingEntity.setInstanceType(ec2OnDemandPricingKey.getInstanceType()); ec2OnDemandPricingEntity.setHourlyPrice(ec2OnDemandPricing.getPricePerHour()); ec2OnDemandPricingDao.saveAndRefresh(ec2OnDemandPricingEntity); ec2OnDemandPricingDao.delete(ec2OnDemandPricingEntity);
@Test public void testGetEc2OnDemandPricingEntities() { // Create database entities required for testing in random order. List<Ec2OnDemandPricingEntity> ec2OnDemandPricingEntities = Arrays .asList(ec2OnDemandPricingDaoTestHelper.createEc2OnDemandPricingEntity(AWS_REGION_NAME_2, EC2_INSTANCE_TYPE_2, HOURLY_PRICE), ec2OnDemandPricingDaoTestHelper.createEc2OnDemandPricingEntity(AWS_REGION_NAME, EC2_INSTANCE_TYPE, HOURLY_PRICE), ec2OnDemandPricingDaoTestHelper.createEc2OnDemandPricingEntity(AWS_REGION_NAME_2, EC2_INSTANCE_TYPE, HOURLY_PRICE), ec2OnDemandPricingDaoTestHelper.createEc2OnDemandPricingEntity(AWS_REGION_NAME, EC2_INSTANCE_TYPE_2, HOURLY_PRICE)); // Retrieve a list of entities and validate the result. assertEquals(Arrays .asList(ec2OnDemandPricingEntities.get(1), ec2OnDemandPricingEntities.get(3), ec2OnDemandPricingEntities.get(2), ec2OnDemandPricingEntities.get(0)), ec2OnDemandPricingDao.getEc2OnDemandPricingEntities()); } }
/** * Creates and persists a new EC2 on-demand pricing entity. * * @param regionName the AWS region name * @param instanceType the EC2 instance type * @param hourlyPrice the hourly price * * @return the newly created entity */ public Ec2OnDemandPricingEntity createEc2OnDemandPricingEntity(String regionName, String instanceType, BigDecimal hourlyPrice) { Ec2OnDemandPricingEntity ec2OnDemandPricingEntity = new Ec2OnDemandPricingEntity(); ec2OnDemandPricingEntity.setRegionName(regionName); ec2OnDemandPricingEntity.setInstanceType(instanceType); ec2OnDemandPricingEntity.setHourlyPrice(hourlyPrice); return ec2OnDemandPricingDao.saveAndRefresh(ec2OnDemandPricingEntity); } }
List<Ec2OnDemandPricingEntity> ec2OnDemandPricingEntities = ec2OnDemandPricingDao.getEc2OnDemandPricingEntities(); ec2OnDemandPricingDao.saveAndRefresh(ec2OnDemandPricingEntity); ec2OnDemandPricingEntity.setInstanceType(ec2OnDemandPricingKey.getInstanceType()); ec2OnDemandPricingEntity.setHourlyPrice(ec2OnDemandPricing.getPricePerHour()); ec2OnDemandPricingDao.saveAndRefresh(ec2OnDemandPricingEntity); ec2OnDemandPricingDao.delete(ec2OnDemandPricingEntity);
when(ec2OnDemandPricingDao.getEc2OnDemandPricingEntities()).thenReturn(ec2OnDemandPricingEntities); verify(ec2OnDemandPricingDao).getEc2OnDemandPricingEntities(); verify(ec2OnDemandPricingDao, times(2)).saveAndRefresh(any(Ec2OnDemandPricingEntity.class)); verify(ec2OnDemandPricingDao).delete(ec2OnDemandPricingEntities.get(0)); verifyNoMoreInteractionsHelper();
/** * Returns a mapping of instance types to on-demand prices for the given AZ and instance types. The on-demand prices are retrieved from database * configurations. The on-demand prices are looked up by the AZ's region name. This method also validates that the given instance types are real instance * types supported by AWS. * * @param availabilityZone the availability zone of the on-demand instances * @param instanceTypes the sizes of the on-demand instances * * @return the map of instance type to on-demand price * @throws ObjectNotFoundException when any of the instance type was not found in the given region */ private Map<String, BigDecimal> getInstanceTypeOnDemandPrices(AvailabilityZone availabilityZone, Set<String> instanceTypes) { Map<String, BigDecimal> instanceTypeOnDemandPrices = new HashMap<>(); for (String instanceType : instanceTypes) { Ec2OnDemandPricingEntity onDemandPrice = ec2OnDemandPricingDao.getEc2OnDemandPricing(availabilityZone.getRegionName(), instanceType); if (onDemandPrice == null) { throw new ObjectNotFoundException( "On-demand price for region '" + availabilityZone.getRegionName() + "' and instance type '" + instanceType + "' not found."); } instanceTypeOnDemandPrices.put(instanceType, onDemandPrice.getHourlyPrice()); } return instanceTypeOnDemandPrices; }
@Test public void testGetEc2OnDemandPricing() { // Create database entities required for testing. Ec2OnDemandPricingEntity ec2OnDemandPricingEntity = ec2OnDemandPricingDaoTestHelper.createEc2OnDemandPricingEntity(AWS_REGION_NAME, EC2_INSTANCE_TYPE, HOURLY_PRICE); // Retrieve and validate this entity. assertEquals(ec2OnDemandPricingEntity, ec2OnDemandPricingDao.getEc2OnDemandPricing(AWS_REGION_NAME, EC2_INSTANCE_TYPE)); // Confirm case sensitivity for the AWS region and EC2 instance type. assertNull(ec2OnDemandPricingDao.getEc2OnDemandPricing(AWS_REGION_NAME.toUpperCase(), EC2_INSTANCE_TYPE)); assertNull(ec2OnDemandPricingDao.getEc2OnDemandPricing(AWS_REGION_NAME.toLowerCase(), EC2_INSTANCE_TYPE)); assertNull(ec2OnDemandPricingDao.getEc2OnDemandPricing(AWS_REGION_NAME, EC2_INSTANCE_TYPE.toUpperCase())); assertNull(ec2OnDemandPricingDao.getEc2OnDemandPricing(AWS_REGION_NAME, EC2_INSTANCE_TYPE.toLowerCase())); // Confirm negative results when using wrong input parameters. assertNull(ec2OnDemandPricingDao.getEc2OnDemandPricing(I_DO_NOT_EXIST, EC2_INSTANCE_TYPE)); assertNull(ec2OnDemandPricingDao.getEc2OnDemandPricing(AWS_REGION_NAME, I_DO_NOT_EXIST)); }