Skip to content

Commit

Permalink
fix(rest):new endpoint to delete all license information in admin tab
Browse files Browse the repository at this point in the history
Signed-off-by: Nikesh kumar <[email protected]>
  • Loading branch information
Nikesh kumar committed Jul 26, 2023
1 parent ec4c39e commit 92e9378
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
13 changes: 12 additions & 1 deletion rest/resource-server/src/docs/asciidoc/licenses.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
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[]
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,12 @@ private HalResource<License> createHalLicense(License sw360License) {
}
return halLicense;
}

@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = LICENSES_URL + "/delete", method = RequestMethod.DELETE)
public ResponseEntity deleteAllLicense() throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
licenseService.deleteAllLicenseInfo(sw360User);
return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@
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.AddDocumentRequestSummary;
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.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.datahandler.thrift.vendors.VendorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
Expand Down Expand Up @@ -75,6 +81,16 @@ 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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());
}

}

0 comments on commit 92e9378

Please sign in to comment.