Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add update endpoint #21

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@Setter
@MongoEntity(database = "mil", collection = "solutions")
public class SolutionEntity extends PanacheMongoEntity {

private String pspId;
private String locationCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ public Uni<Response> createSolution(
@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,
@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) {

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

return solutionService
.getSolutionsCount()
Expand Down Expand Up @@ -263,7 +263,7 @@ public Uni<Response> deleteSolution(
@PathParam(value = "solutionId") String solutionId) {

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

return solutionService.findById(solutionId)
.onFailure()
Expand Down Expand Up @@ -316,6 +316,56 @@ public Uni<Response> deleteSolution(
});
}

@PATCH
@Path("/{solutionId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "mil_papos_admin" })
public Uni<Response> updateSolution(
@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,
@PathParam(value = "solutionId") String solutionId) {

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

return solutionService.findById(solutionId)
.onFailure()
.transform(err -> {
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))
.build());
})
.onItem()
.transformToUni((solutionEntity -> solutionService
.updateSolution(solutionId, solution, solutionEntity)
.onFailure()
.transform(err -> {
Log.errorf(err,
"SolutionResource -> updateSolution: error during update solution [%s]",
solution);

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB,
ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
.onItem()
.transform(solutionUpdated -> {
Log.debugf(
"SolutionResource -> updateSolution: solution updated correctly on DB [%s]",
solutionUpdated);

return Response
.status(Response.Status.NO_CONTENT)
.build();
})));
}

private void checkToken(String toCheck) {
Log.debugf("SolutionResource -> checkToken: sub [%s], pspId/locationCode: [%s]", jwt.getSubject(), toCheck);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class SolutionService {
public SolutionService(SolutionRepository solutionRepository) {
this.solutionRepository = solutionRepository;
}


/**
* Create a new solution starting from a solutionDto.
Expand Down Expand Up @@ -82,6 +83,26 @@ public Uni<Void> deleteSolution(SolutionEntity solution) {
.transform(solutionDeleted -> solutionDeleted);
}

/**
* Update solution starting from a solutionDto.
*
* @param solutionDto dto of modified solution
* @param solutionId solutionId of old solution to be modified
* @return solution updated
*/
public Uni<SolutionEntity> updateSolution(String solutionId, SolutionDto solutionDto, SolutionEntity oldSolution) {
Log.debugf("SolutionService -> updateTerminal - Input parameters: %s, %s, %s", solutionId, solutionDto, oldSolution);
oldSolution.setLocationCode(solutionDto.locationCode());
oldSolution.setPspId(solutionDto.pspId());

return solutionRepository.update(oldSolution)
.onFailure()
.transform(error -> error)
.onItem()
.transform(terminalUpdated -> terminalUpdated);
}


/**
* Returns a number corresponding to the total number of solutions found.
*
Expand Down Expand Up @@ -166,4 +187,6 @@ public Uni<List<SolutionEntity>> findAllByPspAndSolutionId(String pspId, List<St

return solutionRepository.list("pspId = ?1 and _id in ?2", pspId, solutionObjectIds);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ 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";
public static final String ERROR_LOCATIONCODE_MUST_NOT_BE_NULL = MODULE_ID + "000020";



/*
* Service errors code from 000200 to 000500
Expand Down Expand Up @@ -73,6 +76,7 @@ private ErrorCodes() {
private static final String ERROR_AMOUNT_MUST_NOT_BE_NULL_DESCR = "amount must not be null";
private static final String ERROR_STATUS_MUST_NOT_BE_NULL_DESCR = "status must not be null";
private static final String ERROR_SOLUTIONID_MUST_NOT_BE_NULL_DESCR = "solutionId must not be null";
private static final String ERROR_LOCATIONCODE_MUST_NOT_BE_NULL_DESCR = "locationCode 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 terminals";
Expand Down Expand Up @@ -113,6 +117,7 @@ private ErrorCodes() {
public static final String ERROR_AMOUNT_MUST_NOT_BE_NULL_MSG = "[" + ERROR_AMOUNT_MUST_NOT_BE_NULL + "] " + ERROR_AMOUNT_MUST_NOT_BE_NULL_DESCR;
public static final String ERROR_STATUS_MUST_NOT_BE_NULL_MSG = "[" + ERROR_STATUS_MUST_NOT_BE_NULL + "] " + ERROR_STATUS_MUST_NOT_BE_NULL_DESCR;
public static final String ERROR_SOLUTIONID_MUST_NOT_BE_NULL_MSG = "[" + ERROR_SOLUTIONID_MUST_NOT_BE_NULL + "] " + ERROR_SOLUTIONID_MUST_NOT_BE_NULL_DESCR;
public static final String ERROR_LOCATIONCODE_MUST_NOT_BE_NULL_MSG = "[" + ERROR_LOCATIONCODE_MUST_NOT_BE_NULL + "] " + ERROR_LOCATIONCODE_MUST_NOT_BE_NULL_DESCR;

public static final String ERROR_GENERIC_FROM_DB_MSG = "[" + ERROR_GENERIC_FROM_DB + "] " + ERROR_GENERIC_FROM_DB_DESCR;
public static final String ERROR_COUNTING_TERMINALS_MSG = "[" + ERROR_COUNTING_TERMINALS + "] " + ERROR_COUNTING_TERMINALS_DESCR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,78 @@ void testDeleteSolution_500UT() {
Assertions.assertEquals(500, response.statusCode());
}

@Test
@TestSecurity(user = "testUser", roles = { "mil_papos_admin" })
@JwtSecurity(claims = {
@Claim(key = "sub", value = "AGID_01")
})
void testUpdateSolution_204() {
Mockito.when(solutionService.findById(any(String.class)))
.thenReturn(Uni.createFrom().item(solutionEntity));

Mockito.when(solutionService.updateSolution(anyString(), any(SolutionDto.class), any(SolutionEntity.class)))
.thenReturn(Uni.createFrom().item(solutionEntity));

Response response = given()
.contentType(ContentType.JSON)
.header("RequestId", "1a2b3c4d-5e6f-789a-bcde-f0123456789a")
.and()
.body(solutionDto)
.when()
.patch("/d43d21a5-f8a7-4a68-8320-60b8f342c4aa")
.then()
.extract().response();

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

@Test
@TestSecurity(user = "testUser", roles = { "mil_papos_admin" })
@JwtSecurity(claims = {
@Claim(key = "sub", value = "AGID_01")
})
void testUpdateSolution_500SI() {

Mockito.when(solutionService.findById(anyString()))
.thenReturn(Uni.createFrom().failure(new WebApplicationException()));

Response response = given()
.contentType(ContentType.JSON)
.header("RequestId", "1a2b3c4d-5e6f-789a-bcde-f0123456789a")
.and()
.body(solutionDto)
.when()
.patch("/d43d21a5-f8a7-4a68-8320-60b8f342c4aa")
.then()
.extract().response();

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

@Test
@TestSecurity(user = "testUser", roles = { "mil_papos_admin" })
@JwtSecurity(claims = {
@Claim(key = "sub", value = "AGID_01")
})
void testUpdateSolution_500UT() {

Mockito.when(solutionService.findById(anyString()))
.thenReturn(Uni.createFrom().item(solutionEntity));

Mockito.when(solutionService.updateSolution(anyString(), any(SolutionDto.class), any(SolutionEntity.class)))
.thenReturn(Uni.createFrom().failure(new WebApplicationException()));

Response response = given()
.contentType(ContentType.JSON)
.header("RequestId", "1a2b3c4d-5e6f-789a-bcde-f0123456789a")
.and()
.body(solutionDto)
.when()
.patch("/d43d21a5-f8a7-4a68-8320-60b8f342c4aa")
.then()
.extract().response();

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,29 @@ void testFindByLocationCode_Success() {
result.subscribe()
.with(list -> Assertions.assertEquals(mockedListSolution(), list));
}
}


@Test
void testUpdateSolution_Success() {
Mockito.when(solutionRepository.update(any(SolutionEntity.class)))
.thenReturn(Uni.createFrom().item(solutionEntity));

Uni<SolutionEntity> result = solutionService.updateSolution("solutionId", solutionDto, solutionEntity);

result.subscribe()
.with(entity -> Assertions.assertEquals(solutionEntity, entity));
}

@Test
void testUpdateSolution_Failure() {
Mockito.when(solutionRepository.update(any(SolutionEntity.class)))
.thenReturn(Uni.createFrom().failure(new WebApplicationException()));

Uni<SolutionEntity> result = solutionService.updateSolution("solutionId", solutionDto, solutionEntity);

result.subscribe()
.withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(WebApplicationException.class);
}

}
Loading