Skip to content

Commit

Permalink
[MODINVSTOR-1243] Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Aug 26, 2024
1 parent 7106030 commit 65f277a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/folio/inventory/resources/Items.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ private OkapiHttpClient createHttpClient(
exception.toString())));
}

protected CollectionResourceClient createItemsStorageClient(
private CollectionResourceClient createItemsStorageClient(
OkapiHttpClient client,
WebContext context)
throws MalformedURLException {
Expand Down Expand Up @@ -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()));
}
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/org/folio/inventory/resources/TenantItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -102,6 +103,10 @@ private JsonObject constructResponse(List<JsonObject> 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),
Expand All @@ -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()));
}

}
104 changes: 104 additions & 0 deletions src/test/java/api/items/TenantItemApiTests.java
Original file line number Diff line number Diff line change
@@ -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<JsonObject> 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<JsonObject> 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<String, String> itemToTenantIds) {
return new TenantItemPairCollection()
.withItemTenantPairs(itemToTenantIds.entrySet().stream()
.map(pair -> new TenantItemPair().withItemId(pair.getKey()).withTenantId(pair.getValue()))
.toList());
}

}

0 comments on commit 65f277a

Please sign in to comment.