Skip to content

Commit

Permalink
Added junit test for contract-signed
Browse files Browse the repository at this point in the history
  • Loading branch information
giampieroferrara committed Dec 9, 2024
1 parent 7a6e76d commit 3e85a8d
Showing 1 changed file with 87 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -54,14 +57,14 @@ void getContract() {
final String onboardingId = "onboardingId";
RestResponse.ResponseBuilder<File> 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
Expand All @@ -71,26 +74,79 @@ void getAttachment() {
final String filename = "filename";
RestResponse.ResponseBuilder<File> 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
@TestSecurity(user = "userJwt")
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<Long> 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<Long> 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());
}

}

0 comments on commit 3e85a8d

Please sign in to comment.