Skip to content

Commit

Permalink
feat(rest): delete a vendor by id.
Browse files Browse the repository at this point in the history
Signed-off-by: Rudra Chopra <[email protected]>
rudra-superrr authored and GMishx committed Nov 8, 2024
1 parent 453eff7 commit fa17c2f
Showing 4 changed files with 54 additions and 4 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
@@ -69,6 +69,17 @@ include::{snippets}/should_document_get_vendor_releases/http-response.adoc[]
===== Links
include::{snippets}/should_document_get_vendor_releases/links.adoc[]

[[resources-vendor-delete]]
==== Delete a vendor

A `DELETE` request will delete vendor by id.

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

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

[[resources-vendor-create]]
==== Creating a vendor

Original file line number Diff line number Diff line change
@@ -141,6 +141,15 @@ public void deleteVendor(Vendor vendor, User sw360User) {
}
}

public RequestStatus deleteVendorByid(String vendorId, User sw360User) {
try {
VendorService.Iface sw360VendorClient = getThriftVendorClient();
RequestStatus requestStatus = sw360VendorClient.deleteVendor(vendorId, sw360User);
return requestStatus;
} catch (TException e) {
throw new RuntimeException(e);
}
}
public void deleteAllVendors(User sw360User) {
try {
VendorService.Iface sw360VendorClient = getThriftVendorClient();
Original file line number Diff line number Diff line change
@@ -124,8 +124,8 @@ public ResponseEntity<EntityModel<Vendor>> getVendor(
public ResponseEntity<CollectionModel<EntityModel<Release>>> getReleases(
@Parameter(description = "The id of the vendor to get.")
@PathVariable("id") String id
) throws TException{
try{
) throws TException {
try {
Set<Release> releases = vendorService.getAllReleaseList(id);
List<EntityModel<Release>> resources = new ArrayList<>();
releases.forEach(rel -> {
@@ -141,6 +141,29 @@ public ResponseEntity<CollectionModel<EntityModel<Release>>> getReleases(
}
}

@Operation(
summary = "Delete a vendor.",
description = "Delete vendor by id.",
tags = {"Vendor"}
)
@RequestMapping(value = VENDORS_URL + "/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteVendor(
@Parameter(description = "The id of the vendor to be deleted.")
@PathVariable("id") String id
) {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
Vendor sw360Vendor = vendorService.getVendorById(id);
if (sw360Vendor == null) {
return new ResponseEntity<>("Vendor with id " + id + " not found.", HttpStatus.NOT_FOUND);
}
RequestStatus requestStatus = vendorService.deleteVendorByid(id, sw360User);
if (requestStatus == RequestStatus.SUCCESS) {
return new ResponseEntity<>("Vendor with full name " + sw360Vendor.getFullname() + " deleted successfully.", HttpStatus.OK);
} else {
return new ResponseEntity<>("Vendor with full name " + sw360Vendor.getFullname() + " cannot be deleted.", HttpStatus.BAD_REQUEST);
}
}

@Operation(
summary = "Create a new vendor.",
description = "Create a new vendor.",
Original file line number Diff line number Diff line change
@@ -36,8 +36,7 @@
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
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.request.MockMvcRequestBuilders.*;
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;
@@ -102,6 +101,7 @@ public void before() throws TException{
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.deleteVendorByid(any(), any())).willReturn(RequestStatus.SUCCESS);
given(this.vendorServiceMock.exportExcel()).willReturn(ByteBuffer.allocate(10000));

when(this.vendorServiceMock.createVendor(any())).then(invocation ->
@@ -177,6 +177,13 @@ public void should_document_get_vendor_releases() throws Exception {
)));
}

@Test
public void should_document_delete_vendor() throws Exception {
mockMvc.perform(delete("/api/vendors/" + vendor.getId())
.header("Authorization", TestHelper.generateAuthHeader(testUserId, testUserPassword))
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

@Test
public void should_document_create_vendor() throws Exception {

0 comments on commit fa17c2f

Please sign in to comment.