Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
[SELC-4929] refactor: Replace Notification Manager with AWS SDK (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuraf authored May 20, 2024
1 parent e180799 commit f6baf2d
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 828 deletions.
37 changes: 5 additions & 32 deletions app/src/main/resources/config/core-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,13 @@ mscore.destination-mails = ${DESTINATION_MAILS:}
mscore.enableSendDelegationMail=${SEND_DELEGATION_PEC:false}
mscore.sendEmailToInstitution = ${ONBOARDING_SEND_EMAIL_TO_INSTITUTION:false}
mscore.institutionAlternativeEmail = ${ONBOARDING_INSTITUTION_ALTERNATIVE_EMAIL}
mscore.onboarding-expiring-date = ${ONBOARDING_EXPIRING_DATE:60}

mscore.mail-template.placeholders.onboarding.completePath = ${MAIL_TEMPLATE_COMPLETE_PATH}
mscore.mail-template.placeholders.onboarding.completePathPt = ${MAIL_TEMPLATE_PT_COMPLETE_PATH}
mscore.mail-template.placeholders.onboarding.completePathFd = ${MAIL_TEMPLATE_FD_COMPLETE_NOTIFICATION_PATH}
mscore.mail-template.placeholders.onboarding.completeProductName = productName
mscore.mail-template.placeholders.onboarding.completeSelfcarePlaceholder= ${SELFCARE_URL}
mscore.mail-template.placeholders.onboarding.completeSelfcareName = selfcareURL

mscore.mail-template.placeholders.onboarding.autocompletePath = ${MAIL_TEMPLATE_AUTOCOMPLETE_PATH:}
mscore.mail-template.placeholders.onboarding.delegationNotificationPath = ${MAIL_TEMPLATE_DELEGATION_NOTIFICATION_PATH}
mscore.mail-template.placeholders.onboarding.delegationUserNotificationPath = ${MAIL_TEMPLATE_DELEGATION_USER_NOTIFICATION_PATH}
mscore.mail-template.placeholders.onboarding.delegationPartnerName = partnerName

mscore.mail-template.placeholders.onboarding.path = ${MAIL_TEMPLATE_PATH}
mscore.mail-template.placeholders.onboarding.userName = requesterName
mscore.mail-template.placeholders.onboarding.userSurname = requesterSurname
mscore.mail-template.placeholders.onboarding.productName = productName
mscore.mail-template.placeholders.onboarding.institutionDescription = institutionName

mscore.mail-template.placeholders.onboarding.confirmTokenName= confirmTokenURL
mscore.mail-template.placeholders.onboarding.confirmTokenPlaceholder = ${MAIL_ONBOARDING_CONFIRMATION_LINK}
mscore.mail-template.placeholders.onboarding.rejectTokenName = rejectTokenURL
mscore.mail-template.placeholders.onboarding.rejectTokenPlaceholder = ${MAIL_ONBOARDING_REJECTION_LINK}
mscore.mail-template.placeholders.onboarding.adminLink = ${SELFCARE_ADMIN_NOTIFICATION_URL}

mscore.mail-template.placeholders.onboarding.notificationPath = ${MAIL_TEMPLATE_NOTIFICATION_PATH}
mscore.mail-template.placeholders.onboarding.registrationRequestPath = ${MAIL_TEMPLATE_REGISTRATION_REQUEST_PT_PATH}
mscore.mail-template.placeholders.onboarding.registrationNotificationAdminPath = ${MAIL_TEMPLATE_REGISTRATION_NOTIFICATION_ADMIN_PATH}
mscore.mail-template.placeholders.onboarding.notificationAdminEmail = ${ADDRESS_EMAIL_NOTIFICATION_ADMIN}
mscore.mail-template.placeholders.onboarding.notificationProductName = productName
mscore.mail-template.placeholders.onboarding.notificationRequesterName = requesterName
mscore.mail-template.placeholders.onboarding.notificationRequesterSurname = requesterSurname

mscore.mail-template.placeholders.onboarding.rejectPath = ${MAIL_TEMPLATE_REJECT_PATH}
mscore.mail-template.placeholders.onboarding.rejectProductName=productName
mscore.mail-template.placeholders.onboarding.rejectOnboardingUrlPlaceholder=onboardingUrl
mscore.mail-template.placeholders.onboarding.rejectOnboardingUrlValue=${MAIL_ONBOARDING_URL}


scheduler.threads.max-number=${THREADS_SCHEDULE_MAX_NUMBER:1}
scheduler.fixed-delay.delay=${SCHEDULER_FIXED_DELAY:20000}
Expand All @@ -55,3 +23,8 @@ scheduler.regenerate-kafka-queue.enabled=${SCHEDULER_REGENERATE_KAFKA_QUEUE_ENAB
mscore.blob-storage.container-product=${PRODUCT_STORAGE_CONTAINER:selc-d-product}
mscore.blob-storage.filepath-product = products.json
mscore.blob-storage.connection-string-product = ${BLOB_STORAGE_PRODUCT_CONNECTION_STRING:UseDevelopmentStorage=true;}

#SES
mscore.aws-ses-secret-id=${AWS_SES_ACCESS_KEY_ID:secret-id-example}
mscore.aws-ses-secret-key=${AWS_SES_SECRET_ACCESS_KEY:secret-key-example}
mscore.aws-ses-region=${AWS_SES_REGION:eu-south-1}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ public interface EmailConnector {

void sendMail(String templateName, List<String> destinationMail, File pdf, String productName, Map<String, String> mailParameters, String nameFile);

void sendMailPNPG(String templateName, String destinationMail, String businessName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public class CoreConfig {
private List<String> destinationMails;
private String institutionAlternativeEmail;
private boolean sendEmailToInstitution;
private Integer onboardingExpiringDate;
private boolean infoCamereEnable;
private boolean enableSendDelegationMail;
private BlobStorage blobStorage;
private String awsSesSecretId;
private String awsSesSecretKey;
private String awsSesRegion;

@Data
public static class BlobStorage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,11 @@
@ToString
public class MailTemplateConfig {

private String completePath;
private String completeProductName;
private String completeSelfcarePlaceholder;
private String completeSelfcareName;
private String completePathFd;
private String completePathPt;

private String autocompletePath;

private String delegationNotificationPath;
private String delegationUserNotificationPath;
private String delegationPartnerName;

private String path;
private String userName;
private String userSurname;
private String productName;
private String institutionDescription;

private String confirmTokenName;
private String confirmTokenPlaceholder;
private String rejectTokenName;
private String rejectTokenPlaceholder;
private String adminLink;

private String notificationPath;
private String notificationAdminEmail;
private String notificationProductName;
private String notificationRequesterName;
private String notificationRequesterSurname;

private String rejectPath;
private String rejectProductName;
private String rejectOnboardingUrlPlaceholder;
private String rejectOnboardingUrlValue;

private String registrationRequestPath;
private String registrationNotificationAdminPath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static it.pagopa.selfcare.mscore.constant.GenericError.ERROR_DURING_COMPRESS_FILE;
import static it.pagopa.selfcare.mscore.constant.GenericError.ERROR_DURING_SEND_MAIL;
import static it.pagopa.selfcare.mscore.constant.ProductId.PROD_PN;

@Slf4j
@Service
Expand Down Expand Up @@ -83,42 +80,6 @@ public void sendMail(String templateName, List<String> destinationMail, File pdf
log.trace("sendMessage end");
}

@Override
public void sendMailPNPG(String templateName, String destinationMail, String businessName) {
log.trace("sendMailPNPG start");
log.debug("sendMailPNPG templateName = {}, destinationMail = {}, businessName = {}", templateName, destinationMail, businessName);
try {
log.info("START - sendMail to {}, for product {}", destinationMail, PROD_PN);
String template = fileStorageConnector.getTemplateFile(templateName);
MailTemplate mailTemplate = mapper.readValue(template, MailTemplate.class);
Map<String, String> mailParameters = new HashMap<>();
mailParameters.put("businessName", businessName);
String html = StringSubstitutor.replace(mailTemplate.getBody(), mailParameters);
log.trace("sendMessage start");
MimeMessage mimeMessage = mailSender.createMimeMessage();

MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");

List<String> destinationMails = Objects.nonNull(coreConfig.getDestinationMails()) && !coreConfig.getDestinationMails().isEmpty()
? coreConfig.getDestinationMails()
: List.of(destinationMail);

log.debug("sendMailPNPG destinationMails = {}", destinationMails);

message.setSubject(mailTemplate.getSubject());
message.setFrom(coreConfig.getSenderMail());
message.setTo(destinationMails.toArray(new String[0]));
message.setText(html, true);

mailSender.send(mimeMessage);
log.info("END - sendMail to {}, for product {}", destinationMail, PROD_PN);
} catch (Exception e) {
log.error(ERROR_DURING_SEND_MAIL.getMessage() + ":", e.getMessage(), e);
throw new MsCoreException(ERROR_DURING_SEND_MAIL.getMessage(), ERROR_DURING_SEND_MAIL.getCode());
}
log.trace("sendMailPNPG end");
}

public byte[] zipBytes(String filename, File pdf) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,76 +97,5 @@ void testSendMail2() {
verify(fileStorageConnector).getTemplateFile(any());
}

@Test
void testSendMailPNPG() throws IOException {
final String senderMail = "senderMail";
ArrayList<String> destinationMails = new ArrayList<>();
final String destinationMail = "destinationMail";
final String templateName = "templateName";
final String templateFile = "templateFile";
final String businessName = "businessName";

when(fileStorageConnector.getTemplateFile(any())).thenReturn(templateFile);
MailTemplate mailTemplate = new MailTemplate();
mailTemplate.setBody("body");
mailTemplate.setSubject("subject");
when(mapper.readValue(anyString(), eq(MailTemplate.class))).thenReturn(mailTemplate);

MimeMessage mimeMessage = mock(MimeMessage.class);
when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);
when(coreConfig.getSenderMail()).thenReturn(senderMail);
when(coreConfig.getDestinationMails()).thenReturn(destinationMails);

Assertions.assertDoesNotThrow(() -> emailConnector.sendMailPNPG(templateName, destinationMail, businessName));
}

@Test
void testSendMailPNPG_changeDestinationMail() throws IOException {
final String senderMail = "senderMail";
final String destinationMail = "destinationMail";
ArrayList<String> destinationMails = new ArrayList<>();
destinationMails.add(destinationMail);
final String templateName = "templateName";
final String templateFile = "templateFile";
final String businessName = "businessName";

when(fileStorageConnector.getTemplateFile(any())).thenReturn(templateFile);
MailTemplate mailTemplate = new MailTemplate();
mailTemplate.setBody("body");
mailTemplate.setSubject("subject");
when(mapper.readValue(anyString(), eq(MailTemplate.class))).thenReturn(mailTemplate);

MimeMessage mimeMessage = mock(MimeMessage.class);
when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);
when(coreConfig.getSenderMail()).thenReturn(senderMail);
when(coreConfig.getDestinationMails()).thenReturn(destinationMails);

Assertions.assertDoesNotThrow(() -> emailConnector.sendMailPNPG(templateName, destinationMail, businessName));
}

@Test
void testSendMailPGNG_exception() throws JsonProcessingException {
final String senderMail = "senderMail";
final String destinationMail = "destinationMail";
final String templateName = "templateName";
final String templateFile = "templateFile";
final String businessName = "businessName";

when(fileStorageConnector.getTemplateFile(any())).thenReturn(templateFile);
MailTemplate mailTemplate = new MailTemplate();
mailTemplate.setBody("body");
mailTemplate.setSubject("subject");
when(mapper.readValue(anyString(), eq(MailTemplate.class))).thenReturn(mailTemplate);

MimeMessage mimeMessage = mock(MimeMessage.class);
when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);
when(coreConfig.getSenderMail()).thenReturn(senderMail);
when(coreConfig.getDestinationMails()).thenReturn(null);

doThrow(RuntimeException.class).when(javaMailSender).send(any(MimeMessage.class));
Assertions.assertThrows(MsCoreException.class, () -> emailConnector.sendMailPNPG(templateName,destinationMail,businessName));

}

}

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ses</artifactId>
<version>2.20.88</version>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit f6baf2d

Please sign in to comment.