use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.
the class EnvironmentVariableUtilityTest method testHasEnvironment.
@Test
void testHasEnvironment() {
Environment environment = Mockito.mock(Environment.class);
String workingPropertyKey = WORKING_PROPERTY_KEY;
Mockito.when(environment.containsProperty(workingPropertyKey)).thenReturn(Boolean.TRUE);
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
assertFalse(environmentVariableUtility.hasEnvironmentValue("BAD_KEY"));
assertTrue(environmentVariableUtility.hasEnvironmentValue(workingPropertyKey));
}
use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.
the class EnvironmentVariableUtilityTest method testGetEnvironmentValueEmpty.
@Test
void testGetEnvironmentValueEmpty() {
Environment environment = Mockito.mock(Environment.class);
String workingPropertyKey = WORKING_PROPERTY_KEY;
Mockito.when(environment.containsProperty(workingPropertyKey)).thenReturn(Boolean.TRUE);
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
assertTrue(environmentVariableUtility.getEnvironmentValue(workingPropertyKey).isEmpty());
}
use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.
the class EmailEnvironmentHandlerFactoryTest method testEmailAdditionalProperties.
@Test
void testEmailAdditionalProperties() {
Environment environment = Mockito.mock(Environment.class);
EmailGlobalConfigAccessor configAccessor = Mockito.mock(EmailGlobalConfigAccessor.class);
Mockito.when(configAccessor.doesConfigurationExist()).thenReturn(false);
String authRequired = "true";
String from = "noreply@example.com";
String smtpHost = "test.smtp.server.example.com";
String passwordValue = "a test value";
String port = "25";
String username = "testuser";
Predicate<String> hasEnvVarCheck = (variableName) -> !EmailEnvironmentVariableHandlerFactory.OLD_ADDITIONAL_PROPERTY_KEYSET.contains(variableName) || ADDITIONAL_EMAIL_PROPERTY_VARIABLE_NAME.equals(variableName);
Mockito.when(environment.containsProperty(Mockito.anyString())).thenReturn(Boolean.TRUE);
EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.AUTH_REQUIRED_KEY, authRequired);
EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.EMAIL_FROM_KEY, from);
EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.EMAIL_HOST_KEY, smtpHost);
EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.AUTH_PASSWORD_KEY, passwordValue);
EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.EMAIL_PORT_KEY, port);
EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.AUTH_USER_KEY, username);
EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, ADDITIONAL_EMAIL_PROPERTY_VARIABLE_NAME, "true");
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
EnvironmentVariableHandlerFactory factory = new EmailEnvironmentVariableHandlerFactory(configAccessor, environmentVariableUtility, validator);
EnvironmentVariableHandler handler = factory.build();
EnvironmentProcessingResult result = handler.updateFromEnvironment();
assertEquals(ChannelKeys.EMAIL.getDisplayName(), handler.getName());
assertTrue(result.hasValues());
assertEquals(authRequired, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.AUTH_REQUIRED_KEY).orElse("Auth required value missing"));
assertEquals(from, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.EMAIL_FROM_KEY).orElse("SMTP from value missing"));
assertEquals(smtpHost, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.EMAIL_HOST_KEY).orElse("SMTP host value missing"));
assertEquals(AlertConstants.MASKED_VALUE, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.AUTH_PASSWORD_KEY).orElse("Auth password value missing"));
assertEquals(port, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.EMAIL_PORT_KEY).orElse("SMTP port value missing"));
assertEquals(username, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.AUTH_USER_KEY).orElse("Auth user value missing"));
assertEquals("true", result.getVariableValue(ADDITIONAL_EMAIL_PROPERTY_VARIABLE_NAME).orElse("Additional email property value missing"));
}
use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.
the class EmailEnvironmentHandlerFactoryTest method testEmailMissingFromEnvironment.
@Test
void testEmailMissingFromEnvironment() {
Environment environment = Mockito.mock(Environment.class);
EmailGlobalConfigAccessor configAccessor = Mockito.mock(EmailGlobalConfigAccessor.class);
Mockito.when(configAccessor.doesConfigurationExist()).thenReturn(false);
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
EnvironmentVariableHandlerFactory factory = new EmailEnvironmentVariableHandlerFactory(configAccessor, environmentVariableUtility, validator);
EnvironmentVariableHandler handler = factory.build();
EnvironmentProcessingResult result = handler.updateFromEnvironment();
assertEquals(ChannelKeys.EMAIL.getDisplayName(), handler.getName());
assertFalse(result.hasValues());
}
use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.
the class AlertStartupInitializerTest method testInitializeConfigsEmptyInitializerList.
@Test
public void testInitializeConfigsEmptyInitializerList() throws Exception {
Environment environment = Mockito.mock(Environment.class);
DescriptorAccessor baseDescriptorAccessor = Mockito.mock(DescriptorAccessor.class);
ConfigurationModelConfigurationAccessor baseConfigurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
DescriptorMap descriptorMap = new DescriptorMap(List.of(), List.of());
EncryptionUtility encryptionUtility = Mockito.mock(EncryptionUtility.class);
Mockito.when(encryptionUtility.isInitialized()).thenReturn(Boolean.TRUE);
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
EnvironmentVariableProcessor environmentVariableProcessor = new EnvironmentVariableProcessor(List.of());
ConfigurationFieldModelConverter modelConverter = new ConfigurationFieldModelConverter(encryptionUtility, baseDescriptorAccessor, List.of());
FieldModelProcessor fieldModelProcessor = new FieldModelProcessor(modelConverter, new DescriptorProcessor(descriptorMap, baseConfigurationModelConfigurationAccessor, List.of()));
SettingsUtility settingsUtility = Mockito.mock(SettingsUtility.class);
Mockito.when(settingsUtility.getKey()).thenReturn(new SettingsDescriptorKey());
AlertStartupInitializer initializer = new AlertStartupInitializer(descriptorMap, environmentVariableUtility, baseDescriptorAccessor, baseConfigurationModelConfigurationAccessor, modelConverter, fieldModelProcessor, settingsUtility, environmentVariableProcessor);
initializer.initializeComponent();
// called to get the settings component configuration and fields.
Mockito.verify(baseDescriptorAccessor, Mockito.times(1)).getFieldsForDescriptor(Mockito.any(DescriptorKey.class), Mockito.any(ConfigContextEnum.class));
Mockito.verify(baseConfigurationModelConfigurationAccessor, Mockito.times(1)).getConfigurationsByDescriptorKeyAndContext(Mockito.any(DescriptorKey.class), Mockito.any(ConfigContextEnum.class));
// nothing should be saved
// Mockito.verify(baseConfigurationAccessor, Mockito.times(1)).createConfiguration(Mockito.anyString(), Mockito.any(ConfigContextEnum.class), Mockito.anyCollection());
}
Aggregations