From c835e91b3ecc1511de1dc2b642feaeaa2b150cf2 Mon Sep 17 00:00:00 2001 From: Luca Consalvi Date: Mon, 25 Nov 2024 11:36:26 +0100 Subject: [PATCH] add vars and buggeed controller test --- .../service/ActivationPayerServiceImpl.java | 1 - .../env/cstar-d-weu-rtp/terraform.tfvars | 4 +- .../env/cstar-p-weu-rtp/terraform.tfvars | 4 +- .../env/cstar-u-weu-rtp/terraform.tfvars | 4 +- .../ActivationAPIControllerImplTest.java | 62 ++++++++++++++++++- 5 files changed, 65 insertions(+), 10 deletions(-) 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 6eba452..de921e6 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,6 @@ 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(Mono.defer(() -> activationDBRepository.save(payer))); } diff --git a/src/main/terraform/env/cstar-d-weu-rtp/terraform.tfvars b/src/main/terraform/env/cstar-d-weu-rtp/terraform.tfvars index 3a16fd4..f59f8fb 100644 --- a/src/main/terraform/env/cstar-d-weu-rtp/terraform.tfvars +++ b/src/main/terraform/env/cstar-d-weu-rtp/terraform.tfvars @@ -22,8 +22,8 @@ tags = { # ------------------------------------------------------------------------------ cae_name = "cstar-d-mcshared-cae" cae_resource_group_name = "cstar-d-mcshared-app-rg" -id_name = "cstar-d-mcshared-auth-id" -id_resource_group_name = "cstar-d-mcshared-identity-rg" +id_name = "cstar-d-weu-rtp-activator-id" +id_resource_group_name = "cstar-d-weu-rtp-identity-rg" # ------------------------------------------------------------------------------ # Names of key vault secrets. diff --git a/src/main/terraform/env/cstar-p-weu-rtp/terraform.tfvars b/src/main/terraform/env/cstar-p-weu-rtp/terraform.tfvars index 4546fc2..5c28c31 100644 --- a/src/main/terraform/env/cstar-p-weu-rtp/terraform.tfvars +++ b/src/main/terraform/env/cstar-p-weu-rtp/terraform.tfvars @@ -22,8 +22,8 @@ tags = { # ------------------------------------------------------------------------------ cae_name = "cstar-p-mcshared-cae" cae_resource_group_name = "cstar-p-mcshared-app-rg" -id_name = "cstar-p-mcshared-auth-id" -id_resource_group_name = "cstar-p-mcshared-identity-rg" +id_name = "cstar-p-weu-rtp-activator-id" +id_resource_group_name = "cstar-p-weu-rtp-identity-rg" # ------------------------------------------------------------------------------ # Names of key vault secrets. diff --git a/src/main/terraform/env/cstar-u-weu-rtp/terraform.tfvars b/src/main/terraform/env/cstar-u-weu-rtp/terraform.tfvars index 7272c32..3ae714a 100644 --- a/src/main/terraform/env/cstar-u-weu-rtp/terraform.tfvars +++ b/src/main/terraform/env/cstar-u-weu-rtp/terraform.tfvars @@ -22,8 +22,8 @@ tags = { # ------------------------------------------------------------------------------ cae_name = "cstar-u-mcshared-cae" cae_resource_group_name = "cstar-u-mcshared-app-rg" -id_name = "cstar-u-mcshared-auth-id" -id_resource_group_name = "cstar-u-mcshared-identity-rg" +id_name = "cstar-u-weu-rtp-activator-id" +id_resource_group_name = "cstar-u-weu-rtp-identity-rg" # ------------------------------------------------------------------------------ # Names of key vault secrets. diff --git a/src/test/java/it/gov/pagopa/rtp/activator/controller/ActivationAPIControllerImplTest.java b/src/test/java/it/gov/pagopa/rtp/activator/controller/ActivationAPIControllerImplTest.java index 46178e9..23f45b4 100644 --- a/src/test/java/it/gov/pagopa/rtp/activator/controller/ActivationAPIControllerImplTest.java +++ b/src/test/java/it/gov/pagopa/rtp/activator/controller/ActivationAPIControllerImplTest.java @@ -1,39 +1,69 @@ 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.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.model.generated.ActivationReqDto; import it.gov.pagopa.rtp.activator.model.generated.PayerDto; +import it.gov.pagopa.rtp.activator.service.ActivationPayerService; import it.gov.pagopa.rtp.activator.utils.Users; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + 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.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.boot.test.mock.mockito.SpyBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Import; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.BodyInserters; +import java.net.URI; +import java.time.Instant; import java.util.UUID; import static it.gov.pagopa.rtp.activator.utils.Users.SERVICE_PROVIDER_ID; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity; - @ExtendWith(SpringExtension.class) -@WebFluxTest(controllers = {ActivationAPIControllerImpl.class}) +@WebFluxTest(controllers = { ActivationAPIControllerImpl.class }) @Import(SecurityConfig.class) class ActivationAPIControllerImplTest { @Autowired ApplicationContext context; - WebTestClient web; + @Mock + private ActivationPayerService activationPayerService; + + @Mock + private ActivationPropertiesConfig activationPropertiesConfig; + + @InjectMocks + private ActivationAPIControllerImpl activationAPIController; + + private ActivationReqDto activationReqDto; + + private WebTestClient web; @BeforeEach public void setup() { + activationReqDto = new ActivationReqDto(); web = WebTestClient .bindToApplicationContext(this.context) .apply(springSecurity()) @@ -41,6 +71,32 @@ public void setup() { .build(); } + @Test + @Users.RtpWriter + public void testActivatePayerSuccessful() { + Payer payer = new Payer(PayerID.createNew(), "RTP_SP_ID", "FISCAL_CODE", Instant.now()); + when(activationPayerService.activatePayer(any(String.class), any(String.class))).thenReturn(Mono.just(payer)); + when(activationPropertiesConfig.getBaseUrl()).thenReturn("http://localhost:8080/"); + web.post() + .uri(uriBuilder -> uriBuilder.path("/activation").queryParam("requestId", UUID.randomUUID().toString()) + .queryParam("version", "v1").build()) + .contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(activationReqDto)).exchange() + .expectStatus().isCreated().expectHeader() + .location("http://localhost:8080/" + payer.payerID().toString()); + } + + @Test + @Users.RtpWriter + public void testActivatePayerAlreadyExists() { + when(activationPayerService.activatePayer(any(String.class), any(String.class))) + .thenReturn(Mono.error(new PayerAlreadyExists())); + web.post() + .uri(uriBuilder -> uriBuilder.path("/activation").queryParam("requestId", UUID.randomUUID().toString()) + .queryParam("version", "v1").build()) + .contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(activationReqDto)).exchange() + .expectStatus().isEqualTo(409); + } + @Test @Users.RtpWriter void shouldCreateNewActivation() {