From 96593e64173cb85f8b268f642bd096ce2f44fc15 Mon Sep 17 00:00:00 2001 From: Nikesh kumar Date: Fri, 15 Mar 2024 13:26:00 +0530 Subject: [PATCH] feat(rest): create new endpoint to delete ModerationRequests by id. Signed-off-by: Nikesh kumar --- .../src/docs/asciidoc/moderationRequests.adoc | 18 ++++++++++++++++++ .../ModerationRequestController.java | 18 ++++++++++++++++++ .../Sw360ModerationRequestService.java | 12 ++++++++++++ .../restdocs/ModerationRequestSpecTest.java | 17 ++++++++++++++++- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/rest/resource-server/src/docs/asciidoc/moderationRequests.adoc b/rest/resource-server/src/docs/asciidoc/moderationRequests.adoc index c08d7123a0..a01424208b 100644 --- a/rest/resource-server/src/docs/asciidoc/moderationRequests.adoc +++ b/rest/resource-server/src/docs/asciidoc/moderationRequests.adoc @@ -146,3 +146,21 @@ include::{snippets}/should_document_get_moderationrequests_submission/curl-reque ===== Example response include::{snippets}/should_document_get_moderationrequests_submission/http-response.adoc[] + +[[resources-moderationRequest-delete]] +==== Delete Moderation Requests + +A `DELETE` method will delete the moderation request. + +===== Request parameter +include::{snippets}/should_document_get_moderationrequests_delete/request-parameters.adoc[] + +===== Response structure +include::{snippets}/should_document_get_moderationrequests_delete/response-fields.adoc[] + +===== Example request +include::{snippets}/should_document_get_moderationrequests_delete/curl-request.adoc[] + +===== Example response +include::{snippets}/should_document_get_moderationrequests_delete/http-response.adoc[] + diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/moderationrequest/ModerationRequestController.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/moderationrequest/ModerationRequestController.java index 090b4884d5..714f9038a9 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/moderationrequest/ModerationRequestController.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/moderationrequest/ModerationRequestController.java @@ -28,6 +28,7 @@ import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException; import org.eclipse.sw360.datahandler.thrift.ModerationState; import org.eclipse.sw360.datahandler.thrift.PaginationData; +import org.eclipse.sw360.datahandler.thrift.RequestStatus; import org.eclipse.sw360.datahandler.thrift.components.Component; import org.eclipse.sw360.datahandler.thrift.components.Release; import org.eclipse.sw360.datahandler.thrift.moderation.DocumentType; @@ -52,10 +53,12 @@ import org.springframework.hateoas.server.RepresentationModelProcessor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.HttpClientErrorException; import javax.servlet.http.HttpServletRequest; + import java.net.URISyntaxException; import java.util.*; @@ -342,4 +345,19 @@ private ResponseEntity> getModerationResponse HttpStatus status = resources == null ? HttpStatus.NO_CONTENT : HttpStatus.OK; return new ResponseEntity<>(resources, status); } + + @Operation( + summary = "Delete moderation request.", + description = "Delete delete moderation request of the service.", + tags = {"ModerationRequest"} + ) + @PreAuthorize("hasAuthority('WRITE')") + @RequestMapping(value = MODERATION_REQUEST_URL + "/delete/{id}", method = RequestMethod.DELETE) + public ResponseEntity deleteModerationRequest(HttpServletRequest request,@PathVariable String id, ModerationRequest moderationRequest) throws TException { + User sw360User = restControllerHelper.getSw360UserFromAuthentication(); + moderationRequest = sw360ModerationRequestService.getModerationRequestById(id); + RequestStatus requestStatus = sw360ModerationRequestService.deleteModerationRequestInfo(sw360User,id, moderationRequest); + HttpStatus status = HttpStatus.OK; + return new ResponseEntity<>(requestStatus, status); + } } diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/moderationrequest/Sw360ModerationRequestService.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/moderationrequest/Sw360ModerationRequestService.java index 34b5b2a9d2..be4f83ae55 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/moderationrequest/Sw360ModerationRequestService.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/moderationrequest/Sw360ModerationRequestService.java @@ -23,6 +23,7 @@ import org.eclipse.sw360.datahandler.thrift.ModerationState; import org.eclipse.sw360.datahandler.thrift.PaginationData; import org.eclipse.sw360.datahandler.thrift.RemoveModeratorRequestStatus; +import org.eclipse.sw360.datahandler.thrift.RequestStatus; import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest; import org.eclipse.sw360.datahandler.thrift.moderation.ModerationService; @@ -253,4 +254,15 @@ public ModerationState assignRequest(@NotNull ModerationRequest request, @NotNul getThriftModerationClient().setInProgress(request.getId(), reviewer); return ModerationState.INPROGRESS; } + + public RequestStatus deleteModerationRequestInfo(@NotNull User sw360User, @NotNull String id, @NotNull ModerationRequest moderationRequest) + throws TTransportException, TException { + if (moderationRequest.getModerators().contains(sw360User.getEmail())) { + throw new AccessDeniedException("User is not assigned as a moderator for the request."); + } else if (!isOpenModerationRequest(moderationRequest)) { + throw new InvalidParameterException("Moderation request is not in open state."); + } else { + return getThriftModerationClient().deleteModerationRequest(id,sw360User); + } + } } diff --git a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ModerationRequestSpecTest.java b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ModerationRequestSpecTest.java index f4b4add45c..594c88f427 100644 --- a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ModerationRequestSpecTest.java +++ b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ModerationRequestSpecTest.java @@ -13,6 +13,7 @@ import org.apache.thrift.TException; import org.eclipse.sw360.datahandler.thrift.ModerationState; import org.eclipse.sw360.datahandler.thrift.PaginationData; +import org.eclipse.sw360.datahandler.thrift.RequestStatus; import org.eclipse.sw360.datahandler.thrift.Visibility; import org.eclipse.sw360.datahandler.thrift.components.ComponentType; import org.eclipse.sw360.datahandler.thrift.components.ECCStatus; @@ -62,6 +63,7 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath; import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; import static org.springframework.restdocs.request.RequestDocumentation.requestParameters; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -84,6 +86,9 @@ public class ModerationRequestSpecTest extends TestRestDocsSpecBase { @MockBean private Sw360ModerationRequestService moderationRequestServiceMock; + @MockBean + private ModerationRequest moderationRequest; + @Before public void before() throws TException, IOException { Set moderatorList = new HashSet<>(); @@ -129,7 +134,7 @@ public void before() throws TException, IOException { project2Deletions.setProjectType(ProjectType.CUSTOMER); project2Deletions.setVisbility(Visibility.BUISNESSUNIT_AND_MODERATORS); - ModerationRequest moderationRequest = new ModerationRequest(); + moderationRequest = new ModerationRequest(); moderationRequest.setId("MR-101"); moderationRequest.setTimestamp(System.currentTimeMillis() / 1000L - 172800); moderationRequest.setDocumentId("R-101"); @@ -204,6 +209,7 @@ public void before() throws TException, IOException { given(this.moderationRequestServiceMock.acceptRequest(eq(moderationRequest), eq("Changes looks good."), any())).willReturn(ModerationState.APPROVED); given(this.moderationRequestServiceMock.assignRequest(eq(moderationRequest), any())).willReturn(ModerationState.INPROGRESS); given(this.moderationRequestServiceMock.getRequestsByRequestingUser(any(), any())).willReturn(requestsByState); + given(this.moderationRequestServiceMock.deleteModerationRequestInfo(any(), any(), any())).willReturn(RequestStatus.SUCCESS); } @Test @@ -494,4 +500,13 @@ public void should_document_get_moderationrequests_submission() throws Exception subsectionWithPath("_links").description("<> to other resources") ))); } + + @Test + public void should_document_get_moderationrequests_delete() throws Exception { + String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword); + mockMvc.perform(delete("/api/moderationrequest/delete/" + moderationRequest.getId()) + .header("Authorization", "Bearer " + accessToken) + .accept(MediaTypes.HAL_JSON)) + .andExpect(status().isOk()); + } }