From 65f277ae1b24db9ae97a07f37b68576b6784575f Mon Sep 17 00:00:00 2001 From: saba_zedginidze Date: Mon, 26 Aug 2024 16:31:08 +0400 Subject: [PATCH] [MODINVSTOR-1243] Add integration test --- .../org/folio/inventory/resources/Items.java | 4 +- .../inventory/resources/TenantItems.java | 20 +++- .../java/api/items/TenantItemApiTests.java | 104 ++++++++++++++++++ 3 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 src/test/java/api/items/TenantItemApiTests.java diff --git a/src/main/java/org/folio/inventory/resources/Items.java b/src/main/java/org/folio/inventory/resources/Items.java index a3f19a79c..17c1ac4a4 100644 --- a/src/main/java/org/folio/inventory/resources/Items.java +++ b/src/main/java/org/folio/inventory/resources/Items.java @@ -508,7 +508,7 @@ private OkapiHttpClient createHttpClient( exception.toString()))); } - protected CollectionResourceClient createItemsStorageClient( + private CollectionResourceClient createItemsStorageClient( OkapiHttpClient client, WebContext context) throws MalformedURLException { @@ -718,7 +718,7 @@ private void respondWithItemRepresentation ( }); } - protected void invalidOkapiUrlResponse(RoutingContext routingContext, WebContext context) { + private void invalidOkapiUrlResponse(RoutingContext routingContext, WebContext context) { ServerErrorResponse.internalError(routingContext.response(), String.format("Invalid Okapi URL: %s", context.getOkapiLocation())); } diff --git a/src/main/java/org/folio/inventory/resources/TenantItems.java b/src/main/java/org/folio/inventory/resources/TenantItems.java index f129d9cdb..abafbbf0e 100644 --- a/src/main/java/org/folio/inventory/resources/TenantItems.java +++ b/src/main/java/org/folio/inventory/resources/TenantItems.java @@ -9,6 +9,7 @@ import java.lang.invoke.MethodHandles; import java.net.MalformedURLException; import java.net.URI; +import java.net.URL; import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; @@ -33,15 +34,15 @@ import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.client.WebClient; -public class TenantItems extends Items { +public class TenantItems extends AbstractInventoryResource { private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass()); - private static final String TENANT_ITEMS_PATH = "/inventory/tenant-items"; - private static final String ITEMS_FIELD = "items"; - private static final String TOTAL_RECORDS_FIELD = "items"; + public static final String TENANT_ITEMS_PATH = "/inventory/tenant-items"; + public static final String ITEMS_FIELD = "items"; + public static final String TOTAL_RECORDS_FIELD = "totalRecords"; - public TenantItems(final Storage storage, final HttpClient client) { + public TenantItems(Storage storage, HttpClient client) { super(storage, client); } @@ -102,6 +103,10 @@ private JsonObject constructResponse(List items) { ); } + private CollectionResourceClient createItemsStorageClient(OkapiHttpClient client, WebContext context) throws MalformedURLException { + return new CollectionResourceClient(client, new URL(context.getOkapiLocation() + "/item-storage/items")); + } + private OkapiHttpClient createHttpClient(String tenantId, WebContext context, RoutingContext routingContext) throws MalformedURLException { return new OkapiHttpClient(WebClient.wrap(client), @@ -114,4 +119,9 @@ private OkapiHttpClient createHttpClient(String tenantId, WebContext context, format("Failed to contact storage module: %s", exception.toString()))); } + private void invalidOkapiUrlResponse(RoutingContext routingContext, WebContext context) { + ServerErrorResponse.internalError(routingContext.response(), + String.format("Invalid Okapi URL: %s", context.getOkapiLocation())); + } + } diff --git a/src/test/java/api/items/TenantItemApiTests.java b/src/test/java/api/items/TenantItemApiTests.java new file mode 100644 index 000000000..6c2ff9d0e --- /dev/null +++ b/src/test/java/api/items/TenantItemApiTests.java @@ -0,0 +1,104 @@ +package api.items; + +import static api.ApiTestSuite.COLLEGE_TENANT_ID; +import static api.ApiTestSuite.CONSORTIA_TENANT_ID; +import static api.support.InstanceSamples.smallAngryPlanet; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.assertj.core.api.Assertions.assertThat; +import static org.folio.inventory.resources.TenantItems.ITEMS_FIELD; +import static org.folio.inventory.resources.TenantItems.TENANT_ITEMS_PATH; +import static org.folio.inventory.resources.TenantItems.TOTAL_RECORDS_FIELD; +import static org.folio.inventory.support.ItemUtil.ID; + +import java.net.MalformedURLException; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.folio.TenantItemPair; +import org.folio.TenantItemPairCollection; +import org.folio.inventory.support.JsonArrayHelper; +import org.folio.inventory.support.http.client.OkapiHttpClient; +import org.folio.inventory.support.http.client.Response; +import org.junit.Test; +import org.junit.runner.RunWith; + +import api.support.ApiRoot; +import api.support.ApiTests; +import api.support.InstanceApiClient; +import api.support.builders.HoldingRequestBuilder; +import api.support.builders.ItemRequestBuilder; +import api.support.http.ResourceClient; +import io.vertx.core.json.JsonObject; +import junitparams.JUnitParamsRunner; + +@RunWith(JUnitParamsRunner.class) +public class TenantItemApiTests extends ApiTests { + + @Test + public void testTenantItemsGetFromDifferentTenants() throws MalformedURLException, + ExecutionException, InterruptedException, TimeoutException { + + createConsortiumInstanceHoldingItem(); + createCollegeInstanceHoldingItem(); + + var consortiumItem = getItems(consortiumOkapiClient, 1).get(0); + var collegeItem = getItems(collegeOkapiClient, 1).get(0); + + var tenantItemPariCollection = constructTenantItemPairCollection(Map.of( + CONSORTIA_TENANT_ID, consortiumItem.getString(ID), + COLLEGE_TENANT_ID, collegeItem.getString(ID) + )); + var response = okapiClient.post(TENANT_ITEMS_PATH, JsonObject.mapFrom(tenantItemPariCollection)) + .toCompletableFuture().get(5, TimeUnit.SECONDS); + assertThat(response.getStatusCode()).isEqualTo(200); + var items = extractItems(response, 2); + + assertThat(items).contains(consortiumItem, collegeItem); + } + + private void createConsortiumInstanceHoldingItem() { + createInstanceHoldingItem(consortiumItemsClient, consortiumHoldingsStorageClient, consortiumOkapiClient); + } + + private void createCollegeInstanceHoldingItem() { + createInstanceHoldingItem(collegeItemsClient, collegeHoldingsStorageClient, collegeOkapiClient); + } + + private void createInstanceHoldingItem(ResourceClient itemStorageClient, ResourceClient holdingsStorageClient, OkapiHttpClient okapiHttpClient) { + var instanceId = UUID.randomUUID(); + InstanceApiClient.createInstance(okapiHttpClient, smallAngryPlanet(instanceId)); + var holdingId = holdingsStorageClient.create(new HoldingRequestBuilder() + .forInstance(instanceId)).getId(); + itemStorageClient.create(new ItemRequestBuilder().forHolding(holdingId) + .book().canCirculate().withBarcode(String.valueOf(Math.random() * 100))); + } + + private List getItems(OkapiHttpClient okapiHttpClient, int expected) + throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException { + + var itemsResponse = okapiHttpClient.get(ApiRoot.items()).toCompletableFuture().get(5, SECONDS); + assertThat(itemsResponse.getStatusCode()).isEqualTo(200); + + return extractItems(itemsResponse, expected); + } + + private List extractItems(Response itemsResponse, int expected) { + var itemsCollection = itemsResponse.getJson(); + var items = JsonArrayHelper.toList(itemsCollection.getJsonArray(ITEMS_FIELD)); + assertThat(items).hasSize(expected); + assertThat(itemsCollection.getInteger(TOTAL_RECORDS_FIELD)).isEqualTo(expected); + return items; + } + + private TenantItemPairCollection constructTenantItemPairCollection(Map itemToTenantIds) { + return new TenantItemPairCollection() + .withItemTenantPairs(itemToTenantIds.entrySet().stream() + .map(pair -> new TenantItemPair().withItemId(pair.getKey()).withTenantId(pair.getValue())) + .toList()); + } + +}