From 833f6aa49d68e7a255093ecea894a4917479ad39 Mon Sep 17 00:00:00 2001 From: Luca Consalvi Date: Mon, 25 Nov 2024 10:01:31 +0100 Subject: [PATCH] add tests --- .../repository/ActivationMapper.java | 2 +- .../service/ActivationPayerServiceImpl.java | 5 +- .../rtp/activator/domain/PayerIDTest.java | 22 +++++++ .../repository/ActivationMapperTest.java | 47 ++++++++++++++ .../ActivationPayerServiceImplTest.java | 63 +++++++++++++++++++ 5 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/test/java/it/gov/pagopa/rtp/activator/domain/PayerIDTest.java create mode 100644 src/test/java/it/gov/pagopa/rtp/activator/repository/ActivationMapperTest.java create mode 100644 src/test/java/it/gov/pagopa/rtp/activator/service/ActivationPayerServiceImplTest.java diff --git a/src/main/java/it/gov/pagopa/rtp/activator/repository/ActivationMapper.java b/src/main/java/it/gov/pagopa/rtp/activator/repository/ActivationMapper.java index da7d12a..e8cc816 100644 --- a/src/main/java/it/gov/pagopa/rtp/activator/repository/ActivationMapper.java +++ b/src/main/java/it/gov/pagopa/rtp/activator/repository/ActivationMapper.java @@ -19,7 +19,7 @@ public Payer toDomain(ActivationEntity activationEntity) { } public ActivationEntity toDbEntity(Payer payer) { - return ActivationEntity.builder().id(payer.payerID().toString()) + return ActivationEntity.builder().id(payer.payerID().getId().toString()) .fiscalCode(payer.fiscalCode()) .rtpSpId(payer.rtpSpId()) .effectiveActivationDate(payer.effectiveActivationDate()) diff --git a/src/main/java/it/gov/pagopa/rtp/activator/service/ActivationPayerServiceImpl.java b/src/main/java/it/gov/pagopa/rtp/activator/service/ActivationPayerServiceImpl.java index 5e9b5ec..6eba452 100644 --- a/src/main/java/it/gov/pagopa/rtp/activator/service/ActivationPayerServiceImpl.java +++ b/src/main/java/it/gov/pagopa/rtp/activator/service/ActivationPayerServiceImpl.java @@ -26,7 +26,8 @@ public Mono activatePayer(String rtpSpId, String fiscalCode) { Payer payer = new Payer(payerID, rtpSpId, fiscalCode, Instant.now()); return activationDBRepository.findByFiscalCode(fiscalCode) - .flatMap(existingEntity -> Mono.error(new PayerAlreadyExists())) - .switchIfEmpty(activationDBRepository.save(payer)); + + .flatMap(existingEntity -> Mono.error(new PayerAlreadyExists())) + .switchIfEmpty(Mono.defer(() -> activationDBRepository.save(payer))); } } \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/rtp/activator/domain/PayerIDTest.java b/src/test/java/it/gov/pagopa/rtp/activator/domain/PayerIDTest.java new file mode 100644 index 0000000..92cc471 --- /dev/null +++ b/src/test/java/it/gov/pagopa/rtp/activator/domain/PayerIDTest.java @@ -0,0 +1,22 @@ +package it.gov.pagopa.rtp.activator.domain; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.util.UUID; + +public class PayerIDTest { + + @Test + public void testCreateNew() { + PayerID payerID = PayerID.createNew(); + assertNotNull(payerID); + assertNotNull(payerID.getId()); + } + + @Test + public void testConstructor() { + UUID uuid = UUID.randomUUID(); + PayerID payerID = new PayerID(uuid); + assertNotNull(payerID); + assertEquals(uuid, payerID.getId()); + } +} diff --git a/src/test/java/it/gov/pagopa/rtp/activator/repository/ActivationMapperTest.java b/src/test/java/it/gov/pagopa/rtp/activator/repository/ActivationMapperTest.java new file mode 100644 index 0000000..2bf4be1 --- /dev/null +++ b/src/test/java/it/gov/pagopa/rtp/activator/repository/ActivationMapperTest.java @@ -0,0 +1,47 @@ +package it.gov.pagopa.rtp.activator.repository; + +import org.junit.jupiter.api.Test; + +import it.gov.pagopa.rtp.activator.domain.Payer; +import it.gov.pagopa.rtp.activator.domain.PayerID; + +import static org.junit.jupiter.api.Assertions.*; + +import java.time.Instant; +import java.util.UUID; + +public class ActivationMapperTest { + + private ActivationMapper mapper = new ActivationMapper(); + + @Test + public void testToDomain() { + ActivationEntity activationEntity = new ActivationEntity(); + activationEntity.setId(UUID.randomUUID().toString()); + activationEntity.setRtpSpId("RTP_SP_ID"); + activationEntity.setFiscalCode("FISCAL_CODE"); + activationEntity.setEffectiveActivationDate(Instant.ofEpochSecond(1732517304)); + + Payer payer = mapper.toDomain(activationEntity); + + assertNotNull(payer); + assertEquals(activationEntity.getId(), payer.payerID().getId().toString()); + assertEquals(activationEntity.getRtpSpId(), payer.rtpSpId()); + assertEquals(activationEntity.getFiscalCode(), payer.fiscalCode()); + assertEquals(activationEntity.getEffectiveActivationDate(), payer.effectiveActivationDate()); + } + + @Test + public void testToDbEntity() { + PayerID payerID = new PayerID(UUID.randomUUID()); + Payer payer = new Payer(payerID, "RTP_SP_ID", "FISCAL_CODE", Instant.ofEpochSecond(1732517304)); + + ActivationEntity activationEntity = mapper.toDbEntity(payer); + + assertNotNull(activationEntity); + assertEquals(payer.payerID().getId().toString(), activationEntity.getId()); + assertEquals(payer.rtpSpId(), activationEntity.getRtpSpId()); + assertEquals(payer.fiscalCode(), activationEntity.getFiscalCode()); + assertEquals(payer.effectiveActivationDate(), activationEntity.getEffectiveActivationDate()); + } +} diff --git a/src/test/java/it/gov/pagopa/rtp/activator/service/ActivationPayerServiceImplTest.java b/src/test/java/it/gov/pagopa/rtp/activator/service/ActivationPayerServiceImplTest.java new file mode 100644 index 0000000..67979ed --- /dev/null +++ b/src/test/java/it/gov/pagopa/rtp/activator/service/ActivationPayerServiceImplTest.java @@ -0,0 +1,63 @@ +package it.gov.pagopa.rtp.activator.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import it.gov.pagopa.rtp.activator.domain.Payer; +import it.gov.pagopa.rtp.activator.domain.PayerID; +import it.gov.pagopa.rtp.activator.domain.errors.PayerAlreadyExists; +import it.gov.pagopa.rtp.activator.repository.ActivationDBRepository; + +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import java.time.Instant; + + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ActivationPayerServiceImplTest { + + @Mock + private ActivationDBRepository activationDBRepository; + + @InjectMocks + private ActivationPayerServiceImpl activationPayerService; + + private Payer payer; + + @BeforeEach + public void setUp() { + payer = new Payer(PayerID.createNew(), "RTP_SP_ID", "FISCAL_CODE", Instant.now()); + + } + + @Test + public void testActivatePayerSuccessful() { + when(activationDBRepository.findByFiscalCode("FISCAL_CODE")).thenReturn(Mono.empty()); + when(activationDBRepository.save(any())).thenReturn(Mono.just(payer)); + + Mono result = activationPayerService.activatePayer("RTP_SP_ID", "FISCAL_CODE"); + + StepVerifier.create(result) + .expectNext(payer) + .verifyComplete(); + } + + @Test + public void testActivatePayerAlreadyExists() { + when(activationDBRepository.findByFiscalCode("FISCAL_CODE")).thenReturn(Mono.just(payer)); + + Mono result = activationPayerService.activatePayer("RTP_SP_ID", "FISCAL_CODE"); + + StepVerifier.create(result) + .expectError(PayerAlreadyExists.class) + .verify(); + } +}