Skip to content

Commit

Permalink
fix(rest): create new endpoint for import spdx 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 Aug 29, 2023
1 parent 05a2760 commit 9445da8
Show file tree
Hide file tree
Showing 4 changed files with 41 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 @@ -97,4 +97,15 @@ A `DELETE` request will delete all licenses info.
include::{snippets}/should_document_delete_all_license_info/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_delete_all_license_info/http-response.adoc[]
include::{snippets}/should_document_delete_all_license_info/http-response.adoc[]

[[import-SPDX-info]]
==== Import SPDX information

A `IMPORT` request will import SPDX info.

===== Example request
include::{snippets}/should_document_import_SPDX_info/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_import_SPDX_info/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.hateoas.server.RepresentationModelProcessor;
import org.springframework.hateoas.CollectionModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.prepost.PreAuthorize;
Expand All @@ -35,6 +36,8 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;
Expand Down Expand Up @@ -166,4 +169,12 @@ public ResponseEntity deleteAllLicense() throws TException {
licenseService.deleteAllLicenseInfo(sw360User);
return new ResponseEntity<>(HttpStatus.OK);
}

@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = LICENSES_URL + "/import/SPDX", method = RequestMethod.POST)
public ResponseEntity<?> importSPDX() throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
licenseService.importSpdxInformation(sw360User);
return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,13 @@ private LicenseService.Iface getThriftLicenseClient() throws TTransportException
TProtocol protocol = new TCompactProtocol(thriftClient);
return new LicenseService.Client(protocol);
}

public void importSpdxInformation(User sw360User) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestSummary deleteLicenseStatus = sw360LicenseClient.importAllSpdxLicenses(sw360User);
} else {
throw new HttpMessageNotReadableException("Unable to import All Spdx license. User is not admin");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void before() throws TException {
given(this.licenseServiceMock.getLicenseById(eq(license.getId()))).willReturn(license);
Mockito.doNothing().when(licenseServiceMock).deleteLicenseById(any(), any());
Mockito.doNothing().when(licenseServiceMock).deleteAllLicenseInfo(any());
Mockito.doNothing().when(licenseServiceMock).importSpdxInformation(any());
obligation1 = new Obligation();
obligation1.setId("0001");
obligation1.setTitle("Obligation 1");
Expand Down Expand Up @@ -213,4 +214,12 @@ public void should_document_delete_all_license_info() throws Exception {
.andExpect(status().isOk());
}

@Test
public void should_document_import_SPDX_info() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(post("/api/licenses/" + "/import/SPDX")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}
}

0 comments on commit 9445da8

Please sign in to comment.