From 078733e994904248e2e313ba72d5844f1d6a8e87 Mon Sep 17 00:00:00 2001 From: Roman Barannyk <53909129+roman-barannyk@users.noreply.github.com> Date: Mon, 16 Sep 2024 17:17:16 +0300 Subject: [PATCH] [CIRCSTORE-526] Add ILR support for publishing batch request update events (#489) * CIRCSTORE-521 add request batch event publishing * CIRCSTORE-521 update logging * CIRCSTORE-521 fix broken test * CIRCSTORE-521 revert comments * CIRCSTORE-521 refactoring * CIRCSTORE-521 create test * CIRCSTORE-521 update test * CIRCSTORE-521 rename method * CIRCSTORE-521 use uuid reference for schema * CIRCSTORE-521 add events cleaning * CIRCSTORE-521 extend request queue reordering schema * CIRCSTORE-521 conflict resolving * CIRCSTORE-526 test refactoring --- ramls/request-queue-reordering.json | 15 ++++++++++++++- .../request/RequestBatchResourceService.java | 17 +++++++++++------ .../org/folio/rest/api/RequestBatchAPITest.java | 12 ++++++++---- .../support/matchers/DomainEventAssertions.java | 7 +++++-- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/ramls/request-queue-reordering.json b/ramls/request-queue-reordering.json index f76bcdd8..8098f1dd 100644 --- a/ramls/request-queue-reordering.json +++ b/ramls/request-queue-reordering.json @@ -9,6 +9,16 @@ "type": "string", "$ref": "raml-util/schemas/uuid.schema" }, + "itemId": { + "description": "Item ID of reordered requests", + "type": "string", + "$ref": "raml-util/schemas/uuid.schema" + }, + "requestLevel": { + "description": "Level of the request - Item or Title", + "type": "string", + "enum": ["Item", "Title"] + }, "requestIds": { "description": "Array of requests ids", "type": "array", @@ -18,5 +28,8 @@ } } }, - "additionalProperties": false + "additionalProperties": false, + "required": [ + "requestLevel" + ] } diff --git a/src/main/java/org/folio/service/request/RequestBatchResourceService.java b/src/main/java/org/folio/service/request/RequestBatchResourceService.java index 857899c4..c863b193 100644 --- a/src/main/java/org/folio/service/request/RequestBatchResourceService.java +++ b/src/main/java/org/folio/service/request/RequestBatchResourceService.java @@ -30,7 +30,7 @@ import io.vertx.sqlclient.RowSet; public class RequestBatchResourceService { - private static final Logger LOG = LoggerFactory.getLogger(RequestBatchResourceService.class); + private static final Logger log = LoggerFactory.getLogger(RequestBatchResourceService.class); private static final String REMOVE_POSITIONS_SQL = "UPDATE %s.%s SET jsonb = jsonb - 'position' WHERE id::text IN (%s)"; @@ -70,7 +70,7 @@ public RequestBatchResourceService(Context context, Map okapiHea public void executeRequestBatchUpdate(List requests, Handler> onFinishHandler) { - LOG.debug("Removing positions for all request to go through positions constraint"); + log.debug("Removing positions for all request to go through positions constraint"); List>>> allDatabaseOperations = new ArrayList<>(); @@ -85,24 +85,29 @@ public void executeRequestBatchUpdate(List requests, allDatabaseOperations.add(removePositionBatch); allDatabaseOperations.addAll(updateRequestsBatch); - LOG.info("Executing batch update, total records to update [{}] (including remove positions)", + log.info("Executing batch update, total records to update [{}] (including remove positions)", allDatabaseOperations.size()); RequestQueueReordering payload = mapRequestsToPayload(requests); - LOG.info("executeRequestBatchUpdate:: instanceId: {}, requests: {}", - payload.getInstanceId(), payload.getRequestIds()); + log.info("executeRequestBatchUpdate:: instanceId: {}, itemId: {}, requestLevel: {}, " + + "requests: {}", payload.getInstanceId(), payload.getItemId(), payload.getRequestLevel(), + payload.getRequestIds()); batchResourceService.executeBatchUpdate(allDatabaseOperations, onFinishHandler) .compose(v -> eventPublisher.publishCreated(payload.getInstanceId(), payload)); } private RequestQueueReordering mapRequestsToPayload(List requests) { + var firstRequest = requests.get(0); return new RequestQueueReordering() .withRequestIds(requests.stream() .map(Request::getId) .toList()) - .withInstanceId(requests.get(0).getInstanceId()); + .withInstanceId(firstRequest.getInstanceId()) + .withItemId(firstRequest.getItemId()) + .withRequestLevel(RequestQueueReordering.RequestLevel.valueOf( + firstRequest.getRequestLevel().name())); } private Function>> removePositionsForRequestsBatch( diff --git a/src/test/java/org/folio/rest/api/RequestBatchAPITest.java b/src/test/java/org/folio/rest/api/RequestBatchAPITest.java index 2ee133c0..234733aa 100644 --- a/src/test/java/org/folio/rest/api/RequestBatchAPITest.java +++ b/src/test/java/org/folio/rest/api/RequestBatchAPITest.java @@ -3,6 +3,7 @@ import static org.folio.rest.api.RequestsApiTest.requestStorageUrl; import static org.folio.rest.api.StorageTestSuite.TENANT_ID; import static org.folio.rest.api.StorageTestSuite.storageUrl; +import static org.folio.rest.jaxrs.model.RequestQueueReordering.RequestLevel.TITLE; import static org.folio.rest.support.kafka.FakeKafkaConsumer.removeAllEvents; import static org.folio.rest.support.matchers.DomainEventAssertions.assertRequestQueueReorderingEvent; import static org.hamcrest.MatcherAssert.assertThat; @@ -22,6 +23,7 @@ import org.folio.rest.impl.RequestsBatchAPI; import org.folio.rest.jaxrs.model.Request; +import org.folio.rest.jaxrs.model.RequestQueueReordering; import org.folio.rest.support.ApiTests; import org.folio.rest.support.JsonResponse; import org.folio.rest.support.Response; @@ -245,13 +247,15 @@ public void shouldPublishKafkaEventWhenUpdateRequestPositionsInBatchForTheInstan .map(JsonObject.class::cast) .sorted(Comparator.comparingInt(obj -> obj.getInteger("position"))) .toList(); + String firstRequestId = firstRequest.getString("id"); + String secondRequestId = secondRequest.getString("id"); assertThat(requestsSorted.get(0).getInteger("position"), is(1)); assertThat(requestsSorted.get(1).getInteger("position"), is(2)); - assertThat(requestsSorted.get(0).getString("id"), is(secondRequest.getString("id"))); - assertThat(requestsSorted.get(1).getString("id"), is(firstRequest.getString("id"))); + assertThat(requestsSorted.get(0).getString("id"), is(secondRequestId)); + assertThat(requestsSorted.get(1).getString("id"), is(firstRequestId)); - assertRequestQueueReorderingEvent(instanceId.toString(), List.of( - firstRequest.getString("id"), secondRequest.getString("id"))); + assertRequestQueueReorderingEvent(instanceId.toString(), + requestsSorted.get(1).getString("itemId"), List.of(firstRequestId, secondRequestId), TITLE); } private JsonObject getAllRequestsForItem(UUID itemId) throws Exception { diff --git a/src/test/java/org/folio/rest/support/matchers/DomainEventAssertions.java b/src/test/java/org/folio/rest/support/matchers/DomainEventAssertions.java index 9abcc193..f3fda249 100644 --- a/src/test/java/org/folio/rest/support/matchers/DomainEventAssertions.java +++ b/src/test/java/org/folio/rest/support/matchers/DomainEventAssertions.java @@ -33,6 +33,7 @@ import org.awaitility.Awaitility; import org.awaitility.core.ConditionFactory; +import org.folio.rest.jaxrs.model.RequestQueueReordering; import org.folio.service.event.DomainEventType; import io.vertx.core.MultiMap; @@ -111,13 +112,15 @@ public static void assertCreateEventForRequest(JsonObject request) { assertCreateEvent(getLastRequestEvent(requestId), request); } - public static void assertRequestQueueReorderingEvent(String instanceId, - List requestIds) { + public static void assertRequestQueueReorderingEvent(String instanceId, String itemId, + List requestIds, RequestQueueReordering.RequestLevel requestLevel) { await().until(() -> getRequestQueueReorderingEvents().size(), greaterThan(0)); JsonObject payload = new JsonObject() .put("instanceId", instanceId) + .put("itemId", itemId) + .put("requestLevel", requestLevel.value()) .put("requestIds", new JsonArray(requestIds)); assertCreateEvent(getFirstRequestQueueReorderingEvent(), payload);