Skip to content

Commit

Permalink
feat(rest): Endpoint to add comment on a clearing request
Browse files Browse the repository at this point in the history
Signed-off-by: Akshit Joshi <akshit.joshi@siemens-healthineers.com>
  • Loading branch information
akshitjoshii committed Aug 19, 2024
1 parent 828c05a commit ca0f747
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 12 deletions.
22 changes: 22 additions & 0 deletions rest/resource-server/src/docs/asciidoc/clearingRequests.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,25 @@ include::{snippets}/should_document_get_clearingrequests_by_state/curl-request.a
===== Example response
include::{snippets}/should_document_get_clearingrequests_by_state/http-response.adoc[]

[[resources-add-comment-to-clearingrequest]]
==== Add a new comment to a clearing request

A `POST` request will add a new comment to a specific clearing request.

===== Request Parameters

include::{snippets}/should_document_add_comment_to_clearingrequest/request-parameters.adoc[]

===== Request Example

include::{snippets}/should_document_add_comment_to_clearingrequest/curl-request.adoc[]

===== Response Structure

include::{snippets}/should_document_add_comment_to_clearingrequest/response-fields.adoc[]

===== Response Example

include::{snippets}/should_document_add_comment_to_clearingrequest/http-response.adoc[]


Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Set;
import java.util.TreeSet;

import io.swagger.v3.oas.annotations.media.Content;
import jakarta.servlet.http.HttpServletRequest;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -26,6 +27,7 @@
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.ClearingRequestState;
import org.eclipse.sw360.datahandler.thrift.Comment;
import org.eclipse.sw360.datahandler.thrift.projects.ClearingRequest;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.users.User;
Expand All @@ -36,11 +38,13 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
Expand Down Expand Up @@ -165,6 +169,37 @@ public ResponseEntity<CollectionModel<EntityModel<ClearingRequest>>> getMyCleari
return new ResponseEntity<>(resources, status);
}

@Operation(
summary = "Add a new comment to a clearing request.",
description = "Create a new comment for the clearing request.",
tags = {"ClearingRequest"}
)
@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = CLEARING_REQUEST_URL + "/{id}/comments", method = RequestMethod.POST)
public ResponseEntity<?> addComment(
@Parameter(description = "ID of the clearing request")
@PathVariable("id") String crId,
@Parameter(description = "Comment to be added to the clearing request",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = Comment.class)))
@RequestBody Comment comment,
HttpServletRequest request
) {
try {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
ClearingRequest existingClearingRequest = sw360ClearingRequestService.getClearingRequestById(crId, sw360User);
ClearingRequest updatedClearingRequest = sw360ClearingRequestService.addCommentToClearingRequest(crId, comment, sw360User);
HalResource<ClearingRequest> halClearingRequest = createHalClearingRequestWithAllDetails(updatedClearingRequest, sw360User);
return new ResponseEntity<>(updatedClearingRequest.getComments(), HttpStatus.OK);
}catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}catch (ResourceNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Clearing request not found in database.");
}catch (TException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while processing the request.");
}
}

@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(linkTo(ClearingRequestController.class).slash("api" + CLEARING_REQUEST_URL).withRel("clearingRequests"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransportException;
import org.eclipse.sw360.datahandler.thrift.ClearingRequestState;
import org.eclipse.sw360.datahandler.thrift.Comment;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.moderation.ModerationService;
import org.eclipse.sw360.datahandler.thrift.projects.ClearingRequest;
Expand Down Expand Up @@ -88,4 +90,25 @@ public Set<ClearingRequest> getMyClearingRequests(User sw360User, ClearingReques
return clearingrequests;
}

public ClearingRequest addCommentToClearingRequest(String crId, Comment comment, User sw360User) throws TException {
if (crId == null || crId.isBlank()) {
throw new IllegalArgumentException("Clearing request ID cannot be null or empty.");
}
if (comment.getText() == null || comment.getText().trim().isEmpty()) {
throw new IllegalArgumentException("Comment text cannot be empty.");
}

comment.setCommentedBy(sw360User.getEmail());
comment.setText(comment.getText().trim());

ModerationService.Iface clearingRequestClient = getThriftModerationClient();
RequestStatus requestStatus = clearingRequestClient.addCommentToClearingRequest(crId, comment, sw360User);
if (requestStatus != RequestStatus.SUCCESS) {
throw new TException("Error adding comment to clearing request");
}
return getClearingRequestById(crId, sw360User);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@
import static org.mockito.BDDMockito.given;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.ClearingRequestPriority;
Expand Down Expand Up @@ -61,10 +56,12 @@ public class ClearingRequestSpecTest extends TestRestDocsSpecBase {

@MockBean
private Sw360ClearingRequestService clearingRequestServiceMock;
ClearingRequest clearingRequest = new ClearingRequest();
ClearingRequest cr1 = new ClearingRequest();
ClearingRequest cr2 = new ClearingRequest();

@Before
public void before() throws TException, IOException {
ClearingRequest clearingRequest = new ClearingRequest();
clearingRequest.setId("CR-101");
clearingRequest.setAgreedClearingDate("12-07-2020");
clearingRequest.setClearingState(ClearingRequestState.ACCEPTED);
Expand All @@ -78,7 +75,6 @@ public void before() throws TException, IOException {

Set<ClearingRequest> clearingrequests = new HashSet<>();
Set<ClearingRequest> clearingrequestsbystate = new HashSet<>();
ClearingRequest cr1 = new ClearingRequest();
cr1.setId("CR-2");
cr1.setAgreedClearingDate("12-10-2020");
cr1.setClearingState(ClearingRequestState.ACCEPTED);
Expand All @@ -89,7 +85,6 @@ public void before() throws TException, IOException {
cr1.setRequestedClearingDate("10-08-2020");
cr1.setRequestingUser("test.user@sw60.org");

ClearingRequest cr2 = new ClearingRequest();
cr2.setId("CR-3");
cr2.setAgreedClearingDate("24-10-2020");
cr2.setClearingState(ClearingRequestState.NEW);
Expand Down Expand Up @@ -119,10 +114,11 @@ public void before() throws TException, IOException {
List<Comment> comments = new ArrayList<Comment>();
Comment comment = new Comment();
comment.setText("comment text 1");
comment.setCommentedBy("test.user@sw360.org");
comment.setCommentedBy("test.user@sw60.org");

comments.add(comment);
clearingRequest.setComments(comments);
given(this.clearingRequestServiceMock.addCommentToClearingRequest(eq(clearingRequest.getId()), any(), any())).willReturn(clearingRequest);

given(this.clearingRequestServiceMock.getClearingRequestById(eq(clearingRequest.getId()), any())).willReturn(clearingRequest);
given(this.clearingRequestServiceMock.getClearingRequestByProjectId(eq(clearingRequest.getProjectId()), any())).willReturn(clearingRequest);
Expand Down Expand Up @@ -236,4 +232,27 @@ public void should_document_get_clearingrequests_by_state() throws Exception {
)));
}

@Test
public void should_add_comment_to_clearing_request() throws Exception {
Map<String, Object> comment = new LinkedHashMap<>();
comment.put("text", "comment text 1");
String accessToken = TestHelper.generateAuthHeader(testUserId, testUserPassword);

this.mockMvc.perform(
post("/api/clearingrequest/" + clearingRequest.getId() + "/comments")
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(comment))
.header("Authorization", accessToken))
.andExpect(status().isOk())
.andDo(this.documentationHandler.document(
requestFields(
fieldWithPath("text").description("The text of the comment")
),
responseFields(
fieldWithPath("[].text").description("The text of the comment"),
fieldWithPath("[].commentedBy").description("The user who made the comment")
)
));
}

}

0 comments on commit ca0f747

Please sign in to comment.