Skip to content

Commit

Permalink
feat(rest): endpoint to update a vendor.
Browse files Browse the repository at this point in the history
Signed-off-by: Rudra Chopra <prabhuchopra@gmail.com>
  • Loading branch information
rudra-superrr committed Sep 10, 2024
1 parent 159ad6a commit 26d4021
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
11 changes: 11 additions & 0 deletions rest/resource-server/src/docs/asciidoc/vendors.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ include::{snippets}/should_document_create_vendor/http-response.adoc[]
===== Links
include::{snippets}/should_document_create_vendor/links.adoc[]

[[resources-vendor-update]]
==== Updating a vendor

A `PATCH` request is used to update a vendor.

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

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

[[resources-vendor-export]]
==== Export vendor list

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ public void updateVendor(Vendor vendor, User sw360User) {
}
}

public RequestStatus vendorUpdate(Vendor vendor, User sw360User, String id) {
try {
VendorService.Iface sw360VendorClient = getThriftVendorClient();
Vendor existingVendor = sw360VendorClient.getByID(id);
if (existingVendor != null) {
if (vendor.getShortname() != null) {
existingVendor.setShortname(vendor.getShortname());
}
if (vendor.getFullname() != null) {
existingVendor.setFullname(vendor.getFullname());
}
if (vendor.getUrl() != null) {
existingVendor.setUrl(vendor.getUrl());
}
}
RequestStatus requestStatus = sw360VendorClient.updateVendor(existingVendor, sw360User);
return requestStatus;
} catch (TException e) {
throw new RuntimeException(e);
}
}

public void deleteVendor(Vendor vendor, User sw360User) {
try {
VendorService.Iface sw360VendorClient = getThriftVendorClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import lombok.RequiredArgsConstructor;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.common.SW360Utils;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.rest.resourceserver.core.HalResource;
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper;
Expand Down Expand Up @@ -129,6 +131,30 @@ public ResponseEntity<?> createVendor(
return ResponseEntity.created(location).body(halResource);
}

@Operation(
summary = "Update a vendor.",
description = "Update a vendor.",
tags = {"Vendor"}
)
@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = VENDORS_URL + "/{id}", method = RequestMethod.PATCH)
public ResponseEntity<?> updateVendor(
@Parameter(description = "The id of the vendor")
@PathVariable("id") String id,
@Parameter(description = "The vendor to be updated.")
@RequestBody Vendor vendor
) {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus status = vendorService.vendorUpdate(vendor, sw360User, id);
if (RequestStatus.SUCCESS.equals(status)) {
return new ResponseEntity<>("Vendor updated successfully", HttpStatus.OK);
} else if (RequestStatus.DUPLICATE.equals(status)) {
return new ResponseEntity<>("A Vendor with same fullname '" + vendor.getFullname() + "' already exists!", HttpStatus.CONFLICT);
} else {
return new ResponseEntity<>("sw360 vendor with id '" + id + " cannot be updated.", HttpStatus.CONFLICT);
}
}

@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(linkTo(VendorController.class).slash("api" + VENDORS_URL).withRel("vendors"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.eclipse.sw360.rest.resourceserver.restdocs;

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.rest.resourceserver.TestHelper;
import org.eclipse.sw360.rest.resourceserver.vendor.Sw360VendorService;
Expand Down Expand Up @@ -40,6 +41,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.queryParameters;

Expand All @@ -56,6 +58,7 @@ public class VendorSpecTest extends TestRestDocsSpecBase {
private Sw360VendorService vendorServiceMock;

private Vendor vendor;
private Vendor vendor3;

@Before
public void before() throws TException{
Expand All @@ -71,11 +74,18 @@ public void before() throws TException{
vendor2.setShortname("Pivotal");
vendor2.setUrl("https://pivotal.io/");

vendor3 = new Vendor();
vendor3.setId("987567468");
vendor3.setFullname("AMazon Ltd");
vendor3.setShortname("AMazon");
vendor3.setUrl("https://AMazon.io/");

List<Vendor> vendorList = new ArrayList<>();
vendorList.add(vendor);
vendorList.add(vendor2);

given(this.vendorServiceMock.getVendors()).willReturn(vendorList);
given(this.vendorServiceMock.vendorUpdate(any(), any(), any())).willReturn(RequestStatus.SUCCESS);
given(this.vendorServiceMock.getVendorById(eq(vendor.getId()))).willReturn(vendor);
given(this.vendorServiceMock.exportExcel()).willReturn(ByteBuffer.allocate(10000));

Expand Down Expand Up @@ -162,6 +172,20 @@ public void should_document_create_vendor() throws Exception {
)));
}

@Test
public void should_document_update_vendor() throws Exception {
Map<String, Object> updateVendor = new HashMap<>();
updateVendor.put("fullName", "Amazon Ltd");
updateVendor.put("shortName", "Amazon");
updateVendor.put("url", "https://Amazon.io/");
mockMvc.perform(patch("/api/vendors/" + vendor3.getId())
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(updateVendor))
.header("Authorization", TestHelper.generateAuthHeader(testUserId, testUserPassword))
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

@Test
public void should_document_get_export_vendor() throws Exception{
mockMvc.perform(get("/api/vendors/exportVendorDetails")
Expand Down

0 comments on commit 26d4021

Please sign in to comment.