Skip to content

Commit

Permalink
CIRC-1961 Add new error codes (#1392)
Browse files Browse the repository at this point in the history
* CIRC-1961 Add new error codes

* CIRC-1961 Fix tests
  • Loading branch information
alexanderkurash authored Dec 1, 2023
1 parent fbe8bbf commit 0dccb3e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import static java.lang.String.format;
import static org.folio.circulation.domain.representations.RequestProperties.PICKUP_SERVICE_POINT_ID;
import static org.folio.circulation.domain.representations.RequestProperties.REQUEST_TYPE;
import static org.folio.circulation.support.ErrorCode.INSTANCE_ALREADY_REQUESTED;
import static org.folio.circulation.support.ErrorCode.ITEM_ALREADY_REQUESTED;
import static org.folio.circulation.support.ErrorCode.ITEM_OF_THIS_INSTANCE_ALREADY_REQUESTED;
import static org.folio.circulation.support.ErrorCode.MOVING_REQUEST_TO_THE_SAME_ITEM;
import static org.folio.circulation.support.ValidationErrorFailure.failedValidation;
import static org.folio.circulation.support.results.Result.of;
import static org.folio.circulation.support.results.Result.succeeded;
Expand Down Expand Up @@ -254,31 +257,38 @@ private static Result<RequestAndRelatedRecords> alreadyRequestedFailure(
Request requestBeingPlaced = requestAndRelatedRecords.getRequest();
HashMap<String, String> parameters = new HashMap<>();
String message;
ErrorCode errorCode;

if (requestBeingPlaced.isTitleLevel()) {
if (existingRequest.isTitleLevel()) {
parameters.put(REQUESTER_ID, requestBeingPlaced.getUserId());
parameters.put(INSTANCE_ID, requestBeingPlaced.getInstanceId());

message = requestBeingPlaced.getOperation() == Operation.MOVE
? "Not allowed to move title level page request to the same item"
: "This requester already has an open request for this instance";
if (requestBeingPlaced.getOperation() == Operation.MOVE) {
message = "Not allowed to move title level page request to the same item";
errorCode = MOVING_REQUEST_TO_THE_SAME_ITEM;
} else {
message = "This requester already has an open request for this instance";
errorCode = INSTANCE_ALREADY_REQUESTED;
}
} else {
parameters.put(REQUESTER_ID, requestBeingPlaced.getUserId());
parameters.put(INSTANCE_ID, requestBeingPlaced.getInstanceId());

message = "This requester already has an open request for one of the instance's items";
errorCode = ITEM_OF_THIS_INSTANCE_ALREADY_REQUESTED;
}
} else {
parameters.put(REQUESTER_ID, requestBeingPlaced.getUserId());
parameters.put(ITEM_ID, requestBeingPlaced.getItemId());
parameters.put(REQUEST_ID, requestBeingPlaced.getId());

message = "This requester already has an open request for this item";
errorCode = ITEM_ALREADY_REQUESTED;
}
log.info("alreadyRequestedFailure:: message: {}", message);
log.info("alreadyRequestedFailure:: message: {}, errorCode: {}", message, errorCode);

return failedValidation(message, parameters, ITEM_ALREADY_REQUESTED);
return failedValidation(message, parameters, errorCode);
}

static boolean isTheSameRequester(RequestAndRelatedRecords it, Request that) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/folio/circulation/support/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public enum ErrorCode {
CANNOT_CREATE_PAGE_TLR_WITHOUT_ITEM_ID,
MOVING_REQUEST_TO_THE_SAME_ITEM,
ITEM_ALREADY_REQUESTED,
ITEM_OF_THIS_INSTANCE_ALREADY_REQUESTED,
INSTANCE_ALREADY_REQUESTED,
HOLD_SHELF_REQUESTS_REQUIRE_PICKUP_SERVICE_POINT
}
41 changes: 27 additions & 14 deletions src/test/java/api/requests/RequestsAPICreationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@
import static org.folio.circulation.domain.representations.logs.LogEventType.NOTICE_ERROR;
import static org.folio.circulation.domain.representations.logs.LogEventType.REQUEST_CREATED_THROUGH_OVERRIDE;
import static org.folio.circulation.support.ErrorCode.FULFILLMENT_PREFERENCE_IS_NOT_ALLOWED;
import static org.folio.circulation.support.ErrorCode.HOLD_SHELF_REQUESTS_REQUIRE_PICKUP_SERVICE_POINT;
import static org.folio.circulation.support.ErrorCode.HOLD_AND_RECALL_TLR_NOT_ALLOWED_PAGEABLE_AVAILABLE_ITEM_FOUND;
import static org.folio.circulation.support.ErrorCode.HOLD_SHELF_REQUESTS_REQUIRE_PICKUP_SERVICE_POINT;
import static org.folio.circulation.support.ErrorCode.INSTANCE_ALREADY_REQUESTED;
import static org.folio.circulation.support.ErrorCode.ITEM_ALREADY_REQUESTED;
import static org.folio.circulation.support.ErrorCode.ITEM_OF_THIS_INSTANCE_ALREADY_REQUESTED;
import static org.folio.circulation.support.ErrorCode.REQUESTER_ALREADY_HAS_LOAN_FOR_ONE_OF_INSTANCES_ITEMS;
import static org.folio.circulation.support.ErrorCode.REQUESTER_ALREADY_HAS_THIS_ITEM_ON_LOAN;
import static org.folio.circulation.support.ErrorCode.REQUEST_LEVEL_IS_NOT_ALLOWED;
Expand Down Expand Up @@ -102,7 +105,6 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import api.support.builders.AddInfoRequestBuilder;
import org.apache.http.HttpStatus;
import org.awaitility.Awaitility;
import org.folio.circulation.domain.ItemStatus;
Expand Down Expand Up @@ -132,6 +134,7 @@

import api.support.APITests;
import api.support.TlrFeatureStatus;
import api.support.builders.AddInfoRequestBuilder;
import api.support.builders.Address;
import api.support.builders.CheckInByBarcodeRequestBuilder;
import api.support.builders.HoldingBuilder;
Expand Down Expand Up @@ -697,6 +700,7 @@ void cannotCreateTlrWhenUserAlreadyRequestedTheSameTitle() {
assertThat(postResponse, hasStatus(HTTP_UNPROCESSABLE_ENTITY));
assertThat(postResponse.getJson(), hasErrorWith(allOf(
hasMessage("This requester already has an open request for this instance"),
hasCode(INSTANCE_ALREADY_REQUESTED),
hasParameter("requesterId", usersFixture.james().getId().toString()),
hasParameter("instanceId", instanceId.toString()))));
}
Expand Down Expand Up @@ -727,6 +731,7 @@ void cannotCreateTlrWhenUserAlreadyRequestedAnItemFromTheSameTitle() {
assertThat(response.getJson(), hasErrors(1));
assertThat(response.getJson(), hasErrorWith(allOf(
hasMessage("This requester already has an open request for one of the instance's items"),
hasCode(ITEM_OF_THIS_INSTANCE_ALREADY_REQUESTED),
hasParameter("requesterId", jessica.getId().toString()),
hasParameter("instanceId", item1.getInstanceId().toString()))));
}
Expand Down Expand Up @@ -1497,8 +1502,10 @@ void cannotCreateItemLevelRequestIfTitleLevelRequestForInstanceAlreadyCreated()
patronId, pickupServicePointId, instanceId, secondItem));

assertThat(response, hasStatus(HTTP_UNPROCESSABLE_ENTITY));
assertThat(response.getJson(), hasErrorWith(hasMessage(
"This requester already has an open request for this item")));
assertThat(response.getJson(), hasErrorWith(allOf(
hasMessage("This requester already has an open request for this item"),
hasCode(ITEM_ALREADY_REQUESTED)
)));
}

@Test
Expand All @@ -1517,8 +1524,10 @@ void cannotCreateTitleLevelRequestIfItemLevelRequestAlreadyCreated() {
Response response = requestsClient.attemptCreate(buildPageTitleLevelRequest(
patronId, pickupServicePointId, instanceId));
assertThat(response, hasStatus(HTTP_UNPROCESSABLE_ENTITY));
assertThat(response.getJson(), hasErrorWith(hasMessage(
"This requester already has an open request for one of the instance's items")));
assertThat(response.getJson(), hasErrorWith(allOf(
hasMessage("This requester already has an open request for one of the instance's items"),
hasCode(ITEM_OF_THIS_INSTANCE_ALREADY_REQUESTED)
)));
}

@Test
Expand Down Expand Up @@ -1556,8 +1565,10 @@ void cannotCreateTwoTitleLevelRequestsForSameInstance() {
Response response = requestsClient.attemptCreate(buildPageTitleLevelRequest(
userId, pickupServicePointId, instanceId));
assertThat(response, hasStatus(HTTP_UNPROCESSABLE_ENTITY));
assertThat(response.getJson(), hasErrorWith(hasMessage(
"This requester already has an open request for this instance")));
assertThat(response.getJson(), hasErrorWith(allOf(
hasMessage("This requester already has an open request for this instance"),
hasCode(INSTANCE_ALREADY_REQUESTED)
)));
assertThat(requestsClient.getAll(), hasSize(1));
}

Expand All @@ -1575,8 +1586,10 @@ void cannotCreateTwoItemLevelRequestsForSameItem() {
Response response = requestsClient.attemptCreate(buildItemLevelRequest(userId,
pickupServicePointId, instanceId, item));
assertThat(response, hasStatus(HTTP_UNPROCESSABLE_ENTITY));
assertThat(response.getJson(), hasErrorWith(hasMessage(
"This requester already has an open request for this item")));
assertThat(response.getJson(), hasErrorWith(allOf(
hasMessage("This requester already has an open request for this item"),
hasCode(ITEM_ALREADY_REQUESTED)
)));
assertThat(requestsClient.getAll(), hasSize(1));
}

Expand Down Expand Up @@ -2127,10 +2140,10 @@ void cannotCreateTwoRequestsFromTheSameUserForTheSameItem() {

assertThat(response, hasStatus(HTTP_UNPROCESSABLE_ENTITY));
assertThat(response.getJson(), hasErrors(1));
assertThat(
response.getJson(),
hasErrorWith(hasMessage("This requester already has an open request for this item"))
);
assertThat(response.getJson(), hasErrorWith(allOf(
hasMessage("This requester already has an open request for this item"),
hasCode(ITEM_ALREADY_REQUESTED)
)));
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/api/requests/scenarios/MoveRequestTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import static org.folio.circulation.domain.representations.ItemProperties.CALL_NUMBER_COMPONENTS;
import static org.folio.circulation.domain.representations.RequestProperties.REQUEST_TYPE;
import static org.folio.circulation.domain.representations.logs.LogEventType.REQUEST_MOVED;
import static org.folio.circulation.support.ErrorCode.ITEM_ALREADY_REQUESTED;
import static org.folio.circulation.support.ErrorCode.MOVING_REQUEST_TO_THE_SAME_ITEM;
import static org.folio.circulation.support.utils.ClockUtil.getClock;
import static org.folio.circulation.support.utils.ClockUtil.getZonedDateTime;
import static org.folio.circulation.support.utils.ClockUtil.setClock;
Expand Down Expand Up @@ -428,7 +428,7 @@ void cannotMoveTlrToTheSameItem() {
Response response = requestsFixture.attemptMove(new MoveRequestBuilder(nodPage.getId(), item.getId()));
assertThat(response.getJson(), hasErrorWith(allOf(
hasMessage("Not allowed to move title level page request to the same item"),
hasCode(ITEM_ALREADY_REQUESTED),
hasCode(MOVING_REQUEST_TO_THE_SAME_ITEM),
hasParameter("requesterId", jessica.getId().toString()),
hasParameter("instanceId", item.getInstanceId().toString()))));
}
Expand Down

0 comments on commit 0dccb3e

Please sign in to comment.