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

Commit

Permalink
[SELC-5169] feat : Added record PecNotification in persistOnboarding (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenbegiqi authored Jul 8, 2024
1 parent aa1ec3a commit bd592f5
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 16 deletions.
5 changes: 4 additions & 1 deletion app/src/main/resources/config/core-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ mscore.blob-storage.connection-string-product = ${BLOB_STORAGE_PRODUCT_CONNECTIO
#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}
mscore.aws-ses-region=${AWS_SES_REGION:eu-south-1}

mscore.sending-frequency-pec-notification=${SENDING_FREQUENCY_PEC_NOTIFICATION:30}
mscore.epoch-date-pec-notification=${EPOCH_DATE_PEC_NOTIFICATION:2024-01-01}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public enum GenericError {
UPDATE_USER_INSTITUTION_ERROR("0000", "Error while updating InstitutionUser for id %s"),
GENERIC_ERROR("0000", "Generic Error"),
DELETE_ONBOARDED_OPERATION_ERROR("0000", "Error while deleting Onboarded Institution"),
DELETE_NOTIFICATION_OPERATION_ERROR ("0000", "PecNotificationEntity was not deleted because either it does not exist or there are multiple records");
DELETE_NOTIFICATION_OPERATION_ERROR ("0000", "PecNotificationEntity was not deleted because either it does not exist or there are multiple records"),
INVALID_INSERT_PEC_NOTIFICATION_ERROR ("0001", "PecNotificationEntity was not inserted because either it exist or the data are invalid");

private final String code;
private final String detail;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class PecNotification {
private String institutionId;
private String productId;
private Integer moduleDayOfTheEpoch;
private String digitalAddress;

private OffsetDateTime createdAt;
private OffsetDateTime updatedAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean insertPecNotification(PecNotification pecNotification){

PecNotificationEntity pecNotificationEntity = this.pecNotificationMapper.convertToPecNotificationEntity(pecNotification);

boolean exist = repository.existsById(pecNotificationEntity.getId());
boolean exist = repository.existsByInstitutionIdAndProductId(pecNotificationEntity.getInstitutionId(), pecNotificationEntity.getProductId());

if (exist){
log.trace("Cannot insert the PecNotification: {}, as it already exists in the collection", pecNotification.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.mongodb.repository.MongoRepository;

public interface PecNotificationRepository extends MongoRepository<PecNotificationEntity, String>, MongoCustomConnector {
boolean existsByInstitutionIdAndProductId(String institutionId, String productId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.pagopa.selfcare.mscore.connector.dao.model;

import it.pagopa.selfcare.mscore.connector.dao.model.inner.UserBindingEntity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;
Expand All @@ -9,7 +8,6 @@
import org.springframework.data.mongodb.core.mapping.Sharded;

import java.time.OffsetDateTime;
import java.util.List;

@Data
@NoArgsConstructor
Expand All @@ -23,6 +21,7 @@ public class PecNotificationEntity {
private String institutionId;
private String productId;
private Integer moduleDayOfTheEpoch;
private String digitalAddress;

private OffsetDateTime createdAt;
private OffsetDateTime updatedAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void findAndDeletePecNotification_notExist() {
@Test
void insertPecNotification_success() {
when(pecNotificationMapper.convertToPecNotificationEntity(pecNotification)).thenReturn(pecNotificationEntity);
when(repository.existsById(pecNotificationEntity.getId())).thenReturn(false);
when(repository.existsByInstitutionIdAndProductId(any(), any())).thenReturn(false);

boolean result = pecNotificationConnector.insertPecNotification(pecNotification);

Expand All @@ -95,7 +95,7 @@ void insertPecNotification_success() {
@Test
void insertPecNotification_alreadyExists() {
when(pecNotificationMapper.convertToPecNotificationEntity(pecNotification)).thenReturn(pecNotificationEntity);
when(repository.existsById(pecNotificationEntity.getId())).thenReturn(true);
when(repository.existsByInstitutionIdAndProductId(any(), any())).thenReturn(true);

boolean result = pecNotificationConnector.insertPecNotification(pecNotification);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
import it.pagopa.selfcare.mscore.model.institution.Institution;
import it.pagopa.selfcare.mscore.model.institution.Onboarding;
import it.pagopa.selfcare.mscore.model.onboarding.VerifyOnboardingFilters;
import it.pagopa.selfcare.mscore.model.pecnotification.PecNotification;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import static it.pagopa.selfcare.mscore.constant.GenericError.*;

Expand All @@ -29,16 +34,23 @@ public class OnboardingServiceImpl implements OnboardingService {
private final InstitutionService institutionService;
private final InstitutionConnector institutionConnector;
private final PecNotificationConnector pecNotificationConnector;
private Integer sendingFrequencyPecNotification;
private String epochDatePecNotification;
private LocalDate currentDate = LocalDate.now();

public OnboardingServiceImpl(OnboardingDao onboardingDao,
InstitutionService institutionService,
InstitutionConnector institutionConnector,
PecNotificationConnector pecNotificationConnector) {
PecNotificationConnector pecNotificationConnector,
@Value("${mscore.sending-frequency-pec-notification}") Integer sendingFrequencyPecNotification,
@Value("${mscore.epoch-date-pec-notification}") String epochDatePecNotification) {

this.onboardingDao = onboardingDao;
this.institutionService = institutionService;
this.institutionConnector = institutionConnector;
this.pecNotificationConnector = pecNotificationConnector;
this.sendingFrequencyPecNotification = sendingFrequencyPecNotification;
this.epochDatePecNotification = epochDatePecNotification;
}

@Override
Expand Down Expand Up @@ -67,10 +79,39 @@ public void verifyOnboardingInfoByFilters(VerifyOnboardingFilters filters) {
}
}

public void insertPecNotification(String institutionId, String productId, String digitalAddress) {

PecNotification pecNotification = new PecNotification();
pecNotification.setId(UUID.randomUUID().toString());
pecNotification.setCreatedAt(OffsetDateTime.now());
pecNotification.setProductId(productId);
pecNotification.setInstitutionId(institutionId);
pecNotification.setModuleDayOfTheEpoch(calculateModuleDayOfTheEpoch());
pecNotification.setDigitalAddress(digitalAddress);

if (!pecNotificationConnector.insertPecNotification(pecNotification)){
throw new InvalidRequestException(INVALID_INSERT_PEC_NOTIFICATION_ERROR.getMessage(), INVALID_INSERT_PEC_NOTIFICATION_ERROR.getCode());
}

}

public int calculateModuleDayOfTheEpoch() {
LocalDate epochStart = LocalDate.parse(this.epochDatePecNotification);
long daysDiff = ChronoUnit.DAYS.between(epochStart, this.currentDate);
int moduleDayOfTheEpoch = (int) (daysDiff % this.sendingFrequencyPecNotification);
return moduleDayOfTheEpoch;
}

@Override
public Institution persistOnboarding(String institutionId, String
productId, Onboarding onboarding, StringBuilder httpStatus) {

Institution institution = persistAndGetInstitution(institutionId, productId, onboarding, httpStatus);
this.insertPecNotification(institutionId, productId, institution.getDigitalAddress());
return institution;
}

private Institution persistAndGetInstitution(String institutionId, String productId, Onboarding onboarding, StringBuilder httpStatus) {
log.trace("persistForUpdate start");
log.debug("persistForUpdate institutionId = {}, productId = {}", institutionId, productId);
onboarding.setStatus(RelationshipState.ACTIVE);
Expand All @@ -86,9 +127,9 @@ public Institution persistOnboarding(String institutionId, String
if (Optional.ofNullable(institution.getOnboarding()).flatMap(onboardings -> onboardings.stream()
.filter(item -> item.getProductId().equals(productId) && UtilEnumList.VALID_RELATIONSHIP_STATES.contains(item.getStatus()))
.findAny()).isPresent()) {

httpStatus.append(HttpStatus.OK.value());
return institution;
return institution;
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
import it.pagopa.selfcare.mscore.model.institution.Institution;
import it.pagopa.selfcare.mscore.model.institution.Onboarding;
import it.pagopa.selfcare.mscore.model.onboarding.*;
import it.pagopa.selfcare.mscore.model.pecnotification.PecNotification;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.ReflectionTestUtils;


import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;

import static it.pagopa.selfcare.mscore.constant.GenericError.DELETE_NOTIFICATION_OPERATION_ERROR;
import static it.pagopa.selfcare.mscore.constant.GenericError.ONBOARDING_OPERATION_ERROR;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@ContextConfiguration(classes = {OnboardingServiceImpl.class})
Expand Down Expand Up @@ -134,12 +134,16 @@ void testVerifyOnboardingInfo5() {
@Test
void persistOnboarding_whenUserExistsOnRegistry() {

ReflectionTestUtils.setField(onboardingServiceImpl, "sendingFrequencyPecNotification", 30);
ReflectionTestUtils.setField(onboardingServiceImpl, "epochDatePecNotification", "2024-01-01");

Onboarding onboarding = dummyOnboarding();
onboarding.setStatus(UtilEnumList.VALID_RELATIONSHIP_STATES.get(0));

Institution institution = new Institution();
institution.setId("institutionId");
institution.setOnboarding(List.of(onboarding, dummyOnboarding()));

when(pecNotificationConnector.insertPecNotification(any(PecNotification.class))).thenReturn(true);
when(institutionConnector.findById(institution.getId())).thenReturn(institution);

String institutionId = institution.getId();
Expand Down Expand Up @@ -195,6 +199,9 @@ void persistOnboarding_shouldRollback() {
@Test
void persistOnboarding_whenUserNotExistsOnRegistry() {

ReflectionTestUtils.setField(onboardingServiceImpl, "sendingFrequencyPecNotification", 30);
ReflectionTestUtils.setField(onboardingServiceImpl, "epochDatePecNotification", "2024-01-01");

String pricingPlan = "pricingPlan";
String productId = "productId";
Billing billing = new Billing();
Expand All @@ -214,7 +221,7 @@ void persistOnboarding_whenUserNotExistsOnRegistry() {
Institution institution = new Institution();
institution.setId("institutionId");
institution.setOnboarding(List.of(onboarding));

institution.setDigitalAddress("[email protected]");

Token token = new Token();
token.setId(onboarding.getTokenId());
Expand All @@ -225,6 +232,7 @@ void persistOnboarding_whenUserNotExistsOnRegistry() {
token.setStatus(onboarding.getStatus());
token.setContractSigned(onboarding.getContract());

when(pecNotificationConnector.insertPecNotification(any(PecNotification.class))).thenReturn(true);
when(institutionConnector.findById(institution.getId())).thenReturn(institution);
when(institutionConnector.findAndUpdate(any(), any(), any(), any())).thenReturn(institution);

Expand All @@ -240,6 +248,11 @@ void persistOnboarding_whenUserNotExistsOnRegistry() {
assertEquals(actual.getCreatedAt().getDayOfYear(), LocalDate.now().getDayOfYear());
assertEquals(HttpStatus.CREATED.value(), Integer.parseInt(statusCode.toString()));

ArgumentCaptor< PecNotification > argCaptor = ArgumentCaptor.forClass(PecNotification.class);
verify(pecNotificationConnector, times(1)). insertPecNotification(argCaptor.capture());
assertEquals(productId, argCaptor.getValue().getProductId());
assertEquals("institutionId", argCaptor.getValue().getInstitutionId());
assertEquals("[email protected]", argCaptor.getValue().getDigitalAddress());
}

private Onboarding dummyOnboarding() {
Expand Down Expand Up @@ -291,5 +304,22 @@ void deleteOnboardedInstitution_deletePecNotificationFails() {
verify(institutionConnector, times(1)).findAndDeleteOnboarding(institutionId, productId);
verify(pecNotificationConnector, times(1)).findAndDeletePecNotification(institutionId, productId);
}

@Test
public void testCalculateModuleDayOfTheEpoch() {
LocalDate mockCurrentDate = LocalDate.of(2024, 2, 1); // 31 days after epoch

ReflectionTestUtils.setField(onboardingServiceImpl, "sendingFrequencyPecNotification", 30);
ReflectionTestUtils.setField(onboardingServiceImpl, "epochDatePecNotification", "2024-01-01");
ReflectionTestUtils.setField(onboardingServiceImpl, "currentDate", mockCurrentDate);

int result = onboardingServiceImpl.calculateModuleDayOfTheEpoch();

LocalDate epochStart = LocalDate.parse("2024-01-01");
long daysDiff = ChronoUnit.DAYS.between(epochStart, mockCurrentDate);
int expected = (int) (daysDiff % 30);

assertEquals(expected, result);
}
}

0 comments on commit bd592f5

Please sign in to comment.