ConfigUtil { public static void applyDefaultConfiguration(Configuration configuration, @Nullable ConfigDescription configDescription) { if (configDescription != null) { for (ConfigDescriptionParameter parameter : configDescription.getParameters()) { String defaultValue = parameter.getDefault(); if (defaultValue != null && configuration.get(parameter.getName()) == null) { if (parameter.isMultiple()) { if (defaultValue.contains(DEFAULT_LIST_DELIMITER)) { List<Object> values = (List<Object>) List.of(defaultValue.split(DEFAULT_LIST_DELIMITER)) .stream() .map(String::trim) .filter(not(String::isEmpty)) .map(value -> ConfigUtil.getDefaultValueAsCorrectType(parameter.getName(), parameter.getType(), value)) .filter(Objects::nonNull) .collect(Collectors.toList()); Integer multipleLimit = parameter.getMultipleLimit(); if (multipleLimit != null && values.size() > multipleLimit.intValue()) { LoggerFactory.getLogger(ConfigUtil.class).warn( "Number of default values ({}) for parameter '{}' is greater than multiple limit ({})", values.size(), parameter.getName(), multipleLimit); } configuration.put(parameter.getName(), values); } else { Object value = ConfigUtil.getDefaultValueAsCorrectType(parameter); if (value != null) { configuration.put(parameter.getName(), Arrays.asList(value)); } } } else { Object value = ConfigUtil.getDefaultValueAsCorrectType(parameter); if (value != null) { configuration.put(parameter.getName(), value); } } } } } }  static @Nullable Object getDefaultValueAsCorrectType(ConfigDescriptionParameter parameter); static void applyDefaultConfiguration(Configuration configuration,
            @Nullable ConfigDescription configDescription); static Map<String, @Nullable Object> normalizeTypes(Map<String, @Nullable Object> configuration); static Object normalizeType(Object value, @Nullable ConfigDescriptionParameter configDescriptionParameter); static @Nullable Map<String, Object> normalizeTypes(@Nullable Map<String, Object> configuration,
            List<ConfigDescription> configDescriptions); static Object normalizeType(Object value);  }
@Test public void verifyApplyDefaultConfigurationReturnsNullIfNotSet() { Configuration configuration = new Configuration(); ConfigDescription configDescription = ConfigDescriptionBuilder.create(configUri) .withParameter(configDescriptionParameterBuilder2.build()).build(); ConfigUtil.applyDefaultConfiguration(configuration, configDescription); assertThat(configuration.get("p2"), is(nullValue())); }
@Test public void verifyApplyDefaultConfigurationReturnsAListWithASingleValues() { configDescriptionParameterBuilder1.withDefault("2.5"); Configuration configuration = new Configuration(); ConfigDescription configDescription = ConfigDescriptionBuilder.create(configUri) .withParameter(configDescriptionParameterBuilder1.build()).build(); ConfigUtil.applyDefaultConfiguration(configuration, configDescription); verifyValuesOfConfiguration(configuration.get("p1"), 1, List.of(new BigDecimal("2.5"))); }
@Test public void verifyApplyDefaultConfigurationReturnsAListWithMultipleValues() { configDescriptionParameterBuilder1.withDefault("2.3,2.4,2.5"); Configuration configuration = new Configuration(); ConfigDescription configDescription = ConfigDescriptionBuilder.create(configUri) .withParameter(configDescriptionParameterBuilder1.build()).build(); ConfigUtil.applyDefaultConfiguration(configuration, configDescription); verifyValuesOfConfiguration(configuration.get("p1"), 3, List.of(new BigDecimal("2.3"), new BigDecimal("2.4"), new BigDecimal("2.5"))); }
@Test public void verifyApplyDefaultConfigurationIgnoresWrongTypes() { configDescriptionParameterBuilder1.withDefault("2.3,2.4,foo,2.5"); Configuration configuration = new Configuration(); ConfigDescription configDescription = ConfigDescriptionBuilder.create(configUri) .withParameter(configDescriptionParameterBuilder1.build()).build(); ConfigUtil.applyDefaultConfiguration(configuration, configDescription); verifyValuesOfConfiguration(configuration.get("p1"), 3, List.of(new BigDecimal("2.3"), new BigDecimal("2.4"), new BigDecimal("2.5"))); }
@Test public void verifyApplyDefaultConfigurationReturnsAListWithTrimmedValues() { configDescriptionParameterBuilder2.withDefault("first value, second value ,third value,,,"); Configuration configuration = new Configuration(); ConfigDescription configDescription = ConfigDescriptionBuilder.create(configUri) .withParameter(configDescriptionParameterBuilder2.build()).build(); ConfigUtil.applyDefaultConfiguration(configuration, configDescription); verifyValuesOfConfiguration(configuration.get("p2"), 3, List.of("first value", "second value", "third value")); }