Skip to content

Commit 8c8359b

Browse files
committed
feat(rest): endpoint to merge vendor.
Signed-off-by: Rudra Chopra <prabhuchopra@gmail.com>
1 parent c1fbd1b commit 8c8359b

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

rest/resource-server/src/docs/asciidoc/vendors.adoc

+14
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,17 @@ include::{snippets}/should_document_get_export_vendor/curl-request.adoc[]
118118

119119
===== Example response
120120
include::{snippets}/should_document_get_export_vendor/http-response.adoc[]
121+
122+
[[resources-vendor-merge]]
123+
==== Merge vendors
124+
125+
A `PATCH` request will merge the source vendor with the target vendor.
126+
127+
===== Request structure
128+
include::{snippets}/should_document_merge_vendor/request-fields.adoc[]
129+
130+
===== Example request
131+
include::{snippets}/should_document_merge_vendor/curl-request.adoc[]
132+
133+
===== Example response
134+
include::{snippets}/should_document_merge_vendor/http-response.adoc[]

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/vendor/Sw360VendorService.java

+18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.thrift.TException;
1515
import org.apache.thrift.transport.TTransportException;
1616
import org.eclipse.sw360.datahandler.common.CommonUtils;
17+
import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException;
1718
import org.eclipse.sw360.datahandler.thrift.AddDocumentRequestStatus;
1819
import org.eclipse.sw360.datahandler.thrift.AddDocumentRequestSummary;
1920
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
@@ -27,6 +28,7 @@
2728
import org.springframework.beans.factory.annotation.Value;
2829
import org.springframework.dao.DataIntegrityViolationException;
2930
import org.springframework.http.converter.HttpMessageNotReadableException;
31+
import org.springframework.security.access.AccessDeniedException;
3032
import org.springframework.stereotype.Service;
3133

3234
import java.nio.ByteBuffer;
@@ -186,4 +188,20 @@ public Set<Release> getAllReleaseList(String vendorId) throws TException {
186188
Set<Release> releases = componentsClient.getReleasesByVendorId(vendorId);
187189
return releases;
188190
}
191+
192+
public RequestStatus mergeVendors(String vendorTargetId, String vendorSourceId, Vendor vendorSelection, User user) throws TException, ResourceClassNotFoundException {
193+
VendorService.Iface sw360VendorClient = getThriftVendorClient();
194+
RequestStatus requestStatus;
195+
requestStatus = sw360VendorClient.mergeVendors(vendorTargetId, vendorSourceId, vendorSelection, user);
196+
197+
if (requestStatus == RequestStatus.IN_USE) {
198+
throw new HttpMessageNotReadableException("Vendor already in use as source or target vendor has an open MR");
199+
} else if (requestStatus == RequestStatus.FAILURE) {
200+
throw new ResourceClassNotFoundException("Internal server error while merging the vendors");
201+
} else if (requestStatus == RequestStatus.ACCESS_DENIED) {
202+
throw new AccessDeniedException("Access denied");
203+
}
204+
205+
return requestStatus;
206+
}
189207
}

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/vendor/VendorController.java

+21
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ public ResponseEntity<?> exportVendor(HttpServletResponse response) throws TExce
274274
return ResponseEntity.ok(HttpStatus.OK);
275275
}
276276

277+
@PreAuthorize("hasAuthority('WRITE')")
278+
@Operation(
279+
summary = "Merge two vendors.",
280+
description = "Merge source vendor into target vendor.",
281+
tags = {"Vendor"}
282+
)
283+
@RequestMapping(value = VENDORS_URL + "/mergeVendors", method = RequestMethod.PATCH)
284+
public ResponseEntity<RequestStatus> mergeVendors(
285+
@Parameter(description = "The id of the merge target vendor.")
286+
@RequestParam(value = "mergeTargetId", required = true) String mergeTargetId,
287+
@Parameter(description = "The id of the merge source vendor.")
288+
@RequestParam(value = "mergeSourceId", required = true) String mergeSourceId,
289+
@Parameter(description = "The merge selection.")
290+
@RequestBody Vendor mergeSelection
291+
) throws TException, ResourceClassNotFoundException {
292+
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
293+
// perform the real merge, update merge target and delete merge sources
294+
RequestStatus requestStatus = vendorService.mergeVendors(mergeTargetId, mergeSourceId, mergeSelection, sw360User);
295+
return new ResponseEntity<>(requestStatus, HttpStatus.OK);
296+
}
297+
277298
private void copyDataStreamToResponse(HttpServletResponse response, ByteBuffer buffer) throws IOException {
278299
FileCopyUtils.copy(buffer.array(), response.getOutputStream());
279300
}

rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/VendorSpecTest.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.eclipse.sw360.rest.resourceserver.restdocs;
1111

1212
import org.apache.thrift.TException;
13+
import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException;
1314
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
1415
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
1516
import org.eclipse.sw360.datahandler.thrift.components.Release;
@@ -36,11 +37,10 @@
3637
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
3738
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
3839
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
40+
import static org.springframework.restdocs.request.RequestDocumentation.*;
3941
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
4042
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
4143
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
42-
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
43-
import static org.springframework.restdocs.request.RequestDocumentation.queryParameters;
4444

4545
@RunWith(SpringJUnit4ClassRunner.class)
4646
public class VendorSpecTest extends TestRestDocsSpecBase {
@@ -58,7 +58,7 @@ public class VendorSpecTest extends TestRestDocsSpecBase {
5858
private Vendor vendor3;
5959

6060
@Before
61-
public void before() throws TException{
61+
public void before() throws TException, ResourceClassNotFoundException {
6262
vendor = new Vendor();
6363
vendor.setId("876876776");
6464
vendor.setFullname("Google Inc.");
@@ -99,6 +99,7 @@ public void before() throws TException{
9999

100100
given(this.vendorServiceMock.getAllReleaseList(eq(vendor.getId()))).willReturn(releases);
101101
given(this.vendorServiceMock.getVendors()).willReturn(vendorList);
102+
given(this.vendorServiceMock.mergeVendors(eq(vendor.getId()),eq(vendor2.getId()), any(), any())).willReturn(RequestStatus.SUCCESS);
102103
given(this.vendorServiceMock.vendorUpdate(any(), any(), any())).willReturn(RequestStatus.SUCCESS);
103104
given(this.vendorServiceMock.getVendorById(eq(vendor.getId()))).willReturn(vendor);
104105
given(this.vendorServiceMock.deleteVendorByid(any(), any())).willReturn(RequestStatus.SUCCESS);
@@ -235,4 +236,27 @@ public void should_document_get_export_vendor() throws Exception{
235236
.andExpect(status().isOk())
236237
.andDo(this.documentationHandler.document());
237238
}
239+
240+
@Test
241+
public void should_document_merge_vendor() throws Exception{
242+
mockMvc.perform(patch("/api/vendors/mergeVendors")
243+
.contentType(MediaTypes.HAL_JSON)
244+
.content(this.objectMapper.writeValueAsString(vendor))
245+
.header("Authorization", TestHelper.generateAuthHeader(testUserId, testUserPassword))
246+
.param("mergeTargetId", "87654321")
247+
.param("mergeSourceId", "17653524")
248+
.accept(MediaTypes.HAL_JSON))
249+
.andExpect(status().isOk())
250+
.andDo(this.documentationHandler.document(
251+
queryParameters(
252+
parameterWithName("mergeSourceId").description("Id of a source vendor to merge"),
253+
parameterWithName("mergeTargetId").description("Id of a target vendor to merge")
254+
),
255+
requestFields(
256+
fieldWithPath("fullName").description("The full name of the vendor"),
257+
fieldWithPath("shortName").description("The short name of the vendor"),
258+
fieldWithPath("url").description("The vendor's home page URL"),
259+
fieldWithPath("type").description("The type of document")
260+
)));
261+
}
238262
}

0 commit comments

Comments
 (0)