diff --git a/src/main/java/it/pagopa/pn/ec/commons/utils/LogUtils.java b/src/main/java/it/pagopa/pn/ec/commons/utils/LogUtils.java index 32ceb3952..a98e04692 100644 --- a/src/main/java/it/pagopa/pn/ec/commons/utils/LogUtils.java +++ b/src/main/java/it/pagopa/pn/ec/commons/utils/LogUtils.java @@ -97,12 +97,8 @@ private LogUtils() { public static final String PEC_GET_ATTACHMENTS = "PecService.getAttachments()"; public static final String PEC_DOWNLOAD_ATTACHMENT = "PecService.downloadAttachment()"; public static final String PEC_SEND_MAIL = "PecService.sendMail()"; - public static final String PN_PEC_SEND_MAIL = "PnPecService.sendMail()"; - public static final String PEC_SEND_MESSAGE = "PnPecService.sendMessage()"; - public static final String PEC_GET_UNREAD_MESSAGES = "PnPecService.getUnreadMessages()"; + public static final String PEC_SEND_MESSAGE = "PecService.sendMessage()"; public static final String PEC_SET_MESSAGE_ID_IN_REQUEST_METADATA = "PecService.setMessageIdInRequestMetadata()"; - public static final String PEC_MARK_MESSAGE_AS_READ = "PnPecService.markMessageAsRead()"; - //PAPER public static final String SEND_PAPER_ENGAGE_REQUEST = "sendPaperEngageRequest"; @@ -222,8 +218,11 @@ private LogUtils() { //PN-PEC public static final String PN_PEC = "pn-pec"; - public static final String PEC_GET_MESSAGE_COUNT = "PnPecService.getMessagesCount()"; - public static final String PEC_DELETE_MESSAGE = "PnPecService.deleteMessage()"; + public static final String PN_EC_PEC_SEND_MAIL = "PnEcPecService.sendMail()"; + public static final String PN_EC_PEC_GET_UNREAD_MESSAGES = "PnEcPecService.getUnreadMessages()"; + public static final String PN_EC_PEC_GET_MESSAGE_COUNT = "PnEcPecService.getMessagesCount()"; + public static final String PN_EC_PEC_MARK_MESSAGE_AS_READ = "PnEcPecService.markMessageAsRead()"; + public static final String PN_EC_PEC_DELETE_MESSAGE = "PnEcPecService.deleteMessage()"; public static final String NOT_VALID_FOR_DELETE = "Event with requestId '{}' is not valid for delete."; } diff --git a/src/main/java/it/pagopa/pn/ec/pec/service/impl/PecService.java b/src/main/java/it/pagopa/pn/ec/pec/service/impl/PecService.java index 917ab4113..89e9cad49 100644 --- a/src/main/java/it/pagopa/pn/ec/pec/service/impl/PecService.java +++ b/src/main/java/it/pagopa/pn/ec/pec/service/impl/PecService.java @@ -28,11 +28,10 @@ import it.pagopa.pn.ec.pec.exception.MaxSizeExceededException; import it.pagopa.pn.ec.pec.model.pojo.PecPresaInCaricoInfo; import it.pagopa.pn.ec.rest.v1.dto.*; -import it.pagopa.pn.library.pec.service.PnPecService; +import it.pagopa.pn.library.pec.service.PnEcPecService; import lombok.CustomLog; import org.apache.commons.io.output.CountingOutputStream; import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.DependsOn; import org.springframework.scheduling.annotation.Scheduled; @@ -75,7 +74,7 @@ public class PecService extends PresaInCaricoService implements QueueOperationsService { private final SqsService sqsService; - private final PnPecService pnPecService; + private final PnEcPecService pnPecService; private final GestoreRepositoryCall gestoreRepositoryCall; private final AttachmentServiceImpl attachmentService; private final DownloadCall downloadCall; @@ -85,7 +84,7 @@ public class PecService extends PresaInCaricoService implements QueueOperationsS private final PnPecConfigurationProperties pnPecProps; private String idSaved; - protected PecService(AuthService authService,@Qualifier("pnPecServiceImpl") PnPecService pnPecService, GestoreRepositoryCall gestoreRepositoryCall, SqsService sqsService + protected PecService(AuthService authService, PnEcPecService pnPecService, GestoreRepositoryCall gestoreRepositoryCall, SqsService sqsService , AttachmentServiceImpl attachmentService, DownloadCall downloadCall, NotificationTrackerSqsName notificationTrackerSqsName, PecSqsQueueName pecSqsQueueName, @Value("${lavorazione-pec.max-thread-pool-size}") Integer maxThreadPoolSize, PnPecConfigurationProperties pnPecProps) { super(authService); this.pnPecService = pnPecService; diff --git a/src/main/java/it/pagopa/pn/ec/pec/utils/MessageIdUtils.java b/src/main/java/it/pagopa/pn/ec/pec/utils/MessageIdUtils.java index 700411bf8..c77ad27f1 100644 --- a/src/main/java/it/pagopa/pn/ec/pec/utils/MessageIdUtils.java +++ b/src/main/java/it/pagopa/pn/ec/pec/utils/MessageIdUtils.java @@ -5,6 +5,9 @@ import it.pagopa.pn.ec.pec.exception.MessageIdException; import org.springframework.util.Base64Utils; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; + public class MessageIdUtils { private static final String SEPARATORE = "~"; @@ -47,17 +50,22 @@ public static String encodeMessageId(String idClient, String idRequest) { */ public static PresaInCaricoInfo decodeMessageId(String messageId) { try { - var splitAtPipe = messageId.split(SEPARATORE); + var splitAtPipe = removeBracketsFromMessageId(messageId).split(SEPARATORE); var base64ClientId = splitAtPipe[0]; var base64RequestId = splitAtPipe[1].split(String.valueOf(DOMAIN.charAt(0)))[0]; var decodedClientId = new String(Base64Utils.decodeFromString(base64ClientId)); var decodedRequestId = new String(Base64Utils.decodeFromString(base64RequestId)); - //Rimuove le parentesi angolari da inizio clientID e fine requestID, se presenti. - return new PresaInCaricoInfo(decodedRequestId.endsWith(">") ? decodedRequestId.substring(0, decodedRequestId.length() - 1) : decodedRequestId, - decodedClientId.startsWith("<") ? decodedClientId.substring(1) : decodedClientId, - new StepError()); + return new PresaInCaricoInfo(decodedRequestId, decodedClientId, new StepError()); } catch (Exception e) { throw new MessageIdException.DecodeMessageIdException(); } } + + public static String removeBracketsFromMessageId(String messageId) { + //Rimuove le parentesi angolari dal messageID. + messageId = URLDecoder.decode(messageId, StandardCharsets.UTF_8); + if (messageId.startsWith("<") && messageId.endsWith(">")) + messageId = messageId.substring(1, messageId.length() - 1); + return messageId; + } } diff --git a/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/scheduler/ScaricamentoEsitiPecScheduler.java b/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/scheduler/ScaricamentoEsitiPecScheduler.java index d857f89c8..6a0179dd4 100644 --- a/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/scheduler/ScaricamentoEsitiPecScheduler.java +++ b/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/scheduler/ScaricamentoEsitiPecScheduler.java @@ -2,28 +2,28 @@ import it.pagopa.pn.commons.utils.MDCUtils; import it.pagopa.pn.library.pec.model.IPostacert; +import it.pagopa.pn.library.pec.model.pojo.PnEcPecListOfMessages; import it.pagopa.pn.library.pec.model.pojo.PnPostacert; import it.pagopa.pn.library.pec.service.DaticertService; import it.pagopa.pn.ec.commons.service.SqsService; import it.pagopa.pn.ec.scaricamentoesitipec.configurationproperties.ScaricamentoEsitiPecProperties; import it.pagopa.pn.ec.scaricamentoesitipec.model.pojo.RicezioneEsitiPecDto; import it.pagopa.pn.ec.scaricamentoesitipec.utils.ScaricamentoEsitiPecUtils; -import it.pagopa.pn.library.pec.pojo.PnListOfMessages; -import it.pagopa.pn.library.pec.service.PnPecService; +import it.pagopa.pn.library.pec.service.PnEcPecService; import lombok.CustomLog; import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; +import reactor.util.function.Tuples; import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Predicate; import static it.pagopa.pn.ec.commons.utils.EmailUtils.*; import static it.pagopa.pn.ec.commons.utils.LogUtils.*; -import static it.pagopa.pn.ec.pec.utils.MessageIdUtils.DOMAIN; +import static it.pagopa.pn.ec.pec.utils.MessageIdUtils.*; import static it.pagopa.pn.ec.scaricamentoesitipec.constant.PostacertTypes.POSTA_CERTIFICATA; @Component @@ -32,13 +32,13 @@ public class ScaricamentoEsitiPecScheduler { private final DaticertService daticertService; private final SqsService sqsService; private final ScaricamentoEsitiPecProperties scaricamentoEsitiPecProperties; - private final PnPecService pnPecService; + private final PnEcPecService pnPecService; @Value("${scaricamento-esiti-pec.limit-rate}") private Integer limitRate; @Value("${pn.ec.storage.sqs.messages.staging.bucket}") private String storageSqsMessagesStagingBucket; - public ScaricamentoEsitiPecScheduler(DaticertService daticertService, SqsService sqsService, ScaricamentoEsitiPecProperties scaricamentoEsitiPecProperties, @Qualifier("pnPecServiceImpl") PnPecService pnPecService) { + public ScaricamentoEsitiPecScheduler(DaticertService daticertService, SqsService sqsService, ScaricamentoEsitiPecProperties scaricamentoEsitiPecProperties, PnEcPecService pnPecService) { this.daticertService = daticertService; this.sqsService = sqsService; this.scaricamentoEsitiPecProperties = scaricamentoEsitiPecProperties; @@ -47,7 +47,7 @@ public ScaricamentoEsitiPecScheduler(DaticertService daticertService, SqsService private final Predicate isPostaCertificataPredicate = postacert -> postacert.getTipo().equals(POSTA_CERTIFICATA); private final Predicate endsWithDomainPredicate = postacert -> postacert.getDati().getMsgid().endsWith(DOMAIN); - private final Predicate hasNoMessages = pnListOfMessages -> Objects.isNull(pnListOfMessages) || Objects.isNull(pnListOfMessages.getMessages()) || pnListOfMessages.getMessages().isEmpty(); + private final Predicate hasNoMessages = pnEcPecListOfMessages -> Objects.isNull(pnEcPecListOfMessages) || Objects.isNull(pnEcPecListOfMessages.getMessages()) || pnEcPecListOfMessages.getMessages().isEmpty(); @Scheduled(cron = "${PnEcCronScaricamentoEsitiPec ?:0 */5 * * * *}") public void scaricamentoEsitiPecScheduler() { @@ -60,13 +60,15 @@ public void scaricamentoEsitiPecScheduler() { MDCUtils.addMDCToContextAndExecute(pnPecService.getMessageCount() .then(Mono.defer(() -> pnPecService.getUnreadMessages(Integer.parseInt(scaricamentoEsitiPecProperties.getMessagesLimit())))) .flatMap(pnGetMessagesResponse -> { - var listOfMessages = pnGetMessagesResponse.getPnListOfMessages(); + var listOfMessages = pnGetMessagesResponse.getPnEcPecListOfMessages(); if (hasNoMessages.test(listOfMessages)) hasMessages.set(false); return Mono.justOrEmpty(listOfMessages); }) - .flatMapIterable(PnListOfMessages::getMessages) - .flatMap(message -> { + .flatMapIterable(PnEcPecListOfMessages::getMessages) + .flatMap(pnEcMessage -> { + byte[] message = pnEcMessage.getMessage(); + String providerName = pnEcMessage.getProviderName(); var mimeMessage = getMimeMessage(message); var messageID = getMessageIdFromMimeMessage(mimeMessage); @@ -93,8 +95,7 @@ public void scaricamentoEsitiPecScheduler() { .map(postacert -> { var dati = postacert.getDati(); var msgId = dati.getMsgid(); - if (msgId.startsWith("<") && msgId.endsWith(">")) - dati.setMsgid(msgId.substring(1, msgId.length() - 1)); + dati.setMsgid(removeBracketsFromMessageId(msgId)); log.debug(SCARICAMENTO_ESITI_PEC + "- PEC '{}' has '{}' msgId", finalMessageID, msgId); return postacert; }) @@ -120,12 +121,12 @@ public void scaricamentoEsitiPecScheduler() { .receiversDomain(getDomainFromAddress(getFromFromMimeMessage(mimeMessage)[0])) .retry(0) .build())) - .thenReturn(finalMessageID); + .thenReturn(Tuples.of(finalMessageID, providerName)); } - else return Mono.just(finalMessageID); + else return Mono.just(Tuples.of(finalMessageID, providerName)); }) //Marca il messaggio come letto. - .flatMap(pnPecService::markMessageAsRead, limitRate) + .flatMap(tuple -> pnPecService.markMessageAsRead(tuple.getT1(), tuple.getT2()), limitRate) .doOnError(throwable -> log.fatal(SCARICAMENTO_ESITI_PEC, throwable)) .onErrorResume(throwable -> Mono.empty()) .repeat(hasMessages::get) diff --git a/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/service/CancellazioneRicevutePecService.java b/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/service/CancellazioneRicevutePecService.java index b4f89e91d..2549cdc94 100644 --- a/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/service/CancellazioneRicevutePecService.java +++ b/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/service/CancellazioneRicevutePecService.java @@ -7,17 +7,18 @@ import it.pagopa.pn.ec.commons.rest.call.ec.gestorerepository.GestoreRepositoryCall; import it.pagopa.pn.ec.rest.v1.dto.EventsDto; import it.pagopa.pn.ec.scaricamentoesitipec.model.pojo.CancellazioneRicevutePecDto; -import it.pagopa.pn.library.pec.service.PnPecService; +import it.pagopa.pn.library.pec.service.PnEcPecService; import lombok.CustomLog; +import org.apache.commons.lang3.tuple.MutablePair; +import org.apache.commons.lang3.tuple.Pair; import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; - import java.util.concurrent.Semaphore; +import static it.pagopa.pn.ec.commons.constant.Status.ACCEPTED; import static it.pagopa.pn.ec.commons.utils.LogUtils.*; import static it.pagopa.pn.ec.commons.utils.RequestUtils.concatRequestId; @@ -25,11 +26,11 @@ @CustomLog public class CancellazioneRicevutePecService { - private final PnPecService pnPecService; + private final PnEcPecService pnPecService; private final GestoreRepositoryCall gestoreRepositoryCall; private final Semaphore semaphore; - public CancellazioneRicevutePecService(@Qualifier("pnPecServiceImpl") PnPecService pnPecService, GestoreRepositoryCall gestoreRepositoryCall, @Value("${cancellazione-ricevute-pec.max-thread-pool-size}") Integer maxThreadPoolSize) { + public CancellazioneRicevutePecService(PnEcPecService pnPecService, GestoreRepositoryCall gestoreRepositoryCall, @Value("${cancellazione-ricevute-pec.max-thread-pool-size}") Integer maxThreadPoolSize) { this.pnPecService = pnPecService; this.gestoreRepositoryCall = gestoreRepositoryCall; log.debug("{} max thread pool size : {} ", CANCELLAZIONE_RICEVUTE_PEC, maxThreadPoolSize); @@ -66,15 +67,17 @@ public Mono cancellazioneRicevutePec(final CancellazioneRicevutePecDto can return Mono.justOrEmpty(requestDto.getRequestMetadata()) .flatMapMany(requestMetadataDto -> Flux.fromIterable(requestMetadataDto.getEventsList())) .map(EventsDto::getDigProgrStatus) - .filter(digitalProgressStatusDto -> digitalLegal.getEventCode().getValue().equals(digitalProgressStatusDto.getStatusCode())) - .next() - .doOnSuccess(digitalProgressStatusDto -> { - if (digitalProgressStatusDto == null) - log.warn(NOT_VALID_FOR_DELETE, requestId); + .reduce(new MutablePair(), (pair, digitalProgressStatusDto) -> { + if (digitalProgressStatusDto.getStatus().equals(ACCEPTED.getStatusTransactionTableCompliant())) + pair.setRight(digitalProgressStatusDto.getGeneratedMessage().getId()); + else if (digitalLegal.getEventCode().getValue().equals(digitalProgressStatusDto.getStatusCode())) + pair.setLeft(digitalProgressStatusDto.getGeneratedMessage().getId()); + return pair; }); }) - .map(digitalProgressStatusDto -> digitalProgressStatusDto.getGeneratedMessage().getId()) - .flatMap(pnPecService::deleteMessage) + .filter(pair -> pair.getLeft() != null && pair.getRight() != null) + .doOnDiscard(Pair.class, pair -> log.warn(NOT_VALID_FOR_DELETE, requestId)) + .flatMap(pair -> pnPecService.deleteMessage(pair.getLeft(), pair.getRight())) .doOnError(throwable -> log.fatal(CANCELLAZIONE_RICEVUTE_PEC, throwable, throwable.getMessage())) .doOnSuccess(result -> acknowledgment.acknowledge()) .doFinally(signalType -> semaphore.release()); diff --git a/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/service/LavorazioneEsitiPecService.java b/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/service/LavorazioneEsitiPecService.java index 3a862fbfb..644bdb3cb 100644 --- a/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/service/LavorazioneEsitiPecService.java +++ b/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/service/LavorazioneEsitiPecService.java @@ -35,6 +35,8 @@ import reactor.util.function.Tuples; import java.net.URI; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; diff --git a/src/main/java/it/pagopa/pn/library/pec/model/pojo/PnEcPecGetMessagesResponse.java b/src/main/java/it/pagopa/pn/library/pec/model/pojo/PnEcPecGetMessagesResponse.java new file mode 100644 index 000000000..a97aaacd0 --- /dev/null +++ b/src/main/java/it/pagopa/pn/library/pec/model/pojo/PnEcPecGetMessagesResponse.java @@ -0,0 +1,16 @@ +package it.pagopa.pn.library.pec.model.pojo; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class PnEcPecGetMessagesResponse { + + private PnEcPecListOfMessages pnEcPecListOfMessages; + private int numOfMessages; + +} diff --git a/src/main/java/it/pagopa/pn/library/pec/model/pojo/PnEcPecListOfMessages.java b/src/main/java/it/pagopa/pn/library/pec/model/pojo/PnEcPecListOfMessages.java new file mode 100644 index 000000000..f768b7b6b --- /dev/null +++ b/src/main/java/it/pagopa/pn/library/pec/model/pojo/PnEcPecListOfMessages.java @@ -0,0 +1,18 @@ +package it.pagopa.pn.library.pec.model.pojo; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class PnEcPecListOfMessages { + + private List messages; + +} diff --git a/src/main/java/it/pagopa/pn/library/pec/model/pojo/PnEcPecMessage.java b/src/main/java/it/pagopa/pn/library/pec/model/pojo/PnEcPecMessage.java new file mode 100644 index 000000000..f7544f22e --- /dev/null +++ b/src/main/java/it/pagopa/pn/library/pec/model/pojo/PnEcPecMessage.java @@ -0,0 +1,25 @@ +package it.pagopa.pn.library.pec.model.pojo; + +import lombok.*; +import lombok.Data; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class PnEcPecMessage { + + @ToString.Exclude + private byte[] message; + private String providerName; + + public PnEcPecMessage message(byte[] message) { + this.message = message; + return this; + } + + public PnEcPecMessage providerName(String providerName) { + this.providerName = providerName; + return this; + } + +} diff --git a/src/main/java/it/pagopa/pn/library/pec/service/PnEcPecService.java b/src/main/java/it/pagopa/pn/library/pec/service/PnEcPecService.java new file mode 100644 index 000000000..478461203 --- /dev/null +++ b/src/main/java/it/pagopa/pn/library/pec/service/PnEcPecService.java @@ -0,0 +1,16 @@ +package it.pagopa.pn.library.pec.service; + +import it.pagopa.pn.library.pec.model.pojo.PnEcPecGetMessagesResponse; +import reactor.core.publisher.Mono; + +public interface PnEcPecService { + Mono sendMail(byte[] message); + + Mono markMessageAsRead(String messageID, String providerName); + + Mono deleteMessage(String messageID, String senderMessageID); + + Mono getUnreadMessages(int limit); + + Mono getMessageCount(); +} diff --git a/src/main/java/it/pagopa/pn/library/pec/service/impl/PnPecServiceImpl.java b/src/main/java/it/pagopa/pn/library/pec/service/impl/PnEcPecServiceImpl.java similarity index 63% rename from src/main/java/it/pagopa/pn/library/pec/service/impl/PnPecServiceImpl.java rename to src/main/java/it/pagopa/pn/library/pec/service/impl/PnEcPecServiceImpl.java index a12b3cd25..da2b2ed83 100644 --- a/src/main/java/it/pagopa/pn/library/pec/service/impl/PnPecServiceImpl.java +++ b/src/main/java/it/pagopa/pn/library/pec/service/impl/PnEcPecServiceImpl.java @@ -8,9 +8,11 @@ import it.pagopa.pn.library.pec.exception.aruba.ArubaCallMaxRetriesExceededException; import it.pagopa.pn.library.pec.exception.pecservice.NamirialProviderMaxRetriesExceededException; import it.pagopa.pn.library.pec.exception.pecservice.MaxRetriesExceededException; -import it.pagopa.pn.library.pec.pojo.PnGetMessagesResponse; -import it.pagopa.pn.library.pec.pojo.PnListOfMessages; +import it.pagopa.pn.library.pec.model.pojo.PnEcPecGetMessagesResponse; +import it.pagopa.pn.library.pec.model.pojo.PnEcPecListOfMessages; +import it.pagopa.pn.library.pec.model.pojo.PnEcPecMessage; import it.pagopa.pn.library.pec.service.ArubaService; +import it.pagopa.pn.library.pec.service.PnEcPecService; import it.pagopa.pn.library.pec.service.PnPecService; import lombok.CustomLog; import org.springframework.beans.factory.annotation.Autowired; @@ -19,6 +21,7 @@ import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import reactor.util.function.Tuples; import reactor.util.retry.Retry; import java.time.Duration; @@ -30,7 +33,7 @@ @CustomLog @Service -public class PnPecServiceImpl implements PnPecService { +public class PnEcPecServiceImpl implements PnEcPecService { private final ArubaService arubaService; private final com.namirial.pec.library.service.PnPecServiceImpl namirialService; @@ -44,7 +47,7 @@ public class PnPecServiceImpl implements PnPecService { @Autowired - public PnPecServiceImpl(@Qualifier("arubaServiceImpl") ArubaService arubaService, com.namirial.pec.library.service.PnPecServiceImpl namirialService, PnPecConfigurationProperties props, + public PnEcPecServiceImpl(@Qualifier("arubaServiceImpl") ArubaService arubaService, com.namirial.pec.library.service.PnPecServiceImpl namirialService, PnPecConfigurationProperties props, PnPecRetryStrategyProperties retryStrategyProperties, CloudWatchPecMetrics cloudWatchPecMetrics) { this.arubaService = arubaService; this.namirialService = namirialService; @@ -74,74 +77,70 @@ private Retry getPnPecRetryStrategy(String clientMethodName, PnPecService servic @Override public Mono sendMail(byte[] message) { - log.logStartingProcess(PN_PEC_SEND_MAIL); + log.logStartingProcess(PN_EC_PEC_SEND_MAIL); PnPecService provider = getProviderWrite(); return provider .sendMail(message) - .retryWhen(getPnPecRetryStrategy(PN_PEC_SEND_MAIL, provider)) - .doOnSuccess(result -> log.logEndingProcess(PN_PEC_SEND_MAIL)) - .doOnError(throwable -> log.logEndingProcess(PN_PEC_SEND_MAIL, false, throwable.getMessage())); + .retryWhen(getPnPecRetryStrategy(PN_EC_PEC_SEND_MAIL, provider)) + .doOnSuccess(result -> log.logEndingProcess(PN_EC_PEC_SEND_MAIL)) + .doOnError(throwable -> log.logEndingProcess(PN_EC_PEC_SEND_MAIL, false, throwable.getMessage())); } @Override - public Mono getUnreadMessages(int limit) { - log.logStartingProcess(PEC_GET_UNREAD_MESSAGES); + public Mono getUnreadMessages(int limit) { + log.logStartingProcess(PN_EC_PEC_GET_UNREAD_MESSAGES); return Flux.fromIterable(getProvidersRead()) .flatMap(provider -> provider.getUnreadMessages(limit) - .retryWhen(getPnPecRetryStrategy(PEC_GET_UNREAD_MESSAGES, provider))) - .flatMap(response -> { - var listOfMessages = response.getPnListOfMessages(); + .retryWhen(getPnPecRetryStrategy(PN_EC_PEC_GET_UNREAD_MESSAGES, provider)) + .map(pnGetMessagesResponse -> Tuples.of(pnGetMessagesResponse, getProviderName(provider)))) + .flatMap(tuple -> { + var listOfMessages = tuple.getT1().getPnListOfMessages(); + var providerName = tuple.getT2(); if (listOfMessages == null) { return Flux.empty(); } else { - return Flux.fromIterable(listOfMessages.getMessages()); + return Flux.fromIterable(listOfMessages.getMessages()).map(message -> new PnEcPecMessage(message, providerName)); } }) .collectList() - .flatMap(messages -> Mono.just(new PnGetMessagesResponse(messages.isEmpty() ? null : new PnListOfMessages(messages), messages.size())) - ) - .doOnSuccess(result -> log.logEndingProcess(PEC_GET_UNREAD_MESSAGES)) - .doOnError(throwable -> log.logEndingProcess(PEC_GET_UNREAD_MESSAGES, false, throwable.getMessage())); - } - - - - @Override - public Mono markMessageAsRead(String messageID) { - log.logStartingProcess(PEC_MARK_MESSAGE_AS_READ); - PnPecService provider = getProvider(messageID); - return provider.markMessageAsRead(messageID) - .retryWhen(getPnPecRetryStrategy(PEC_MARK_MESSAGE_AS_READ, provider)) - .then() - .doOnSuccess(result -> log.logEndingProcess(PEC_MARK_MESSAGE_AS_READ)) - .doOnError(throwable -> log.logEndingProcess(PEC_MARK_MESSAGE_AS_READ, false, throwable.getMessage())); + .flatMap(messages -> Mono.just(new PnEcPecGetMessagesResponse(messages.isEmpty() ? null : new PnEcPecListOfMessages(messages), messages.size()))) + .doOnSuccess(result -> log.logEndingProcess(PN_EC_PEC_GET_UNREAD_MESSAGES)) + .doOnError(throwable -> log.logEndingProcess(PN_EC_PEC_GET_UNREAD_MESSAGES, false, throwable.getMessage())); } @Override public Mono getMessageCount() { - log.logStartingProcess(PEC_GET_MESSAGE_COUNT); + log.logStartingProcess(PN_EC_PEC_GET_MESSAGE_COUNT); return Flux.fromIterable(getProvidersRead()) .flatMap(provider -> provider.getMessageCount() .flatMap(count -> cloudWatchPecMetrics.publishMessageCount(Long.valueOf(count), getMetricNamespace(provider)).thenReturn(count)) - .retryWhen(getPnPecRetryStrategy(PEC_GET_MESSAGE_COUNT, provider))) + .retryWhen(getPnPecRetryStrategy(PN_EC_PEC_GET_MESSAGE_COUNT, provider))) .reduce(0, Integer::sum) - .doOnSuccess(result -> log.logEndingProcess(PEC_GET_MESSAGE_COUNT)) - .doOnError(throwable -> log.logEndingProcess(PEC_GET_MESSAGE_COUNT, false, throwable.getMessage())); + .doOnSuccess(result -> log.logEndingProcess(PN_EC_PEC_GET_MESSAGE_COUNT)) + .doOnError(throwable -> log.logEndingProcess(PN_EC_PEC_GET_MESSAGE_COUNT, false, throwable.getMessage())); } - @Override - public Mono deleteMessage(String messageID) { - log.logStartingProcess(PEC_DELETE_MESSAGE); - - PnPecService provider = getProvider(messageID); + public Mono markMessageAsRead(String messageID, String providerName) { + log.logStartingProcess(PN_EC_PEC_MARK_MESSAGE_AS_READ); + PnPecService provider = getProviderByName(providerName); + return provider.markMessageAsRead(messageID) + .retryWhen(getPnPecRetryStrategy(PN_EC_PEC_MARK_MESSAGE_AS_READ, provider)) + .then() + .doOnSuccess(result -> log.logEndingProcess(PN_EC_PEC_MARK_MESSAGE_AS_READ)) + .doOnError(throwable -> log.logEndingProcess(PN_EC_PEC_MARK_MESSAGE_AS_READ, false, throwable.getMessage())); + } + @Override + public Mono deleteMessage(String messageID, String senderMessageID) { + log.logStartingProcess(PN_EC_PEC_DELETE_MESSAGE); + PnPecService provider = getProviderByMessageId(senderMessageID); return provider.deleteMessage(messageID) - .retryWhen(getPnPecRetryStrategy(PEC_DELETE_MESSAGE, provider)) + .retryWhen(getPnPecRetryStrategy(PN_EC_PEC_DELETE_MESSAGE, provider)) .then() - .doOnSuccess(result -> log.logEndingProcess(PEC_DELETE_MESSAGE)) - .doOnError(throwable -> log.logEndingProcess(PEC_DELETE_MESSAGE, false, throwable.getMessage())); + .doOnSuccess(result -> log.logEndingProcess(PN_EC_PEC_DELETE_MESSAGE)) + .doOnError(throwable -> log.logEndingProcess(PN_EC_PEC_DELETE_MESSAGE, false, throwable.getMessage())); } @@ -180,7 +179,7 @@ private List getProvidersRead() { return services; } - private PnPecService getProvider(String messageID) { + private PnPecService getProviderByMessageId(String messageID) { if (isAruba(messageID)) { log.debug(ARUBA_PROVIDER_SELECTED); return arubaService; @@ -190,6 +189,30 @@ private PnPecService getProvider(String messageID) { } } + private PnPecService getProviderByName(String providerName) { + if (providerName.equals(ARUBA_PROVIDER)) { + log.debug(ARUBA_PROVIDER_SELECTED); + return arubaService; + } else if (providerName.equals(NAMIRIAL_PROVIDER)) { + log.debug(NAMIRIAL_PROVIDER_SELECTED); + return namirialService; + } else { + log.debug(ERROR_PARSING_PROPERTY_VALUES); + throw new IllegalArgumentException(ERROR_PARSING_PROPERTY_VALUES); + } + } + + private String getProviderName(PnPecService service) { + if (service instanceof ArubaService) { + return ARUBA_PROVIDER; + } else if (service instanceof com.namirial.pec.library.service.PnPecServiceImpl) { + return NAMIRIAL_PROVIDER; + } else { + log.debug(ERROR_PARSING_PROPERTY_VALUES); + throw new IllegalArgumentException(ERROR_PARSING_PROPERTY_VALUES); + } + } + private String getMetricNamespace(PnPecService service) { if (service instanceof ArubaService) { return arubaProviderNamespace; diff --git a/src/test/java/it/pagopa/pn/ec/pec/service/impl/PecServiceTest.java b/src/test/java/it/pagopa/pn/ec/pec/service/impl/PecServiceTest.java index 38424bda8..07cfa678e 100644 --- a/src/test/java/it/pagopa/pn/ec/pec/service/impl/PecServiceTest.java +++ b/src/test/java/it/pagopa/pn/ec/pec/service/impl/PecServiceTest.java @@ -14,8 +14,7 @@ import it.pagopa.pn.ec.rest.v1.dto.*; import it.pagopa.pn.ec.testutils.annotation.SpringBootTestWebEnv; import it.pagopa.pn.library.pec.service.ArubaService; -import it.pagopa.pn.library.pec.service.PnPecService; -import it.pagopa.pn.library.pec.service.impl.PnPecServiceImpl; +import it.pagopa.pn.library.pec.service.PnEcPecService; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -87,7 +86,7 @@ class PecServiceTest { @SpyBean private PnPecConfigurationProperties pnPecConfigurationProperties; @SpyBean - private PnPecServiceImpl pnPecService; + private PnEcPecService pnPecService; @Mock private Acknowledgment acknowledgment; @Value("${aruba.pec.sender}") diff --git a/src/test/java/it/pagopa/pn/ec/pec/service/impl/PnPecServiceTest.java b/src/test/java/it/pagopa/pn/ec/pec/service/impl/PnPecServiceTest.java index 41bc2b0c2..39374119e 100644 --- a/src/test/java/it/pagopa/pn/ec/pec/service/impl/PnPecServiceTest.java +++ b/src/test/java/it/pagopa/pn/ec/pec/service/impl/PnPecServiceTest.java @@ -13,17 +13,17 @@ import it.pagopa.pn.library.exceptions.PnSpapiTemporaryErrorException; import it.pagopa.pn.library.pec.exception.aruba.ArubaCallMaxRetriesExceededException; import it.pagopa.pn.library.pec.exception.pecservice.*; +import it.pagopa.pn.library.pec.model.pojo.PnEcPecGetMessagesResponse; import it.pagopa.pn.library.pec.pojo.PnGetMessagesResponse; import it.pagopa.pn.library.pec.pojo.PnListOfMessages; import it.pagopa.pn.library.pec.service.ArubaService; -import it.pagopa.pn.library.pec.service.PnPecService; -import it.pagopa.pn.library.pec.service.impl.PnPecServiceImpl; +import it.pagopa.pn.library.pec.service.PnEcPecService; +import it.pagopa.pn.library.pec.service.impl.PnEcPecServiceImpl; import lombok.CustomLog; import org.joda.time.DateTime; import org.joda.time.DateTimeUtils; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.SpyBean; @@ -39,6 +39,8 @@ import static it.pagopa.pn.ec.pec.service.impl.PecServiceTest.createDigitalNotificationRequest; import static it.pagopa.pn.ec.testutils.constant.EcCommonRestApiConstant.DEFAULT_ID_CLIENT_HEADER_VALUE; import static it.pagopa.pn.ec.testutils.constant.EcCommonRestApiConstant.DEFAULT_REQUEST_IDX; +import static it.pagopa.pn.library.pec.utils.PnPecUtils.ARUBA_PROVIDER; +import static it.pagopa.pn.library.pec.utils.PnPecUtils.NAMIRIAL_PROVIDER; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -55,8 +57,7 @@ class PnPecServiceTest { @MockBean private com.namirial.pec.library.service.PnPecServiceImpl namirialService; @Autowired - @Qualifier("pnPecServiceImpl") - private PnPecService pnPecService; + private PnEcPecService pnPecService; @MockBean private GestoreRepositoryCall gestoreRepositoryCall; @SpyBean @@ -76,7 +77,7 @@ class PnPecServiceTest { private final String PERMANENT_EXCEPTION = "test permanent exception"; private final String MESSAGE = "test message"; private final String ARUBA_MESSAGE_ID = "opec21010.20231006185001.00057.206.1.59@pec.aruba.it"; - private final String NAMIRIAL_MESSAGE_ID = "opec21010.20231006185001.00057.206.1.59@pec.namirial.it.test"; + private final String NAMIRIAL_MESSAGE_ID = "opec21010.20231006185001.00057.206.1.59@sicurezzapostale.it"; private final String DATE_R_ARUBA_W_ARUBA = "2022-12-02T00:00:00Z"; private final String DATE_R_ARUBA_NAM_W_NAM = "2023-01-02T23:59:58Z"; @@ -230,11 +231,11 @@ void getUnreadMessagesExpectBothOk() { when(arubaService.getUnreadMessages(anyInt())).thenReturn(Mono.just(arubaMessages)); when(namirialService.getUnreadMessages(anyInt())).thenReturn(Mono.just(namirialProviderMessages)); - Mono combinedMessages = pnPecService.getUnreadMessages(6); + Mono combinedMessages = pnPecService.getUnreadMessages(6); StepVerifier.create(combinedMessages).expectNextMatches(response -> response.getNumOfMessages() == arubaMessages.getNumOfMessages() + namirialProviderMessages.getNumOfMessages() - && response.getPnListOfMessages().getMessages().size() == arubaMessages.getPnListOfMessages().getMessages().size() + namirialProviderMessages.getPnListOfMessages().getMessages().size()) + && response.getPnEcPecListOfMessages().getMessages().size() == arubaMessages.getPnListOfMessages().getMessages().size() + namirialProviderMessages.getPnListOfMessages().getMessages().size()) .expectComplete() .verify(); verify(arubaService, times(1)).getUnreadMessages(6); @@ -251,7 +252,7 @@ void getUnreadMessagesNamirialProviderKo() { when(arubaService.getUnreadMessages(anyInt())).thenReturn(Mono.just(arubaMessages)); when(namirialService.getUnreadMessages(anyInt())).thenReturn(Mono.error(permanentException)); - Mono combinedMessages = pnPecService.getUnreadMessages(6); + Mono combinedMessages = pnPecService.getUnreadMessages(6); StepVerifier.create(combinedMessages) .expectError(PnSpapiPermanentErrorException.class) @@ -269,7 +270,7 @@ void getUnreadMessagesArubaKo() { when(arubaService.getUnreadMessages(anyInt())).thenReturn(Mono.error(permanentException)); when(namirialService.getUnreadMessages(anyInt())).thenReturn(Mono.just(namirialProviderMessages)); - Mono combinedMessages = pnPecService.getUnreadMessages(6); + Mono combinedMessages = pnPecService.getUnreadMessages(6); StepVerifier.create(combinedMessages) .expectError(PnSpapiPermanentErrorException.class) @@ -286,7 +287,7 @@ void getUnreadMessagesBothKo() { when(arubaService.getUnreadMessages(anyInt())).thenReturn(Mono.error(permanentException)); when(namirialService.getUnreadMessages(anyInt())).thenReturn(Mono.error(permanentException)); - Mono combinedMessages = pnPecService.getUnreadMessages(6); + Mono combinedMessages = pnPecService.getUnreadMessages(6); StepVerifier.create(combinedMessages) .expectError(PnSpapiPermanentErrorException.class) @@ -305,11 +306,11 @@ void getUnreadMesagesBothEmpty() { when(arubaService.getUnreadMessages(anyInt())).thenReturn(Mono.just(arubaMessages)); when(namirialService.getUnreadMessages(anyInt())).thenReturn(Mono.just(namirialProviderMessages)); - Mono combinedMessages = pnPecService.getUnreadMessages(6); + Mono combinedMessages = pnPecService.getUnreadMessages(6); StepVerifier.create(combinedMessages) .expectNextMatches(response -> response.getNumOfMessages() == 0 - && response.getPnListOfMessages() == null) + && response.getPnEcPecListOfMessages() == null) .expectComplete() .verify(); @@ -325,7 +326,7 @@ void getUnreadMessagesNamirialProviderRetriesExceeded() { when(arubaService.getUnreadMessages(anyInt())).thenReturn(Mono.just(arubaMessages)); when(namirialService.getUnreadMessages(anyInt())).thenReturn(Mono.error(temporaryException)); - Mono combinedMessages = pnPecService.getUnreadMessages(6); + Mono combinedMessages = pnPecService.getUnreadMessages(6); StepVerifier.create(combinedMessages) .expectError(NamirialProviderMaxRetriesExceededException.class) @@ -343,7 +344,7 @@ void getUnreadMessagesArubaRetriesExceeded() { when(arubaService.getUnreadMessages(anyInt())).thenReturn(Mono.error(temporaryException)); when(namirialService.getUnreadMessages(anyInt())).thenReturn(Mono.just(namirialProviderMessages)); - Mono combinedMessages = pnPecService.getUnreadMessages(6); + Mono combinedMessages = pnPecService.getUnreadMessages(6); StepVerifier.create(combinedMessages) .expectError(ArubaCallMaxRetriesExceededException.class) @@ -360,7 +361,7 @@ void getUnreadMessagesBothRetriesExceeded() { when(arubaService.getUnreadMessages(anyInt())).thenReturn(Mono.error(temporaryException)); when(namirialService.getUnreadMessages(anyInt())).thenReturn(Mono.error(temporaryException)); - Mono combinedMessages = pnPecService.getUnreadMessages(6); + Mono combinedMessages = pnPecService.getUnreadMessages(6); StepVerifier.create(combinedMessages) .expectError(ArubaCallMaxRetriesExceededException.class) @@ -510,7 +511,7 @@ void markMessageAsReadFromArubaOk() { when(arubaService.markMessageAsRead(ARUBA_MESSAGE_ID)).thenReturn(Mono.empty()); when(namirialService.markMessageAsRead(ARUBA_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); - StepVerifier.create(pnPecService.markMessageAsRead(ARUBA_MESSAGE_ID)).expectComplete().verify(); + StepVerifier.create(pnPecService.markMessageAsRead(ARUBA_MESSAGE_ID, ARUBA_PROVIDER)).expectComplete().verify(); verify(arubaService, times(1)).markMessageAsRead(ARUBA_MESSAGE_ID); verify(namirialService, never()).markMessageAsRead(anyString()); @@ -523,7 +524,7 @@ void markMessageAsReadFromNamirialOk() { when(arubaService.markMessageAsRead(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); when(namirialService.markMessageAsRead(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.empty()); - StepVerifier.create(pnPecService.markMessageAsRead(NAMIRIAL_MESSAGE_ID)).expectComplete().verify(); + StepVerifier.create(pnPecService.markMessageAsRead(NAMIRIAL_MESSAGE_ID, NAMIRIAL_PROVIDER)).expectComplete().verify(); verify(arubaService, never()).markMessageAsRead(NAMIRIAL_MESSAGE_ID); verify(namirialService, times(1)).markMessageAsRead(anyString()); @@ -536,7 +537,7 @@ void markMessageAsReadFromArubaKo() { when(arubaService.markMessageAsRead(ARUBA_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); when(namirialService.markMessageAsRead(ARUBA_MESSAGE_ID)).thenReturn(Mono.error(temporaryException)); - StepVerifier.create(pnPecService.markMessageAsRead(ARUBA_MESSAGE_ID)).expectError(PnSpapiPermanentErrorException.class).verify(); + StepVerifier.create(pnPecService.markMessageAsRead(ARUBA_MESSAGE_ID, ARUBA_PROVIDER)).expectError(PnSpapiPermanentErrorException.class).verify(); verify(arubaService, times(1)).markMessageAsRead(ARUBA_MESSAGE_ID); verify(namirialService, never()).markMessageAsRead(anyString()); @@ -549,7 +550,7 @@ void markMessageAsReadFromNamirialKo() { when(arubaService.markMessageAsRead(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.error(temporaryException)); when(namirialService.markMessageAsRead(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); - StepVerifier.create(pnPecService.markMessageAsRead(NAMIRIAL_MESSAGE_ID)).expectError(PnSpapiPermanentErrorException.class).verify(); + StepVerifier.create(pnPecService.markMessageAsRead(NAMIRIAL_MESSAGE_ID, NAMIRIAL_PROVIDER)).expectError(PnSpapiPermanentErrorException.class).verify(); verify(arubaService, never()).markMessageAsRead(NAMIRIAL_MESSAGE_ID); verify(namirialService, times(1)).markMessageAsRead(anyString()); @@ -562,7 +563,7 @@ void markMessageAsReadFromArubaRetriesExceeded() { when(arubaService.markMessageAsRead(ARUBA_MESSAGE_ID)).thenReturn(Mono.error(temporaryException)); when(namirialService.markMessageAsRead(ARUBA_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); - StepVerifier.create(pnPecService.markMessageAsRead(ARUBA_MESSAGE_ID)).expectError(ArubaCallMaxRetriesExceededException.class).verify(); + StepVerifier.create(pnPecService.markMessageAsRead(ARUBA_MESSAGE_ID, ARUBA_PROVIDER)).expectError(ArubaCallMaxRetriesExceededException.class).verify(); verify(arubaService, times(1)).markMessageAsRead(ARUBA_MESSAGE_ID); verify(namirialService, never()).markMessageAsRead(anyString()); @@ -575,7 +576,7 @@ void markMessageAsReadFromNamirialRetriesExceeded() { when(arubaService.markMessageAsRead(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); when(namirialService.markMessageAsRead(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.error(temporaryException)); - StepVerifier.create(pnPecService.markMessageAsRead(NAMIRIAL_MESSAGE_ID)).expectError(MaxRetriesExceededException.class).verify(); + StepVerifier.create(pnPecService.markMessageAsRead(NAMIRIAL_MESSAGE_ID, NAMIRIAL_PROVIDER)).expectError(MaxRetriesExceededException.class).verify(); verify(arubaService, never()).markMessageAsRead(NAMIRIAL_MESSAGE_ID); verify(namirialService, times(1)).markMessageAsRead(anyString()); @@ -591,7 +592,7 @@ void deleteMessageFromArubaOk() { when(arubaService.deleteMessage(ARUBA_MESSAGE_ID)).thenReturn(Mono.empty()); when(namirialService.deleteMessage(ARUBA_MESSAGE_ID)).thenReturn(Mono.empty()); - StepVerifier.create(pnPecService.deleteMessage(ARUBA_MESSAGE_ID)).expectComplete().verify(); + StepVerifier.create(pnPecService.deleteMessage(ARUBA_MESSAGE_ID, ARUBA_MESSAGE_ID)).expectComplete().verify(); verify(arubaService, times(1)).deleteMessage(ARUBA_MESSAGE_ID); verify(namirialService, never()).deleteMessage(anyString()); @@ -605,7 +606,7 @@ void deleteMessageFromNamirialOk() { when(arubaService.deleteMessage(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.empty()); when(namirialService.deleteMessage(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.empty()); - StepVerifier.create(pnPecService.deleteMessage(NAMIRIAL_MESSAGE_ID)).expectComplete().verify(); + StepVerifier.create(pnPecService.deleteMessage(NAMIRIAL_MESSAGE_ID, NAMIRIAL_MESSAGE_ID)).expectComplete().verify(); verify(arubaService, never()).deleteMessage(NAMIRIAL_MESSAGE_ID); verify(namirialService, times(1)).deleteMessage(anyString()); @@ -619,7 +620,7 @@ void deleteMessageFromArubaKo() { when(arubaService.deleteMessage(ARUBA_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); when(namirialService.deleteMessage(ARUBA_MESSAGE_ID)).thenReturn(Mono.error(temporaryException)); - StepVerifier.create(pnPecService.deleteMessage(ARUBA_MESSAGE_ID)).expectError(PnSpapiPermanentErrorException.class).verify(); + StepVerifier.create(pnPecService.deleteMessage(ARUBA_MESSAGE_ID, ARUBA_MESSAGE_ID)).expectError(PnSpapiPermanentErrorException.class).verify(); verify(arubaService, times(1)).deleteMessage(ARUBA_MESSAGE_ID); verify(namirialService, never()).deleteMessage(anyString()); @@ -633,7 +634,7 @@ void deleteMessageFromNamirialKo() { when(namirialService.deleteMessage(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); - StepVerifier.create(pnPecService.deleteMessage(NAMIRIAL_MESSAGE_ID)).expectError(PnSpapiPermanentErrorException.class).verify(); + StepVerifier.create(pnPecService.deleteMessage(NAMIRIAL_MESSAGE_ID, NAMIRIAL_MESSAGE_ID)).expectError(PnSpapiPermanentErrorException.class).verify(); verify(arubaService, never()).deleteMessage(NAMIRIAL_MESSAGE_ID); verify(namirialService, times(1)).deleteMessage(anyString()); @@ -646,7 +647,7 @@ void deleteMessageFromArubaRetriesExceeded() { when(arubaService.deleteMessage(ARUBA_MESSAGE_ID)).thenReturn(Mono.error(temporaryException)); when(namirialService.deleteMessage(ARUBA_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); - StepVerifier.create(pnPecService.deleteMessage(ARUBA_MESSAGE_ID)).expectError(ArubaCallMaxRetriesExceededException.class).verify(); + StepVerifier.create(pnPecService.deleteMessage(ARUBA_MESSAGE_ID, ARUBA_MESSAGE_ID)).expectError(ArubaCallMaxRetriesExceededException.class).verify(); verify(arubaService, times(1)).deleteMessage(ARUBA_MESSAGE_ID); verify(namirialService, never()).deleteMessage(anyString()); @@ -659,7 +660,7 @@ void deleteMessageFromNamirialRetriesExceeded() { when(arubaService.deleteMessage(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.error(permanentException)); when(namirialService.deleteMessage(NAMIRIAL_MESSAGE_ID)).thenReturn(Mono.error(temporaryException)); - StepVerifier.create(pnPecService.deleteMessage(NAMIRIAL_MESSAGE_ID)).expectError(MaxRetriesExceededException.class).verify(); + StepVerifier.create(pnPecService.deleteMessage(NAMIRIAL_MESSAGE_ID, NAMIRIAL_MESSAGE_ID)).expectError(MaxRetriesExceededException.class).verify(); verify(arubaService, never()).deleteMessage(NAMIRIAL_MESSAGE_ID); verify(namirialService, times(1)).deleteMessage(anyString()); @@ -676,11 +677,11 @@ void isArubaTest() { messageIDs.add("opec21010.20231006185001.00057.206.1.59@test.com"); messageIDs.add(NAMIRIAL_MESSAGE_ID); - Assertions.assertTrue(PnPecServiceImpl.isAruba(messageIDs.get(0))); - Assertions.assertTrue(PnPecServiceImpl.isAruba(messageIDs.get(1))); - Assertions.assertTrue(PnPecServiceImpl.isAruba(messageIDs.get(2))); - Assertions.assertFalse(PnPecServiceImpl.isAruba(messageIDs.get(3))); - Assertions.assertFalse(PnPecServiceImpl.isAruba(messageIDs.get(4))); + Assertions.assertTrue(PnEcPecServiceImpl.isAruba(messageIDs.get(0))); + Assertions.assertTrue(PnEcPecServiceImpl.isAruba(messageIDs.get(1))); + Assertions.assertTrue(PnEcPecServiceImpl.isAruba(messageIDs.get(2))); + Assertions.assertFalse(PnEcPecServiceImpl.isAruba(messageIDs.get(3))); + Assertions.assertFalse(PnEcPecServiceImpl.isAruba(messageIDs.get(4))); } diff --git a/src/test/java/it/pagopa/pn/ec/scaricamentoesitipec/service/CancellazioneRicevutePecServiceTest.java b/src/test/java/it/pagopa/pn/ec/scaricamentoesitipec/service/CancellazioneRicevutePecServiceTest.java index afb926438..ffb837299 100644 --- a/src/test/java/it/pagopa/pn/ec/scaricamentoesitipec/service/CancellazioneRicevutePecServiceTest.java +++ b/src/test/java/it/pagopa/pn/ec/scaricamentoesitipec/service/CancellazioneRicevutePecServiceTest.java @@ -7,7 +7,7 @@ import it.pagopa.pn.ec.scaricamentoesitipec.model.pojo.CancellazioneRicevutePecDto; import it.pagopa.pn.ec.testutils.annotation.SpringBootTestWebEnv; import it.pagopa.pn.library.pec.exception.pecservice.DeleteMessageException; -import it.pagopa.pn.library.pec.service.PnPecService; +import it.pagopa.pn.library.pec.service.PnEcPecService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; @@ -16,6 +16,8 @@ import java.util.List; +import static it.pagopa.pn.ec.commons.constant.Status.ACCEPTED; +import static it.pagopa.pn.ec.commons.constant.Status.DELIVERED; import static org.mockito.Mockito.when; @SpringBootTestWebEnv @@ -27,8 +29,8 @@ public class CancellazioneRicevutePecServiceTest { Acknowledgment acknowledgment; @MockBean GestoreRepositoryCall gestoreRepositoryCall; - @MockBean(name = "pnPecServiceImpl") - PnPecService pnPecService; + @MockBean + PnEcPecService pnPecService; private final String CLIENT_ID = "CLIENT_ID"; private final String REQUEST_ID = "REQUEST_ID"; @@ -40,10 +42,12 @@ void cancellazioneRicevuteOk() { DigitalProgressStatusDto digitalProgressStatusDto1 = new DigitalProgressStatusDto(); digitalProgressStatusDto1.setStatusCode(LegalMessageSentDetails.EventCodeEnum.C001.getValue()); + digitalProgressStatusDto1.setStatus(ACCEPTED.getStatusTransactionTableCompliant()); digitalProgressStatusDto1.setGeneratedMessage(new GeneratedMessageDto().id(MESSAGE_ID)); DigitalProgressStatusDto digitalProgressStatusDto2 = new DigitalProgressStatusDto(); digitalProgressStatusDto2.setStatusCode(LegalMessageSentDetails.EventCodeEnum.C000.getValue()); + digitalProgressStatusDto2.setStatus(DELIVERED.getStatusTransactionTableCompliant()); digitalProgressStatusDto2.setGeneratedMessage(new GeneratedMessageDto().id(MESSAGE_ID)); EventsDto eventsDto1 = new EventsDto(); @@ -55,7 +59,7 @@ void cancellazioneRicevuteOk() { RequestDto requestDto = buildRequestDto(eventsDto1, eventsDto2); when(gestoreRepositoryCall.getRichiesta(CLIENT_ID, REQUEST_ID)).thenReturn(Mono.just(requestDto)); - when(pnPecService.deleteMessage(MESSAGE_ID)).thenReturn(Mono.just("").then()); + when(pnPecService.deleteMessage(MESSAGE_ID, MESSAGE_ID)).thenReturn(Mono.just("").then()); var testMono = cancellazioneRicevutePecService.cancellazioneRicevutePec(cancellazioneRicevutePecDto, REQUEST_ID, acknowledgment); StepVerifier.create(testMono).verifyComplete(); @@ -75,16 +79,26 @@ void cancellazioneRicevuteGestoreRepositoryKo() { void cancellazioneRicevuteDeleteMessageKo() { CancellazioneRicevutePecDto cancellazioneRicevutePecDto = buildCancellazioneRicevutePecDto(); - DigitalProgressStatusDto digitalProgressStatusDto = new DigitalProgressStatusDto(); - digitalProgressStatusDto.setStatusCode(LegalMessageSentDetails.EventCodeEnum.C000.getValue()); - digitalProgressStatusDto.setGeneratedMessage(new GeneratedMessageDto().id(MESSAGE_ID)); - EventsDto eventsDto = new EventsDto(); - eventsDto.setDigProgrStatus(digitalProgressStatusDto); + DigitalProgressStatusDto digitalProgressStatusDto1 = new DigitalProgressStatusDto(); + digitalProgressStatusDto1.setStatusCode(LegalMessageSentDetails.EventCodeEnum.C001.getValue()); + digitalProgressStatusDto1.setStatus(ACCEPTED.getStatusTransactionTableCompliant()); + digitalProgressStatusDto1.setGeneratedMessage(new GeneratedMessageDto().id(MESSAGE_ID)); - RequestDto requestDto = buildRequestDto(eventsDto); + DigitalProgressStatusDto digitalProgressStatusDto2 = new DigitalProgressStatusDto(); + digitalProgressStatusDto2.setStatusCode(LegalMessageSentDetails.EventCodeEnum.C000.getValue()); + digitalProgressStatusDto2.setStatus(DELIVERED.getStatusTransactionTableCompliant()); + digitalProgressStatusDto2.setGeneratedMessage(new GeneratedMessageDto().id(MESSAGE_ID)); + + EventsDto eventsDto1 = new EventsDto(); + eventsDto1.setDigProgrStatus(digitalProgressStatusDto1); + + EventsDto eventsDto2 = new EventsDto(); + eventsDto2.setDigProgrStatus(digitalProgressStatusDto2); + + RequestDto requestDto = buildRequestDto(eventsDto1, eventsDto2); when(gestoreRepositoryCall.getRichiesta(CLIENT_ID, REQUEST_ID)).thenReturn(Mono.just(requestDto)); - when(pnPecService.deleteMessage(MESSAGE_ID)).thenReturn(Mono.error(new DeleteMessageException(MESSAGE_ID))); + when(pnPecService.deleteMessage(MESSAGE_ID, MESSAGE_ID)).thenReturn(Mono.error(new DeleteMessageException(MESSAGE_ID))); var testMono = cancellazioneRicevutePecService.cancellazioneRicevutePec(cancellazioneRicevutePecDto, REQUEST_ID, acknowledgment); StepVerifier.create(testMono).expectError(DeleteMessageException.class).verify(); @@ -96,6 +110,7 @@ void cancellazioneRicevuteNotValidForDelete() { DigitalProgressStatusDto digitalProgressStatusDto = new DigitalProgressStatusDto(); digitalProgressStatusDto.setStatusCode(LegalMessageSentDetails.EventCodeEnum.C001.getValue()); + digitalProgressStatusDto.setStatus(DELIVERED.getStatusTransactionTableCompliant()); digitalProgressStatusDto.setGeneratedMessage(new GeneratedMessageDto().id(MESSAGE_ID)); EventsDto eventsDto = new EventsDto(); eventsDto.setDigProgrStatus(digitalProgressStatusDto); @@ -103,7 +118,7 @@ void cancellazioneRicevuteNotValidForDelete() { RequestDto requestDto = buildRequestDto(eventsDto); when(gestoreRepositoryCall.getRichiesta(CLIENT_ID, REQUEST_ID)).thenReturn(Mono.just(requestDto)); - when(pnPecService.deleteMessage(MESSAGE_ID)).thenReturn(Mono.just("").then()); + when(pnPecService.deleteMessage(MESSAGE_ID, MESSAGE_ID)).thenReturn(Mono.just("").then()); var testMono = cancellazioneRicevutePecService.cancellazioneRicevutePec(cancellazioneRicevutePecDto, REQUEST_ID, acknowledgment); StepVerifier.create(testMono).verifyComplete(); diff --git a/src/test/java/it/pagopa/pn/ec/scaricamentoesitipec/service/ScaricamentoEsitiPecServiceTest.java b/src/test/java/it/pagopa/pn/ec/scaricamentoesitipec/service/ScaricamentoEsitiPecServiceTest.java index 6918431e6..e10caad5a 100644 --- a/src/test/java/it/pagopa/pn/ec/scaricamentoesitipec/service/ScaricamentoEsitiPecServiceTest.java +++ b/src/test/java/it/pagopa/pn/ec/scaricamentoesitipec/service/ScaricamentoEsitiPecServiceTest.java @@ -34,6 +34,7 @@ import jakarta.mail.MessagingException; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.List; @@ -166,7 +167,7 @@ private RequestDto pecRequest() { } private RicezioneEsitiPecDto buildRicezioneEsitiPecDto(String tipoPostacert, String tipoDestinatario) throws MessagingException, IOException { - String msgId = encodeMessageId("<" + CLIENT_ID, PEC_REQUEST_IDX + ">"); + String msgId = URLEncoder.encode("<" + encodeMessageId(CLIENT_ID, PEC_REQUEST_IDX) + ">", StandardCharsets.UTF_8); var daticertBytes = generateDaticertAccettazione(tipoPostacert,"sender@dgsspa.com", "receiverAddress@pagopa.it", "replyTo", "subject", "gestoreMittente", "03/11/1999", "00:00:00", msgId, tipoDestinatario).toString().getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream daticertOutput = new ByteArrayOutputStream(); daticertOutput.write(daticertBytes);