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

Commit

Permalink
Merge branch 'release-dev' into feature/SELC-3684
Browse files Browse the repository at this point in the history
  • Loading branch information
pierpaolodidato89 authored Jan 11, 2024
2 parents 8ce5a65 + d77636c commit aaa10e1
Show file tree
Hide file tree
Showing 23 changed files with 273 additions and 257 deletions.
22 changes: 11 additions & 11 deletions app/src/main/resources/swagger/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"name" : "Token",
"description" : "Token Controller"
}, {
"name" : "scheduler",
"description" : "Scheduler Controller"
"name" : "kafka",
"description" : "Queue Notification Controller"
} ],
"paths" : {
"/migration/institution" : {
Expand Down Expand Up @@ -4488,12 +4488,12 @@
} ]
}
},
"/scheduler" : {
"/notification-event/contracts" : {
"post" : {
"tags" : [ "scheduler" ],
"summary" : "start",
"description" : "Service to start scheduler to resend old messages to DL",
"operationId" : "startUsingPOST",
"tags" : [ "kafka" ],
"summary" : "resendContracts",
"description" : "Service to resend contract notifications on SC-Contracts topic",
"operationId" : "resendContractsUsingPOST",
"parameters" : [ {
"name" : "size",
"in" : "query",
Expand Down Expand Up @@ -4555,12 +4555,12 @@
} ]
}
},
"/scheduler/users" : {
"/notification-event/users" : {
"post" : {
"tags" : [ "scheduler" ],
"summary" : "startUsers",
"tags" : [ "kafka" ],
"summary" : "resendUsers",
"description" : "Service to resend old user onboardings to the SCUsers kafka queue, it can send the onboardings of a single user or also retrieve all the users for a given set of products in a paged manner by passing page and size",
"operationId" : "startUsersUsingPOST",
"operationId" : "resendUsersUsingPOST",
"parameters" : [ {
"name" : "size",
"in" : "query",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SwaggerConfigTest {
UserService userService;

@MockBean
SchedulerService schedulerService;
QueueNotificationService queueNotificationService;

@Autowired
WebApplicationContext context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ 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 @@ -21,13 +21,16 @@
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 @@ -79,6 +82,43 @@ 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,5 +97,76 @@ 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));

}

}

Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
public class DelegationServiceImpl implements DelegationService {

private final DelegationConnector delegationConnector;
private final NotificationService notificationService;
private final MailNotificationService notificationService;
private final InstitutionService institutionService;
private static final String PROD_PAGOPA = "prod-pagopa";

public DelegationServiceImpl(DelegationConnector delegationConnector,
NotificationService notificationService,
MailNotificationService notificationService,
InstitutionService institutionService) {
this.delegationConnector = delegationConnector;
this.notificationService = notificationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;
import java.util.Map;

public interface NotificationService {
public interface MailNotificationService {

void setCompletedPGOnboardingMail(String destinationMail, String businessName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import it.pagopa.selfcare.mscore.config.CoreConfig;
import it.pagopa.selfcare.mscore.config.MailTemplateConfig;
import it.pagopa.selfcare.mscore.core.util.MailParametersMapper;
import it.pagopa.selfcare.mscore.exception.MsCoreException;
import it.pagopa.selfcare.mscore.model.institution.Institution;
import it.pagopa.selfcare.mscore.model.institution.WorkContact;
import it.pagopa.selfcare.mscore.model.notification.MessageRequest;
Expand All @@ -22,12 +21,12 @@
import java.io.File;
import java.util.*;

import static it.pagopa.selfcare.mscore.constant.GenericError.ERROR_DURING_SEND_MAIL;
import static it.pagopa.selfcare.mscore.constant.ProductId.*;
import static it.pagopa.selfcare.mscore.constant.ProductId.PROD_FD;
import static it.pagopa.selfcare.mscore.constant.ProductId.PROD_FD_GARANTITO;

@Slf4j
@Service
public class NotificationServiceImpl implements NotificationService {
public class MailNotificationServiceImpl implements MailNotificationService {

private static final String MAIL_PARAMETER_LOG = "mailParameters: {}";
private static final String DESTINATION_MAIL_LOG = "destinationMails: {}";
Expand All @@ -43,15 +42,15 @@ public class NotificationServiceImpl implements NotificationService {
private final CoreConfig coreConfig;

@Autowired
public NotificationServiceImpl(NotificationServiceConnector notificationConnector,
FileStorageConnector fileStorageConnector,
InstitutionConnector institutionConnector,
ProductConnector productConnector,
ObjectMapper mapper,
MailTemplateConfig mailTemplateConfig,
EmailConnector emailConnector,
MailParametersMapper mailParametersMapper,
CoreConfig coreConfig) {
public MailNotificationServiceImpl(NotificationServiceConnector notificationConnector,
FileStorageConnector fileStorageConnector,
InstitutionConnector institutionConnector,
ProductConnector productConnector,
ObjectMapper mapper,
MailTemplateConfig mailTemplateConfig,
EmailConnector emailConnector,
MailParametersMapper mailParametersMapper,
CoreConfig coreConfig) {
this.notificationConnector = notificationConnector;
this.fileStorageConnector = fileStorageConnector;
this.institutionConnector = institutionConnector;
Expand All @@ -65,18 +64,10 @@ public NotificationServiceImpl(NotificationServiceConnector notificationConnecto

@Override
public void setCompletedPGOnboardingMail(String destinationMail, String businessName) {
try {
log.info("START - sendMail to {}, for product {}", destinationMail, PROD_PN);
String template = fileStorageConnector.getTemplateFile(mailTemplateConfig.getPath());
MailTemplate mailTemplate = mapper.readValue(template, MailTemplate.class);
MessageRequest messageRequest = constructMessageRequest(destinationMail, businessName, mailTemplate);
log.trace("sendMessage start");
notificationConnector.sendNotificationToUser(messageRequest);
} 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("sendMessage end");
log.trace("setCompletedPGOnboardingMail start");
log.debug("setCompletedPGOnboardingMail destinationMail = {}, businessName = {}", destinationMail, businessName);
emailConnector.sendMailPNPG(mailTemplateConfig.getPath(), destinationMail, businessName);
log.trace("setCompletedPGOnboardingMail end");
}

public void sendAutocompleteMail(List<String> destinationMail, Map<String, String> templateParameters, File file, String fileName, String productName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import it.pagopa.selfcare.mscore.model.QueueEvent;
import it.pagopa.selfcare.mscore.model.aggregation.UserInstitutionAggregation;
import it.pagopa.selfcare.mscore.model.aggregation.UserInstitutionFilter;
import it.pagopa.selfcare.mscore.model.institution.Billing;
import it.pagopa.selfcare.mscore.model.institution.Institution;
import it.pagopa.selfcare.mscore.model.institution.Onboarding;
import it.pagopa.selfcare.mscore.model.onboarding.*;
Expand Down Expand Up @@ -57,7 +56,7 @@ public class OnboardingServiceImpl implements OnboardingService {
private final UserRelationshipService userRelationshipService;
private final UserEventService userEventService;
private final ContractService contractService;
private final NotificationService notificationService;
private final MailNotificationService notificationService;
private final UserNotificationService userNotificationService;
private final PagoPaSignatureConfig pagoPaSignatureConfig;
private final MailTemplateConfig mailTemplateConfig;
Expand All @@ -71,7 +70,7 @@ public OnboardingServiceImpl(OnboardingDao onboardingDao,
UserRelationshipService userRelationshipService,
ContractService contractService,
UserEventService userEventService,
NotificationService notificationService,
MailNotificationService notificationService,
UserNotificationService userNotificationService,
PagoPaSignatureConfig pagoPaSignatureConfig,
OnboardingInstitutionStrategyFactory institutionStrategyFactory,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.pagopa.selfcare.mscore.core;

import java.util.List;
import java.util.Optional;

public interface QueueNotificationService {

void sendContracts(Optional<Integer> size, List<String> productsFilter);

void sendUsers(Optional<Integer> size, Optional<Integer> page, List<String> productsFilter, Optional<String> userId);
}
Loading

0 comments on commit aaa10e1

Please sign in to comment.