Skip to content

Commit

Permalink
feat(rest): get releases used by 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 authored and GMishx committed Oct 22, 2024
1 parent 5702dc5 commit 00d70bc
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
16 changes: 16 additions & 0 deletions rest/resource-server/src/docs/asciidoc/vendors.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ include::{snippets}/should_document_get_vendor/http-response.adoc[]
===== Links
include::{snippets}/should_document_get_vendor/links.adoc[]

[[resources-vendor-get-releases]]
==== Get vendor releases

A `GET` request will list the releases used by the vendor.

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

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

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

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

[[resources-vendor-create]]
==== Creating a vendor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.logging.log4j.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
import org.eclipse.sw360.datahandler.permissions.PermissionUtils;
import org.apache.thrift.protocol.TSimpleJSONProtocol;
import org.apache.thrift.transport.TTransportException;
import org.eclipse.sw360.datahandler.common.CommonUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.datahandler.thrift.vendors.VendorService;
import org.eclipse.sw360.datahandler.thrift.components.ComponentService;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataIntegrityViolationException;
Expand All @@ -29,6 +31,7 @@

import java.nio.ByteBuffer;
import java.util.List;
import java.util.Set;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
Expand Down Expand Up @@ -154,6 +157,10 @@ private VendorService.Iface getThriftVendorClient() throws TTransportException {
return new ThriftClients().makeVendorClient();
}

public ComponentService.Iface getThriftComponentClient() throws TTransportException {
return new ThriftClients().makeComponentClient();
}

public ByteBuffer exportExcel() throws TException {
List<Vendor> vendors = getAllVendorList();
VendorService.Iface sw360VendorClient = getThriftVendorClient();
Expand All @@ -164,4 +171,10 @@ private List<Vendor> getAllVendorList() throws TException {
VendorService.Iface sw360VendorClient = getThriftVendorClient();
return sw360VendorClient.getAllVendors();
}

public Set<Release> getAllReleaseList(String vendorId) throws TException {
ComponentService.Iface componentsClient = getThriftComponentClient();
Set<Release> releases = componentsClient.getReleasesByVendorId(vendorId);
return releases;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.sw360.datahandler.resourcelists.PaginationResult;
import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException;
import org.eclipse.sw360.datahandler.common.SW360Constants;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.springframework.data.domain.Pageable;

import java.io.IOException;
Expand All @@ -45,6 +46,7 @@
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Set;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

Expand Down Expand Up @@ -110,6 +112,32 @@ public ResponseEntity<EntityModel<Vendor>> getVendor(
return new ResponseEntity<>(halResource, HttpStatus.OK);
}

@Operation(
summary = "Get the releases used by the vendor.",
description = "Get the releases by vendor id.",
tags = {"Vendor"}
)
@RequestMapping(value = VENDORS_URL + "/{id}/releases", method = RequestMethod.GET)
public ResponseEntity<CollectionModel<EntityModel<Release>>> getReleases(
@Parameter(description = "The id of the vendor to get.")
@PathVariable("id") String id
) throws TException{
try{
Set<Release> releases = vendorService.getAllReleaseList(id);
List<EntityModel<Release>> resources = new ArrayList<>();
releases.forEach(rel -> {
Release embeddedRelease = restControllerHelper.convertToEmbeddedRelease(rel);
resources.add(EntityModel.of(embeddedRelease));
});
CollectionModel<EntityModel<Release>> relResources = restControllerHelper.createResources(resources);

HttpStatus status = relResources == null ? HttpStatus.NO_CONTENT : HttpStatus.OK;
return new ResponseEntity<>(relResources, status);
} catch (TException e) {
throw new TException(e.getMessage());
}
}

@Operation(
summary = "Create a new vendor.",
description = "Create a new vendor.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.rest.resourceserver.TestHelper;
import org.eclipse.sw360.rest.resourceserver.vendor.Sw360VendorService;
import org.junit.Before;
Expand All @@ -23,10 +24,7 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -84,6 +82,23 @@ public void before() throws TException{
vendorList.add(vendor);
vendorList.add(vendor2);

Set<Release> releases = new HashSet<>();
Release release1 = new Release();
release1.setId("12345");
release1.setName("Release_1");
release1.setVersion("1.0.0");
release1.setVendor(vendor);

Release release2 = new Release();
release2.setId("123456");
release2.setName("Release_2");
release2.setVersion("2.0.0");
release2.setVendor(vendor);

releases.add(release1);
releases.add(release2);

given(this.vendorServiceMock.getAllReleaseList(eq(vendor.getId()))).willReturn(releases);
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);
Expand Down Expand Up @@ -143,6 +158,25 @@ public void should_document_get_vendor() throws Exception {
)));
}

@Test
public void should_document_get_vendor_releases() throws Exception {
mockMvc.perform(get("/api/vendors/" + vendor.getId() + "/releases")
.header("Authorization", TestHelper.generateAuthHeader(testUserId, testUserPassword))
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk())
.andDo(this.documentationHandler.document(
links(
linkWithRel("curies").description("Curies are used for online documentation")
),
responseFields(
subsectionWithPath("_embedded.sw360:releases.[]id").description("Id of the release"),
subsectionWithPath("_embedded.sw360:releases.[]name").description("The name of the release"),
subsectionWithPath("_embedded.sw360:releases.[]version").description("The version of the release"),
subsectionWithPath("_embedded.sw360:releases.[]_links").description("Self <<resources-index-links,Links>> to Release resource\""),
subsectionWithPath("_links").description("<<resources-index-links,Links>> to other resources")
)));
}


@Test
public void should_document_create_vendor() throws Exception {
Expand Down

0 comments on commit 00d70bc

Please sign in to comment.