Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

feat: [MCIL-41, MCIL-43] Added service, controller and test for delete terminal #36

Merged
merged 4 commits into from
May 2, 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 @@ -180,4 +180,59 @@ public Uni<Response> updateTerminal(
});
});
}

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

Log.debugf("TerminalResource -> deleteTerminal - Input requestId, terminalUuid: %s, %s", requestId, terminalUuid);

String serviceProviderId = jwt.getSubject();

return terminalService.findTerminal(serviceProviderId, terminalUuid)
.onFailure()
.transform(err -> {
Log.errorf(err, "TerminalResource -> deleteTerminal: error during search terminal with terminalUuid, serviceProviderId: [%s, %s]", terminalUuid, serviceProviderId);

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(terminalEntity -> {
if (terminalEntity == null) {
Log.errorf("TerminalResource -> deleteTerminal: error 404 during searching terminal with terminalUuid, serviceProviderId: [%s, %s]", terminalUuid, serviceProviderId);

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

return terminalService.deleteTerminal(terminalEntity)
.onFailure()
.transform(err -> {
Log.errorf(err, "TerminalResource -> deleteTerminal: error during deleting terminal [%s]", terminalEntity);

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(terminalUpdated -> {
Log.debugf("TerminalResource -> deleteTerminal: terminal deleted correctly on DB [%s]", terminalUpdated);

return Response
.status(Response.Status.NO_CONTENT)
.build();
});
});
}
}
15 changes: 15 additions & 0 deletions src/main/java/it/pagopa/swclient/mil/service/TerminalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,19 @@ private TerminalEntity createTerminalEntity(TerminalDto terminalDto, String serv

return terminalEntity;
}

/**
* Update terminal starting from a terminalDto.
*
* @param terminal terminal to be deleted
* @return void
*/
public Uni<Void> deleteTerminal(TerminalEntity terminal) {

return terminalRepository.delete(terminal)
.onFailure()
.transform(error -> error)
.onItem()
.transform(terminalDeleted -> terminalDeleted);
}
}
4 changes: 2 additions & 2 deletions src/main/java/it/pagopa/swclient/mil/util/ErrorCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private ErrorCodes() {
public static final String ERROR_GENERIC_FROM_DB = MODULE_ID + "000201";
public static final String ERROR_COUNTING_TERMINALS = MODULE_ID + "000202";
public static final String ERROR_LIST_TERMINALS = MODULE_ID + "000203";
public static final String ERROR_TERMINAL_NOT_FOUND = MODULE_ID + "000203";
public static final String ERROR_TERMINAL_NOT_FOUND = MODULE_ID + "000204";



Expand All @@ -60,7 +60,7 @@ private ErrorCodes() {
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_LIST_TERMINALS_DESCR = "error occurred while retrieving list of paginated terminals";
private static final String ERROR_TERMINAL_NOT_FOUND_DESCR = "terminal not founded on db";
private static final String ERROR_TERMINAL_NOT_FOUND_DESCR = "terminal not found on db";



Expand Down
94 changes: 94 additions & 0 deletions src/test/java/it/pagopa/swclient/mil/TerminalResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,98 @@ void testUpdateTerminal_500UT() {
Assertions.assertEquals(500, response.statusCode());
}

@Test
@TestSecurity(user = "testUser", roles = {"pos_service_provider"})
@JwtSecurity(claims = {
@Claim(key = "sub", value = "1234567890")
})
void testDeleteTerminal_204() {
Mockito.when(terminalService.findTerminal(any(String.class), any(String.class)))
.thenReturn(Uni.createFrom().item(terminalEntity));

Mockito.when(terminalService.deleteTerminal(any(TerminalEntity.class)))
.thenReturn(Uni.createFrom().voidItem());

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

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

@Test
@TestSecurity(user = "testUser", roles = {"pos_service_provider"})
@JwtSecurity(claims = {
@Claim(key = "sub", value = "1234567890")
})
void testDeleteTerminal_404() {
terminalEntity = null;
Mockito.when(terminalService.findTerminal(any(String.class), any(String.class)))
.thenReturn(Uni.createFrom().item(terminalEntity));

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

terminalEntity = TerminalTestData.getCorrectTerminalEntity();
Assertions.assertEquals(404, response.statusCode());
}

@Test
@TestSecurity(user = "testUser", roles = {"pos_service_provider"})
@JwtSecurity(claims = {
@Claim(key = "sub", value = "1234567890")
})
void testDeleteTerminal_500FT() {
Mockito.when(terminalService.findTerminal(any(String.class), any(String.class)))
.thenReturn(Uni.createFrom().failure(new WebApplicationException()));

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

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

@Test
@TestSecurity(user = "testUser", roles = {"pos_service_provider"})
@JwtSecurity(claims = {
@Claim(key = "sub", value = "1234567890")
})
void testDeleteTerminal_500UT() {
Mockito.when(terminalService.findTerminal(any(String.class), any(String.class)))
.thenReturn(Uni.createFrom().item(terminalEntity));

Mockito.when(terminalService.deleteTerminal(any(TerminalEntity.class)))
.thenReturn(Uni.createFrom().failure(new WebApplicationException()));

Response response = given()
.contentType(ContentType.JSON)
.header("RequestId", "1a2b3c4d-5e6f-789a-bcde-f0123456789a")
.and()
.body(terminalDto)
.when()
.delete("/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 @@ -134,7 +134,28 @@ void testUpdateTerminal_Failure() {
.assertFailedWith(WebApplicationException.class);
}

@Test
void testDeleteTerminal_Success() {
Mockito.when(terminalRepository.delete(any(TerminalEntity.class)))
.thenReturn(Uni.createFrom().voidItem());

Uni<Void> result = terminalService.deleteTerminal(terminalEntity);

result.subscribe()
.with(Assertions::assertNull);
}

@Test
void testDeleteTerminal_Failure() {
Mockito.when(terminalRepository.delete(any(TerminalEntity.class)))
.thenReturn(Uni.createFrom().failure(new WebApplicationException()));

Uni<Void> result = terminalService.deleteTerminal(terminalEntity);

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

private List<TerminalEntity> mockedList() {
TerminalEntity te1 = new TerminalEntity();
Expand Down
Loading