ConfigurationHelper { public static String getProperty(ConfigurationValue configurationValue, Environment environment) { return getProperty(configurationValue, String.class, environment); }  static String getProperty(ConfigurationValue configurationValue, Environment environment); static T getProperty(ConfigurationValue configurationValue, Class<T> targetType, Environment environment); BigDecimal getBigDecimalRequiredProperty(ConfigurationValue configurationValue); BigDecimal getBigDecimalRequiredProperty(ConfigurationValue configurationValue, Environment environment); Boolean getBooleanProperty(ConfigurationValue configurationValue); Boolean getBooleanProperty(ConfigurationValue configurationValue, Environment environment); BigDecimal getNonNegativeBigDecimalRequiredProperty(ConfigurationValue configurationValue); BigDecimal getNonNegativeBigDecimalRequiredProperty(ConfigurationValue configurationValue, Environment environment); String getProperty(ConfigurationValue configurationValue); T getProperty(ConfigurationValue configurationValue, Class<T> targetType); String getPropertyAsString(ConfigurationValue configurationValue); String getRequiredProperty(ConfigurationValue configurationValue); String getRequiredProperty(ConfigurationValue configurationValue, Environment environment);  }
@Test public void testGetProperty() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; String expectedValue = "test"; MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), expectedValue); String value = ConfigurationHelper.getProperty(configurationValue, String.class, environment); assertNotNull("value", value); assertEquals("value", expectedValue, value); }
@Test public void testGetPropertyReturnDefaultWhenPropertyDoesNotExists() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; String expectedValue = (String) configurationValue.getDefaultValue(); MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), expectedValue); String value = ConfigurationHelper.getProperty(configurationValue, String.class, environment); assertNotNull("value", value); assertEquals("value", expectedValue, value); }
@Test public void testGetPropertyReturnDefaultWhenValueConversionFails() throws Exception { ConfigurationValue configurationValue = ConfigurationValue.BUSINESS_OBJECT_DATA_GET_ALL_MAX_RESULT_COUNT; Integer expectedValue = (Integer) configurationValue.getDefaultValue(); MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), "NOT_AN_INTEGER"); executeWithoutLogging(ConfigurationHelper.class, () -> { Integer value = ConfigurationHelper.getProperty(configurationValue, Integer.class, environment); assertNotNull("value", value); assertEquals("value", expectedValue, value); }); }
@Test public void testGetPropertyThrowsWhenDefaultTypeDoesNotMatch() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; Class<?> expectedDefaultType = configurationValue.getDefaultValue().getClass(); Class<?> givenType = Integer.class; MockEnvironment environment = new MockEnvironment(); try { ConfigurationHelper.getProperty(configurationValue, givenType, environment); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception type", IllegalStateException.class, e.getClass()); assertEquals("thrown exception message", "targetType \"" + givenType + "\" is not assignable from the default value of type \"" + expectedDefaultType + "\" for configuration value " + "\"HERD_ENVIRONMENT\".", e.getMessage()); } }
@Test public void testGetPropertyReturnNullWhenPropertyDoesNotExistsAndNoDefaultsConfigured() { ConfigurationValue configurationValue = ConfigurationValue.HIBERNATE_DIALECT; MockEnvironment environment = new MockEnvironment(); String value = ConfigurationHelper.getProperty(configurationValue, String.class, environment); assertNull("value", value); }
@Test public void testGetPropertyValidationThrowsWhenConfigurationValueIsNull() { MockEnvironment environment = new MockEnvironment(); try { ConfigurationHelper.getProperty(null, String.class, environment); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception type", IllegalStateException.class, e.getClass()); assertEquals("thrown exception message", "configurationValue is required", e.getMessage()); } }
@Test public void testGetPropertyValidationThrowsWhenTargetTypeIsNull() { MockEnvironment environment = new MockEnvironment(); try { ConfigurationHelper.getProperty(ConfigurationValue.HERD_ENVIRONMENT, null, environment); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception type", IllegalStateException.class, e.getClass()); assertEquals("thrown exception message", "targetType is required", e.getMessage()); } }
@Test public void testGetPropertyNoTargetType() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; MockEnvironment environment = new MockEnvironment(); String value = ConfigurationHelper.getProperty(configurationValue, environment); assertEquals("value", configurationValue.getDefaultValue(), value); }
@Test public void testGetPropertyInstance() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; String value = configurationHelper.getProperty(configurationValue, String.class); assertEquals("value", configurationValue.getDefaultValue(), value); }
@Test public void testGetPropertyInstanceNoTargetType() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; String value = configurationHelper.getProperty(configurationValue); assertEquals("value", configurationValue.getDefaultValue(), value); }