Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(license): Listing obligations by license #2234

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public List<Obligation> getObligationsByIds(List<String> ids) throws TException
return handler.getObligationsByIds(ids);
}

@Override
public List<Obligation> getObligationsByLicenseId(String id) throws TException {
assertNotEmpty(id);
return handler.getObligationsByLicenseId(id);
}



////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,12 @@ public List<Obligation> getObligationsByIds(Collection<String> ids) {
return obligations;
}

public List<Obligation> getObligationsByLicenseId(String id) throws SW360Exception {
License license = licenseRepository.get(id);
Set<String> ids = license.getObligationDatabaseIds();
return getObligationsByIds(ids);
}

public LicenseType getLicenseTypeById(String id) {
return licenseTypeRepository.get(id);
}
Expand Down
5 changes: 5 additions & 0 deletions libraries/datahandler/src/main/thrift/licenses.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ service LicenseService {
**/
list<Obligation> getObligationsByIds( 1: list<string> ids);

/**
* get obligations with license id
**/
list<Obligation> getObligationsByLicenseId( 1: string id);

LicenseType getLicenseTypeById( 1: string id);

/**
Expand Down
17 changes: 17 additions & 0 deletions rest/resource-server/src/docs/asciidoc/licenses.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ include::{snippets}/should_document_get_licenses/http-response.adoc[]
===== Links
include::{snippets}/should_document_get_licenses/links.adoc[]

[[resources-obligation-list]]
==== Listing obligations

A `GET` request will list obligations of license.

===== Response structure
include::{snippets}/should_document_get_obligations_by_license/response-fields.adoc[]

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

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

===== Links
include::{snippets}/should_document_get_obligations_by_license/links.adoc[]

[[resources-license-get]]
==== Get a single license

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ public ResponseEntity<CollectionModel<EntityModel<License>>> getLicenses() throw
return new ResponseEntity<>(resources, HttpStatus.OK);
}

@RequestMapping(value = LICENSES_URL + "/{id}/obligations", method = RequestMethod.GET)
public ResponseEntity<CollectionModel<EntityModel<Obligation>>> getObligationsByLicenseId(
@PathVariable("id") String id) throws TException {
List<Obligation> obligations = licenseService.getObligationsByLicenseId(id);
List<EntityModel<Obligation>> obligationResources = new ArrayList<>();
obligations.forEach(o -> {
Obligation embeddedObligation = restControllerHelper.convertToEmbeddedObligation(o);
obligationResources.add(EntityModel.of(embeddedObligation));
});
CollectionModel<EntityModel<Obligation>> resources = CollectionModel.of(obligationResources);
return new ResponseEntity<>(resources, HttpStatus.OK);
}

@Operation(
summary = "Get a specific license.",
description = "Get a specific license.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ public RequestStatus updateLicense(License license, User sw360User) throws TExce
return sw360LicenseClient.updateLicense(license, sw360User, sw360User);
}

public List<Obligation> getObligationsByLicenseId(String id) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
List<Obligation> obligations = sw360LicenseClient.getObligationsByLicenseId(id);
if (CommonUtils.isNullOrEmptyCollection(obligations)) {
return Collections.emptyList();
}
return obligations;
}

public void checkObligationIds(Set<String> obligationIds) throws TException {
if (obligationIds.isEmpty()) {
throw new HttpMessageNotReadableException("Cannot update because no obliagtion id input");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class LicenseSpecTest extends TestRestDocsSpecBase {
@MockBean
private SW360ReportService sw360ReportServiceMock;

private License license, license3;
private License license, license2, license3;
private Obligation obligation1, obligation2;
private RequestSummary requestSummary = new RequestSummary();

Expand All @@ -91,7 +91,7 @@ public void before() throws TException, IOException {
license.setNote("License's Note");
license.setExternalLicenseLink("https://spdx.org/licenses/Apache-2.0.html");

License license2 = new License();
license2 = new License();
license2.setId("MIT");
license2.setFullname("The MIT License (MIT)");
license2.setShortname("MIT");
Expand Down Expand Up @@ -140,6 +140,12 @@ public void before() throws TException, IOException {
obligation2.setText("This is text of Obligation 2");
obligation2.setObligationType(ObligationType.OBLIGATION);
obligation2.setObligationLevel(ObligationLevel.LICENSE_OBLIGATION);

List<Obligation> obligations = Arrays.asList(obligation1, obligation2);
Set<String> obligationIds = new HashSet<>(Arrays.asList(obligation1.getId(), obligation2.getId()));
license2.setObligationDatabaseIds(obligationIds);
license2.setObligations(obligations);
given(this.licenseServiceMock.getObligationsByLicenseId(any())).willReturn(obligations);
}

@Test
Expand Down Expand Up @@ -197,6 +203,25 @@ public void should_document_get_license() throws Exception {
)));
}

@Test
public void should_document_get_obligations_by_license() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(get("/api/licenses/"+ license2.getId()+ "/obligations")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk())
.andDo(this.documentationHandler.document(
links(
linkWithRel("curies").description("Curies are used for online documentation")
),
responseFields(
subsectionWithPath("_embedded.sw360:obligations[]title").description("The title of the obligation"),
subsectionWithPath("_embedded.sw360:obligations[]obligationType").description("The type of the obligation"),
subsectionWithPath("_embedded.sw360:obligations").description("An array of <<resources-obligations, Obligations resources>>"),
subsectionWithPath("_links").description("<<resources-index-links,Links>> to other resources")
)));
}

@Test
public void should_document_create_license() throws Exception {
Map<String, String> licenseRequestBody = new HashMap<>();
Expand Down