From fd13d194382ed66f5368d90ac06d731782c62779 Mon Sep 17 00:00:00 2001 From: rudra-superrr Date: Mon, 10 Jul 2023 11:52:40 +0530 Subject: [PATCH] feat(rest): listing license clearing info of a project. Signed-off-by: rudra-superrr --- .../src/docs/asciidoc/projects.adoc | 14 +++++++ .../core/RestControllerHelper.java | 28 +++++++++++++ .../project/ProjectController.java | 42 +++++++++++++++++++ .../restdocs/ProjectSpecTest.java | 35 ++++++++++++++++ 4 files changed, 119 insertions(+) diff --git a/rest/resource-server/src/docs/asciidoc/projects.adoc b/rest/resource-server/src/docs/asciidoc/projects.adoc index d228858d96..c4e160d68d 100644 --- a/rest/resource-server/src/docs/asciidoc/projects.adoc +++ b/rest/resource-server/src/docs/asciidoc/projects.adoc @@ -567,6 +567,20 @@ include::{snippets}/should_document_unlink_packages/curl-request.adoc[] ===== Example response include::{snippets}/should_document_unlink_packages/http-response.adoc[] +[[resources-project-get-project-license-clearing-information]] +==== Listing license clearing information + +A `GET` request will get all releases of license clearing + +===== Response structure +include::{snippets}/should_document_get_license_clearing/response-fields.adoc[] + +===== Example request +include::{snippets}/should_document_get_license_clearing/curl-request.adoc[] + +===== Example response +include::{snippets}/should_document_get_license_clearing/http-response.adoc[] + [[resources-project-get-download-licenseinfo]] ==== Download License Info diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/RestControllerHelper.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/RestControllerHelper.java index 3c41e67ccc..9c3f36e342 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/RestControllerHelper.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/RestControllerHelper.java @@ -508,6 +508,16 @@ public void addEmbeddedPackage(HalResource halResource, Package pkg) { halResource.addEmbeddedResource("sw360:packages", halPackage); } + public HalResource addEmbeddedReleaseLinks(Release release) { + final Release embeddedRelease = convertToEmbeddedLinkedRelease(release); + final HalResource releaseResource = new HalResource<>(embeddedRelease); + Link releaseLink = linkTo(ReleaseController.class) + .slash("api/releases/" + embeddedRelease.getId()).withSelfRel(); + releaseResource.add(releaseLink); + + return releaseResource; + } + public void addEmbeddedAttachments( HalResource halResource, Set attachments) { @@ -749,6 +759,24 @@ public License convertToEmbeddedLicense(String licenseId) { return embeddedLicense; } + public Release convertToEmbeddedLinkedRelease(Release release) { + Release embeddedRelease = new Release(); + embeddedRelease.setId(release.getId()); + embeddedRelease.setMainlineState(release.getMainlineState()); + embeddedRelease.setClearingState(release.getClearingState()); + embeddedRelease.setMainLicenseIds(release.getMainLicenseIds()); + embeddedRelease.setOtherLicenseIds(release.getOtherLicenseIds()); + embeddedRelease.setName(release.getName()); + embeddedRelease.setComponentType(release.getComponentType()); + embeddedRelease.setVersion(release.getVersion()); + embeddedRelease.setType(null); + return embeddedRelease; + } + + public void addEmbeddedProjectReleases(HalResource halResource, List> releases) { + halResource.addEmbeddedResource("sw360:release", releases); + } + public User convertToEmbeddedUser(User user) { User embeddedUser = new User(); embeddedUser.setId(user.getId()); 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 0637cb2982..cfca05c8c8 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 @@ -96,6 +96,8 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.util.FileCopyUtils; import org.springframework.util.MultiValueMap; +import org.springframework.hateoas.Link; +import org.eclipse.sw360.rest.resourceserver.release.ReleaseController; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -336,6 +338,29 @@ public ResponseEntity>> getProjectsFiltered mapOfProjects, true, sw360Projects, false); } + @RequestMapping(value = PROJECTS_URL + "/{id}/licenseClearing", method = RequestMethod.GET) + public ResponseEntity licenseClearing(@PathVariable("id") String id, @RequestParam(value = "transitive", required = true) String transitive, HttpServletRequest request) + throws URISyntaxException, TException { + + final User sw360User = restControllerHelper.getSw360UserFromAuthentication(); + Project sw360Project = projectService.getProjectForUserById(id, sw360User); + + final Set releaseIds = projectService.getReleaseIds(id, sw360User, transitive); + List releases = releaseIds.stream().map(relId -> wrapTException(() -> { + final Release sw360Release = releaseService.getReleaseForUserById(relId, sw360User); + releaseService.setComponentDependentFieldsInRelease(sw360Release, sw360User); + return sw360Release; + })).collect(Collectors.toList()); + + List> releaseList = releases.stream().map(sw360Release -> wrapTException(() -> { + final HalResource releaseResource = restControllerHelper.addEmbeddedReleaseLinks(sw360Release); + return releaseResource; + })).collect(Collectors.toList()); + + HalResource userHalResource = createHalLicenseClearing(sw360Project, releaseList); + return new ResponseEntity<>(userHalResource, HttpStatus.OK); + } + @RequestMapping(value = PROJECTS_URL + "/{id}", method = RequestMethod.GET) public ResponseEntity> getProject( @PathVariable("id") String id) throws TException { @@ -1157,6 +1182,23 @@ public RepositoryLinksResource process(RepositoryLinksResource resource) { return resource; } + private HalResource createHalLicenseClearing(Project sw360Project, List> releases) { + Project sw360 = new Project(); + Map releaseIdToUsage = sw360Project.getReleaseIdToUsage(); + sw360.setReleaseIdToUsage(sw360Project.getReleaseIdToUsage()); + sw360.setLinkedProjects(sw360Project.getLinkedProjects()); + sw360.unsetState(); + sw360.unsetProjectType(); + sw360.unsetVisbility(); + sw360.unsetSecurityResponsibles(); + HalResource halProject = new HalResource<>(sw360); + + if (releaseIdToUsage != null) { + restControllerHelper.addEmbeddedProjectReleases(halProject, releases); + } + return halProject; + } + private HalResource createHalProject(Project sw360Project, User sw360User) throws TException { HalResource halProject = new HalResource<>(sw360Project); User projectCreator = restControllerHelper.getUserByEmail(sw360Project.getCreatedBy()); diff --git a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ProjectSpecTest.java b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ProjectSpecTest.java index 32f118240b..4d2d857ec3 100644 --- a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ProjectSpecTest.java +++ b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ProjectSpecTest.java @@ -507,6 +507,9 @@ public void before() throws TException, IOException { release.setClearingState(ClearingState.APPROVED); release.setExternalIds(Collections.singletonMap("mainline-id-component", "1432")); release.setPackageIds(linkedPackages); + release.setMainlineState(MainlineState.MAINLINE); + release.setMainLicenseIds(new HashSet<>(Arrays.asList("GPL-2.0-or-later", "Apache-2.0"))); + release.setOtherLicenseIds(new HashSet<>(Arrays.asList("LGPL-2.0"))); Release release2 = new Release(); release2.setId("5578999"); @@ -523,6 +526,9 @@ public void before() throws TException, IOException { release2.setClearingState(ClearingState.APPROVED); release2.setExternalIds(Collections.singletonMap("mainline-id-component", "1771")); release2.setComponentType(ComponentType.COTS); + release2.setMainlineState(MainlineState.MAINLINE); + release2.setMainLicenseIds(new HashSet<>(Arrays.asList("Apache-2.0"))); + release2.setOtherLicenseIds(new HashSet<>(Arrays.asList("GPL-2.0"))); Release rel = new Release(); Map releaseExternalIds = new HashMap<>(); @@ -1114,6 +1120,35 @@ public void should_document_get_projects_by_externalIds() throws Exception { ))); } + @Test + public void should_document_get_license_clearing() throws Exception { + String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword); + mockMvc.perform(get("/api/projects/" + project.getId() + "/licenseClearing") + .header("Authorization", "Bearer " + accessToken) + .param("transitive", "true") + .param("page", "0") + .param("page_entries", "5") + .param("sort", "name,desc") + .accept(MediaTypes.HAL_JSON)) + .andExpect(status().isOk()) + .andDo(this.documentationHandler.document( + requestParameters( + parameterWithName("transitive").description("Get the transitive releases"), + parameterWithName("page").description("Page of releases"), + parameterWithName("page_entries").description("Amount of releases page"), + parameterWithName("sort").description("Defines order of the releases") + ), + responseFields( + fieldWithPath("enableSvm").description("Security vulnerability monitoring flag"), + fieldWithPath("considerReleasesFromExternalList").description("Consider list of releases from existing external list"), + fieldWithPath("enableVulnerabilitiesDisplay").description("Displaying vulnerabilities flag."), + subsectionWithPath("linkedReleases").description("The relationship between linked releases of the project"), + subsectionWithPath("linkedProjects").description("The `linked project id` - metadata of linked projects (`enableSvm` - whether linked projects will be part of SVM, `projectRelationship` - relationship between linked project and the project. Possible values: " + Arrays.asList(ProjectRelationship.values())), + subsectionWithPath("_embedded.sw360:release").description("An array of linked releases"), + subsectionWithPath("_links").description("<> to other resources") + ))); + } + @Test public void should_document_get_linked_projects() throws Exception { String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);