Skip to content

Commit

Permalink
add endpoint and test for get all solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaconsalvi committed Jul 30, 2024
1 parent 75fb5d5 commit 57f035c
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 14 deletions.
Binary file added .mvn/wrapper/maven-wrapper.jar
Binary file not shown.
Empty file modified mvnw
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.pagopa.swclient.mil.papos.model;


import java.util.List;
import io.quarkus.runtime.annotations.RegisterForReflection;
import it.pagopa.swclient.mil.papos.dao.SolutionEntity;

@RegisterForReflection
public record SolutionPageResponse(List<SolutionEntity> terminals, PageMetadata page) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import io.quarkus.logging.Log;
import io.smallrye.mutiny.Uni;
import it.pagopa.swclient.mil.papos.model.PageMetadata;
import it.pagopa.swclient.mil.papos.model.SolutionDto;
import it.pagopa.swclient.mil.papos.model.SolutionPageResponse;
import it.pagopa.swclient.mil.papos.service.SolutionService;
import it.pagopa.swclient.mil.papos.util.ErrorCodes;
import it.pagopa.swclient.mil.papos.util.Errors;
Expand All @@ -27,19 +29,19 @@ public SolutionResource(SolutionService solutionService) {
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({"mil_papos_admin"})
@RolesAllowed({ "mil_papos_admin" })
public Uni<Response> createSolution(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.REQUEST_ID_PATTERN) String requestId,
@HeaderParam("RequestId") @NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG) @Pattern(regexp = RegexPatterns.REQUEST_ID_PATTERN) String requestId,
@Valid @NotNull(message = ErrorCodes.ERROR_DTO_MUST_NOT_BE_NULL_MSG) SolutionDto solution) {

Log.debugf("SolutionResource -> createSolution - Input requestId, solutionDto: %s, %s", requestId, solution);

return solutionService.createSolution(solution)
.onFailure()
.transform(err -> {
Log.errorf(err, "SolutionResource -> createSolution: unexpected error during persist for solution [%s]", solution);
Log.errorf(err,
"SolutionResource -> createSolution: unexpected error during persist for solution [%s]",
solution);

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
Expand All @@ -48,7 +50,8 @@ public Uni<Response> createSolution(
})
.onItem()
.transform(solutionSaved -> {
Log.debugf("SolutionResource -> createSolution: solution saved correctly on DB [%s]", solutionSaved);
Log.debugf("SolutionResource -> createSolution: solution saved correctly on DB [%s]",
solutionSaved);

return Response
.status(Response.Status.CREATED)
Expand All @@ -57,23 +60,79 @@ public Uni<Response> createSolution(
});
}

@GET
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "mil_papos_admin" })
public Uni<Response> getSolutions(
@HeaderParam("RequestId") @NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG) @Pattern(regexp = RegexPatterns.REQUEST_ID_PATTERN) String requestId,
@QueryParam("page") int pageNumber,
@QueryParam("size") int pageSize) {

return solutionService
.getSolutionsCount()
.onFailure()
.transform(err -> {
Log.errorf(err, "SolutionResource -> findAll: error while counting all solutions");

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_COUNTING_SOLUTIONS,
ErrorCodes.ERROR_COUNTING_SOLUTIONS_MSG))
.build());
})
.onItem()
.transformToUni(numberOfSolutions -> {
Log.debugf("SolutionResource -> findAll: found a total count of [%s] solutions", numberOfSolutions);

return solutionService.findSolutions(requestId, pageNumber, pageSize)
.onFailure()
.transform(err -> {
Log.errorf(err,
"SolutionResources -> findAll: Error while retrieving list of solutions, index and size [%s, %s]",
pageNumber, pageSize);

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_LIST_SOLUTIONS,
ErrorCodes.ERROR_LIST_SOLUTIONS_MSG))
.build());
})
.onItem()
.transform(solutionsPaged -> {
Log.debugf(
"SolutionResource -> findAll: size of list of solutions paginated found: [%s]",
solutionsPaged.size());

int totalPages = (int) Math.ceil((double) numberOfSolutions / pageSize);
PageMetadata pageMetadata = new PageMetadata(pageSize, numberOfSolutions, totalPages);

return Response
.status(Response.Status.OK)
.entity(new SolutionPageResponse(solutionsPaged, pageMetadata))
.build();
});
});
}

@GET
@Path("/{solutionId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({"mil_papos_admin"})
@RolesAllowed({ "mil_papos_admin" })
public Uni<Response> findSolution(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.REQUEST_ID_PATTERN) String requestId,
@HeaderParam("RequestId") @NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG) @Pattern(regexp = RegexPatterns.REQUEST_ID_PATTERN) String requestId,
@PathParam(value = "solutionId") String solutionId) {

Log.debugf("SolutionResource -> findSolution: Input requestId, solutionId: %s, %s", requestId, solutionId);

return solutionService.findById(solutionId)
.onFailure()
.transform(err -> {
Log.errorf(err, "SolutionResource -> findSolution: error during search solution with solutionId: [%s]", solutionId);
Log.errorf(err,
"SolutionResource -> findSolution: error during search solution with solutionId: [%s]",
solutionId);

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
Expand All @@ -83,11 +142,14 @@ public Uni<Response> findSolution(
.onItem()
.transformToUni(solution -> {
if (solution == null) {
Log.errorf("SolutionResource -> findSolution: error 404 during searching solution with solutionId: [%s, %s]", solutionId);
Log.errorf(
"SolutionResource -> findSolution: error 404 during searching solution with solutionId: [%s, %s]",
solutionId);

return Uni.createFrom().failure(new NotFoundException(Response
.status(Response.Status.NOT_FOUND)
.entity(new Errors(ErrorCodes.ERROR_SOLUTION_NOT_FOUND, ErrorCodes.ERROR_SOLUTION_NOT_FOUND_MSG))
.entity(new Errors(ErrorCodes.ERROR_SOLUTION_NOT_FOUND,
ErrorCodes.ERROR_SOLUTION_NOT_FOUND_MSG))
.build()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import jakarta.enterprise.context.ApplicationScoped;
import org.bson.types.ObjectId;

import java.util.List;

@ApplicationScoped
public class SolutionService {

Expand Down Expand Up @@ -36,6 +38,17 @@ public Uni<SolutionEntity> createSolution(SolutionDto solutionDto) {
.onItem()
.transform(terminalSaved -> terminalSaved);
}

/**
* Find all the solutions.
*
* @param requestId
* @return Solutions found
*/
public Uni<List<SolutionEntity>> findSolutions(String requestId, int pageNumber, int pageSize) {
Log.debugf("SolutionService -> findSolutions - Input requestId: %s, pageNumber: %s, size: %s", requestId, pageNumber, pageSize);
return solutionRepository.findAll().page(pageNumber,pageSize).list();
}

/**
* Find first solution equals to solutionId given in input.
Expand All @@ -48,4 +61,16 @@ public Uni<SolutionEntity> findById(String solutionId) {

return solutionRepository.findById(new ObjectId(solutionId));
}


/**
* Returns a number corresponding to the total number of solutions found.
*
* @return a number
*/
public Uni<Long> getSolutionsCount() {
Log.debugf("SolutionService -> getSolutionsCount");

return solutionRepository.count();
}
}
13 changes: 12 additions & 1 deletion src/main/java/it/pagopa/swclient/mil/papos/util/ErrorCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private ErrorCodes() {
public static final String ERROR_AMOUNT_MUST_NOT_BE_NULL = MODULE_ID + "000017";
public static final String ERROR_STATUS_MUST_NOT_BE_NULL = MODULE_ID + "000018";
public static final String ERROR_SOLUTIONID_MUST_NOT_BE_NULL = MODULE_ID + "000019";


/*
* Service errors code from 000200 to 000500
Expand All @@ -47,6 +48,9 @@ private ErrorCodes() {
public static final String ERROR_TRANSACTION_NOT_FOUND = MODULE_ID + "000209";
public static final String ERROR_CHECK_TOKEN = MODULE_ID + "000210";
public static final String ERROR_SOLUTION_NOT_FOUND = MODULE_ID + "000211";
public static final String ERROR_LIST_SOLUTIONS = MODULE_ID + "000212";
public static final String ERROR_COUNTING_SOLUTIONS = MODULE_ID + "000213";


/*
* Error descriptions
Expand All @@ -72,7 +76,7 @@ private ErrorCodes() {
private static final String ERROR_SOLUTIONID_MUST_NOT_BE_NULL_DESCR = "solutionId must not be null";

private static final String ERROR_GENERIC_FROM_DB_DESCR = "unexpected error from db";
private static final String ERROR_COUNTING_TERMINALS_DESCR = "error occurred while counting terminal";
private static final String ERROR_COUNTING_TERMINALS_DESCR = "error occurred while counting terminals";
private static final String ERROR_LIST_TERMINALS_DESCR = "error occurred while retrieving list of paginated terminals";
private static final String ERROR_TERMINAL_NOT_FOUND_DESCR = "terminal not found on db";
private static final String ERROR_BULKLOADSTATUS_NOT_FOUND_DESCR = "bulkLoadStatus not found on db";
Expand All @@ -83,6 +87,10 @@ private ErrorCodes() {
private static final String ERROR_TRANSACTION_NOT_FOUND_DESCR = "transaction not found on db";
private static final String ERROR_CHECK_TOKEN_DESCR = "check token fails, subject differs from pspId/payeeCode";
private static final String ERROR_SOLUTION_NOT_FOUND_DESCR = "solution not found on db";
private static final String ERROR_LIST_SOLUTIONS_DESCR = "error occurred while retrieving list of paginated solutions";
private static final String ERROR_COUNTING_SOLUTIONS_DESCR = "error occurred while counting solutions";



/*
* Error complete message
Expand Down Expand Up @@ -119,5 +127,8 @@ private ErrorCodes() {
public static final String ERROR_TRANSACTION_NOT_FOUND_MSG = "[" + ERROR_TRANSACTION_NOT_FOUND + "] " + ERROR_TRANSACTION_NOT_FOUND_DESCR;
public static final String ERROR_CHECK_TOKEN_MSG = "[" + ERROR_CHECK_TOKEN + "] " + ERROR_CHECK_TOKEN_DESCR;
public static final String ERROR_SOLUTION_NOT_FOUND_MSG = "[" + ERROR_SOLUTION_NOT_FOUND + "] " + ERROR_SOLUTION_NOT_FOUND_DESCR;
public static final String ERROR_LIST_SOLUTIONS_MSG = "[" + ERROR_LIST_SOLUTIONS + "] " + ERROR_LIST_SOLUTIONS_DESCR;
public static final String ERROR_COUNTING_SOLUTIONS_MSG = "[" + ERROR_COUNTING_SOLUTIONS + "] " + ERROR_COUNTING_SOLUTIONS_DESCR;


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static io.restassured.RestAssured.given;
import static org.mockito.ArgumentMatchers.any;

import java.util.ArrayList;

@QuarkusTest
@TestHTTPEndpoint(SolutionResource.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -135,4 +137,29 @@ void testFindByIdSolutionEndpoint_404() {
solutionEntity = TestData.getCorrectSolutionEntity();
Assertions.assertEquals(404, response.statusCode());
}



@Test
@TestSecurity(user = "testUser", roles = {"mil_papos_admin"})
void testFindAll_200() {
Mockito.when(solutionService.getSolutionsCount())
.thenReturn(Uni.createFrom().item(10L));

Mockito.when(solutionService.findSolutions("requestid", 0, 10))
.thenReturn(Uni.createFrom().item(new ArrayList<>()));

Response response = given()
.contentType(ContentType.JSON)
.header("RequestId", "1a2b3c4d-5e6f-789a-bcde-f0123456789a")
.queryParam("page", 0)
.queryParam("size", 10)
.when()
.get("/")
.then()
.extract().response();

Assertions.assertEquals(200, response.statusCode());
}

}

0 comments on commit 57f035c

Please sign in to comment.