Skip to content

Commit

Permalink
CIRCSTORE-422 fix NPE for requests with no position (#452)
Browse files Browse the repository at this point in the history
* CIRCSTORE-422 fix NPE for requests with no position

* CIRCSTORE-422 fix code smell

* CIRCSTORE-422 revert unnecessary changes

* CIRCSTORE-422 remove redundant checking
  • Loading branch information
roman-barannyk authored Feb 29, 2024
1 parent 2cac6f0 commit 012f4b7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ private Future<Void> reorderGroupedRequests(Conn conn,
private Future<Void> updateRequestsPositions(Conn conn,
List<Request> requests) {

requests.sort(Comparator.comparingInt(Request::getPosition));
requests.sort(Comparator.comparing(Request::getPosition, Comparator.nullsLast(
Integer::compareTo)));
AtomicInteger pos = new AtomicInteger(1);
Future<Void> future = succeededFuture();

Expand Down
69 changes: 68 additions & 1 deletion src/test/java/org/folio/rest/api/RequestExpirationApiTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.folio.rest.api;

import lombok.SneakyThrows;
import static org.folio.rest.api.RequestsApiTest.requestStorageUrl;
import static org.folio.rest.api.StorageTestSuite.TENANT_ID;
import static org.folio.rest.api.StorageTestSuite.prepareTenant;
Expand Down Expand Up @@ -47,6 +46,7 @@
import org.junit.Test;

import io.vertx.core.json.JsonObject;
import lombok.SneakyThrows;

public class RequestExpirationApiTest extends ApiTests {

Expand Down Expand Up @@ -1048,6 +1048,73 @@ public void shouldOnlyExpireRequestsForSpecifiedTenant() {
assertThat(requestFromDummyTenant.getInteger("position"), nullValue());
}

@Test
public void shouldRecalculateQueueAfterRequestExpirationWhenNoPositionRequestExists()
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {

stubTlrSettings(true);
UUID firstRequestId = UUID.randomUUID();
UUID secondRequestId = UUID.randomUUID();
UUID thirdRequestId = UUID.randomUUID();
UUID instanceId = UUID.randomUUID();

createEntity(
new RequestRequestBuilder()
.page()
.withRequestLevel("Title")
.withId(firstRequestId)
.withHoldShelfExpirationDate(new DateTime().plusDays(1))
.withInstanceId(instanceId)
.withItemId(null)
.withHoldingsRecordId(null)
.withPosition(1)
.withStatus(OPEN_AWAITING_PICKUP)
.create(),
requestStorageUrl());

createEntity(
new RequestRequestBuilder()
.page()
.withRequestLevel("Title")
.withId(secondRequestId)
.withHoldShelfExpirationDate(new DateTime(2022, 1, 30, 10, 22, 54, DateTimeZone.UTC))
.withInstanceId(instanceId)
.withItemId(null)
.withHoldingsRecordId(null)
.withPosition(2)
.withStatus(OPEN_AWAITING_PICKUP)
.create(),
requestStorageUrl());

createEntity(
new RequestRequestBuilder()
.page()
.withRequestLevel("Title")
.withId(thirdRequestId)
.withHoldShelfExpirationDate(new DateTime().plusDays(2))
.withInstanceId(instanceId)
.withItemId(null)
.withHoldingsRecordId(null)
.withPosition(null)
.withStatus(OPEN_AWAITING_PICKUP)
.create(),
requestStorageUrl());

expireRequests();

JsonObject firstRequestById = getById(requestStorageUrl(String.format("/%s", firstRequestId)));
assertThat(firstRequestById.getString("status"), is(OPEN_AWAITING_PICKUP));
assertThat(firstRequestById.getString("position"), is("1"));

JsonObject secondRequestById = getById(requestStorageUrl(String.format("/%s", secondRequestId)));
assertThat(secondRequestById.getString("status"), is(CLOSED_PICKUP_EXPIRED));
assertThat(secondRequestById.containsKey("position"), is(false));

JsonObject thirdRequestById = getById(requestStorageUrl(String.format("/%s", thirdRequestId)));
assertThat(thirdRequestById.getString("status"), is(OPEN_AWAITING_PICKUP));
assertThat(thirdRequestById.getString("position"), is("2"));
}

@SneakyThrows
private void expireRequestsForTenant(String tenantId) {
final var createCompleted = new CompletableFuture<Response>();
Expand Down

0 comments on commit 012f4b7

Please sign in to comment.