From 64cfc2c8c75bf2a1a647bf3c6ef77955781950c5 Mon Sep 17 00:00:00 2001 From: Luca Consalvi Date: Fri, 6 Dec 2024 15:25:36 +0100 Subject: [PATCH] add tests for rtp --- build.gradle | 24 ++-- .../controller/SendAPIControllerImpl.java | 4 +- .../ActivationAPIControllerImplTest.java | 3 +- .../controller/SendAPIControllerImplTest.java | 111 ++++++++++++++++++ .../activator/domain/rtp/ResourceIDTest.java | 26 ++++ .../ActivationPayerServiceImplTest.java | 1 - .../service/rtp/SendRTPServiceTest.java | 78 ++++++++++++ .../gov/pagopa/rtp/activator/utils/Users.java | 13 +- 8 files changed, 239 insertions(+), 21 deletions(-) rename src/test/java/it/gov/pagopa/rtp/activator/controller/{activation => }/ActivationAPIControllerImplTest.java (97%) create mode 100644 src/test/java/it/gov/pagopa/rtp/activator/controller/SendAPIControllerImplTest.java create mode 100644 src/test/java/it/gov/pagopa/rtp/activator/domain/rtp/ResourceIDTest.java create mode 100644 src/test/java/it/gov/pagopa/rtp/activator/service/rtp/SendRTPServiceTest.java diff --git a/build.gradle b/build.gradle index f1c5f0b..5ee965b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,3 @@ -import org.openapitools.generator.gradle.plugin.tasks.GenerateTask - plugins { id 'java' id 'org.springframework.boot' version '3.3.5' @@ -82,7 +80,7 @@ sourceSets { } } -tasks.register('generateOpenAPIActivate', GenerateTask) { +task generateOpenAPIActivate(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) { generatorName.set("spring") inputSpec.set("$rootDir/openapi/activation.openapi.yaml") outputDir.set("$projectDir/build/generated") @@ -112,7 +110,7 @@ tasks.register('generateOpenAPIActivate', GenerateTask) { } -tasks.register('generateOpenAPISend', GenerateTask) { +task generateOpenAPISend(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) { generatorName.set("spring") inputSpec.set("$rootDir/openapi/send.openapi.yaml") outputDir.set("$projectDir/build/generated") @@ -141,22 +139,21 @@ tasks.register('generateOpenAPISend', GenerateTask) { ]) } -tasks.register('generateOpenAPIEPC', GenerateTask) { - generatorName.set("java") +task generateOpenAPIEPC(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) { + generatorName.set("spring") inputSpec.set("$rootDir/openapi/EPC133-22 v3.1 - SRTP scheme rulebook v3.1 related API specifications_optimized.yaml") outputDir.set("$projectDir/build/generated") - apiPackage.set("it.gov.pagopa.rtp.activator.client.epc.api") - modelPackage.set("it.gov.pagopa.rtp.activator.client.epc.model") - invokerPackage.set("it.gov.pagopa.rtp.activator.client.epc.invoker") + apiPackage.set("it.gov.pagopa.rtp.activator.controller.generated.epc") + modelPackage.set("it.gov.pagopa.rtp.activator.model.generated.epc") modelNameSuffix.set("Dto") generateApiTests.set(false) generateApiDocumentation.set(false) generateApiTests.set(false) generateModelTests.set(false) - library.set("webclient") + library.set("spring-boot") configOptions.set([ "dateLibrary" : "java8", - "useJakartaEe" : "true", + "useSpringBoot3" : "true", "interfaceOnly" : "true", "useTags" : "true", "useSwaggerUI" : "false", @@ -187,7 +184,7 @@ bootRun { ] } -tasks.withType(JavaExec).configureEach { +tasks.withType(JavaExec) { jvmArgs += '-Djava.security.properties=src/main/resources/custom.security' } @@ -209,5 +206,4 @@ sonarqube { property 'sonar.organization', 'pagopa' property 'sonar.projectKey', 'pagopa_rtp-activator' } -} - +} \ No newline at end of file diff --git a/src/main/java/it/gov/pagopa/rtp/activator/controller/SendAPIControllerImpl.java b/src/main/java/it/gov/pagopa/rtp/activator/controller/SendAPIControllerImpl.java index 162aef7..21db209 100644 --- a/src/main/java/it/gov/pagopa/rtp/activator/controller/SendAPIControllerImpl.java +++ b/src/main/java/it/gov/pagopa/rtp/activator/controller/SendAPIControllerImpl.java @@ -27,8 +27,8 @@ public Mono> createRtp(Mono createRtpDto, ServerWebExchange exchange) { return createRtpDto .flatMap(t -> sendRTPService.send(t.getNoticeNumber(), t.getAmount(), t.getDescription(), - t.getExpiryDate(), t.getPayerId(), t.getPayee().getName(), t.getPayee().getPayeeId(), "null", - "null", "null", "null")) + t.getExpiryDate(), t.getPayerId(), t.getPayee().getName(), t.getPayee().getPayeeId(), "endToEndId", + "iban", "payTrxRef", "flgConf")) .thenReturn(ResponseEntity.status(201).build()); } } diff --git a/src/test/java/it/gov/pagopa/rtp/activator/controller/activation/ActivationAPIControllerImplTest.java b/src/test/java/it/gov/pagopa/rtp/activator/controller/ActivationAPIControllerImplTest.java similarity index 97% rename from src/test/java/it/gov/pagopa/rtp/activator/controller/activation/ActivationAPIControllerImplTest.java rename to src/test/java/it/gov/pagopa/rtp/activator/controller/ActivationAPIControllerImplTest.java index 7542c33..0c065bc 100644 --- a/src/test/java/it/gov/pagopa/rtp/activator/controller/activation/ActivationAPIControllerImplTest.java +++ b/src/test/java/it/gov/pagopa/rtp/activator/controller/ActivationAPIControllerImplTest.java @@ -1,8 +1,7 @@ -package it.gov.pagopa.rtp.activator.controller.activation; +package it.gov.pagopa.rtp.activator.controller; import it.gov.pagopa.rtp.activator.configuration.ActivationPropertiesConfig; import it.gov.pagopa.rtp.activator.configuration.SecurityConfig; -import it.gov.pagopa.rtp.activator.controller.ActivationAPIControllerImpl; import it.gov.pagopa.rtp.activator.domain.errors.PayerAlreadyExists; import it.gov.pagopa.rtp.activator.domain.payer.Payer; import it.gov.pagopa.rtp.activator.domain.payer.PayerID; diff --git a/src/test/java/it/gov/pagopa/rtp/activator/controller/SendAPIControllerImplTest.java b/src/test/java/it/gov/pagopa/rtp/activator/controller/SendAPIControllerImplTest.java new file mode 100644 index 0000000..4ecdf7f --- /dev/null +++ b/src/test/java/it/gov/pagopa/rtp/activator/controller/SendAPIControllerImplTest.java @@ -0,0 +1,111 @@ +package it.gov.pagopa.rtp.activator.controller; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Import; +import org.springframework.http.HttpStatus; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.aot.DisabledInAotMode; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.reactive.server.WebTestClient; + +import it.gov.pagopa.rtp.activator.configuration.SecurityConfig; + +import it.gov.pagopa.rtp.activator.model.generated.send.CreateRtpDto; +import it.gov.pagopa.rtp.activator.model.generated.send.PayeeDto; +import it.gov.pagopa.rtp.activator.service.rtp.SendRTPService; +import it.gov.pagopa.rtp.activator.utils.Users; +import reactor.core.publisher.Mono; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; +import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity; + +import java.time.LocalDate; + +@ExtendWith(SpringExtension.class) +@WebFluxTest(controllers = { SendAPIControllerImpl.class }) +@Import({ SecurityConfig.class }) +@DisabledInAotMode +public class SendAPIControllerImplTest { + + @MockBean + private SendRTPService sendRTPService; + + private WebTestClient webTestClient; + + @Autowired + private ApplicationContext context; + + @BeforeEach + void setup() { + webTestClient = WebTestClient + .bindToApplicationContext(context) + .apply(springSecurity()) + .configureClient() + .build(); + } + + @Test + @Users.RtpSenderWriter + void testSendRtpSuccessful() { + + when(sendRTPService.send(anyString(), anyInt(), anyString(), any(), anyString(), anyString(), anyString(), + anyString(), anyString(), anyString(), anyString())) + .thenReturn(Mono.empty()); + + webTestClient.post() + .uri("/rtps") + .bodyValue(generateSendRequest()) + .exchange() + .expectStatus() + .isCreated() + .expectBody() + .isEmpty(); + } + + @Test + @Users.RtpSenderWriter + void testSendRtpWithWrongBody() { + + when(sendRTPService.send(anyString(), anyInt(), anyString(), any(), anyString(), anyString(), anyString(), + anyString(), anyString(), anyString(), anyString())) + .thenReturn(Mono.empty()); + + webTestClient.post() + .uri("/rtps") + .bodyValue(generateWrongSendRequest()) + .exchange() + .expectStatus() + .isEqualTo(HttpStatus.BAD_REQUEST); + } + + @Test + @WithMockUser + void userWithoutEnoughPermissionShouldNotSendRtp() { + webTestClient.post() + .uri("/rtps") + .bodyValue(generateSendRequest()) + .exchange() + .expectStatus() + .isEqualTo(HttpStatus.FORBIDDEN); + } + + private CreateRtpDto generateSendRequest() { + return new CreateRtpDto("311111111112222222", 1, "description", LocalDate.now(), "payerId", + new PayeeDto("77777777777", "payeeName")); + } + + private CreateRtpDto generateWrongSendRequest() { + return new CreateRtpDto("noticenumber", 1, "description", LocalDate.now(), "payerId", + new PayeeDto("dsds", "payeeName")); + } +} diff --git a/src/test/java/it/gov/pagopa/rtp/activator/domain/rtp/ResourceIDTest.java b/src/test/java/it/gov/pagopa/rtp/activator/domain/rtp/ResourceIDTest.java new file mode 100644 index 0000000..77567e4 --- /dev/null +++ b/src/test/java/it/gov/pagopa/rtp/activator/domain/rtp/ResourceIDTest.java @@ -0,0 +1,26 @@ +package it.gov.pagopa.rtp.activator.domain.rtp; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.util.UUID; + +import org.junit.jupiter.api.Test; + +public class ResourceIDTest { + @Test + void testCreateNew() { + ResourceID resourceID = ResourceID.createNew(); + assertNotNull(resourceID); + assertNotNull(resourceID.getId()); + } + + @Test + void testConstructor() { + UUID uuid = UUID.randomUUID(); + ResourceID resourceID = new ResourceID(uuid); + assertNotNull(resourceID); + assertEquals(uuid, resourceID.getId()); + } + +} diff --git a/src/test/java/it/gov/pagopa/rtp/activator/service/activation/ActivationPayerServiceImplTest.java b/src/test/java/it/gov/pagopa/rtp/activator/service/activation/ActivationPayerServiceImplTest.java index 979bb6e..1b4dc31 100644 --- a/src/test/java/it/gov/pagopa/rtp/activator/service/activation/ActivationPayerServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/rtp/activator/service/activation/ActivationPayerServiceImplTest.java @@ -11,7 +11,6 @@ import it.gov.pagopa.rtp.activator.domain.payer.Payer; import it.gov.pagopa.rtp.activator.domain.payer.PayerID; import it.gov.pagopa.rtp.activator.repository.activation.ActivationDBRepository; -import it.gov.pagopa.rtp.activator.service.activation.ActivationPayerServiceImpl; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; diff --git a/src/test/java/it/gov/pagopa/rtp/activator/service/rtp/SendRTPServiceTest.java b/src/test/java/it/gov/pagopa/rtp/activator/service/rtp/SendRTPServiceTest.java new file mode 100644 index 0000000..2b4520c --- /dev/null +++ b/src/test/java/it/gov/pagopa/rtp/activator/service/rtp/SendRTPServiceTest.java @@ -0,0 +1,78 @@ +package it.gov.pagopa.rtp.activator.service.rtp; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.net.URI; +import java.time.LocalDate; +import java.time.LocalDateTime; + +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.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; + +import it.gov.pagopa.rtp.activator.domain.rtp.ResourceID; +import it.gov.pagopa.rtp.activator.domain.rtp.Rtp; +import it.gov.pagopa.rtp.activator.model.generated.epc.SepaRequestToPayRequestResourceDto; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +@ExtendWith(MockitoExtension.class) +public class SendRTPServiceTest { + + @Mock + private SepaRequestToPayMapper sepaRequestToPayMapper; + + @InjectMocks + private SendRTPServiceImpl sendRTPService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testSend() { + String noticeNumber = "12345"; + Integer amount = 100; + String description = "Payment Description"; + LocalDate expiryDate = LocalDate.now(); + String payerId = "payerId"; + String payeeName = "Payee Name"; + String payeeId = "payeeId"; + String endToEndId = "endToEndId"; + String iban = "IT60X0542811101000000123456"; + String payTrxRef = "payTrxRef"; + String flgConf = "flgConf"; + + Rtp expectedRtp = new Rtp(noticeNumber, amount, description, expiryDate, payerId, payeeName, payeeId, + ResourceID.createNew(), LocalDateTime.now(), payeeId, endToEndId, iban, payTrxRef, flgConf); + + SepaRequestToPayRequestResourceDto mockSepaRequestToPayRequestResource = new SepaRequestToPayRequestResourceDto( + URI.create("http://callback.url")); + + when(sepaRequestToPayMapper.toRequestToPay(any(Rtp.class))).thenReturn(mockSepaRequestToPayRequestResource); + + Mono result = sendRTPService.send(noticeNumber, amount, description, expiryDate, payerId, payeeName, + payeeId, endToEndId, iban, payTrxRef, flgConf); + StepVerifier.create(result) + .expectNextMatches(rtp -> rtp.noticeNumber().equals(expectedRtp.noticeNumber()) + && rtp.amount().equals(expectedRtp.amount()) + && rtp.description().equals(expectedRtp.description()) + && rtp.expiryDate().equals(expectedRtp.expiryDate()) + && rtp.payerId().equals(expectedRtp.payerId()) + && rtp.payeeName().equals(expectedRtp.payeeName()) + && rtp.payeeId().equals(expectedRtp.payeeId()) + && rtp.endToEndId().equals(expectedRtp.endToEndId()) + && rtp.iban().equals(expectedRtp.iban()) && rtp.payTrxRef().equals(expectedRtp.payTrxRef()) + && rtp.flgConf().equals(expectedRtp.flgConf())) + .verifyComplete(); + verify(sepaRequestToPayMapper, times(1)).toRequestToPay(any(Rtp.class)); + } +} diff --git a/src/test/java/it/gov/pagopa/rtp/activator/utils/Users.java b/src/test/java/it/gov/pagopa/rtp/activator/utils/Users.java index 7e8596f..5266473 100644 --- a/src/test/java/it/gov/pagopa/rtp/activator/utils/Users.java +++ b/src/test/java/it/gov/pagopa/rtp/activator/utils/Users.java @@ -12,11 +12,20 @@ public class Users { public static final String ACTIVATION_WRITE_ROLE = "write_rtp_activations"; public static final String ACTIVATION_READ_ROLE = "read_rtp_activations"; + public static final String SENDER_WRITER_ROLDE = "write_rtp_send"; + @Retention(RetentionPolicy.RUNTIME) @WithMockUser(value = SERVICE_PROVIDER_ID, roles = ACTIVATION_WRITE_ROLE) - public @interface RtpWriter { } + public @interface RtpWriter { + } @Retention(RetentionPolicy.RUNTIME) @WithMockUser(value = SERVICE_PROVIDER_ID, roles = ACTIVATION_READ_ROLE) - public @interface RtpReader { } + public @interface RtpReader { + } + + @Retention(RetentionPolicy.RUNTIME) + @WithMockUser(value = SERVICE_PROVIDER_ID, roles = SENDER_WRITER_ROLDE) + public @interface RtpSenderWriter { + } }