Skip to content

Commit

Permalink
[CIRCSTORE-526] Add ILR support for publishing batch request update e…
Browse files Browse the repository at this point in the history
…vents (#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
  • Loading branch information
roman-barannyk authored Sep 16, 2024
1 parent b35fa1d commit 078733e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
15 changes: 14 additions & 1 deletion ramls/request-queue-reordering.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -18,5 +28,8 @@
}
}
},
"additionalProperties": false
"additionalProperties": false,
"required": [
"requestLevel"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)";

Expand Down Expand Up @@ -70,7 +70,7 @@ public RequestBatchResourceService(Context context, Map<String, String> okapiHea
public void executeRequestBatchUpdate(List<Request> requests,
Handler<AsyncResult<Void>> 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<Function<SQLConnection, Future<RowSet<Row>>>> allDatabaseOperations =
new ArrayList<>();

Expand All @@ -85,24 +85,29 @@ public void executeRequestBatchUpdate(List<Request> 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<Request> 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<SQLConnection, Future<RowSet<Row>>> removePositionsForRequestsBatch(
Expand Down
12 changes: 8 additions & 4 deletions src/test/java/org/folio/rest/api/RequestBatchAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,13 +112,15 @@ public static void assertCreateEventForRequest(JsonObject request) {
assertCreateEvent(getLastRequestEvent(requestId), request);
}

public static void assertRequestQueueReorderingEvent(String instanceId,
List<String> requestIds) {
public static void assertRequestQueueReorderingEvent(String instanceId, String itemId,
List<String> 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);
Expand Down

0 comments on commit 078733e

Please sign in to comment.