From 10858588e5919246c1ca69ac974d3f55876c7c97 Mon Sep 17 00:00:00 2001 From: Rudra Chopra Date: Thu, 12 Sep 2024 15:00:42 +0530 Subject: [PATCH] feat(rest): endpoint to get list of obligations depending upon obligation level. Signed-off-by: Rudra Chopra --- .../src/docs/asciidoc/projects.adoc | 17 +++++ .../project/ProjectController.java | 57 +++++++++++++++- .../project/Sw360ProjectService.java | 24 +++++++ .../restdocs/.VendorSpecTest.java.swp | Bin 0 -> 16384 bytes .../restdocs/ProjectSpecTest.java | 62 ++++++++++++++---- 5 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/.VendorSpecTest.java.swp diff --git a/rest/resource-server/src/docs/asciidoc/projects.adoc b/rest/resource-server/src/docs/asciidoc/projects.adoc index 007becec8c..d1a893af75 100644 --- a/rest/resource-server/src/docs/asciidoc/projects.adoc +++ b/rest/resource-server/src/docs/asciidoc/projects.adoc @@ -508,6 +508,23 @@ include::{snippets}/should_document_get_license_obligations/curl-request.adoc[] ===== Example response include::{snippets}/should_document_get_license_obligations/http-response.adoc[] +[[resources-project-get-project-page-obligations-information]] +==== List project page obligations + +A `GET` request will list obligations of a project depending upon the type of obigation level. + +===== Request parameter +include::{snippets}/should_document_get_project_page_obligations/request-parameters.adoc[] + +===== Response structure +include::{snippets}/should_document_get_project_page_obligations/response-fields.adoc[] + +===== Example request +include::{snippets}/should_document_get_project_page_obligations/curl-request.adoc[] + +===== Example response +include::{snippets}/should_document_get_project_page_obligations/http-response.adoc[] + [[resources-projects-create]] ==== Creating a project diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java index fa025b1cbe..df612c917e 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java @@ -2445,7 +2445,7 @@ private Map createPaginationMetadata(Pageable pageable, Map responseBody = new HashMap<>(); responseBody.put("page", pagination); - responseBody.put("licenseObligations", paginatedMap); + responseBody.put("obligations", paginatedMap); return responseBody; } @@ -2488,6 +2488,61 @@ public ResponseEntity getLicenseObligations(Pageable pageable, return new ResponseEntity<>(halObligation, HttpStatus.OK); } + @Operation( + description = "Get obligation data of project tab.", + tags = {"Project"} + ) + @RequestMapping(value = PROJECTS_URL + "/{id}/obligation", method = RequestMethod.GET) + public ResponseEntity getObligations(Pageable pageable, + @Parameter(description = "Project ID.") @PathVariable("id") String id, + @Parameter(description = "Obligation Level") + @RequestParam(value = "obligationLevel", required = true) String OblLevel) + throws TException { + final User sw360User = restControllerHelper.getSw360UserFromAuthentication(); + final Project sw360Project = projectService.getProjectForUserById(id, sw360User); + final Map releaseIdToAcceptedCLI = Maps.newHashMap(); + List releases = new ArrayList<>();; + ObligationList obligation = new ObligationList(); + Map obligationStatusMap = Maps.newHashMap(); + Map oblData = Maps.newHashMap(); + Map filterData = Maps.newHashMap(); + + List releaseIds = new ArrayList<>(sw360Project.getReleaseIdToUsage().keySet()); + for (final String releaseId : releaseIds) { + Release sw360Release = releaseService.getReleaseForUserById(releaseId, sw360User); + if (sw360Release.getAttachmentsSize() > 0) { + releases.add(sw360Release); + } + } + if (CommonUtils.isNotNullEmptyOrWhitespace(sw360Project.getLinkedObligationId())) { + obligation = projectService.getObligationData(sw360Project.getLinkedObligationId(), sw360User); + obligationStatusMap = CommonUtils.nullToEmptyMap(obligation.getLinkedObligationStatus()); + oblData = projectService.setObligationsFromAdminSection(sw360User, obligationStatusMap, sw360Project, OblLevel); + } else { + oblData = projectService.setObligationsFromAdminSection(sw360User, new HashMap(), sw360Project, OblLevel); + } + + if (OblLevel.equalsIgnoreCase("License")) { + for (Map.Entry entry : obligationStatusMap.entrySet()) { + String key = entry.getKey(); + if (!key.equals("compObl") && !key.equals("projectObl") && !key.equals("orgObl")) { + filterData.put(key, entry.getValue()); + } + } + releaseIdToAcceptedCLI.putAll(SW360Utils.getReleaseIdtoAcceptedCLIMappings(filterData)); + oblData = projectService.setLicenseInfoWithObligations(filterData, releaseIdToAcceptedCLI, releases, sw360User); + for (Map.Entry entry : oblData.entrySet()) { + ObligationStatusInfo statusInfo = entry.getValue(); + Set limitedSet = releaseService.getReleasesForUserByIds(statusInfo.getReleaseIdToAcceptedCLI().keySet()); + statusInfo.setReleases(limitedSet); + } + } + + Map responseBody = createPaginationMetadata(pageable, oblData); + HalResource> halObligation = new HalResource<>(responseBody); + return new ResponseEntity<>(halObligation, HttpStatus.OK); + } + @PreAuthorize("hasAuthority('WRITE')") @Operation( summary = "Add licenseObligations from license DB", diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/Sw360ProjectService.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/Sw360ProjectService.java index 8e5f863adc..764008fce5 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/Sw360ProjectService.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/Sw360ProjectService.java @@ -25,6 +25,8 @@ import org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException; import org.eclipse.sw360.datahandler.thrift.AddDocumentRequestStatus; import org.eclipse.sw360.datahandler.thrift.AddDocumentRequestSummary; +import org.eclipse.sw360.datahandler.thrift.licenses.Obligation; +import org.eclipse.sw360.datahandler.thrift.licenses.ObligationLevel; import org.eclipse.sw360.datahandler.thrift.projects.ClearingRequest; import org.eclipse.sw360.datahandler.thrift.PaginationData; import org.eclipse.sw360.datahandler.thrift.ProjectReleaseRelationship; @@ -437,6 +439,28 @@ public ObligationList getObligationData(String linkedObligationId, User user) th return sw360ProjectClient.getLinkedObligations(linkedObligationId, user); } + public Map setObligationsFromAdminSection(User user, Map obligationStatusMap, + Project project, String OblLevel) throws TException { + List obligations = SW360Utils.getObligations(); + Map updatedObligationStatusMap = Maps.newHashMap(); + ThriftClients thriftClients = new ThriftClients(); + ComponentService.Iface componentClient = thriftClients.makeComponentClient(); + + if (OblLevel.equalsIgnoreCase("Project")) { + updatedObligationStatusMap = SW360Utils.getProjectComponentOrganisationLicenseObligationToDisplay( + obligationStatusMap, obligations, ObligationLevel.PROJECT_OBLIGATION, true); + return updatedObligationStatusMap; + } else if (OblLevel.equalsIgnoreCase("Organization")) { + updatedObligationStatusMap = SW360Utils.getProjectComponentOrganisationLicenseObligationToDisplay( + obligationStatusMap, obligations, ObligationLevel.ORGANISATION_OBLIGATION, true); + return updatedObligationStatusMap; + } else if (OblLevel.equalsIgnoreCase("Component")) { + updatedObligationStatusMap = SW360Utils.getProjectComponentOrganisationLicenseObligationToDisplay( + obligationStatusMap, obligations, ObligationLevel.COMPONENT_OBLIGATION, true); + return updatedObligationStatusMap; + } return updatedObligationStatusMap; + } + public Map setLicenseInfoWithObligations(Map obligationStatusMap, Map releaseIdToAcceptedCLI, List releases, User user) { diff --git a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/.VendorSpecTest.java.swp b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/.VendorSpecTest.java.swp new file mode 100644 index 0000000000000000000000000000000000000000..7c4457c87e4baeb4b754bc54390df23ccff476fc GIT binary patch literal 16384 zcmeHNPi!1l8J~~Di0v8S(dIG6jdZ_3D0heB&QbiE_-g`5APnZ_sAju;LLCMHya zY#e;?_SET#!OfkP=QP8LqE9{}#zh4mK^)m^oU>?$MHL>|6(Jk0TRT=Ih#D1Udv3^C zUZjC5z4oiZ6u*?o$oKqCP%zVqEXD}9!nd}QN3 zRYo!GLIZ^c3Jnw*C^S%LpwK{}fkFfKrv^lL)c6{v{-I?0Pbc>y`|kfsuCvMWXZE!p zO@2=$_wVm(Pkt7+LIZ^c3Jnw*C^S%LpwK{}fkFd?1_})n8YnbSXy60TfaMrQ8FF8e z2|xb-pXUGneb_Kw1%3_e0kgoJj~m8&z#G7y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(0aca2$9QxqAzk1|9^C0Y`!Nkf+}UIB)`Z3-h=Q#6SSZdA$e}SD}GI10S#kZmCpQ zf64?pz2I$~#XwiJoNUsylsKXEA!U6{$qb|R)4Pqlpr)h1O zmYdypf-cRkY(7=HaygH>$#C9Ar~$6MQyx&rQv!Tb0;fl#tPY&gAnKWq?}y*FcU*UKGA-bp7f+=ru^sB}8oz+-iwD zMkHs>*Mn{5_pfV*lZrWgq4!d-CH)+NT&S3~BUdmL%gu9L|1e`!Db@PTEKJHQy`v6= zK-L#B{> zb(bqUVH(ERZ*k;dKX(hGt-Qi$e>d#8NXS}+>^!eJWs7G09qK5Q_R{Y!m13D@_4Lc+ zidwiilI4?>;F7duRuy`s-iMij?myTJP)Cw1&JD8zNg%JWyt79)bwB%4VY9TUve(iW zbr`oJR}}@#((G&}!XI}LJKh{uWaSl^>dws(5s(1UWJEe-iRpKR^?%C~FIRY<>~%z` zdJBGmlV;FjB$T1{#@b40tZekN?R}Cbq`Mz-M5#t1%j9V|s)d(Hpp;=@fQJ*?*;P4G zWm}HWVWObwOgYCKsEH9q+f!=MW|lL?L5pDYnEb&UOz&d#|JpaQL#^B}C8iSh$+_oC zm8p=uua%N!mz06b5@%jL&swt9suzuyud0-scMNoHZis~{2`L`Om7@%UH obligationStatusMap = Map.of(obligation.getTitle(), osi); + Map obligationStatusMapFromAdminSection = new HashMap<>(); ObligationList obligationLists = new ObligationList(); obligationLists.setProjectId(project8.getId()); @@ -563,6 +564,7 @@ public void before() throws TException, IOException { given(this.projectServiceMock.getProjectForUserById(eq(project8.getId()), any())).willReturn(project8); given(this.projectServiceMock.getLicenseInfoAttachmentUsage(eq(project8.getId()))).willReturn(licenseInfoUsages); given(this.projectServiceMock.getObligationData(eq(project8.getLinkedObligationId()), any())).willReturn(obligationLists); + given(this.projectServiceMock.setObligationsFromAdminSection(any(), any(), any(), any())).willReturn(obligationStatusMapFromAdminSection); given(this.projectServiceMock.setLicenseInfoWithObligations(eq(obligationStatusMap), eq(releaseIdToAcceptedCLI), any(), any())).willReturn(obligationStatusMap); given(this.projectServiceMock.getLicensesFromAttachmentUsage(eq(licenseInfoUsages), any())).willReturn(licensesFromAttachmentUsage); given(this.projectServiceMock.getLicenseObligationData(eq(licensesFromAttachmentUsage), any())).willReturn(obligationStatusMap); @@ -876,12 +878,12 @@ public void should_document_get_obligations_from_license_db() throws Exception { parameterWithName("page_entries").description("Amount of projects per page") ), responseFields( - subsectionWithPath("licenseObligations.obligation_title").description("Title of license obligation"), - subsectionWithPath("licenseObligations.obligation_title.text").description("Text of license obligation"), - subsectionWithPath("licenseObligations.obligation_title.licenseIds[]").description("List of licenseIds"), - subsectionWithPath("licenseObligations.obligation_title.id").description("Id of the obligation"), - subsectionWithPath("licenseObligations.obligation_title.releaseIdToAcceptedCLI").description("Releases having accepted attachments"), - subsectionWithPath("licenseObligations.obligation_title.obligationType").description("Type of the obligation"), + subsectionWithPath("obligations.obligation_title").description("Title of license obligation"), + subsectionWithPath("obligations.obligation_title.text").description("Text of license obligation"), + subsectionWithPath("obligations.obligation_title.licenseIds[]").description("List of licenseIds"), + subsectionWithPath("obligations.obligation_title.id").description("Id of the obligation"), + subsectionWithPath("obligations.obligation_title.releaseIdToAcceptedCLI").description("Releases having accepted attachments"), + subsectionWithPath("obligations.obligation_title.obligationType").description("Type of the obligation"), fieldWithPath("page").description("Additional paging information"), fieldWithPath("page.size").description("Number of obligations per page"), fieldWithPath("page.totalElements").description("Total number of all license obligations"), @@ -1126,14 +1128,46 @@ public void should_document_get_license_obligations() throws Exception { parameterWithName("page_entries").description("Amount of projects per page") ), responseFields( - subsectionWithPath("licenseObligations.obligation_title").description("Title of license obligation"), - subsectionWithPath("licenseObligations.obligation_title.text").description("Text of license obligation"), - subsectionWithPath("licenseObligations.obligation_title.releaseIdToAcceptedCLI").description("Release Ids having accepted attachments"), - subsectionWithPath("licenseObligations.obligation_title.licenseIds[]").description("List of licenseIds"), - subsectionWithPath("licenseObligations.obligation_title.comment").description("Comment on the obligation"), - subsectionWithPath("licenseObligations.obligation_title.status").description("Status of the obligation"), - subsectionWithPath("licenseObligations.obligation_title.id").description("Id of the obligation"), - subsectionWithPath("licenseObligations.obligation_title.obligationType").description("Type of the obligation"), + subsectionWithPath("obligations.obligation_title").description("Title of license obligation"), + subsectionWithPath("obligations.obligation_title.text").description("Text of license obligation"), + subsectionWithPath("obligations.obligation_title.releaseIdToAcceptedCLI").description("Release Ids having accepted attachments"), + subsectionWithPath("obligations.obligation_title.licenseIds[]").description("List of licenseIds"), + subsectionWithPath("obligations.obligation_title.comment").description("Comment on the obligation"), + subsectionWithPath("obligations.obligation_title.status").description("Status of the obligation"), + subsectionWithPath("obligations.obligation_title.id").description("Id of the obligation"), + subsectionWithPath("obligations.obligation_title.obligationType").description("Type of the obligation"), + fieldWithPath("page").description("Additional paging information"), + fieldWithPath("page.size").description("Number of obligations per page"), + fieldWithPath("page.totalElements").description("Total number of all license obligations"), + fieldWithPath("page.totalPages").description("Total number of pages"), + fieldWithPath("page.number").description("Number of the current page") + ))); + } + + @Test + public void should_document_get_project_page_obligations() throws Exception { + mockMvc.perform(get("/api/projects/" + project8.getId() + "/obligation") + .header("Authorization", TestHelper.generateAuthHeader(testUserId, testUserPassword)) + .param("obligationLevel", "License") + .param("page", "0") + .param("page_entries", "5") + .accept(MediaTypes.HAL_JSON)) + .andExpect(status().isOk()) + .andDo(this.documentationHandler.document( + requestParameters( + parameterWithName("obligationLevel").description("Possible values are: [LICENSE, PROJECT, COMPONENT or ORGANIZATION]"), + parameterWithName("page").description("Page of projects"), + parameterWithName("page_entries").description("Amount of projects per page") + ), + responseFields( + subsectionWithPath("obligations.obligation_title").description("Title of license obligation"), + subsectionWithPath("obligations.obligation_title.text").description("Text of license obligation"), + subsectionWithPath("obligations.obligation_title.releaseIdToAcceptedCLI").description("Release Ids having accepted attachments"), + subsectionWithPath("obligations.obligation_title.licenseIds[]").description("List of licenseIds"), + subsectionWithPath("obligations.obligation_title.comment").description("Comment on the obligation"), + subsectionWithPath("obligations.obligation_title.status").description("Status of the obligation"), + subsectionWithPath("obligations.obligation_title.id").description("Id of the obligation"), + subsectionWithPath("obligations.obligation_title.obligationType").description("Type of the obligation"), fieldWithPath("page").description("Additional paging information"), fieldWithPath("page.size").description("Number of obligations per page"), fieldWithPath("page.totalElements").description("Total number of all license obligations"),