-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): Create new endpoint to download component template in csv…
… format. Signed-off-by: Nikesh kumar <[email protected]>
- Loading branch information
Nikesh kumar
committed
Jan 19, 2024
1 parent
ddfbe48
commit 043523c
Showing
6 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Copyright Siemens AG, 2024. Part of the SW360 Portal Project. | ||
// | ||
// This program and the accompanying materials are made | ||
// available under the terms of the Eclipse Public License 2.0 | ||
// which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 | ||
// | ||
|
||
[[resources-importExport]] | ||
=== ImportExport | ||
|
||
The ImportExport resource is used to get and upload request. | ||
|
||
[[download-import-export]] | ||
==== Download component template. | ||
|
||
A `GET` request help to download the component template in csv format. | ||
|
||
===== Example request | ||
include::{snippets}/should_document_get_download_component_template/curl-request.adoc[] | ||
|
||
===== Example response | ||
include::{snippets}/should_document_get_download_component_template/http-response.adoc[] |
75 changes: 75 additions & 0 deletions
75
.../main/java/org/eclipse/sw360/rest/resourceserver/importexport/ImportExportController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright Siemens AG, 2024-2025. | ||
* Part of the SW360 Portal Project. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.sw360.rest.resourceserver.importexport; | ||
|
||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.thrift.TException; | ||
import org.eclipse.sw360.datahandler.thrift.users.User; | ||
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.rest.webmvc.BasePathAwareController; | ||
import org.springframework.data.rest.webmvc.RepositoryLinksResource; | ||
import org.springframework.hateoas.server.RepresentationModelProcessor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@BasePathAwareController | ||
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) | ||
@RestController | ||
@SecurityRequirement(name = "tokenAuth") | ||
public class ImportExportController implements RepresentationModelProcessor<RepositoryLinksResource> { | ||
|
||
public static final String IMPORTEXPORT_URL = "/importExport"; | ||
|
||
@NonNull | ||
private final RestControllerHelper restControllerHelper; | ||
|
||
@NonNull | ||
private final Sw360ImportExportService importExportService; | ||
|
||
@Override | ||
public RepositoryLinksResource process(RepositoryLinksResource resource) { | ||
resource.add(linkTo(ImportExportController.class).slash("api/importExport").withRel("importExport")); | ||
return resource; | ||
} | ||
|
||
@Operation( | ||
summary = "Download csv component template.", | ||
description = "Download csv component template.", | ||
tags = {"ImportExport"}, | ||
parameters = { | ||
@Parameter(name = "Accept", in = ParameterIn.HEADER, required = true, example = "application/zip"), | ||
} | ||
) | ||
@PreAuthorize("hasAuthority('WRITE')") | ||
@RequestMapping(value = IMPORTEXPORT_URL + "/downloadComponentTemplate", method = RequestMethod.GET) | ||
public void downloadLicenseArchive( HttpServletRequest request, HttpServletResponse response ) throws TException, IOException { | ||
User sw360User = restControllerHelper.getSw360UserFromAuthentication(); | ||
importExportService.getDownloadCsvComponentTemplate(sw360User,request,response); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...ain/java/org/eclipse/sw360/rest/resourceserver/importexport/Sw360ImportExportService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright Siemens AG, 2024-2025. | ||
* Part of the SW360 Portal Project. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.sw360.rest.resourceserver.importexport; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.thrift.TException; | ||
import org.eclipse.sw360.datahandler.common.SW360Utils; | ||
import org.eclipse.sw360.datahandler.permissions.PermissionUtils; | ||
import org.eclipse.sw360.datahandler.thrift.SW360Exception; | ||
import org.eclipse.sw360.datahandler.thrift.users.User; | ||
import org.eclipse.sw360.datahandler.thrift.users.UserGroup; | ||
import org.eclipse.sw360.exporter.CSVExport; | ||
import org.eclipse.sw360.importer.ComponentCSVRecord; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.rest.webmvc.ResourceNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.FileCopyUtils; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) | ||
public class Sw360ImportExportService { | ||
public void getDownloadCsvComponentTemplate(User sw360User, HttpServletRequest request, | ||
HttpServletResponse response) throws TException, IOException { | ||
String fileConstant="ComponentsReleasesVendorsSample.csv"; | ||
if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) { | ||
throw new RuntimeException("Unable to download CSV component template. User is not admin"); | ||
} | ||
final Iterable<Iterable<String>> inputIterable = ImmutableList.of(ComponentCSVRecord.getSampleInputIterable()); | ||
final Iterable<String> csvHeaderIterable = ComponentCSVRecord.getCSVHeaderIterable(); | ||
|
||
ByteArrayInputStream byteArrayInputStream = CSVExport.createCSV(csvHeaderIterable, inputIterable); | ||
String filename = String.format(fileConstant, SW360Utils.getCreatedOn()); | ||
response.setHeader("Content-Disposition", String.format("Components; filename=\"%s\"", filename)); | ||
FileCopyUtils.copy(byteArrayInputStream, response.getOutputStream()); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ImportExportSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Siemens AG, 2024-2025. | ||
* Part of the SW360 Portal Project. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.sw360.rest.resourceserver.restdocs; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.thrift.TException; | ||
import org.eclipse.sw360.rest.resourceserver.TestHelper; | ||
import org.eclipse.sw360.datahandler.thrift.users.User; | ||
import org.eclipse.sw360.rest.resourceserver.importexport.Sw360ImportExportService; | ||
import org.eclipse.sw360.rest.resourceserver.user.Sw360UserService; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.hateoas.MediaTypes; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
public class ImportExportSpec extends TestRestDocsSpecBase { | ||
|
||
@Value("${sw360.test-user-id}") | ||
private String testUserId; | ||
|
||
@Value("${sw360.test-user-password}") | ||
private String testUserPassword; | ||
|
||
@MockBean | ||
private Sw360UserService userServiceMock; | ||
|
||
@MockBean | ||
private Sw360ImportExportService importExportService; | ||
|
||
@Before | ||
@SuppressWarnings("unchecked") | ||
public void before() throws TException, IOException { | ||
User sw360User = new User(); | ||
sw360User.setId("123456789"); | ||
sw360User.setEmail("[email protected]"); | ||
sw360User.setFullname("John Doe"); | ||
given(this.userServiceMock.getUserByEmailOrExternalId("[email protected]")).willReturn(sw360User); | ||
Mockito.doNothing().when(importExportService).getDownloadCsvComponentTemplate(any(), any(), any()); | ||
|
||
} | ||
|
||
@Test | ||
public void should_document_get_download_component_template() throws Exception { | ||
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword); | ||
mockMvc.perform(get("/api/importExport/downloadComponentTemplate") | ||
.header("Authorization", "Bearer " + accessToken) | ||
.accept(MediaTypes.HAL_JSON)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
} |