diff --git a/pom.xml b/pom.xml
index 49b91ca8a..705f4f467 100644
--- a/pom.xml
+++ b/pom.xml
@@ -451,6 +451,7 @@
${basedir}/ramls/instance-ingress-event.json
${basedir}/ramls/tenantItemPair.json
${basedir}/ramls/tenantItemPairCollection.json
+ ${basedir}/ramls/tenantItemResponse.json
org.folio
true
diff --git a/ramls/tenantItemResponse.json b/ramls/tenantItemResponse.json
new file mode 100644
index 000000000..8bdf521e3
--- /dev/null
+++ b/ramls/tenantItemResponse.json
@@ -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"]
+}
diff --git a/src/main/java/org/folio/inventory/resources/TenantItems.java b/src/main/java/org/folio/inventory/resources/TenantItems.java
index c6079b6f4..c02ac570d 100644
--- a/src/main/java/org/folio/inventory/resources/TenantItems.java
+++ b/src/main/java/org/folio/inventory/resources/TenantItems.java
@@ -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;
@@ -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;
@@ -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";
@@ -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> getItemsWithTenantId(String tenantId, List itemIds, RoutingContext routingContext) {
+ private CompletableFuture> getItemsWithTenantId(String tenantId, List itemIds, RoutingContext routingContext) {
LOG.info("getItemsWithTenantId:: Fetching items - {} from tenant - {}", itemIds, tenantId);
var context = new WebContext(routingContext);
CollectionResourceClient itemsStorageClient;
@@ -98,24 +100,20 @@ private CompletableFuture> getItemsWithTenantId(String tenantId
var itemsFetched = new CompletableFuture();
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 getItemsWithTenantId(String tenantId, Response response) {
+ private List 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 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 {
diff --git a/src/test/java/api/items/TenantItemApiTests.java b/src/test/java/api/items/TenantItemApiTests.java
index 4b0095bcd..5471bda7a 100644
--- a/src/test/java/api/items/TenantItemApiTests.java
+++ b/src/test/java/api/items/TenantItemApiTests.java
@@ -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;
@@ -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 {
@@ -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);
}
@@ -91,7 +93,7 @@ private UUID createInstanceHoldingItem(ResourceClient itemsStorageClient, Resour
private List 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;