Skip to content

Commit

Permalink
Merge pull request #2007 from muhammadali9699/feat/api/splitComponents
Browse files Browse the repository at this point in the history
feat(REST): New endpoint split components.

Reviewed by: eldrin.sanctis@siemens.com
Tested by: kumar.nikesh@siemens.com
  • Loading branch information
ag4ums committed Jul 19, 2023
2 parents 882466d + 7e87c58 commit 50e4f07
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
16 changes: 16 additions & 0 deletions rest/resource-server/src/docs/asciidoc/components.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@ include::{snippets}/should_document_merge_components/http-response.adoc[]
===== Links
include::{snippets}/should_document_merge_components/links.adoc[]

[[resources-components-update]]
==== Split a component

A `PATCH` request is used to split two components

===== Response structure
include::{snippets}/should_document_split_components/response-fields.adoc[]

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

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

===== Links
include::{snippets}/should_document_split_components/links.adoc[]

[[resources-components-delete]]
==== Delete a component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,4 +734,20 @@ public ResponseEntity<RequestStatus> mergeComponents(

return new ResponseEntity<>(requestStatus, HttpStatus.OK);
}

@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = COMPONENTS_URL + "/splitComponents", method = RequestMethod.PATCH)
public ResponseEntity<RequestStatus> splitComponents(
@RequestBody Map<String, Component> componentMap) throws TException {

User sw360User = restControllerHelper.getSw360UserFromAuthentication();

Component srcComponent = componentMap.get("srcComponent");
Component targetComponent = componentMap.get("targetComponent");

// perform the real merge, update merge target and delete merge source
RequestStatus requestStatus = componentService.splitComponents(srcComponent, targetComponent, sw360User);

return new ResponseEntity<>(requestStatus, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,19 @@ public RequestStatus mergeComponents(String componentTargetId, String componentS
return requestStatus;
}

public RequestStatus splitComponents(Component srcComponent, Component targetComponent, User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
RequestStatus requestStatus;
requestStatus = sw360ComponentClient.splitComponent(srcComponent, targetComponent, sw360User);

if (requestStatus == RequestStatus.IN_USE) {
throw new HttpMessageNotReadableException("Component already in use.");
} else if (requestStatus == RequestStatus.FAILURE) {
throw new HttpMessageNotReadableException("Cannot split these components");
} else if (requestStatus == RequestStatus.ACCESS_DENIED) {
throw new RuntimeException("Access denied...!");
}

return requestStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public class ComponentSpecTest extends TestRestDocsSpecBase {
private Attachment sBOMAttachment;
private RequestSummary requestSummary = new RequestSummary();

private Release release;
private Release release2;

@Before
public void before() throws TException, IOException {
Set<String> licenseIds = new HashSet<>();
Expand Down Expand Up @@ -346,7 +349,7 @@ public void before() throws TException, IOException {
given(this.vendorServiceMock.getVendorById("vendorId")).willReturn(vendor);

List<Release> releaseList = new ArrayList<>();
Release release = new Release();
release = new Release();
release.setId("3765276512");
release.setName("Angular 2.3.0");
release.setCpeid("cpe:/a:Google:Angular:2.3.0:");
Expand All @@ -358,7 +361,7 @@ public void before() throws TException, IOException {
release.setComponentId(springComponent.getId());
releaseList.add(release);

Release release2 = new Release();
release2 = new Release();
release2.setId("3765276512");
release2.setName("Angular 2.3.1");
release2.setCpeid("cpe:/a:Google:Angular:2.3.1:");
Expand Down Expand Up @@ -946,6 +949,7 @@ public void should_document_merge_components() throws Exception {
fieldWithPath("blog").description("The blog of component"),
fieldWithPath("homepage").description("The homepage url of the component"),
fieldWithPath("modifiedOn").description("The date the component was modified"),

fieldWithPath("moderators").description("The component moderators"),

fieldWithPath("name").description("The updated name of the component"),
Expand All @@ -966,6 +970,60 @@ public void should_document_merge_components() throws Exception {
)));
}

@Test
public void should_document_split_components() throws Exception {


String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
Component srcComponent = new Component();
srcComponent.setId("17653524");
srcComponent.setName("Angular");
srcComponent.setComponentOwner("John");
srcComponent.setDescription("Angular is a development platform for building mobile and desktop web applications.");
List<Release> releaseList = new ArrayList<>();
releaseList.add(release);
Release release2 = new Release();
releaseList.add(release2);

srcComponent.setReleases(releaseList);
Component targetComponent = new Component();
targetComponent.setId("87654321");
targetComponent.setName("Angular");
targetComponent.setComponentOwner("John");
targetComponent.setDescription("Angular is a development platform for building mobile and desktop web applications.");
targetComponent.setReleases(releaseList);
Map<String, Object> componentsMap = new HashMap<>();
componentsMap.put("srcComponent", srcComponent);
componentsMap.put("targetComponent", targetComponent);

mockMvc.perform(patch("/api/components/splitComponents")
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(componentsMap))
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk())
.andDo(this.documentationHandler.document(
requestFields(
fieldWithPath("srcComponent.id").description("The ID of the source component"),
fieldWithPath("srcComponent.name").description("The name of the source component"),
fieldWithPath("srcComponent.description").description("The description of the source component"),
fieldWithPath("srcComponent.type").description("The type of the source component"),
fieldWithPath("srcComponent.componentOwner").description("The owner of the source component"),
fieldWithPath("srcComponent.visbility").description("The visibility of the source component"),
fieldWithPath("srcComponent.setVisbility").description("Flag indicating if the visibility is set"),
fieldWithPath("srcComponent.setBusinessUnit").description("Flag indicating if the business unit is set"),
fieldWithPath("targetComponent.id").description("The ID of the target component"),
fieldWithPath("targetComponent.name").description("The name of the target component"),
fieldWithPath("targetComponent.description").description("The description of the target component"),
fieldWithPath("targetComponent.type").description("The type of the target component"),
fieldWithPath("targetComponent.componentOwner").description("The owner of the target component"),
fieldWithPath("targetComponent.visbility").description("The visibility of the target component"),
fieldWithPath("targetComponent.setVisbility").description("Flag indicating if the visibility is set"),
fieldWithPath("targetComponent.setBusinessUnit").description("Flag indicating if the business unit is set")

)));
}

@Test
public void should_document_delete_components() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
Expand Down

0 comments on commit 50e4f07

Please sign in to comment.