Skip to content

Commit

Permalink
Merge pull request #2167 from toshiba/release/update_response_single_…
Browse files Browse the repository at this point in the history
…release

fix(licenses): Update Response for api single license and add rest api docs create License

Reviewed by: [email protected]
Tested by: [email protected]
  • Loading branch information
ag4ums authored Nov 8, 2023
2 parents 8c014c7 + 4e329b4 commit 4efed92
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
15 changes: 14 additions & 1 deletion rest/resource-server/src/docs/asciidoc/licenses.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ include::{snippets}/should_document_get_licenses/http-response.adoc[]
===== Links
include::{snippets}/should_document_get_licenses/links.adoc[]


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

Expand All @@ -49,6 +48,20 @@ include::{snippets}/should_document_get_license/http-response.adoc[]
===== Links
include::{snippets}/should_document_get_license/links.adoc[]

[[resources-create-license]]
==== Create a license

A `POST` request will create a license.

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

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

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

[[resources-license-link-obligation]]
==== Link obligations to license

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.sw360.datahandler.thrift.changelogs.ReferenceDocData;
import org.eclipse.sw360.datahandler.thrift.components.*;
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.moderation.ModerationRequest;
import org.eclipse.sw360.datahandler.thrift.packages.Package;
Expand Down Expand Up @@ -84,6 +85,7 @@ public Sw360Module() {
setMixInAnnotation(ProjectUsage.class, Sw360Module.ProjectUsageMixin.class);
setMixInAnnotation(Vendor.class, Sw360Module.VendorMixin.class);
setMixInAnnotation(License.class, Sw360Module.LicenseMixin.class);
setMixInAnnotation(LicenseType.class, Sw360Module.LicenseTypeMixin.class);
setMixInAnnotation(Obligation.class, Sw360Module.ObligationMixin.class);
setMixInAnnotation(Vulnerability.class, Sw360Module.VulnerabilityMixin.class);
setMixInAnnotation(VulnerabilityState.class, Sw360Module.VulnerabilityStateMixin.class);
Expand Down Expand Up @@ -1099,12 +1101,8 @@ static abstract class VendorMixin extends Vendor {
@JsonIgnoreProperties({
"id",
"revision",
"licenseType",
"licenseTypeDatabaseId",
"externalLicenseLink",
"reviewdate",
"obligations",
"obligationDatabaseIds",
"obligationListId",
"osiapproved",
"fsflibre",
Expand Down Expand Up @@ -1173,11 +1171,24 @@ static abstract class LicenseMixin extends License {
abstract public String getShortname();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({
"revision",
"type",
"licenseTypeId",
"setRevision",
"setType",
"setLicenseType",
"setId",
"setLicenseTypeId",
})
static abstract class LicenseTypeMixin extends License {
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({
"id",
"revision",
"whitelist",
"whitelistSize",
"whitelistIterator",
"development",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,10 @@ public HalResource<Obligation> addEmbeddedObligation(Obligation obligation) {
public Obligation convertToEmbeddedObligation(Obligation obligation) {
Obligation embeddedObligation = new Obligation();
embeddedObligation.setTitle(obligation.getTitle());
embeddedObligation.setObligationType(obligation.getObligationType());
embeddedObligation.setId(obligation.getId());
embeddedObligation.setWhitelist(obligation.getWhitelist());
embeddedObligation.setText(obligation.getText());
embeddedObligation.setType(null);
return embeddedObligation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@
import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -66,7 +64,7 @@ public class LicenseSpecTest extends TestRestDocsSpecBase {
@MockBean
private Sw360LicenseService licenseServiceMock;

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

Expand All @@ -84,17 +82,24 @@ public void before() throws TException, IOException {
license.setExternalIds(externalIds);
license.setAdditionalData(Collections.singletonMap("Key", "Value"));
license.setNote("License's Note");
license.setExternalLicenseLink("https://spdx.org/licenses/Apache-2.0.html");

License license2 = new License();
license2.setId("MIT");
license2.setFullname("The MIT License (MIT)");
license2.setShortname("MIT");
license2.setText("placeholder for the MIT license text");
license2.setNote("License2's Note");
license2.setExternalLicenseLink("https://spdx.org/licenses/MIT.html");

List<License> licenseList = new ArrayList<>();
licenseList.add(license);
licenseList.add(license2);

license3 = new License();
license3.setId("Apache-3.0");
license3.setShortname("Apache 3.0");
license3.setFullname("Apache License 3.0");

requestSummary.setRequestStatus(RequestStatus.SUCCESS);
LicenseType licensetype = new LicenseType();
Expand All @@ -105,6 +110,7 @@ public void before() throws TException, IOException {

given(this.licenseServiceMock.getLicenses()).willReturn(licenseList);
given(this.licenseServiceMock.getLicenseById(eq(license.getId()))).willReturn(license);
given(this.licenseServiceMock.createLicense(any(), any())).willReturn(license3);
Mockito.doNothing().when(licenseServiceMock).deleteLicenseById(any(), any());
Mockito.doNothing().when(licenseServiceMock).deleteAllLicenseInfo(any());
Mockito.doNothing().when(licenseServiceMock).importSpdxInformation(any());
Expand Down Expand Up @@ -169,7 +175,9 @@ public void should_document_get_license() throws Exception {
fieldWithPath("fullName").description("The full name of the license"),
fieldWithPath("shortName").description("The short name of the license, optional"),
subsectionWithPath("externalIds").description("When releases are imported from other tools, the external ids can be stored here"),
fieldWithPath("externalLicenseLink").description("The external license link of the license"),
subsectionWithPath("additionalData").description("A place to store additional data used by external tools"),
subsectionWithPath("obligationDatabaseIds").description("The obligationDatabaseIds license link of the license"),
fieldWithPath("text").description("The license's original text"),
fieldWithPath("checked").description("The information, whether the license is already checked, optional and defaults to true"),
subsectionWithPath("OSIApproved").description("The OSI aprroved information, possible values are: " + Arrays.asList(Quadratic.values())),
Expand All @@ -180,6 +188,32 @@ public void should_document_get_license() throws Exception {
)));
}

@Test
public void should_document_create_license() throws Exception {
Map<String, String> licenseRequestBody = new HashMap<>();
licenseRequestBody.put("fullName", "Apache 3.0");
licenseRequestBody.put("shortName", "Apache License 3.0");
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
this.mockMvc.perform(post("/api/licenses")
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(licenseRequestBody))
.header("Authorization", "Bearer " + accessToken))
.andExpect(status().isCreated())
.andDo(this.documentationHandler.document(
requestFields(
fieldWithPath("fullName").description("The fullName of the new license"),
fieldWithPath("shortName").description("The shortname of the origin license")
),
responseFields(
fieldWithPath("fullName").description("The fullName of the license"),
fieldWithPath("shortName").description("The shortname of the license"),
fieldWithPath("checked").description("The information, whether the license is already checked, optional and defaults to true"),
subsectionWithPath("OSIApproved").description("The OSI aprroved information, possible values are: " + Arrays.asList(Quadratic.values())),
fieldWithPath("FSFLibre").description("The FSF libre information, possible values are: " + Arrays.asList(Quadratic.values())),
subsectionWithPath("_links").description("<<resources-index-links,Links>> to other resources")
)));
}

@Test
public void should_document_delete_license() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
Expand Down

0 comments on commit 4efed92

Please sign in to comment.