/** * Gets a property value as {@link BigDecimal}. Additionally it ensures that the property value is not negative. * * @param configurationValue the {@link BigDecimal} configuration value * * @return the non-negative {@link BigDecimal} property value */ public BigDecimal getNonNegativeBigDecimalRequiredProperty(ConfigurationValue configurationValue) { return getNonNegativeBigDecimalRequiredProperty(configurationValue, environment); }
/** * Gets a property value as {@link BigDecimal}. Additionally it ensures that the property value is not negative. * * @param configurationValue the {@link BigDecimal} configuration value * * @return the non-negative {@link BigDecimal} property value */ public BigDecimal getNonNegativeBigDecimalRequiredProperty(ConfigurationValue configurationValue) { return getNonNegativeBigDecimalRequiredProperty(configurationValue, environment); }
/** * Selects the EMR cluster pricing with the lowest core instance price. We will select one pricing randomly if there are multiple pricings that meet the * lowest core price criteria. * <p> * Returns null if the given list is empty * * @param emrClusterPrices the list of pricing to select from * * @return the pricing with the lowest core price */ EmrClusterPriceDto getEmrClusterPriceWithLowestCoreInstancePrice(final List<EmrClusterPriceDto> emrClusterPrices) { final List<EmrClusterPriceDto> lowestCoreInstancePriceEmrClusters = getEmrClusterPricesWithinLowestCoreInstancePriceThreshold(emrClusterPrices, configurationHelper.getNonNegativeBigDecimalRequiredProperty(ConfigurationValue.EMR_CLUSTER_LOWEST_CORE_INSTANCE_PRICE_PERCENTAGE)); if (!lowestCoreInstancePriceEmrClusters.isEmpty()) { // Pick one randomly from the lowest core instance price list final EmrClusterPriceDto selectedEmrClusterPriceDto = lowestCoreInstancePriceEmrClusters.get(new Random().nextInt(lowestCoreInstancePriceEmrClusters.size())); // Log the selected pricing as well as the pricing list LOGGER.info("selectedEmrCluster={} from lowestCoreInstancePriceEmrClusters={}", jsonHelper.objectToJson(selectedEmrClusterPriceDto), jsonHelper.objectToJson(lowestCoreInstancePriceEmrClusters)); return selectedEmrClusterPriceDto; } else { return null; } }
/** * Selects the EMR cluster pricing with the lowest core instance price. We will select one pricing randomly if there are multiple pricings that meet the * lowest core price criteria. * <p> * Returns null if the given list is empty * * @param emrClusterPrices the list of pricing to select from * * @return the pricing with the lowest core price */ EmrClusterPriceDto getEmrClusterPriceWithLowestCoreInstancePrice(final List<EmrClusterPriceDto> emrClusterPrices) { final List<EmrClusterPriceDto> lowestCoreInstancePriceEmrClusters = getEmrClusterPricesWithinLowestCoreInstancePriceThreshold(emrClusterPrices, configurationHelper.getNonNegativeBigDecimalRequiredProperty(ConfigurationValue.EMR_CLUSTER_LOWEST_CORE_INSTANCE_PRICE_PERCENTAGE)); if (!lowestCoreInstancePriceEmrClusters.isEmpty()) { // Pick one randomly from the lowest core instance price list final EmrClusterPriceDto selectedEmrClusterPriceDto = lowestCoreInstancePriceEmrClusters.get(new Random().nextInt(lowestCoreInstancePriceEmrClusters.size())); // Log the selected pricing as well as the pricing list LOGGER.info("selectedEmrCluster={} from lowestCoreInstancePriceEmrClusters={}", jsonHelper.objectToJson(selectedEmrClusterPriceDto), jsonHelper.objectToJson(lowestCoreInstancePriceEmrClusters)); return selectedEmrClusterPriceDto; } else { return null; } }
@Test public void testGetNonNegativeBigDecimalRequiredPropertyValidationThrowsWhenConfigurationValueIsNull() { try { configurationHelper.getNonNegativeBigDecimalRequiredProperty(null); fail("Should throw an IllegalStateException when configuration value is null."); } catch (IllegalStateException e) { assertEquals("configurationValue is required", e.getMessage()); } }
@Test public void testGetNonNegativeBigDecimalRequiredPropertyValueZeroValue() { ConfigurationValue configurationValue = ConfigurationValue.EMR_CLUSTER_LOWEST_CORE_INSTANCE_PRICE_PERCENTAGE; MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), "0"); assertEquals(new BigDecimal("0"), configurationHelper.getNonNegativeBigDecimalRequiredProperty(configurationValue, environment)); }
@Test public void testGetNonNegativeBigDecimalRequiredPropertyValuePositiveValue() { ConfigurationValue configurationValue = ConfigurationValue.EMR_CLUSTER_LOWEST_CORE_INSTANCE_PRICE_PERCENTAGE; MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), "1.00"); assertEquals(new BigDecimal("1.00"), configurationHelper.getNonNegativeBigDecimalRequiredProperty(configurationValue, environment)); }
@Test public void testGetNonNegativeBigDecimalRequiredPropertyValueBlankStringValue() { ConfigurationValue configurationValue = ConfigurationValue.EMR_CLUSTER_LOWEST_CORE_INSTANCE_PRICE_PERCENTAGE; MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), " "); try { configurationHelper.getNonNegativeBigDecimalRequiredProperty(configurationValue, environment); fail("Should throw an IllegalStatueException when property value is not BigDecimal."); } catch (IllegalStateException e) { assertEquals(String.format("Configuration \"%s\" must have a value.", configurationValue.getKey()), e.getMessage()); } }
@Test public void testGetNonNegativeBigDecimalRequiredPropertyValueNegativeValueFail() { ConfigurationValue configurationValue = ConfigurationValue.EMR_CLUSTER_LOWEST_CORE_INSTANCE_PRICE_PERCENTAGE; MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), "-1.00"); try { configurationHelper.getNonNegativeBigDecimalRequiredProperty(configurationValue, environment); fail("Should throw an IllegalStatueException when property value is not BigDecimal."); } catch (IllegalStateException e) { assertEquals(String.format("Configuration \"%s\" has an invalid non-negative BigDecimal value: \"-1.00\".", configurationValue.getKey()), e.getMessage()); } }