Search in sources :

Example 61 with Environment

use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.

the class JiraServerEnvironmentHandlerFactoryTestIT method setupMockedEnvironment.

private Environment setupMockedEnvironment() {
    Environment environment = Mockito.mock(Environment.class);
    Predicate<String> hasEnvVarCheck = (variableName) -> !JiraServerEnvironmentVariableHandlerFactory.VARIABLE_NAMES.contains(variableName);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, JiraServerEnvironmentVariableHandlerFactory.DISABLE_PLUGIN_KEY, TEST_DISABLE_PLUGIN_CHECK);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, JiraServerEnvironmentVariableHandlerFactory.URL_KEY, TEST_URL);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, JiraServerEnvironmentVariableHandlerFactory.PASSWORD_KEY, TEST_PASSWORD);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, JiraServerEnvironmentVariableHandlerFactory.USERNAME_KEY, TEST_USER);
    return environment;
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Autowired(org.springframework.beans.factory.annotation.Autowired) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) EnvironmentVariableHandler(com.synopsys.integration.alert.environment.EnvironmentVariableHandler) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DateUtils(com.synopsys.integration.alert.common.util.DateUtils) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Predicate(java.util.function.Predicate) EnvironmentVariableMockingUtil(com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil) UUID(java.util.UUID) JiraServerEnvironmentVariableHandlerFactory(com.synopsys.integration.alert.channel.jira.server.environment.JiraServerEnvironmentVariableHandlerFactory) Config(com.synopsys.integration.alert.common.rest.model.Config) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) AlertRestConstants(com.synopsys.integration.alert.common.rest.AlertRestConstants) JiraServerGlobalConfigAccessor(com.synopsys.integration.alert.channel.jira.server.database.accessor.JiraServerGlobalConfigAccessor) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) EnvironmentVariableHandlerFactory(com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory) Environment(org.springframework.core.env.Environment) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ChannelKeys(com.synopsys.integration.alert.descriptor.api.model.ChannelKeys) JiraServerGlobalConfigModel(com.synopsys.integration.alert.channel.jira.server.model.JiraServerGlobalConfigModel) AlertConstants(com.synopsys.integration.alert.api.common.model.AlertConstants) Environment(org.springframework.core.env.Environment)

Example 62 with Environment

use of org.springframework.core.env.Environment 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 63 with Environment

use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.

the class EnvironmentVariableProcessorTest method testUpdatingHandler.

@Test
void testUpdatingHandler() {
    Environment environment = Mockito.mock(Environment.class);
    for (String variableName : EnvironmentTestHandlerFactory.VARIABLE_NAMES) {
        Mockito.when(environment.containsProperty(variableName)).thenReturn(Boolean.TRUE);
        Mockito.when(environment.getProperty(variableName)).thenReturn(EnvironmentTestHandlerFactory.DEFAULT_VALUE);
    }
    EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
    EnvironmentTestHandlerFactory handler = new EnvironmentTestHandlerFactory(environmentVariableUtility);
    EnvironmentVariableProcessor processor = new EnvironmentVariableProcessor(List.of(handler));
    processor.updateConfigurations();
    assertTrue(handler.hasUpdateOccurred());
    EnvironmentProcessingResult result = handler.getUpdatedProperties().orElseThrow(() -> new AssertionError("Properties should exist"));
    assertTrue(result.hasValues());
    assertTrue(EnvironmentTestHandlerFactory.VARIABLE_NAMES.containsAll(result.getVariableNames()));
}
Also used : Environment(org.springframework.core.env.Environment) Test(org.junit.jupiter.api.Test)

Example 64 with Environment

use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.

the class EnvironmentVariableProcessorTest method testMissingEnvironmentVariablesHandler.

@Test
void testMissingEnvironmentVariablesHandler() {
    Environment environment = Mockito.mock(Environment.class);
    EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
    EnvironmentTestHandlerFactory handler = new EnvironmentTestHandlerFactory(environmentVariableUtility);
    EnvironmentVariableProcessor processor = new EnvironmentVariableProcessor(List.of(handler));
    processor.updateConfigurations();
    assertFalse(handler.hasUpdateOccurred());
    assertFalse(handler.getUpdatedProperties().stream().allMatch(EnvironmentProcessingResult::hasValues));
}
Also used : Environment(org.springframework.core.env.Environment) Test(org.junit.jupiter.api.Test)

Example 65 with Environment

use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.

the class EnvironmentVariableUtilityTest method testKeyConversion.

@Test
void testKeyConversion() {
    Environment environment = Mockito.mock(Environment.class);
    EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
    String actualPropertyKey = environmentVariableUtility.convertKeyToProperty(DESCRIPTOR_KEY, "specific.property.key");
    assertEquals(WORKING_PROPERTY_KEY, actualPropertyKey);
}
Also used : Environment(org.springframework.core.env.Environment) Test(org.junit.jupiter.api.Test)

Aggregations

Environment (org.springframework.core.env.Environment)161 Test (org.junit.jupiter.api.Test)68 StandardEnvironment (org.springframework.core.env.StandardEnvironment)30 EnvironmentVariableUtility (com.synopsys.integration.alert.environment.EnvironmentVariableUtility)26 MockEnvironment (org.springframework.mock.env.MockEnvironment)24 SpringApplication (org.springframework.boot.SpringApplication)21 EnvironmentProcessingResult (com.synopsys.integration.alert.environment.EnvironmentProcessingResult)19 EnvironmentVariableHandler (com.synopsys.integration.alert.environment.EnvironmentVariableHandler)19 EnvironmentVariableHandlerFactory (com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory)19 HashMap (java.util.HashMap)13 ResourceLoader (org.springframework.core.io.ResourceLoader)13 Collectors (java.util.stream.Collectors)11 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)10 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)9 MutablePropertySources (org.springframework.core.env.MutablePropertySources)8 AlertConstants (com.synopsys.integration.alert.api.common.model.AlertConstants)7 ChannelKeys (com.synopsys.integration.alert.descriptor.api.model.ChannelKeys)7 EnvironmentVariableMockingUtil (com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil)7 Map (java.util.Map)7 Predicate (java.util.function.Predicate)7