From 7c8f8d24b7dac027fe660ea74ac85dcddf2dce9c Mon Sep 17 00:00:00 2001 From: Nikesh kumar Date: Mon, 24 Jul 2023 11:31:51 +0530 Subject: [PATCH] fix(rest):new endpoint to delete all license information in admin tab Signed-off-by: Nikesh kumar --- .../resource-server/src/docs/asciidoc/licenses.adoc | 13 ++++++++++++- .../resourceserver/license/LicenseController.java | 8 ++++++++ .../resourceserver/license/Sw360LicenseService.java | 12 ++++++++++++ .../resourceserver/restdocs/LicenseSpecTest.java | 11 +++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/rest/resource-server/src/docs/asciidoc/licenses.adoc b/rest/resource-server/src/docs/asciidoc/licenses.adoc index 75184fa618..f39d026240 100644 --- a/rest/resource-server/src/docs/asciidoc/licenses.adoc +++ b/rest/resource-server/src/docs/asciidoc/licenses.adoc @@ -86,4 +86,15 @@ A `DELETE` request will delete a single license. include::{snippets}/should_document_delete_license/curl-request.adoc[] ===== Example response -include::{snippets}/should_document_delete_license/http-response.adoc[] \ No newline at end of file +include::{snippets}/should_document_delete_license/http-response.adoc[] + +[[delete-all-license-info]] +==== Delete all licenses info + +A `DELETE` request will delete all licenses info. + +===== Example request +include::{snippets}/should_document_delete_all_license_info/curl-request.adoc[] + +===== Example response +include::{snippets}/should_document_delete_all_license_info/http-response.adoc[] \ No newline at end of file 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 e8cbd21a88..c6839db3d1 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 @@ -158,4 +158,12 @@ private HalResource createHalLicense(License sw360License) { } return halLicense; } + + @PreAuthorize("hasAuthority('WRITE')") + @RequestMapping(value = LICENSES_URL + "/deleteAll", method = RequestMethod.DELETE) + public ResponseEntity deleteAllLicense() throws TException { + User sw360User = restControllerHelper.getSw360UserFromAuthentication(); + licenseService.deleteAllLicenseInfo(sw360User); + return new ResponseEntity<>(HttpStatus.OK); + } } 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 f136dc6069..283866edf0 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 @@ -16,13 +16,16 @@ import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.THttpClient; import org.apache.thrift.transport.TTransportException; +import org.eclipse.sw360.datahandler.permissions.PermissionUtils; import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException; import org.eclipse.sw360.datahandler.thrift.RequestStatus; +import org.eclipse.sw360.datahandler.thrift.RequestSummary; import org.eclipse.sw360.datahandler.thrift.SW360Exception; 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.users.User; +import org.eclipse.sw360.datahandler.thrift.users.UserGroup; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.rest.webmvc.ResourceNotFoundException; @@ -75,6 +78,15 @@ public void deleteLicenseById(String licenseId, User user) throws TException { } } + public void deleteAllLicenseInfo(User user) throws TException { + LicenseService.Iface sw360LicenseClient = getThriftLicenseClient(); + if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, user)) { + RequestSummary deleteLicenseStatus = sw360LicenseClient.deleteAllLicenseInformation(user); + } else { + throw new HttpMessageNotReadableException("Unable to delete license. User is not admin"); + } + } + public License createLicense(License license, User sw360User) throws TException { LicenseService.Iface sw360LicenseClient = getThriftLicenseClient(); license.setId(license.getShortname()); 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 8b4f07f382..00f5b5093d 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 @@ -89,6 +89,7 @@ public void before() throws TException { given(this.licenseServiceMock.getLicenses()).willReturn(licenseList); given(this.licenseServiceMock.getLicenseById(eq(license.getId()))).willReturn(license); Mockito.doNothing().when(licenseServiceMock).deleteLicenseById(any(), any()); + Mockito.doNothing().when(licenseServiceMock).deleteAllLicenseInfo(any()); obligation1 = new Obligation(); obligation1.setId("0001"); obligation1.setTitle("Obligation 1"); @@ -202,4 +203,14 @@ public void should_document_unlink_obligation() throws Exception { .header("Authorization", "Bearer " + accessToken)) .andExpect(status().isOk()); } + + @Test + public void should_document_delete_all_license_info() throws Exception { + String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword); + mockMvc.perform(delete("/api/licenses/" + "/delete") + .header("Authorization", "Bearer " + accessToken) + .accept(MediaTypes.HAL_JSON)) + .andExpect(status().isOk()); + } + }