From d89c0d24bbe2af185932ab6c5253c76a358c8c8f Mon Sep 17 00:00:00 2001 From: Nikesh kumar Date: Tue, 10 Oct 2023 10:41:34 +0530 Subject: [PATCH] fix(rest): Added new endpoint for LicenseType in admin tab Signed-off-by: Nikesh kumar --- .../src/docs/asciidoc/licenses.adoc | 12 +++++++++ .../license/LicenseController.java | 11 +++++++- .../license/Sw360LicenseService.java | 27 ++++++++++++++++--- .../restdocs/LicenseSpecTest.java | 19 ++++++++++++- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/rest/resource-server/src/docs/asciidoc/licenses.adoc b/rest/resource-server/src/docs/asciidoc/licenses.adoc index 62e5d49d64..34e24220d3 100644 --- a/rest/resource-server/src/docs/asciidoc/licenses.adoc +++ b/rest/resource-server/src/docs/asciidoc/licenses.adoc @@ -142,3 +142,15 @@ include::{snippets}/should_document_import_osadl_info/curl-request.adoc[] ===== Example response include::{snippets}/should_document_import_osadl_info/http-response.adoc[] + +[[add-license-type]] +==== create license type + +A `POST` request help to create license type. + +===== Example request +include::{snippets}/should_document_get_create_license_type/curl-request.adoc[] + +===== Example response +include::{snippets}/should_document_get_create_license_type/http-response.adoc[] + diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/LicenseController.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/LicenseController.java index 659ae7ce61..1ff50d0f73 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/LicenseController.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/LicenseController.java @@ -18,6 +18,7 @@ import org.eclipse.sw360.datahandler.thrift.RequestStatus; import org.eclipse.sw360.datahandler.thrift.RequestSummary; import org.eclipse.sw360.datahandler.thrift.licenses.License; +import org.eclipse.sw360.datahandler.thrift.licenses.LicenseType; import org.eclipse.sw360.datahandler.thrift.licenses.Obligation; import org.eclipse.sw360.datahandler.thrift.users.User; import org.eclipse.sw360.rest.resourceserver.core.HalResource; @@ -206,7 +207,7 @@ public ResponseEntity uploadLicenses(@RequestParam("licenseFile") MultipartFi } return ResponseEntity.ok(Series.SUCCESSFUL); } - + @RequestMapping(value = LICENSES_URL + "/import/OSADL", method = RequestMethod.POST) public ResponseEntity importOsadlInfo() throws TException { User sw360User = restControllerHelper.getSw360UserFromAuthentication(); @@ -217,4 +218,12 @@ public ResponseEntity importOsadlInfo() throws TException { HttpStatus status = HttpStatus.OK; return new ResponseEntity<>(requestSummary,status); } + + @RequestMapping(value = LICENSES_URL + "/addLicenseType", method = RequestMethod.POST) + public ResponseEntity createLicenseType(String licenseType, HttpServletRequest request) throws TException { + User sw360User = restControllerHelper.getSw360UserFromAuthentication(); + RequestStatus requestStatus=licenseService.addLicenseType(sw360User, licenseType, request); + HttpStatus status = HttpStatus.OK; + return new ResponseEntity<>(requestStatus,status); + } } diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/Sw360LicenseService.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/Sw360LicenseService.java index 1f31b777df..d9c3056019 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/Sw360LicenseService.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/Sw360LicenseService.java @@ -25,6 +25,7 @@ import org.eclipse.sw360.datahandler.thrift.licenses.License; import org.eclipse.sw360.datahandler.thrift.licenses.Obligation; import org.eclipse.sw360.datahandler.thrift.licenses.LicenseService; +import org.eclipse.sw360.datahandler.thrift.licenses.LicenseType; import org.eclipse.sw360.datahandler.thrift.users.User; import org.eclipse.sw360.datahandler.thrift.users.UserGroup; import org.eclipse.sw360.exporter.LicsExporter; @@ -34,6 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.rest.webmvc.ResourceNotFoundException; +import org.springframework.http.HttpRequest; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.stereotype.Service; import org.springframework.util.FileCopyUtils; @@ -60,6 +62,7 @@ public class Sw360LicenseService { @Value("${sw360.thrift-server-url:http://localhost:8080}") private String thriftServerUrl; private static String CONTENT_TYPE = "application/zip"; + private LicenseType lType; public List getLicenses() throws TException { LicenseService.Iface sw360LicenseClient = getThriftLicenseClient(); @@ -213,11 +216,11 @@ private void copyDataStreamToResponse(HttpServletResponse response, ByteArrayInp } public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteIfExternalIdMatches, boolean overwriteIfIdMatchesEvenWithoutExternalIdMatch) throws IOException, TException { - final HashMap inputMap = new HashMap<>(); + final HashMap inputMap = new HashMap<>(); - if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) { - throw new HttpMessageNotReadableException("Unable to upload license file. User is not admin"); - } + if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) { + throw new HttpMessageNotReadableException("Unable to upload license file. User is not admin"); + } try { InputStream inputStream = file.getInputStream(); ZipTools.extractZipToInputStreamMap(inputStream, inputMap); @@ -231,6 +234,7 @@ public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteI } } } + public RequestSummary importOsadlInformation(User sw360User) throws TException { LicenseService.Iface sw360LicenseClient = getThriftLicenseClient(); if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) { @@ -240,4 +244,19 @@ public RequestSummary importOsadlInformation(User sw360User) throws TException { throw new HttpMessageNotReadableException("Unable to import All Spdx license. User is not admin"); } } + + public RequestStatus addLicenseType(User sw360User, String licenseType, HttpServletRequest request) throws TException { + LicenseService.Iface sw360LicenseClient = getThriftLicenseClient(); + lType.setLicenseType(licenseType); + try { + if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) { + RequestStatus status = sw360LicenseClient.addLicenseType(lType, sw360User); + } else { + throw new HttpMessageNotReadableException("Unable to create License Type. User is not admin"); + } + } catch ( Exception e) { + throw new TException(e.getMessage()); + } + return RequestStatus.SUCCESS; + } } diff --git a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/LicenseSpecTest.java b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/LicenseSpecTest.java index e33eb506a3..8f7aee1207 100644 --- a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/LicenseSpecTest.java +++ b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/LicenseSpecTest.java @@ -11,6 +11,7 @@ import org.apache.thrift.TException; import org.eclipse.sw360.datahandler.thrift.licenses.License; +import org.eclipse.sw360.datahandler.thrift.licenses.LicenseType; import org.eclipse.sw360.rest.resourceserver.TestHelper; import org.eclipse.sw360.rest.resourceserver.license.Sw360LicenseService; import org.eclipse.sw360.datahandler.thrift.licenses.Obligation; @@ -96,6 +97,11 @@ public void before() throws TException, IOException { licenseList.add(license2); requestSummary.setRequestStatus(RequestStatus.SUCCESS); + LicenseType licensetype = new LicenseType(); + licensetype.setId("1234"); + licensetype.setLicenseType("wer"); + licensetype.setLicenseTypeId(123); + licensetype.setType("xyz"); given(this.licenseServiceMock.getLicenses()).willReturn(licenseList); given(this.licenseServiceMock.getLicenseById(eq(license.getId()))).willReturn(license); @@ -105,6 +111,7 @@ public void before() throws TException, IOException { Mockito.doNothing().when(licenseServiceMock).getDownloadLicenseArchive(any(), any(), any()); Mockito.doNothing().when(licenseServiceMock).uploadLicense(any(), any(), anyBoolean(), anyBoolean()); given(this.licenseServiceMock.importOsadlInformation(any())).willReturn(requestSummary); + given(this.licenseServiceMock.addLicenseType(any(),any() , any())).willReturn(RequestStatus.SUCCESS); obligation1 = new Obligation(); obligation1.setId("0001"); obligation1.setTitle("Obligation 1"); @@ -255,7 +262,8 @@ public void should_document_upload_license() throws Exception { .header("Authorization", "Bearer " + accessToken); this.mockMvc.perform(builder).andExpect(status().isOk()); } - + + @Test public void should_document_import_osadl_info() throws Exception { String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword); mockMvc.perform(post("/api/licenses/import/OSADL") @@ -263,4 +271,13 @@ public void should_document_import_osadl_info() throws Exception { .accept(MediaTypes.HAL_JSON)) .andExpect(status().isOk()); } + + @Test + public void should_document_get_create_license_type() throws Exception { + String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword); + mockMvc.perform(post("/api/licenses/" + "/addLicenseType") + .header("Authorization", "Bearer " + accessToken) + .accept(MediaTypes.HAL_JSON)) + .andExpect(status().isOk()); + } }