Search in sources :

Example 6 with ValidatedEmailAddresses

use of com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses in project hub-alert by blackducksoftware.

the class EmailGlobalTestActionTest method testConfigMissingDestinationTest.

@Test
public void testConfigMissingDestinationTest() {
    AuthorizationManager authorizationManager = createAuthorizationManager(255);
    EmailGlobalConfigurationValidator validator = new EmailGlobalConfigurationValidator();
    EmailAddressGatherer emailAddressGatherer = Mockito.mock(EmailAddressGatherer.class);
    Mockito.when(emailAddressGatherer.gatherEmailAddresses(Mockito.any(), Mockito.any())).thenReturn(Set.of());
    JobEmailAddressValidator emailAddressValidator = Mockito.mock(JobEmailAddressValidator.class);
    Mockito.when(emailAddressValidator.validate(Mockito.any(), Mockito.anyCollection())).thenReturn(new ValidatedEmailAddresses(Set.of(), Set.of()));
    MockAlertProperties testAlertProperties = new MockAlertProperties();
    MessageContentGroupCsvCreator messageContentGroupCsvCreator = new MessageContentGroupCsvCreator();
    Gson gson = new Gson();
    EmailAttachmentFileCreator emailAttachmentFileCreator = new EmailAttachmentFileCreator(testAlertProperties, messageContentGroupCsvCreator, gson);
    FreemarkerTemplatingService freemarkerTemplatingService = new FreemarkerTemplatingService();
    EmailMessagingService emailMessagingService = new EmailMessagingService(freemarkerTemplatingService);
    EmailChannelMessagingService emailChannelMessagingService = new EmailChannelMessagingService(testAlertProperties, emailMessagingService, emailAttachmentFileCreator);
    JavamailPropertiesFactory javamailPropertiesFactory = new JavamailPropertiesFactory();
    EmailGlobalTestAction emailGlobalTestAction = new EmailGlobalTestAction(authorizationManager, validator, emailChannelMessagingService, javamailPropertiesFactory, configurationAccessor);
    ConfigurationTestResult testResult = emailGlobalTestAction.testConfigModelContent("", new EmailGlobalConfigModel());
    assertFalse(testResult.isSuccess());
}
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) Gson(com.google.gson.Gson) EmailAddressGatherer(com.synopsys.integration.alert.channel.email.distribution.address.EmailAddressGatherer) ConfigurationTestResult(com.synopsys.integration.alert.common.message.model.ConfigurationTestResult) EmailGlobalConfigurationValidator(com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator) FreemarkerTemplatingService(com.synopsys.integration.alert.service.email.template.FreemarkerTemplatingService) JobEmailAddressValidator(com.synopsys.integration.alert.channel.email.distribution.address.JobEmailAddressValidator) MockAlertProperties(com.synopsys.integration.alert.test.common.MockAlertProperties) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) ValidatedEmailAddresses(com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses) EmailChannelMessagingService(com.synopsys.integration.alert.channel.email.distribution.EmailChannelMessagingService) JavamailPropertiesFactory(com.synopsys.integration.alert.service.email.JavamailPropertiesFactory) Test(org.junit.jupiter.api.Test)

Example 7 with ValidatedEmailAddresses

use of com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses in project hub-alert by blackducksoftware.

the class EmailChannelMessageSender method sendMessages.

@Override
public MessageResult sendMessages(EmailJobDetailsModel emailJobDetails, List<EmailChannelMessageModel> emailMessages) throws AlertException {
    // Validation
    ValidatedEmailAddresses validatedAdditionalEmailAddresses;
    UUID jobId = emailJobDetails.getJobId();
    if (null != jobId) {
        validatedAdditionalEmailAddresses = emailAddressValidator.validate(jobId, emailJobDetails.getAdditionalEmailAddresses());
    } else {
        validatedAdditionalEmailAddresses = new ValidatedEmailAddresses(new HashSet<>(emailJobDetails.getAdditionalEmailAddresses()), Set.of());
    }
    Set<String> invalidEmailAddresses = validatedAdditionalEmailAddresses.getInvalidEmailAddresses();
    if (!invalidEmailAddresses.isEmpty()) {
        emailJobDetails = new EmailJobDetailsModel(emailJobDetails.getJobId(), emailJobDetails.getSubjectLine().orElse(null), emailJobDetails.isProjectOwnerOnly(), emailJobDetails.isAdditionalEmailAddressesOnly(), emailJobDetails.getAttachmentFileType(), new ArrayList<>(validatedAdditionalEmailAddresses.getValidEmailAddresses()));
    }
    // Distribution
    EmailGlobalConfigModel emailServerConfiguration = emailGlobalConfigAccessor.getConfiguration().orElseThrow(() -> new AlertConfigurationException("ERROR: Missing Email global config."));
    SmtpConfig smtpConfig = SmtpConfig.builder().setJavamailProperties(javamailPropertiesFactory.createJavaMailProperties(emailServerConfiguration)).setSmtpFrom(emailServerConfiguration.getSmtpFrom().orElse(null)).setSmtpHost(emailServerConfiguration.getSmtpHost().orElse(null)).setSmtpPort(emailServerConfiguration.getSmtpPort().orElse(-1)).setSmtpAuth(emailServerConfiguration.getSmtpAuth().orElse(false)).setSmtpUsername(emailServerConfiguration.getSmtpUsername().orElse(null)).setSmtpPassword(emailServerConfiguration.getSmtpPassword().orElse(null)).build();
    int totalEmailsSent = 0;
    for (EmailChannelMessageModel message : emailMessages) {
        Set<String> projectHrefs = message.getSource().map(ProjectMessage::getProject).flatMap(LinkableItem::getUrl).map(Set::of).orElse(Set.of());
        Set<String> gatheredEmailAddresses = emailAddressGatherer.gatherEmailAddresses(emailJobDetails, projectHrefs);
        if (gatheredEmailAddresses.isEmpty()) {
            if (invalidEmailAddresses.isEmpty()) {
                throw new AlertException("Could not determine what email addresses to send this content to");
            } else {
                String invalidEmailAddressesString = StringUtils.join(invalidEmailAddresses, ", ");
                throw new AlertException(String.format("No valid email addresses to send this content to. The following email addresses were invalid: %s", invalidEmailAddressesString));
            }
        }
        EmailTarget emailTarget = emailChannelMessagingService.createTarget(message, gatheredEmailAddresses);
        Optional<ProjectMessage> optionalProjectMessage = message.getSource();
        if (optionalProjectMessage.isPresent()) {
            EmailAttachmentFormat attachmentFormat = EmailAttachmentFormat.getValueSafely(emailJobDetails.getAttachmentFileType());
            emailChannelMessagingService.sendMessageWithAttachedProjectMessage(smtpConfig, emailTarget, optionalProjectMessage.get(), attachmentFormat);
        } else {
            emailChannelMessagingService.sendMessage(smtpConfig, emailTarget);
        }
        totalEmailsSent += emailTarget.getEmailAddresses().size();
    }
    // Reporting
    if (!invalidEmailAddresses.isEmpty()) {
        String invalidEmailAddressesString = StringUtils.join(invalidEmailAddresses, ", ");
        String errorMessage = String.format("No emails were sent to the following recipients because they were invalid: %s", invalidEmailAddressesString);
        AlertFieldStatus errorStatus = new AlertFieldStatus(EmailDescriptor.KEY_EMAIL_ADDITIONAL_ADDRESSES, FieldStatusSeverity.ERROR, errorMessage);
        return new MessageResult(errorMessage, List.of(errorStatus));
    }
    return new MessageResult(String.format("Successfully sent %d email(s)", totalEmailsSent));
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) SmtpConfig(com.synopsys.integration.alert.service.email.SmtpConfig) LinkableItem(com.synopsys.integration.alert.common.message.model.LinkableItem) ArrayList(java.util.ArrayList) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) EmailAttachmentFormat(com.synopsys.integration.alert.channel.email.attachment.EmailAttachmentFormat) ProjectMessage(com.synopsys.integration.alert.processor.api.extract.model.project.ProjectMessage) EmailJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.EmailJobDetailsModel) EmailTarget(com.synopsys.integration.alert.service.email.EmailTarget) UUID(java.util.UUID) ValidatedEmailAddresses(com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) HashSet(java.util.HashSet) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)

Example 8 with ValidatedEmailAddresses

use of com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses in project hub-alert by blackducksoftware.

the class EmailGlobalFieldModelTestActionTest method testConfigMissingDestinationTest.

@Test
public void testConfigMissingDestinationTest() {
    JobEmailAddressValidator emailAddressValidator = Mockito.mock(JobEmailAddressValidator.class);
    Mockito.when(emailAddressValidator.validate(Mockito.any(), Mockito.anyCollection())).thenReturn(new ValidatedEmailAddresses(Set.of(), Set.of()));
    MockAlertProperties testAlertProperties = new MockAlertProperties();
    MessageContentGroupCsvCreator messageContentGroupCsvCreator = new MessageContentGroupCsvCreator();
    Gson gson = new Gson();
    EmailAttachmentFileCreator emailAttachmentFileCreator = new EmailAttachmentFileCreator(testAlertProperties, messageContentGroupCsvCreator, gson);
    FreemarkerTemplatingService freemarkerTemplatingService = new FreemarkerTemplatingService();
    EmailMessagingService emailMessagingService = new EmailMessagingService(freemarkerTemplatingService);
    EmailChannelMessagingService emailChannelMessagingService = new EmailChannelMessagingService(testAlertProperties, emailMessagingService, emailAttachmentFileCreator);
    JavamailPropertiesFactory javamailPropertiesFactory = new JavamailPropertiesFactory();
    EmailGlobalFieldModelTestAction emailGlobalFieldModelTestAction = new EmailGlobalFieldModelTestAction(emailChannelMessagingService, javamailPropertiesFactory);
    FieldModel validFieldModel = createFieldModelToTest("");
    try {
        emailGlobalFieldModelTestAction.testConfig("0", validFieldModel, new FieldUtility(Map.of()));
        fail("Expected an exception to be thrown");
    } catch (AlertException e) {
    // Pass
    }
}
Also used : MessageContentGroupCsvCreator(com.synopsys.integration.alert.channel.email.attachment.MessageContentGroupCsvCreator) EmailMessagingService(com.synopsys.integration.alert.service.email.EmailMessagingService) EmailAttachmentFileCreator(com.synopsys.integration.alert.channel.email.attachment.EmailAttachmentFileCreator) Gson(com.google.gson.Gson) FreemarkerTemplatingService(com.synopsys.integration.alert.service.email.template.FreemarkerTemplatingService) JobEmailAddressValidator(com.synopsys.integration.alert.channel.email.distribution.address.JobEmailAddressValidator) MockAlertProperties(com.synopsys.integration.alert.test.common.MockAlertProperties) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) ValidatedEmailAddresses(com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses) EmailChannelMessagingService(com.synopsys.integration.alert.channel.email.distribution.EmailChannelMessagingService) JavamailPropertiesFactory(com.synopsys.integration.alert.service.email.JavamailPropertiesFactory) FieldUtility(com.synopsys.integration.alert.common.persistence.accessor.FieldUtility) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Test(org.junit.jupiter.api.Test)

Aggregations

ValidatedEmailAddresses (com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses)8 JobEmailAddressValidator (com.synopsys.integration.alert.channel.email.distribution.address.JobEmailAddressValidator)7 EmailAttachmentFileCreator (com.synopsys.integration.alert.channel.email.attachment.EmailAttachmentFileCreator)6 MessageContentGroupCsvCreator (com.synopsys.integration.alert.channel.email.attachment.MessageContentGroupCsvCreator)6 EmailMessagingService (com.synopsys.integration.alert.service.email.EmailMessagingService)6 FreemarkerTemplatingService (com.synopsys.integration.alert.service.email.template.FreemarkerTemplatingService)6 MockAlertProperties (com.synopsys.integration.alert.test.common.MockAlertProperties)6 Gson (com.google.gson.Gson)5 EmailChannelMessagingService (com.synopsys.integration.alert.channel.email.distribution.EmailChannelMessagingService)5 EmailAddressGatherer (com.synopsys.integration.alert.channel.email.distribution.address.EmailAddressGatherer)5 Test (org.junit.jupiter.api.Test)5 JavamailPropertiesFactory (com.synopsys.integration.alert.service.email.JavamailPropertiesFactory)4 EmailGlobalConfigModel (com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel)4 EmailJobDetailsModel (com.synopsys.integration.alert.common.persistence.model.job.details.EmailJobDetailsModel)3 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)2 EmailGlobalConfigAccessor (com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor)2 EmailGlobalConfigurationValidator (com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator)2 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)2 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)1 EmailAttachmentFormat (com.synopsys.integration.alert.channel.email.attachment.EmailAttachmentFormat)1