Skip to content

Commit

Permalink
chore: Add collection postman for solutions (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaconsalvi authored Aug 5, 2024
1 parent bf969cd commit bd87215
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 126 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/post-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ jobs:
run: |
newman run src/test/postman/mil-papos.postman_collection.json \
-e src/test/postman/dev.postman_environment.json \
--env-var "clientIdPsp=${{ secrets.NEWMAN_IT__PA_TOKEN_CLIENT_ID }}" \
--env-var "clientSecretPsp=${{ secrets.NEWMAN_IT_PA_TOKEN_CLIENT_SECRET }}" \
--env-var "clientIdPa=${{ secrets.NEWMAN_IT__PSP_TOKEN_CLIENT_ID }}" \
--env-var "clientSecretPa=${{ secrets.NEWMAN_IT_PSP_TOKEN_CLIENT_SECRET }}" \
--env-var "clientIdPsp=${{ secrets.NEWMAN_IT__PSP_TOKEN_CLIENT_ID }}" \
--env-var "clientSecretPsp=${{ secrets.NEWMAN_IT_PSP_TOKEN_CLIENT_SECRET }}" \
--env-var "clientIdPa=${{ secrets.NEWMAN_IT__PA_TOKEN_CLIENT_ID }}" \
--env-var "clientSecretPa=${{ secrets.NEWMAN_IT_PA_TOKEN_CLIENT_SECRET }}" \
--env-var "clientIdAdm=${{ secrets.NEWMAN_IT__PAPOS_ADMIN_TOKEN_CLIENT_ID }}" \
--env-var "clientSecretAdm=${{ secrets.NEWMAN_IT_PAPOS_ADMIN_TOKEN_CLIENT_SECRET }}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package it.pagopa.swclient.mil.papos.resource;

import org.eclipse.microprofile.jwt.JsonWebToken;

import io.quarkus.logging.Log;
import io.smallrye.mutiny.Uni;

Expand All @@ -23,11 +21,9 @@
@Path("/solutions")
public class SolutionResource {
private final SolutionService solutionService;
private final JsonWebToken jwt;

public SolutionResource(SolutionService solutionService, JsonWebToken jwt) {
public SolutionResource(SolutionService solutionService) {
this.solutionService = solutionService;
this.jwt = jwt;
}

@POST
Expand All @@ -39,7 +35,8 @@ public Uni<Response> createSolution(
@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);
Log.debugf("SolutionResource -> createSolution - Input requestId, solutionDto: %s, %s", requestId,
solution);

return solutionService.createSolution(solution)
.onFailure()
Expand All @@ -50,7 +47,8 @@ public Uni<Response> createSolution(

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB, ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB,
ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
.onItem()
Expand All @@ -75,7 +73,8 @@ public Uni<Response> getSolutions(
@QueryParam("page") int pageNumber,
@QueryParam("size") int pageSize) {

Log.debugf("SolutionResource -> getSolutions - Input requestId, pageNumber, pageSize: %s, %s, %s", requestId,
Log.debugf("SolutionResource -> getSolutions - Input requestId, pageNumber, pageSize: %s, %s, %s",
requestId,
pageNumber, pageSize);

return solutionService
Expand All @@ -92,7 +91,8 @@ public Uni<Response> getSolutions(
})
.onItem()
.transformToUni(numberOfSolutions -> {
Log.debugf("SolutionResource -> findAll: found a total count of [%s] solutions", numberOfSolutions);
Log.debugf("SolutionResource -> findAll: found a total count of [%s] solutions",
numberOfSolutions);

return solutionService.findSolutions(pageNumber, pageSize)
.onFailure()
Expand All @@ -103,7 +103,8 @@ public Uni<Response> getSolutions(

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_LIST_SOLUTIONS,
.entity(new Errors(
ErrorCodes.ERROR_LIST_SOLUTIONS,
ErrorCodes.ERROR_LIST_SOLUTIONS_MSG))
.build());
})
Expand All @@ -113,12 +114,16 @@ public Uni<Response> getSolutions(
"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);
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))
.entity(new SolutionPageResponse(
solutionsPaged,
pageMetadata))
.build();
});
});
Expand All @@ -133,7 +138,8 @@ public Uni<Response> findSolution(
@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);
Log.debugf("SolutionResource -> findSolution: Input requestId, solutionId: %s, %s", requestId,
solutionId);

return solutionService.findById(solutionId)
.onFailure()
Expand All @@ -144,7 +150,8 @@ public Uni<Response> findSolution(

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB, ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB,
ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
.onItem()
Expand Down Expand Up @@ -179,64 +186,9 @@ public Uni<Response> findByPspId(
@QueryParam("page") int pageNumber,
@QueryParam("size") int pageSize) {

checkToken(pspId);

return findByAttribute(requestId, "pspId", pspId, pageNumber, pageSize);
}

private Uni<Response> findByAttribute(String requestId, String attributeName, String attributeValue, int pageNumber,
int pageSize) {
Log.debugf(
"SolutionResource -> findBy - Input requestId: %s, attributeName: %s, attributeValue: %s, pageNumber: %s, size: %s",
requestId, attributeName, attributeValue, pageNumber, pageSize);

return solutionService.getSolutionCountByAttribute(attributeName, attributeValue)
.onFailure()
.transform(err -> {
Log.errorf(err, "SolutionResource -> findBy: error while counting solutions for [%s, %s]",
attributeName, attributeValue);

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 -> findBy: found a total count of [%s] solutions", numberOfSolutions);

return solutionService
.getSolutionsListPagedByAttribute(attributeName, attributeValue, pageNumber, pageSize)
.onFailure()
.transform(err -> {
Log.errorf(err,
"SolutionResource -> findBy: Error while retrieving list of solutions for [%s, %s], index and size [%s, %s]",
attributeName, attributeValue, 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(solutionPaged -> {
Log.debugf(
"SolutionResource -> findBy: size of list of solutions paginated found: [%s]",
solutionPaged.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(solutionPaged, pageMetadata))
.build();
});
});
}

@GET
@Path("/findByLocationCode")
@Consumes(MediaType.APPLICATION_JSON)
Expand All @@ -248,8 +200,6 @@ public Uni<Response> findByLocationCode(
@QueryParam("page") int pageNumber,
@QueryParam("size") int pageSize) {

checkToken(locationCode);

return findByAttribute(requestId, "locationCode", locationCode, pageNumber, pageSize);
}

Expand All @@ -274,7 +224,8 @@ public Uni<Response> deleteSolution(

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB, ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB,
ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
.onItem()
Expand All @@ -300,7 +251,8 @@ public Uni<Response> deleteSolution(

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB,
.entity(new Errors(
ErrorCodes.ERROR_GENERIC_FROM_DB,
ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
Expand All @@ -316,7 +268,7 @@ public Uni<Response> deleteSolution(
});
}

@PATCH
@PUT
@Path("/{solutionId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -332,11 +284,14 @@ public Uni<Response> updateSolution(
return solutionService.findById(solutionId)
.onFailure()
.transform(err -> {
Log.errorf(err, "SolutionResource -> updateSolution : error during search solution with solutionId: [%s]", solutionId);
Log.errorf(err,
"SolutionResource -> updateSolution : error during search solution with solutionId: [%s]",
solutionId);

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB, ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB,
ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
.onItem()
Expand All @@ -350,7 +305,8 @@ public Uni<Response> updateSolution(

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB,
.entity(new Errors(
ErrorCodes.ERROR_GENERIC_FROM_DB,
ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
Expand All @@ -366,19 +322,66 @@ public Uni<Response> updateSolution(
})));
}

private void checkToken(String toCheck) {
Log.debugf("SolutionResource -> checkToken: sub [%s], pspId/locationCode: [%s]", jwt.getSubject(), toCheck);
private Uni<Response> findByAttribute(String requestId, String attributeName, String attributeValue,
int pageNumber,
int pageSize) {
Log.debugf(
"SolutionResource -> findBy - Input requestId: %s, attributeName: %s, attributeValue: %s, pageNumber: %s, size: %s",
requestId, attributeName, attributeValue, pageNumber, pageSize);

return solutionService.getSolutionCountByAttribute(attributeName, attributeValue)
.onFailure()
.transform(err -> {
Log.errorf(err, "SolutionResource -> findBy: error while counting solutions for [%s, %s]",
attributeName, attributeValue);

if (!jwt.getSubject().equals(toCheck)) {
Log.errorf(
"SolutionResource -> checkToken: Error while checking token, subject not equals to pspId/locationCode [%s, %s]",
jwt.getSubject(), toCheck);
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 -> findBy: found a total count of [%s] solutions",
numberOfSolutions);

throw new WebApplicationException(Response
.status(Response.Status.UNAUTHORIZED)
.entity(new Errors(ErrorCodes.ERROR_CHECK_TOKEN, ErrorCodes.ERROR_CHECK_TOKEN_MSG))
.build());
}
return solutionService
.getSolutionsListPagedByAttribute(attributeName, attributeValue,
pageNumber, pageSize)
.onFailure()
.transform(err -> {
Log.errorf(err,
"SolutionResource -> findBy: Error while retrieving list of solutions for [%s, %s], index and size [%s, %s]",
attributeName, attributeValue,
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(solutionPaged -> {
Log.debugf(
"SolutionResource -> findBy: size of list of solutions paginated found: [%s]",
solutionPaged.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(
solutionPaged,
pageMetadata))
.build();
});
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Uni<SolutionEntity> updateSolution(String solutionId, SolutionDto solutio
oldSolution.setLocationCode(solutionDto.locationCode());
oldSolution.setPspId(solutionDto.pspId());

return solutionRepository.update(oldSolution)
return solutionRepository.persist(oldSolution)
.onFailure()
.transform(error -> error)
.onItem()
Expand Down
Loading

0 comments on commit bd87215

Please sign in to comment.