Search in sources :

Example 1 with EnvironmentVariableUtility

use of com.synopsys.integration.alert.environment.EnvironmentVariableUtility in project hub-alert by blackducksoftware.

the class EmailEnvironmentHandlerFactoryTestIT method testCleanEnvironment.

@Test
void testCleanEnvironment() {
    Environment environment = setupMockedEnvironment();
    EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
    EnvironmentVariableHandlerFactory factory = new EmailEnvironmentVariableHandlerFactory(emailGlobalConfigAccessor, environmentVariableUtility, validator);
    EnvironmentVariableHandler handler = factory.build();
    EnvironmentProcessingResult result = handler.updateFromEnvironment();
    assertEquals(ChannelKeys.EMAIL.getDisplayName(), handler.getName());
    assertTrue(result.hasValues());
    assertEquals(TEST_AUTH_REQUIRED, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.AUTH_REQUIRED_KEY).orElse("Auth required value missing"));
    assertEquals(TEST_FROM, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.EMAIL_FROM_KEY).orElse("SMTP from value missing"));
    assertEquals(TEST_SMTP_HOST, 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(TEST_PORT, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.EMAIL_PORT_KEY).orElse("SMTP port value missing"));
    assertEquals(TEST_USER, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.AUTH_USER_KEY).orElse("Auth user value missing"));
}
Also used : EnvironmentVariableHandlerFactory(com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Environment(org.springframework.core.env.Environment) EnvironmentVariableHandler(com.synopsys.integration.alert.environment.EnvironmentVariableHandler) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with EnvironmentVariableUtility

use of com.synopsys.integration.alert.environment.EnvironmentVariableUtility in project hub-alert by blackducksoftware.

the class JiraServerEnvironmentVariableHandlerFactoryTest method testSetInEnvironmentUserNameMissing.

@Test
void testSetInEnvironmentUserNameMissing() {
    Environment environment = Mockito.mock(Environment.class);
    JiraServerGlobalConfigAccessor configAccessor = Mockito.mock(JiraServerGlobalConfigAccessor.class);
    Mockito.when(configAccessor.getConfigurationCount()).thenReturn(0L);
    Set<String> expectedVariableNames = JiraServerEnvironmentVariableHandlerFactory.VARIABLE_NAMES;
    String disablePluginCheck = "true";
    String url = "test.jira.server.example.com";
    String passwordValue = "a test value";
    Predicate<String> hasEnvVarCheck = (variableName) -> !JiraServerEnvironmentVariableHandlerFactory.VARIABLE_NAMES.contains(variableName);
    Mockito.when(environment.containsProperty(Mockito.anyString())).thenReturn(Boolean.TRUE);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, JiraServerEnvironmentVariableHandlerFactory.DISABLE_PLUGIN_KEY, disablePluginCheck);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, JiraServerEnvironmentVariableHandlerFactory.URL_KEY, url);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, JiraServerEnvironmentVariableHandlerFactory.PASSWORD_KEY, passwordValue);
    EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
    EnvironmentVariableHandlerFactory factory = new JiraServerEnvironmentVariableHandlerFactory(configAccessor, environmentVariableUtility);
    EnvironmentVariableHandler handler = factory.build();
    EnvironmentProcessingResult result = handler.updateFromEnvironment();
    assertEquals(ChannelKeys.JIRA_SERVER.getDisplayName(), handler.getName());
    assertEquals(expectedVariableNames, handler.getVariableNames());
    assertTrue(result.hasValues());
    assertEquals(disablePluginCheck, result.getVariableValue(JiraServerEnvironmentVariableHandlerFactory.DISABLE_PLUGIN_KEY).orElse("Disable plugin check value missing"));
    assertEquals(url, result.getVariableValue(JiraServerEnvironmentVariableHandlerFactory.URL_KEY).orElse("Url value missing"));
    assertEquals(AlertConstants.MASKED_VALUE, result.getVariableValue(JiraServerEnvironmentVariableHandlerFactory.PASSWORD_KEY).orElse("Password value missing"));
    assertTrue(result.getVariableValue(JiraServerEnvironmentVariableHandlerFactory.USERNAME_KEY).isEmpty());
}
Also used : EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Predicate(java.util.function.Predicate) Set(java.util.Set) EnvironmentVariableMockingUtil(com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil) JiraServerGlobalConfigAccessor(com.synopsys.integration.alert.channel.jira.server.database.accessor.JiraServerGlobalConfigAccessor) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) EnvironmentVariableHandlerFactory(com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Environment(org.springframework.core.env.Environment) EnvironmentVariableHandler(com.synopsys.integration.alert.environment.EnvironmentVariableHandler) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ChannelKeys(com.synopsys.integration.alert.descriptor.api.model.ChannelKeys) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AlertConstants(com.synopsys.integration.alert.api.common.model.AlertConstants) EnvironmentVariableHandlerFactory(com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Environment(org.springframework.core.env.Environment) EnvironmentVariableHandler(com.synopsys.integration.alert.environment.EnvironmentVariableHandler) JiraServerGlobalConfigAccessor(com.synopsys.integration.alert.channel.jira.server.database.accessor.JiraServerGlobalConfigAccessor) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) Test(org.junit.jupiter.api.Test)

Example 3 with EnvironmentVariableUtility

use of com.synopsys.integration.alert.environment.EnvironmentVariableUtility 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"));
}
Also used : EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Predicate(java.util.function.Predicate) Set(java.util.Set) EnvironmentVariableMockingUtil(com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) Stream(java.util.stream.Stream) EnvironmentVariableHandlerFactory(com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Environment(org.springframework.core.env.Environment) EnvironmentVariableHandler(com.synopsys.integration.alert.environment.EnvironmentVariableHandler) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) EmailGlobalConfigurationValidator(com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) ChannelKeys(com.synopsys.integration.alert.descriptor.api.model.ChannelKeys) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AlertConstants(com.synopsys.integration.alert.api.common.model.AlertConstants) EnvironmentVariableHandlerFactory(com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Environment(org.springframework.core.env.Environment) EnvironmentVariableHandler(com.synopsys.integration.alert.environment.EnvironmentVariableHandler) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) Test(org.junit.jupiter.api.Test)

Example 4 with EnvironmentVariableUtility

use of com.synopsys.integration.alert.environment.EnvironmentVariableUtility 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());
}
Also used : EnvironmentVariableHandlerFactory(com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Environment(org.springframework.core.env.Environment) EnvironmentVariableHandler(com.synopsys.integration.alert.environment.EnvironmentVariableHandler) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) Test(org.junit.jupiter.api.Test)

Example 5 with EnvironmentVariableUtility

use of com.synopsys.integration.alert.environment.EnvironmentVariableUtility 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());
}
Also used : FieldModelProcessor(com.synopsys.integration.alert.common.rest.FieldModelProcessor) SettingsUtility(com.synopsys.integration.alert.common.descriptor.accessor.SettingsUtility) EnvironmentVariableProcessor(com.synopsys.integration.alert.environment.EnvironmentVariableProcessor) SettingsDescriptorKey(com.synopsys.integration.alert.component.settings.descriptor.SettingsDescriptorKey) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) DescriptorAccessor(com.synopsys.integration.alert.common.persistence.accessor.DescriptorAccessor) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) ConfigurationFieldModelConverter(com.synopsys.integration.alert.common.persistence.util.ConfigurationFieldModelConverter) EncryptionUtility(com.synopsys.integration.alert.common.security.EncryptionUtility) DescriptorMap(com.synopsys.integration.alert.common.descriptor.DescriptorMap) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) DescriptorProcessor(com.synopsys.integration.alert.common.descriptor.DescriptorProcessor) SettingsDescriptorKey(com.synopsys.integration.alert.component.settings.descriptor.SettingsDescriptorKey) Environment(org.springframework.core.env.Environment) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) Test(org.junit.jupiter.api.Test)

Aggregations

EnvironmentVariableUtility (com.synopsys.integration.alert.environment.EnvironmentVariableUtility)24 Test (org.junit.jupiter.api.Test)24 Environment (org.springframework.core.env.Environment)24 EnvironmentProcessingResult (com.synopsys.integration.alert.environment.EnvironmentProcessingResult)17 EnvironmentVariableHandler (com.synopsys.integration.alert.environment.EnvironmentVariableHandler)17 EnvironmentVariableHandlerFactory (com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory)17 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)7 AlertConstants (com.synopsys.integration.alert.api.common.model.AlertConstants)5 JiraServerGlobalConfigAccessor (com.synopsys.integration.alert.channel.jira.server.database.accessor.JiraServerGlobalConfigAccessor)5 ChannelKeys (com.synopsys.integration.alert.descriptor.api.model.ChannelKeys)5 EnvironmentVariableMockingUtil (com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil)5 Set (java.util.Set)5 Predicate (java.util.function.Predicate)5 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)5 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 Mockito (org.mockito.Mockito)5 EmailGlobalConfigAccessor (com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor)4 DescriptorMap (com.synopsys.integration.alert.common.descriptor.DescriptorMap)4 DescriptorProcessor (com.synopsys.integration.alert.common.descriptor.DescriptorProcessor)4