Skip to content

Commit

Permalink
fix(rest): Added new endpoint for LicenseType 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 Oct 12, 2023
1 parent b58f28c commit 5541267
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
12 changes: 12 additions & 0 deletions rest/resource-server/src/docs/asciidoc/licenses.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,15 @@ include::{snippets}/should_document_upload_license/curl-request.adoc[]

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

[[add-license-type]]
==== create license type

A `POST` request help to create license type.

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
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.users.User;
import org.eclipse.sw360.rest.resourceserver.core.HalResource;
Expand Down Expand Up @@ -204,5 +205,13 @@ public ResponseEntity<?> uploadLicenses(@RequestParam("licenseFile") MultipartFi
throw new TException(e.getMessage());
}
return ResponseEntity.ok(Series.SUCCESSFUL);
}

@RequestMapping(value = LICENSES_URL + "/addLicenseType", method = RequestMethod.POST)
public ResponseEntity<RequestStatus> createLicenseType(String licenseType, HttpServletRequest request) throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus=licenseService.addLicenseType(sw360User, licenseType, request);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus,status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseService;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseType;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
import org.eclipse.sw360.exporter.LicsExporter;
Expand All @@ -34,6 +35,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.http.HttpRequest;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
Expand All @@ -60,6 +62,7 @@ public class Sw360LicenseService {
@Value("${sw360.thrift-server-url:http://localhost:8080}")
private String thriftServerUrl;
private static String CONTENT_TYPE = "application/zip";
private LicenseType lType;

public List<License> getLicenses() throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
Expand Down Expand Up @@ -213,11 +216,11 @@ private void copyDataStreamToResponse(HttpServletResponse response, ByteArrayInp
}

public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteIfExternalIdMatches, boolean overwriteIfIdMatchesEvenWithoutExternalIdMatch) throws IOException, TException {
final HashMap<String, InputStream> inputMap = new HashMap<>();
final HashMap<String, InputStream> inputMap = new HashMap<>();

if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
throw new HttpMessageNotReadableException("Unable to upload license file. User is not admin");
}
if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
throw new HttpMessageNotReadableException("Unable to upload license file. User is not admin");
}
try {
InputStream inputStream = file.getInputStream();
ZipTools.extractZipToInputStreamMap(inputStream, inputMap);
Expand All @@ -231,4 +234,19 @@ public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteI
}
}
}

public RequestStatus addLicenseType(User sw360User, String licenseType, HttpServletRequest request) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
lType.setLicenseType(licenseType);
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus status = sw360LicenseClient.addLicenseType(lType, sw360User);
} else {
throw new HttpMessageNotReadableException("Unable to create License Type. User is not admin");
}
} catch ( Exception e) {
throw new TException(e.getMessage());
}
return RequestStatus.SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseType;
import org.eclipse.sw360.rest.resourceserver.TestHelper;
import org.eclipse.sw360.rest.resourceserver.license.Sw360LicenseService;
import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
import org.eclipse.sw360.datahandler.thrift.licenses.ObligationLevel;
import org.eclipse.sw360.datahandler.thrift.licenses.ObligationType;
import org.eclipse.sw360.datahandler.thrift.Quadratic;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.rest.resourceserver.user.Sw360UserService;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -91,6 +93,12 @@ public void before() throws TException, IOException {
List<License> licenseList = new ArrayList<>();
licenseList.add(license);
licenseList.add(license2);

LicenseType licensetype = new LicenseType();
licensetype.setId("1234");
licensetype.setLicenseType("wer");
licensetype.setLicenseTypeId(123);
licensetype.setType("xyz");

given(this.licenseServiceMock.getLicenses()).willReturn(licenseList);
given(this.licenseServiceMock.getLicenseById(eq(license.getId()))).willReturn(license);
Expand All @@ -99,6 +107,7 @@ public void before() throws TException, IOException {
Mockito.doNothing().when(licenseServiceMock).importSpdxInformation(any());
Mockito.doNothing().when(licenseServiceMock).getDownloadLicenseArchive(any(), any(), any());
Mockito.doNothing().when(licenseServiceMock).uploadLicense(any(), any(), anyBoolean(), anyBoolean());
given(this.licenseServiceMock.addLicenseType(any(),any() , any())).willReturn(RequestStatus.SUCCESS);
obligation1 = new Obligation();
obligation1.setId("0001");
obligation1.setTitle("Obligation 1");
Expand Down Expand Up @@ -249,4 +258,13 @@ public void should_document_upload_license() throws Exception {
.header("Authorization", "Bearer " + accessToken);
this.mockMvc.perform(builder).andExpect(status().isOk());
}

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

0 comments on commit 5541267

Please sign in to comment.