diff --git a/apps/onboarding-ms/src/test/java/it/pagopa/selfcare/onboarding/controller/TokenControllerTest.java b/apps/onboarding-ms/src/test/java/it/pagopa/selfcare/onboarding/controller/TokenControllerTest.java index c8e875a4d..728768714 100644 --- a/apps/onboarding-ms/src/test/java/it/pagopa/selfcare/onboarding/controller/TokenControllerTest.java +++ b/apps/onboarding-ms/src/test/java/it/pagopa/selfcare/onboarding/controller/TokenControllerTest.java @@ -1,5 +1,10 @@ package it.pagopa.selfcare.onboarding.controller; +import static io.restassured.RestAssured.given; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + import io.quarkus.test.InjectMock; import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.common.http.TestHTTPEndpoint; @@ -9,17 +14,15 @@ import io.restassured.http.ContentType; import io.smallrye.mutiny.Uni; import it.pagopa.selfcare.onboarding.entity.Token; +import it.pagopa.selfcare.onboarding.exception.InvalidRequestException; import it.pagopa.selfcare.onboarding.service.TokenService; import jakarta.ws.rs.core.MediaType; -import org.jboss.resteasy.reactive.RestResponse; -import org.junit.jupiter.api.Test; - import java.io.File; import java.util.List; import java.util.UUID; - -import static io.restassured.RestAssured.given; -import static org.mockito.Mockito.*; +import org.jboss.resteasy.reactive.RestResponse; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @QuarkusTest @TestHTTPEndpoint(TokenController.class) @@ -37,15 +40,15 @@ void getToken() { Token token = new Token(); token.setId(UUID.randomUUID().toString()); when(tokenService.getToken(onboardingId)) - .thenReturn(Uni.createFrom().item(List.of(token))); + .thenReturn(Uni.createFrom().item(List.of(token))); given() - .when() - .contentType(ContentType.JSON) - .queryParams("onboardingId", onboardingId) - .get() - .then() - .statusCode(200); + .when() + .contentType(ContentType.JSON) + .queryParams("onboardingId", onboardingId) + .get() + .then() + .statusCode(200); } @Test @@ -54,14 +57,14 @@ void getContract() { final String onboardingId = "onboardingId"; RestResponse.ResponseBuilder response = RestResponse.ResponseBuilder.ok(); when(tokenService.retrieveContractNotSigned(onboardingId)) - .thenReturn(Uni.createFrom().item(response.build())); + .thenReturn(Uni.createFrom().item(response.build())); given() - .when() - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .get("/{onboardingId}/contract", onboardingId) - .then() - .statusCode(200); + .when() + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .get("/{onboardingId}/contract", onboardingId) + .then() + .statusCode(200); } @Test @@ -71,15 +74,15 @@ void getAttachment() { final String filename = "filename"; RestResponse.ResponseBuilder response = RestResponse.ResponseBuilder.ok(); when(tokenService.retrieveAttachment(onboardingId, filename)) - .thenReturn(Uni.createFrom().item(response.build())); + .thenReturn(Uni.createFrom().item(response.build())); given() - .when() - .queryParam("name", filename) - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .get("/{onboardingId}/attachment", onboardingId) - .then() - .statusCode(200); + .when() + .queryParam("name", filename) + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .get("/{onboardingId}/attachment", onboardingId) + .then() + .statusCode(200); } @Test @@ -87,10 +90,63 @@ void getAttachment() { void getAttachmentBadRequest() { final String onboardingId = "onboardingId"; given() - .when() - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .get("/{onboardingId}/attachment", onboardingId) - .then() - .statusCode(400); + .when() + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .get("/{onboardingId}/attachment", onboardingId) + .then() + .statusCode(400); } + + @Test + @TestSecurity(user = "userJwt") + void updateContractSignedTest_OK() { + // given + final String onboardingId = "onboardingId"; + final String contractSigned = "contractSigned"; + + Uni response = Uni.createFrom().item(Long.valueOf(1)); + when(tokenService.updateContractSigned(onboardingId, contractSigned)).thenReturn(response); + + // when + given() + .when() + .queryParam("onboardingId", onboardingId) + .queryParam("contractSigned", contractSigned) + .contentType(MediaType.APPLICATION_JSON) + .put("/contract-signed") + .then() + .statusCode(200); + + // then + Mockito.verify(tokenService, times(1)).updateContractSigned(anyString(), anyString()); + } + + @Test + @TestSecurity(user = "userJwt") + void updateContractSignedTest_KO() { + // given + final String onboardingId = "onboardingId"; + final String contractSigned = "contractSigned"; + + Uni response = Uni.createFrom() + .failure( + new InvalidRequestException( + String.format("Error %S", onboardingId))); + + when(tokenService.updateContractSigned(onboardingId, contractSigned)).thenReturn(response); + + // when + given() + .when() + .queryParam("onboardingId", onboardingId) + .queryParam("contractSigned", contractSigned) + .contentType(MediaType.APPLICATION_JSON) + .put("/contract-signed") + .then() + .statusCode(400); + + // then + Mockito.verify(tokenService, times(1)).updateContractSigned(anyString(), anyString()); + } + }