Search in sources :

Example 6 with EmailGlobalConfigAccessor

use of com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor in project hub-alert by blackducksoftware.

the class EmailGlobalConfigurationModelSaveActionsTest method updateInvalidConversionTest.

@Test
void updateInvalidConversionTest() {
    AtomicReference<EmailConfigurationEntity> savedEntity = new AtomicReference<>();
    AtomicReference<EmailConfigurationsPropertyEntity> savedProperty = new AtomicReference<>();
    EmailConfigurationRepository emailConfigurationRepository = Mockito.mock(EmailConfigurationRepository.class);
    EmailConfigurationPropertiesRepository emailConfigurationPropertiesRepository = Mockito.mock(EmailConfigurationPropertiesRepository.class);
    Mockito.when(emailConfigurationRepository.save(Mockito.any(EmailConfigurationEntity.class))).thenAnswer(invocation -> {
        savedEntity.set(invocation.getArgument(0));
        return savedEntity.get();
    });
    Mockito.when(emailConfigurationRepository.findByName(Mockito.anyString())).thenAnswer(invocation -> Optional.ofNullable(savedEntity.get()));
    Mockito.when(emailConfigurationRepository.findById(Mockito.any())).thenAnswer(invocation -> Optional.ofNullable(savedEntity.get()));
    Mockito.when(emailConfigurationPropertiesRepository.saveAll(Mockito.any(List.class))).thenAnswer(invocation -> {
        Iterable<EmailConfigurationsPropertyEntity> iterable = invocation.getArgument(0);
        for (EmailConfigurationsPropertyEntity entity : iterable) {
            savedProperty.set(entity);
        }
        return List.of(savedProperty.get());
    });
    EmailGlobalConfigAccessor configurationAccessor = new EmailGlobalConfigAccessor(encryptionUtility, emailConfigurationRepository, emailConfigurationPropertiesRepository);
    EmailGlobalCrudActions crudActions = new EmailGlobalCrudActions(authorizationManager, configurationAccessor, validator);
    EmailGlobalConfigurationModelSaveActions saveActions = new EmailGlobalConfigurationModelSaveActions(converter, crudActions, configurationAccessor);
    String newPassword = "updatedPassword";
    String newHost = "updated." + TEST_SMTP_HOST;
    ConfigurationModel configurationModel = createDefaultConfigurationModel();
    saveActions.createConcreteModel(configurationModel);
    updateField(configurationModel, EmailPropertyKeys.JAVAMAIL_PORT_KEY.getPropertyKey(), "badport");
    updateField(configurationModel, EmailPropertyKeys.JAVAMAIL_HOST_KEY.getPropertyKey(), newHost);
    updateField(configurationModel, EmailPropertyKeys.JAVAMAIL_PORT_KEY.getPropertyKey(), newPassword);
    updateField(configurationModel, EmailPropertyKeys.JAVAMAIL_EHLO_KEY.getPropertyKey(), "false");
    saveActions.updateConcreteModel(configurationModel);
    // make sure the values are not the updated values
    EmailConfigurationEntity actualEntity = savedEntity.get();
    assertEquals(TEST_AUTH_PASSWORD, encryptionUtility.decrypt(actualEntity.getAuthPassword()));
    assertEquals(TEST_SMTP_HOST, actualEntity.getSmtpHost());
    assertEquals(Integer.valueOf(TEST_SMTP_PORT), actualEntity.getSmtpPort());
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) EmailConfigurationRepository(com.synopsys.integration.alert.channel.email.database.configuration.EmailConfigurationRepository) EmailConfigurationEntity(com.synopsys.integration.alert.channel.email.database.configuration.EmailConfigurationEntity) EmailConfigurationPropertiesRepository(com.synopsys.integration.alert.channel.email.database.configuration.properties.EmailConfigurationPropertiesRepository) AtomicReference(java.util.concurrent.atomic.AtomicReference) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) EmailGlobalCrudActions(com.synopsys.integration.alert.channel.email.action.EmailGlobalCrudActions) EmailConfigurationsPropertyEntity(com.synopsys.integration.alert.channel.email.database.configuration.properties.EmailConfigurationsPropertyEntity) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 7 with EmailGlobalConfigAccessor

use of com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor in project hub-alert by blackducksoftware.

the class EmailGlobalConfigurationModelSaveActionsTest method updateItemNotFoundTest.

@Test
void updateItemNotFoundTest() {
    AtomicReference<EmailConfigurationEntity> savedEntity = new AtomicReference<>();
    AtomicReference<EmailConfigurationsPropertyEntity> savedProperty = new AtomicReference<>();
    EmailConfigurationRepository emailConfigurationRepository = Mockito.mock(EmailConfigurationRepository.class);
    EmailConfigurationPropertiesRepository emailConfigurationPropertiesRepository = Mockito.mock(EmailConfigurationPropertiesRepository.class);
    Mockito.when(emailConfigurationRepository.save(Mockito.any(EmailConfigurationEntity.class))).thenAnswer(invocation -> {
        savedEntity.set(invocation.getArgument(0));
        return savedEntity.get();
    });
    Mockito.when(emailConfigurationRepository.findByName(Mockito.anyString())).thenAnswer(invocation -> Optional.ofNullable(savedEntity.get()));
    Mockito.when(emailConfigurationRepository.findById(Mockito.any())).thenAnswer(invocation -> Optional.ofNullable(savedEntity.get()));
    Mockito.when(emailConfigurationPropertiesRepository.saveAll(Mockito.any(List.class))).thenAnswer(invocation -> {
        Iterable<EmailConfigurationsPropertyEntity> iterable = invocation.getArgument(0);
        for (EmailConfigurationsPropertyEntity entity : iterable) {
            savedProperty.set(entity);
        }
        return List.of(savedProperty.get());
    });
    EmailGlobalConfigAccessor configurationAccessor = new EmailGlobalConfigAccessor(encryptionUtility, emailConfigurationRepository, emailConfigurationPropertiesRepository);
    EmailGlobalCrudActions crudActions = new EmailGlobalCrudActions(authorizationManager, configurationAccessor, validator);
    EmailGlobalConfigurationModelSaveActions saveActions = new EmailGlobalConfigurationModelSaveActions(converter, crudActions, configurationAccessor);
    String newPassword = "updatedPassword";
    String newHost = "updated." + TEST_SMTP_HOST;
    ConfigurationModel configurationModel = createDefaultConfigurationModel();
    saveActions.createConcreteModel(configurationModel);
    updateField(configurationModel, EmailPropertyKeys.JAVAMAIL_PORT_KEY.getPropertyKey(), "badport");
    updateField(configurationModel, EmailPropertyKeys.JAVAMAIL_HOST_KEY.getPropertyKey(), newHost);
    updateField(configurationModel, EmailPropertyKeys.JAVAMAIL_PORT_KEY.getPropertyKey(), newPassword);
    updateField(configurationModel, EmailPropertyKeys.JAVAMAIL_EHLO_KEY.getPropertyKey(), "false");
    saveActions.updateConcreteModel(configurationModel);
    // make sure the values are not the updated values
    EmailConfigurationEntity actualEntity = savedEntity.get();
    assertEquals(TEST_AUTH_PASSWORD, encryptionUtility.decrypt(actualEntity.getAuthPassword()));
    assertEquals(TEST_SMTP_HOST, actualEntity.getSmtpHost());
    assertEquals(Integer.valueOf(TEST_SMTP_PORT), actualEntity.getSmtpPort());
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) EmailConfigurationRepository(com.synopsys.integration.alert.channel.email.database.configuration.EmailConfigurationRepository) EmailConfigurationEntity(com.synopsys.integration.alert.channel.email.database.configuration.EmailConfigurationEntity) EmailConfigurationPropertiesRepository(com.synopsys.integration.alert.channel.email.database.configuration.properties.EmailConfigurationPropertiesRepository) AtomicReference(java.util.concurrent.atomic.AtomicReference) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) EmailGlobalCrudActions(com.synopsys.integration.alert.channel.email.action.EmailGlobalCrudActions) EmailConfigurationsPropertyEntity(com.synopsys.integration.alert.channel.email.database.configuration.properties.EmailConfigurationsPropertyEntity) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 8 with EmailGlobalConfigAccessor

use of com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor in project hub-alert by blackducksoftware.

the class EmailChannelTestIT method sendEmailTest.

@Test
@Tag(TestTags.CUSTOM_EXTERNAL_CONNECTION)
public void sendEmailTest() {
    MockAlertProperties testAlertProperties = new MockAlertProperties();
    String testEmailRecipient = testProperties.getProperty(TestPropertyKey.TEST_EMAIL_RECIPIENT);
    EmailAttachmentFileCreator emailAttachmentFileCreator = new EmailAttachmentFileCreator(testAlertProperties, new MessageContentGroupCsvCreator(), gson);
    FreemarkerTemplatingService freemarkerTemplatingService = new FreemarkerTemplatingService();
    EmailMessagingService emailMessagingService = new EmailMessagingService(freemarkerTemplatingService);
    JavamailPropertiesFactory javamailPropertiesFactory = new JavamailPropertiesFactory();
    EmailChannelMessagingService emailChannelMessagingService = new EmailChannelMessagingService(testAlertProperties, emailMessagingService, emailAttachmentFileCreator);
    EmailGlobalConfigModel emailGlobalConfig = createEmailGlobalConfig();
    EmailGlobalConfigAccessor emailConfigurationAccessor = Mockito.mock(EmailGlobalConfigAccessor.class);
    Mockito.when(emailConfigurationAccessor.getConfiguration()).thenReturn(Optional.of(emailGlobalConfig));
    JobEmailAddressValidator emailAddressValidator = Mockito.mock(JobEmailAddressValidator.class);
    Mockito.when(emailAddressValidator.validate(Mockito.any(), Mockito.anyCollection())).thenReturn(new ValidatedEmailAddresses(Set.of(testEmailRecipient), Set.of()));
    EmailAddressGatherer emailAddressGatherer = new EmailAddressGatherer(null, null);
    EmailChannelMessageConverter emailChannelMessageConverter = new EmailChannelMessageConverter(new EmailChannelMessageFormatter());
    EmailChannelMessageSender emailChannelMessageSender = new EmailChannelMessageSender(emailConfigurationAccessor, emailAddressGatherer, emailChannelMessagingService, emailAddressValidator, javamailPropertiesFactory);
    EmailChannel emailChannel = new EmailChannel(emailChannelMessageConverter, emailChannelMessageSender);
    List<String> emailAddresses = List.of(testEmailRecipient);
    EmailJobDetailsModel emailJobDetails = new EmailJobDetailsModel(null, EmailChannelTestIT.class.getSimpleName(), false, true, EmailAttachmentFormat.NONE.name(), emailAddresses);
    EmailITTestAssertions.assertSendSimpleMessageSuccess(emailChannel, emailJobDetails);
}
Also used : MessageContentGroupCsvCreator(com.synopsys.integration.alert.channel.email.attachment.MessageContentGroupCsvCreator) EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) EmailMessagingService(com.synopsys.integration.alert.service.email.EmailMessagingService) EmailAttachmentFileCreator(com.synopsys.integration.alert.channel.email.attachment.EmailAttachmentFileCreator) EmailAddressGatherer(com.synopsys.integration.alert.channel.email.distribution.address.EmailAddressGatherer) FreemarkerTemplatingService(com.synopsys.integration.alert.service.email.template.FreemarkerTemplatingService) JobEmailAddressValidator(com.synopsys.integration.alert.channel.email.distribution.address.JobEmailAddressValidator) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) MockAlertProperties(com.synopsys.integration.alert.test.common.MockAlertProperties) EmailJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.EmailJobDetailsModel) JavamailPropertiesFactory(com.synopsys.integration.alert.service.email.JavamailPropertiesFactory) ValidatedEmailAddresses(com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 9 with EmailGlobalConfigAccessor

use of com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor 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 10 with EmailGlobalConfigAccessor

use of com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor 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)

Aggregations

EmailGlobalConfigAccessor (com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor)22 Test (org.junit.jupiter.api.Test)22 EmailGlobalConfigurationValidator (com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator)12 EmailGlobalConfigModel (com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel)11 PermissionKey (com.synopsys.integration.alert.common.persistence.model.PermissionKey)10 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)10 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)10 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)10 AuthenticationTestUtils (com.synopsys.integration.alert.test.common.AuthenticationTestUtils)10 EmailGlobalCrudActions (com.synopsys.integration.alert.channel.email.action.EmailGlobalCrudActions)6 EmailConfigurationEntity (com.synopsys.integration.alert.channel.email.database.configuration.EmailConfigurationEntity)6 EmailConfigurationRepository (com.synopsys.integration.alert.channel.email.database.configuration.EmailConfigurationRepository)6 EmailConfigurationPropertiesRepository (com.synopsys.integration.alert.channel.email.database.configuration.properties.EmailConfigurationPropertiesRepository)6 EmailConfigurationsPropertyEntity (com.synopsys.integration.alert.channel.email.database.configuration.properties.EmailConfigurationsPropertyEntity)6 List (java.util.List)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)5 EnvironmentProcessingResult (com.synopsys.integration.alert.environment.EnvironmentProcessingResult)4 EnvironmentVariableHandler (com.synopsys.integration.alert.environment.EnvironmentVariableHandler)4 EnvironmentVariableHandlerFactory (com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory)4