Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaconsalvi committed Nov 25, 2024
1 parent c8fdcad commit 833f6aa
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public Mono<Payer> activatePayer(String rtpSpId, String fiscalCode) {
Payer payer = new Payer(payerID, rtpSpId, fiscalCode, Instant.now());

return activationDBRepository.findByFiscalCode(fiscalCode)
.flatMap(existingEntity -> Mono.<Payer>error(new PayerAlreadyExists()))
.switchIfEmpty(activationDBRepository.save(payer));

.flatMap(existingEntity -> Mono.<Payer>error(new PayerAlreadyExists()))
.switchIfEmpty(Mono.defer(() -> activationDBRepository.save(payer)));
}
}
22 changes: 22 additions & 0 deletions src/test/java/it/gov/pagopa/rtp/activator/domain/PayerIDTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<Payer> 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<Payer> result = activationPayerService.activatePayer("RTP_SP_ID", "FISCAL_CODE");

StepVerifier.create(result)
.expectError(PayerAlreadyExists.class)
.verify();
}
}

0 comments on commit 833f6aa

Please sign in to comment.