From bcddf76fbf4c951e1a61785f8f727d21a7b01786 Mon Sep 17 00:00:00 2001 From: Keerthi B L Date: Mon, 1 Jul 2024 15:07:44 +0530 Subject: [PATCH] fix(rest) : modified attachment info in response to the moderation request rest api --- .../ModerationRequestController.java | 67 ++++++++++++------- .../restdocs/ModerationRequestSpecTest.java | 9 +-- 2 files changed, 47 insertions(+), 29 deletions(-) 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 a3571db74c..e3bba81d84 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 @@ -61,6 +61,11 @@ import org.springframework.web.client.HttpClientErrorException; import jakarta.servlet.http.HttpServletRequest; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; import java.net.URISyntaxException; import java.util.*; import java.util.function.Function; @@ -75,6 +80,8 @@ @SecurityRequirement(name = "basic") public class ModerationRequestController implements RepresentationModelProcessor { + private static final String REVIEWER = "reviewer"; + private static final String REQUESTING_USER = "requestingUser"; public static final String MODERATION_REQUEST_URL = "/moderationrequest"; @Autowired @@ -124,20 +131,33 @@ public ResponseEntity> getModerationRequests( tags = {"Moderation Requests"} ) @RequestMapping(value = MODERATION_REQUEST_URL + "/{id}", method = RequestMethod.GET) - public ResponseEntity> getModerationRequestById( - @Parameter(description = "The id of the moderation request to be retrieved.") - @PathVariable String id - ) throws TException { + public ResponseEntity>> getModerationRequestById( + @Parameter(description = "The id of the moderation request to be retrieved.") @PathVariable String id) + throws TException { User sw360User = restControllerHelper.getSw360UserFromAuthentication(); - ModerationRequest moderationRequest = filterModerationRequestNoDuplicates( sw360ModerationRequestService.getModerationRequestById(id)); - HalResource halModerationRequest = createHalModerationRequestWithAllDetails(moderationRequest, - sw360User); + Map modObjectMapper = getModObjectMapper(moderationRequest); + Link modLink = linkTo(ReleaseController.class).slash("api/moderationrequest/" + moderationRequest.getId()) + .withSelfRel(); + Map modLinkMap = new LinkedHashMap<>(); + modLinkMap.put("self", modLink); + modObjectMapper.put("_links", modLinkMap); + HalResource> halModerationRequest = createHalModerationRequestWithAllDetails( + modObjectMapper, sw360User); HttpStatus status = halModerationRequest.getContent() == null ? HttpStatus.NO_CONTENT : HttpStatus.OK; return new ResponseEntity<>(halModerationRequest, status); } + private Map getModObjectMapper(ModerationRequest moderationRequest) { + ObjectMapper oMapper = new ObjectMapper(); + oMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + oMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + oMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE); // but only public getters + oMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE); // and none of "is-setters" + return oMapper.convertValue(moderationRequest, Map.class); + } + @Operation( summary = "Get moderation based on state.", description = "List all the ModerationRequest visible to the user based on the state.", @@ -169,38 +189,35 @@ public ResponseEntity> getModerationRequestsB return getModerationResponseEntity(pageable, request, allDetails, modRequestsWithPageData); } - private @NotNull HalResource createHalModerationRequestWithAllDetails( - ModerationRequest moderationRequest, User sw360User) throws TException { - HalResource halModerationRequest = new HalResource<>(moderationRequest); - User requestingUser = restControllerHelper.getUserByEmail(moderationRequest.getRequestingUser()); - restControllerHelper.addEmbeddedUser(halModerationRequest, requestingUser, "requestingUser"); - - if (CommonUtils.isNotNullEmptyOrWhitespace(moderationRequest.getReviewer())) { - User reviewer = restControllerHelper.getUserByEmail(moderationRequest.getReviewer()); - restControllerHelper.addEmbeddedUser(halModerationRequest, reviewer, "reviewer"); + private @NotNull HalResource> createHalModerationRequestWithAllDetails( + Map modObjectMapper, User sw360User) throws TException { + HalResource> halModerationRequest = new HalResource<>(modObjectMapper); + User requestingUser = restControllerHelper.getUserByEmail(modObjectMapper.get(REQUESTING_USER).toString()); + restControllerHelper.addEmbeddedUser(halModerationRequest, requestingUser, REQUESTING_USER); + if (modObjectMapper.get(REVIEWER) != null + && CommonUtils.isNotNullEmptyOrWhitespace(modObjectMapper.get(REVIEWER).toString())) { + User reviewer = restControllerHelper.getUserByEmail(modObjectMapper.get(REVIEWER).toString()); + restControllerHelper.addEmbeddedUser(halModerationRequest, reviewer, REVIEWER); } - - DocumentType documentType = moderationRequest.getDocumentType(); - String documentId = moderationRequest.getDocumentId(); - if (documentType.equals(DocumentType.PROJECT)) { + String documentType = modObjectMapper.get("documentType").toString(); + String documentId = modObjectMapper.get("documentId").toString(); + if (documentType.equals((DocumentType.PROJECT).toString())) { Project project = projectService.getProjectForUserById(documentId, sw360User); restControllerHelper.addEmbeddedProject(halModerationRequest, project, true); - } else if (documentType.equals(DocumentType.RELEASE)) { + } else if (documentType.equals((DocumentType.RELEASE).toString())) { Release release = releaseService.getReleaseForUserById(documentId, sw360User); restControllerHelper.addEmbeddedRelease(halModerationRequest, release); - } else if (documentType.equals(DocumentType.COMPONENT)) { + } else if (documentType.equals((DocumentType.COMPONENT).toString())) { Component component = componentService.getComponentForUserById(documentId, sw360User); restControllerHelper.addEmbeddedComponent(halModerationRequest, component); } - return halModerationRequest; } private @NotNull HalResource createHalModerationRequest(ModerationRequest moderationRequest) { HalResource halModerationRequest = new HalResource<>(moderationRequest); User requestingUser = restControllerHelper.getUserByEmail(moderationRequest.getRequestingUser()); - restControllerHelper.addEmbeddedUser(halModerationRequest, requestingUser, "requestingUser"); - + restControllerHelper.addEmbeddedUser(halModerationRequest, requestingUser, REQUESTING_USER); return halModerationRequest; } 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 acbbad4a9f..6e4fc441cf 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 @@ -312,6 +312,7 @@ public void should_document_get_moderationrequest() throws Exception { ), responseFields( fieldWithPath("id").description("The id of the moderation request."), + fieldWithPath("type").description("The type of the moderation request."), fieldWithPath("timestamp").description("Timestamp (in unix epoch) when the request was created."), fieldWithPath("timestampOfDecision").description("Timestamp (in unix epoch) when the decision on the request was made."), fieldWithPath("documentId").description("The ID of the document for which the moderation request was made."), @@ -320,22 +321,22 @@ public void should_document_get_moderationrequest() throws Exception { subsectionWithPath("moderators.[]").description("List of users who are marked as moderators for the request."), fieldWithPath("documentName").description("Name of the document for which the request was created."), fieldWithPath("moderationState").description("The state of the moderation request. Possible values are: " + Arrays.asList(ModerationState.values())), - fieldWithPath("reviewer").description("User who is currently assigned as the reviewer of the request."), + fieldWithPath("reviewer").description("User who is currently assigned as the reviewer of the request.").optional().type(JsonFieldType.OBJECT), fieldWithPath("requestDocumentDelete").description(""), fieldWithPath("requestingUserDepartment").description("The Business Unit / Group of the Project, for which clearing request is created."), fieldWithPath("componentType").description("Type of the component for which the moderation request is created. Possible values are: " + Arrays.asList(ComponentType.values())), fieldWithPath("commentRequestingUser").description("The comment from requesting user."), - fieldWithPath("commentDecisionModerator").description("The comment from decision making user."), + fieldWithPath("commentDecisionModerator").description("The comment from decision making user.").optional().type(JsonFieldType.OBJECT), subsectionWithPath("componentAdditions").description("Information which user wants to add for the component (if `documentType` is `COMPONENT`).").optional().type(JsonFieldType.VARIES), subsectionWithPath("releaseAdditions").description("Information which user wants to add for the release (if `documentType` is `RELEASE`).").optional().type(JsonFieldType.OBJECT), subsectionWithPath("projectAdditions").description("Information which user wants to add for the project (if `documentType` is `PROJECT`).").optional().type(JsonFieldType.OBJECT), subsectionWithPath("licenseAdditions").description("Information which user wants to add for the license (if `documentType` is `LICENSE`).").optional().type(JsonFieldType.OBJECT), - fieldWithPath("user").description(""), + fieldWithPath("user").description("").optional().type(JsonFieldType.OBJECT), subsectionWithPath("componentDeletions").description("Information which user wants to remove for the component (if `documentType` is `COMPONENT`).").optional().type(JsonFieldType.OBJECT), subsectionWithPath("releaseDeletions").description("Information which user wants to remove for the release (if `documentType` is `RELEASE`).").optional().type(JsonFieldType.OBJECT), subsectionWithPath("projectDeletions").description("Information which user wants to remove for the project (if `documentType` is `PROJECT`).").optional().type(JsonFieldType.OBJECT), subsectionWithPath("licenseDeletions").description("Information which user wants to remove for the license (if `documentType` is `LICENSE`).").optional().type(JsonFieldType.OBJECT), - fieldWithPath("moderatorsSize").description("Number of users in moderators list."), + fieldWithPath("moderatorsSize").description("Number of users in moderators list.").optional().type(JsonFieldType.OBJECT), subsectionWithPath("_embedded.requestingUser").description("<> who created the ModerationRequest."), subsectionWithPath("_embedded.sw360:project").description("<> for which the ModerationRequest was created (if `documentType` is `PROJECT`).").optional().type(JsonFieldType.OBJECT), subsectionWithPath("_embedded.sw360:releases").description("<> for which the ModerationRequest was created (if `documentType` is `RELEASE`).").optional().type(JsonFieldType.ARRAY),