/** * Get the message definition key based on the event type * * @param notificationEvent the notification event * * @return the message definition key */ private String getMessageDefinitionKey(NotificationEvent notificationEvent) { return eventTypeMessageDefinitionKeyMap.get(notificationEvent.getClass()).getKey(); }
@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 testGetPropertyInstanceNoTargetType() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; String value = configurationHelper.getProperty(configurationValue); assertEquals("value", configurationValue.getDefaultValue(), 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 testGetRequiredProperty() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; String value = configurationHelper.getRequiredProperty(configurationValue); assertEquals(configurationValue.getDefaultValue(), value); }
/** * Get the message definition key based on the event type * * @param notificationEvent the notification event * * @return the message definition key */ private String getMessageDefinitionKey(NotificationEvent notificationEvent) { return eventTypeMessageDefinitionKeyMap.get(notificationEvent.getClass()).getKey(); }
@Test public void testGetPropertyInstance() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; String value = configurationHelper.getProperty(configurationValue, String.class); assertEquals("value", configurationValue.getDefaultValue(), value); }
/** * Logs the error message, and then throws {@link IllegalStateException} * * @param configurationValue - {@link ConfigurationValue} * @param targetTypeName - the name of the data type we want to convert the configuration value to(boolean, BigDecimal...etc) * @param stringValue - the configuration value in string type * @param exception - the exception thrown when converting the configuration value from string type to the target data type */ private void logErrorAndThrowIllegalStateException(ConfigurationValue configurationValue, String targetTypeName, String stringValue, Exception exception) { // Create an invalid state exception. IllegalStateException illegalStateException = new IllegalStateException( String.format("Configuration \"%s\" has an invalid %s value: \"%s\".", configurationValue.getKey(), targetTypeName, stringValue), exception); // Log the exception. LOGGER.error(illegalStateException.getMessage(), illegalStateException); // This will produce a 500 HTTP status code error. throw illegalStateException; } }
@Test public void testGetPropertyNoTargetType() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; MockEnvironment environment = new MockEnvironment(); String value = ConfigurationHelper.getProperty(configurationValue, environment); assertEquals("value", configurationValue.getDefaultValue(), value); }
/** * Logs the error message, and then throws {@link IllegalStateException} * * @param configurationValue - {@link ConfigurationValue} * @param targetTypeName - the name of the data type we want to convert the configuration value to(boolean, BigDecimal...etc) * @param stringValue - the configuration value in string type * @param exception - the exception thrown when converting the configuration value from string type to the target data type */ private void logErrorAndThrowIllegalStateException(ConfigurationValue configurationValue, String targetTypeName, String stringValue, Exception exception) { // Create an invalid state exception. IllegalStateException illegalStateException = new IllegalStateException( String.format("Configuration \"%s\" has an invalid %s value: \"%s\".", configurationValue.getKey(), targetTypeName, stringValue), exception); // Log the exception. LOGGER.error(illegalStateException.getMessage(), illegalStateException); // This will produce a 500 HTTP status code error. throw illegalStateException; } }
@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()); } }
@Override public void validateParameters(List<Parameter> parameters) { // This system job accepts only one optional parameter. if (!org.springframework.util.CollectionUtils.isEmpty(parameters)) { Assert.isTrue(parameters.size() == 1, String.format("Too many parameters are specified for \"%s\" system job.", JOB_NAME)); Assert.isTrue(parameters.get(0).getName().equalsIgnoreCase(ConfigurationValue.EC2_ON_DEMAND_PRICING_UPDATE_JOB_EC2_PRICING_LIST_URL.getKey()), String.format("Parameter \"%s\" is not supported by \"%s\" system job.", parameters.get(0).getName(), JOB_NAME)); } }
@Override public void validateParameters(List<Parameter> parameters) { // This system job accepts only one optional parameter. if (!org.springframework.util.CollectionUtils.isEmpty(parameters)) { Assert.isTrue(parameters.size() == 1, String.format("Too many parameters are specified for \"%s\" system job.", JOB_NAME)); Assert.isTrue(parameters.get(0).getName().equalsIgnoreCase(ConfigurationValue.EC2_ON_DEMAND_PRICING_UPDATE_JOB_EC2_PRICING_LIST_URL.getKey()), String.format("Parameter \"%s\" is not supported by \"%s\" system job.", parameters.get(0).getName(), JOB_NAME)); } }
/** * Returns the SQS queue name. Throws {@link IllegalStateException} if SQS queue name is undefined. * * @return the sqs queue name */ private String getSqsQueueName() { String sqsQueueName = configurationHelper.getProperty(ConfigurationValue.SEARCH_INDEX_UPDATE_SQS_QUEUE_NAME); if (StringUtils.isBlank(sqsQueueName)) { throw new IllegalStateException(String.format("SQS queue name not found. Ensure the \"%s\" configuration entry is configured.", ConfigurationValue.SEARCH_INDEX_UPDATE_SQS_QUEUE_NAME.getKey())); } return sqsQueueName; }
/** * Returns the SQS queue name. Throws {@link IllegalStateException} if SQS queue name is undefined. * * @return the sqs queue name */ private String getSqsQueueName() { String sqsQueueName = configurationHelper.getProperty(ConfigurationValue.SEARCH_INDEX_UPDATE_SQS_QUEUE_NAME); if (StringUtils.isBlank(sqsQueueName)) { throw new IllegalStateException(String.format("SQS queue name not found. Ensure the \"%s\" configuration entry is configured.", ConfigurationValue.SEARCH_INDEX_UPDATE_SQS_QUEUE_NAME.getKey())); } return sqsQueueName; }
/** * The quartz driver delegate class, that works with the quartz database. Throws {@link IllegalStateException} if undefined. * * @return the quartz driver delegate class. */ private String getQuartzDatabaseDelegateClass() { String quartzDelegateClass = configurationHelper.getProperty(ConfigurationValue.QUARTZ_JOBSTORE_DRIVER_DELEGATE_CLASS); if (StringUtils.isBlank(quartzDelegateClass)) { throw new IllegalStateException(String.format("Quartz driver delegate class name not found. Ensure the \"%s\" configuration entry is configured.", ConfigurationValue.QUARTZ_JOBSTORE_DRIVER_DELEGATE_CLASS.getKey())); } return quartzDelegateClass; } }
/** * Gets the value and verifies that it is not blank for the given configuration option. * * @param configurationValue the configuration option * * @return the configuration option value */ public String getRequiredConfigurationValue(ConfigurationValue configurationValue) { String value = configurationHelper.getProperty(configurationValue); if (StringUtils.isBlank(value)) { throw new IllegalStateException(String.format("Missing configuration parameter value for key \"%s\".", configurationValue.getKey())); } return value; }
@Override public void validateParameters(List<Parameter> parameters) { // This system job accepts only one optional parameter with an integer value. if (!CollectionUtils.isEmpty(parameters)) { Assert.isTrue(parameters.size() == 1, String.format("Too many parameters are specified for \"%s\" system job.", JOB_NAME)); Assert.isTrue(parameters.get(0).getName().equalsIgnoreCase(ConfigurationValue.CLEANUP_DESTROYED_BDATA_JOB_MAX_BDATA_INSTANCES.getKey()), String.format("Parameter \"%s\" is not supported by \"%s\" system job.", parameters.get(0).getName(), JOB_NAME)); parameterHelper.getParameterValueAsInteger(parameters.get(0)); } }
/** * The quartz driver delegate class, that works with the quartz database. Throws {@link IllegalStateException} if undefined. * * @return the quartz driver delegate class. */ private String getQuartzDatabaseDelegateClass() { String quartzDelegateClass = configurationHelper.getProperty(ConfigurationValue.QUARTZ_JOBSTORE_DRIVER_DELEGATE_CLASS); if (StringUtils.isBlank(quartzDelegateClass)) { throw new IllegalStateException(String.format("Quartz driver delegate class name not found. Ensure the \"%s\" configuration entry is configured.", ConfigurationValue.QUARTZ_JOBSTORE_DRIVER_DELEGATE_CLASS.getKey())); } return quartzDelegateClass; } }
/** * Gets the value and verifies that it is not blank for the given configuration option. * * @param configurationValue the configuration option * * @return the configuration option value */ public String getRequiredConfigurationValue(ConfigurationValue configurationValue) { String value = configurationHelper.getProperty(configurationValue); if (StringUtils.isBlank(value)) { throw new IllegalStateException(String.format("Missing configuration parameter value for key \"%s\".", configurationValue.getKey())); } return value; }