Skip to content

Commit

Permalink
add test for controller
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaconsalvi committed Nov 26, 2024
1 parent 13dcda3 commit 338c1ad
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

import com.azure.spring.data.cosmos.repository.config.EnableReactiveCosmosRepositories;

import reactor.core.publisher.Hooks;

@SpringBootApplication
@EnableReactiveCosmosRepositories
@ConfigurationPropertiesScan()
public class RtpActivatorApplication {

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ management.endpoint.health.probes.enabled=true

#repository
cosmos.credential.db-name=${DB_NAME:rtp}
cosmos.credential.endpoint=${COSMOS_ACCOUNT_RTP_ENDPOINT:""}
cosmos.credential.endpoint=${COSMOS_CONNECTION_STRING:""}


activation.base-url=${BASE_URL:"http://localhost"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
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.repository.ActivationDBRepository;
import it.gov.pagopa.rtp.activator.service.ActivationPayerService;
import it.gov.pagopa.rtp.activator.service.ActivationPayerServiceImpl;
import it.gov.pagopa.rtp.activator.utils.Users;
import reactor.core.publisher.Mono;

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.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.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.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 })
@Import({ SecurityConfig.class })
class ActivationAPIControllerImplTest {

@MockBean
private ActivationPayerService activationPayerService;

@MockBean
private ActivationPropertiesConfig activationPropertiesConfig;

private WebTestClient webTestClient;

@Autowired
private ApplicationContext context;

@BeforeEach
public void setup() {
webTestClient = WebTestClient
.bindToApplicationContext(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/");

webTestClient.post()
.uri(uriBuilder -> uriBuilder.path("/activation")
.queryParam("requestId", UUID.randomUUID().toString())
.queryParam("version", "v1").build())
.contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(generateActivationRequest()))
.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()));
* webTestClient.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() {
* webTestClient.post()
* .uri("/activations")
* .header("RequestId", UUID.randomUUID().toString())
* .header("Version", "v1")
* .bodyValue(generateActivationRequest())
* .exchange()
* .expectStatus().isEqualTo(HttpStatus.CREATED)
* .expectHeader().exists(HttpHeaders.LOCATION);
* }
*
* @Test
*
* @WithMockUser(value = "another", roles = Users.ACTIVATION_WRITE_ROLE)
* void authorizedUserShouldNotActivateForAnotherServiceProvider() {
* webTestClient.post()
* .uri("/activations")
* .header("RequestId", UUID.randomUUID().toString())
* .header("Version", "v1")
* .bodyValue(generateActivationRequest())
* .exchange()
* .expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
* }
*
* @Test
*
* @WithMockUser
* void userWithoutEnoughPermissionShouldNotCreateNewActivation() {
* webTestClient.post()
* .uri("/activations")
* .header("RequestId", UUID.randomUUID().toString())
* .header("Version", "v1")
* .bodyValue(generateActivationRequest())
* .exchange()
* .expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
* }
*/

private ActivationReqDto generateActivationRequest() {
return new ActivationReqDto(new PayerDto("RSSMRA85T10A562S", SERVICE_PROVIDER_ID));
}
}

0 comments on commit 338c1ad

Please sign in to comment.