Skip to content

Commit

Permalink
Merge pull request eclipse-sw360#580 from siemens/fix/GenerateLicense…
Browse files Browse the repository at this point in the history
…InfoREST

fix(rest): Download licenseinfo file rest endpoint.

tested-by: [email protected]
review-by: [email protected]
  • Loading branch information
lepokle authored Jul 17, 2019
2 parents 38d1050 + 12931ff commit c81a988
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
13 changes: 13 additions & 0 deletions rest/resource-server/src/docs/asciidoc/projects.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,16 @@ include::{snippets}/should_document_link_releases/curl-request.adoc[]
===== Example response
include::{snippets}/should_document_link_releases/http-response.adoc[]

[[resources-project-get-download-licenseinfo]]
==== Download License Info

A `GET` request is used to download license info for the project.

===== Request parameter
include::{snippets}/should_document_get_download_license_info/request-parameters.adoc[]

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

===== Example response
include::{snippets}/should_document_get_download_license_info/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public OutputFormatInfo getOutputFormatInfoForGeneratorClass(String generatorCla
}
}

public LicenseInfoFile getLicenseInfoFile(Project project, User sw360User, String generatorClassName,
public LicenseInfoFile getLicenseInfoFile(Project project, User sw360User, String generatorClassNameWithVariant,
Map<String, Set<String>> selectedReleaseAndAttachmentIds, Map<String, Set<LicenseNameWithText>> excludedLicenses) {
try {
LicenseInfoService.Iface sw360LicenseInfoClient = getThriftLicenseInfoClient();
return sw360LicenseInfoClient.getLicenseInfoFile(project, sw360User, generatorClassName, selectedReleaseAndAttachmentIds, excludedLicenses);
return sw360LicenseInfoClient.getLicenseInfoFile(project, sw360User, generatorClassNameWithVariant, selectedReleaseAndAttachmentIds, excludedLicenses);
} catch (TException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ public ResponseEntity<Resources<Resource<License>>> getLicensesOfReleases(@PathV
@RequestMapping(value = PROJECTS_URL + "/{id}/licenseinfo", method = RequestMethod.GET)
public void downloadLicenseInfo(@PathVariable("id") String id,
@RequestParam("generatorClassName") String generatorClassName,
@RequestParam("variant") String variant,
HttpServletResponse response) throws TException, IOException {
final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
final Project sw360Project = projectService.getProjectForUserById(id, sw360User);
Expand All @@ -282,13 +283,14 @@ public void downloadLicenseInfo(@PathVariable("id") String id,

final String projectName = sw360Project.getName();
final String projectVersion = sw360Project.getVersion();
final String timestamp = SW360Utils.getCreatedOnTime().replaceAll("\\s", "_").replace(":", "_");
final OutputFormatInfo outputFormatInfo = licenseInfoService.getOutputFormatInfoForGeneratorClass(generatorClassName);
final String filename = String.format("LicenseInfo-%s%s-%s.%s", projectName,
StringUtils.isBlank(projectVersion) ? "" : "-" + projectVersion, timestamp,
outputFormatInfo.getFileExtension());

final LicenseInfoFile licenseInfoFile = licenseInfoService.getLicenseInfoFile(sw360Project, sw360User, generatorClassName, selectedReleaseAndAttachmentIds, excludedLicenses);
final String timestamp = SW360Utils.getCreatedOnTime().replaceAll("\\s", "_").replace(":", "_");
String outputGeneratorClassNameWithVariant = generatorClassName+"::"+variant;
final OutputFormatInfo outputFormatInfo = licenseInfoService.getOutputFormatInfoForGeneratorClass(generatorClassName);
final String filename = String.format("LicenseInfo-%s%s-%s.%s", projectName,
StringUtils.isBlank(projectVersion) ? "" : "-" + projectVersion, timestamp,
outputFormatInfo.getFileExtension());

final LicenseInfoFile licenseInfoFile = licenseInfoService.getLicenseInfoFile(sw360Project, sw360User, outputGeneratorClassNameWithVariant, selectedReleaseAndAttachmentIds, excludedLicenses);
byte[] byteContent = licenseInfoFile.bufferForGeneratedOutput().array();
response.setContentType(outputFormatInfo.getMimeType());
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
import org.eclipse.sw360.datahandler.thrift.components.ECCStatus;
import org.eclipse.sw360.datahandler.thrift.components.EccInformation;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoFile;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatInfo;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatVariant;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectRelationship;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectType;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.rest.resourceserver.TestHelper;
import org.eclipse.sw360.rest.resourceserver.attachment.Sw360AttachmentService;
import org.eclipse.sw360.rest.resourceserver.licenseinfo.Sw360LicenseInfoService;
import org.eclipse.sw360.rest.resourceserver.project.Sw360ProjectService;
import org.eclipse.sw360.rest.resourceserver.release.Sw360ReleaseService;
import org.eclipse.sw360.rest.resourceserver.user.Sw360UserService;
Expand Down Expand Up @@ -51,6 +55,8 @@
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -76,6 +82,9 @@ public class ProjectSpecTest extends TestRestDocsSpecBase {
@MockBean
private Sw360AttachmentService attachmentServiceMock;

@MockBean
private Sw360LicenseInfoService licenseInfoMockService;

private Project project;
private Attachment attachment;

Expand Down Expand Up @@ -246,6 +255,14 @@ public void before() throws TException, IOException {
new User("[email protected]", "sw360").setId("123456789"));
given(this.userServiceMock.getUserByEmail("[email protected]")).willReturn(
new User("[email protected]", "sw360").setId("209582812"));
OutputFormatInfo outputFormatInfo = new OutputFormatInfo();
outputFormatInfo.setFileExtension("html");
given(this.licenseInfoMockService.getOutputFormatInfoForGeneratorClass(anyObject()))
.willReturn(outputFormatInfo);
LicenseInfoFile licenseInfoFile = new LicenseInfoFile();
licenseInfoFile.setGeneratedOutput(new byte[0]);
given(this.licenseInfoMockService.getLicenseInfoFile(anyObject(), anyObject(), anyObject(), anyObject(),
anyObject())).willReturn(licenseInfoFile);
}

@Test
Expand Down Expand Up @@ -595,4 +612,20 @@ public void should_document_link_releases() throws Exception {
.content(this.objectMapper.writeValueAsString(releaseIds))
.header("Authorization", "Bearer " + accessToken)).andExpect(status().isCreated());
}

@Test
public void should_document_get_download_license_info() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
this.mockMvc.perform(get("/api/projects/" + project.getId()+ "/licenseinfo?generatorClassName=XhtmlGenerator&variant=DISCLOSURE")
.header("Authorization", "Bearer " + accessToken)
.accept("application/xhtml+xml"))
.andExpect(status().isOk())
.andDo(this.documentationHandler
.document(requestParameters(
parameterWithName("generatorClassName")
.description("All possible values for output generator class names are "
+ Arrays.asList("DocxGenerator", "XhtmlGenerator", "TextGenerator")),
parameterWithName("variant").description("All the possible values for variants are "
+ Arrays.asList(OutputFormatVariant.values())))));
}
}

0 comments on commit c81a988

Please sign in to comment.