Skip to content

Commit

Permalink
add tests for rtp
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaconsalvi committed Dec 6, 2024
1 parent 921567a commit 64cfc2c
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 21 deletions.
24 changes: 10 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.openapitools.generator.gradle.plugin.tasks.GenerateTask

plugins {
id 'java'
id 'org.springframework.boot' version '3.3.5'
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -187,7 +184,7 @@ bootRun {
]
}

tasks.withType(JavaExec).configureEach {
tasks.withType(JavaExec) {
jvmArgs += '-Djava.security.properties=src/main/resources/custom.security'
}

Expand All @@ -209,5 +206,4 @@ sonarqube {
property 'sonar.organization', 'pagopa'
property 'sonar.projectKey', 'pagopa_rtp-activator'
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public Mono<ResponseEntity<Void>> createRtp(Mono<CreateRtpDto> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Rtp> 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));
}
}
13 changes: 11 additions & 2 deletions src/test/java/it/gov/pagopa/rtp/activator/utils/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
}

0 comments on commit 64cfc2c

Please sign in to comment.