Skip to content

Commit

Permalink
[MODINV-1070] Modify TenantItems response schema (#759)
Browse files Browse the repository at this point in the history
* [MODINV-1070] Modify TenantItems response schema

* [MODINV-1070] Fix failing test

* [MODINV-1070] Remove unused import

* [MODINV-1070] Remove unused import
  • Loading branch information
Saba-Zedginidze-EPAM authored Aug 29, 2024
1 parent 6d09dae commit e450f3f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@
<path>${basedir}/ramls/instance-ingress-event.json</path>
<path>${basedir}/ramls/tenantItemPair.json</path>
<path>${basedir}/ramls/tenantItemPairCollection.json</path>
<path>${basedir}/ramls/tenantItemResponse.json</path>
</sourcePaths>
<targetPackage>org.folio</targetPackage>
<generateBuilders>true</generateBuilders>
Expand Down
20 changes: 20 additions & 0 deletions ramls/tenantItemResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Collection of pairs of item and tenant IDs",
"type": "object",
"properties": {
"tenantItems": {
"type": "array",
"description": "Items with corresponding tenantIds",
"items": {
"type": "object",
"additionalProperties": true
}
},
"totalRecords": {
"type": "integer"
}
},
"additionalProperties": false,
"required": ["item", "totalRecords"]
}
32 changes: 15 additions & 17 deletions src/main/java/org/folio/inventory/resources/TenantItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.TenantItem;
import org.folio.TenantItemPair;
import org.folio.TenantItemPairCollection;
import org.folio.TenantItemResponse;
import org.folio.inventory.common.WebContext;
import org.folio.inventory.storage.external.CollectionResourceClient;
import org.folio.inventory.support.JsonArrayHelper;
Expand All @@ -28,7 +30,6 @@
import org.folio.inventory.support.http.server.ServerErrorResponse;

import io.vertx.core.http.HttpClient;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
Expand All @@ -45,6 +46,7 @@ public class TenantItems {

private static final String TENANT_ITEMS_PATH = "/inventory/tenant-items";
public static final String ITEMS_FIELD = "items";
public static final String ITEM_FIELD = "item";
public static final String TOTAL_RECORDS_FIELD = "totalRecords";
public static final String TENANT_ID_FIELD = "tenantId";

Expand Down Expand Up @@ -77,11 +79,11 @@ private void getItemsFromTenants(RoutingContext routingContext) {
.map(CompletableFuture::join)
.flatMap(List::stream)
.toList())
.thenApply(this::constructResponse)
.thenAccept(jsonObject -> JsonResponse.success(routingContext.response(), jsonObject));
.thenApply(items -> new TenantItemResponse().withTenantItems(items).withTotalRecords(items.size()))
.thenAccept(response -> JsonResponse.success(routingContext.response(), JsonObject.mapFrom(response)));
}

private CompletableFuture<List<JsonObject>> getItemsWithTenantId(String tenantId, List<String> itemIds, RoutingContext routingContext) {
private CompletableFuture<List<TenantItem>> getItemsWithTenantId(String tenantId, List<String> itemIds, RoutingContext routingContext) {
LOG.info("getItemsWithTenantId:: Fetching items - {} from tenant - {}", itemIds, tenantId);
var context = new WebContext(routingContext);
CollectionResourceClient itemsStorageClient;
Expand All @@ -98,24 +100,20 @@ private CompletableFuture<List<JsonObject>> getItemsWithTenantId(String tenantId
var itemsFetched = new CompletableFuture<Response>();
itemsStorageClient.getAll(getByIdsQuery, itemsFetched::complete);

return itemsFetched.thenApplyAsync(response ->
getItemsWithTenantId(tenantId, response));
return itemsFetched
.thenApply(this::getItems)
.thenApply(items -> items.stream()
.map(item -> new TenantItem()
.withAdditionalProperty(ITEM_FIELD, item)
.withAdditionalProperty(TENANT_ID_FIELD, tenantId))
.toList());
}

private List<JsonObject> getItemsWithTenantId(String tenantId, Response response) {
private List<JsonObject> getItems(Response response) {
if (response.getStatusCode() != HttpStatus.SC_OK || !response.hasBody()) {
return List.of();
}
return JsonArrayHelper.toList(response.getJson(), ITEMS_FIELD).stream()
.map(item -> item.put(TENANT_ID_FIELD, tenantId))
.toList();
}

private JsonObject constructResponse(List<JsonObject> items) {
return JsonObject.of(
ITEMS_FIELD, JsonArray.of(items.toArray()),
TOTAL_RECORDS_FIELD, items.size()
);
return JsonArrayHelper.toList(response.getJson(), ITEMS_FIELD);
}

private CollectionResourceClient createItemsStorageClient(OkapiHttpClient client, WebContext context) throws MalformedURLException {
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/api/items/TenantItemApiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static api.ApiTestSuite.getCanCirculateLoanType;
import static api.support.InstanceSamples.smallAngryPlanet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.folio.inventory.resources.TenantItems.ITEMS_FIELD;
import static org.folio.inventory.resources.TenantItems.ITEM_FIELD;
import static org.folio.inventory.resources.TenantItems.TENANT_ID_FIELD;
import static org.folio.inventory.resources.TenantItems.TOTAL_RECORDS_FIELD;
import static org.folio.inventory.support.ItemUtil.ID;
Expand Down Expand Up @@ -38,6 +38,8 @@
@RunWith(JUnitParamsRunner.class)
public class TenantItemApiTests extends ApiTests {

private static final String TENANT_ITEMS_FIELD = "tenantItems";

@Test
public void testTenantItemsGetFromDifferentTenants() throws MalformedURLException,
ExecutionException, InterruptedException, TimeoutException {
Expand All @@ -59,8 +61,8 @@ public void testTenantItemsGetFromDifferentTenants() throws MalformedURLExceptio
.toCompletableFuture().get(5, TimeUnit.SECONDS);
assertThat(response.getStatusCode()).isEqualTo(200);

consortiumItem.put(TENANT_ID_FIELD, CONSORTIA_TENANT_ID);
collegeItem.put(TENANT_ID_FIELD, COLLEGE_TENANT_ID);
consortiumItem = JsonObject.of(ITEM_FIELD, consortiumItem, TENANT_ID_FIELD, CONSORTIA_TENANT_ID);
collegeItem = JsonObject.of(ITEM_FIELD, collegeItem, TENANT_ID_FIELD, COLLEGE_TENANT_ID);
var items = extractItems(response, 2);
assertThat(items).contains(consortiumItem, collegeItem);
}
Expand Down Expand Up @@ -91,7 +93,7 @@ private UUID createInstanceHoldingItem(ResourceClient itemsStorageClient, Resour

private List<JsonObject> extractItems(Response itemsResponse, int expected) {
var itemsCollection = itemsResponse.getJson();
var items = JsonArrayHelper.toList(itemsCollection.getJsonArray(ITEMS_FIELD));
var items = JsonArrayHelper.toList(itemsCollection.getJsonArray(TENANT_ITEMS_FIELD));
assertThat(items).hasSize(expected);
assertThat(itemsCollection.getInteger(TOTAL_RECORDS_FIELD)).isEqualTo(expected);
return items;
Expand Down

0 comments on commit e450f3f

Please sign in to comment.