Skip to content

Commit

Permalink
add vars and buggeed controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaconsalvi committed Nov 25, 2024
1 parent 833f6aa commit c835e91
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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(Mono.defer(() -> activationDBRepository.save(payer)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/terraform/env/cstar-d-weu-rtp/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/main/terraform/env/cstar-p-weu-rtp/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/main/terraform/env/cstar-u-weu-rtp/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,102 @@
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())
.configureClient()
.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() {
Expand Down

0 comments on commit c835e91

Please sign in to comment.