-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13dcda3
commit 338c1ad
Showing
3 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
src/test/java/it/gov/pagopa/rtp/activator/controller/ActivationAPIControllerImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |