Skip to content

Commit

Permalink
CIRC-2100 Adding and refactoring test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Vignesh-kalyanasundaram committed Jul 30, 2024
1 parent 4bc3e5d commit dac2a0e
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ private static void removeSearchIndexFields(JsonObject request) {

private static void addPrintEventProperties(JsonObject request, PrintEventDetail printEventDetail) {
if (printEventDetail == null) {
log.info("addPrintEventProperties:: printEvent property is null for requestId {}", request.getString("id"));
String msg = "addPrintEventProperties:: printEvent property is null for requestId {}";
log.info(msg, request.getString("id"));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public CompletableFuture<Result<MultipleRecords<Request>>> findPrintEventDetails
log.debug("findPrintEventDetails:: parameters multipleRequests: {}",
() -> multipleRecordsAsString(multipleRequests));
return validatePrintEventFeatureFlag()
.thenCompose(isEnabled -> isEnabled ? fetchAndMapPrintEventDetails(multipleRequests)
.thenCompose(isEnabled -> Boolean.TRUE.equals(isEnabled) ?
fetchAndMapPrintEventDetails(multipleRequests)
: completedFuture(succeeded(multipleRequests)));
}

Expand All @@ -71,6 +72,7 @@ private CompletableFuture<Result<MultipleRecords<Request>>> fetchAndMapPrintEven
}

private CompletableFuture<Boolean> validatePrintEventFeatureFlag() {
log.debug("validatePrintEventFeatureFlag:: Fetching and validating enablePrintLog flag from settings");
return circulationSettingsRepository.findBy("query=name=printEventLogFeature")
.thenApply(res -> Optional.ofNullable(res.value())
.map(records -> records.getRecords().stream()
Expand Down
148 changes: 10 additions & 138 deletions src/test/java/api/printEvents/PrintEventsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,136 +3,35 @@
import api.support.APITests;
import api.support.builders.CirculationSettingBuilder;
import api.support.builders.RequestBuilder;
import api.support.http.IndividualResource;
import api.support.http.UserResource;
import io.vertx.core.json.JsonObject;
import org.folio.circulation.support.http.client.Response;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.IntStream;

import static api.support.http.CqlQuery.exactMatch;
import static api.support.http.InterfaceUrls.printEventsUrl;
import static api.support.matchers.ResponseStatusCodeMatcher.hasStatus;
import static org.folio.HttpStatus.HTTP_CREATED;
import static org.folio.HttpStatus.HTTP_UNPROCESSABLE_ENTITY;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

class PrintEventsTests extends APITests {

private IndividualResource servicePointResource;
private UserResource userResource;

@BeforeEach
void executeBefore() {
userResource = usersFixture.charlotte();
servicePointResource = servicePointsFixture.cd1();
circulationSettingsClient.deleteAll();
}

@Test
void postPrintEventsTest() {
circulationSettingsClient.create(new CirculationSettingBuilder()
.withName("printEventLogFeature")
.withValue(new JsonObject().put("enablePrintLog", true)));
JsonObject printRequest = getPrintEvent();
printRequest.put("requestIds", createMultipleRequests(100, "test-"));
System.out.println(printRequest.getString("requestIds"));
printRequest.put("requestIds", createOneHundredRequests());
Response response = restAssuredClient.post(printRequest, printEventsUrl("/print-events-entry"), "post-print-event");
assertThat(response, hasStatus(HTTP_CREATED));
}

@Test
void postPrintEventsAndFetchPrintDetailsInRequestApi() {
var itemBarcodePrefix = "itemBarcode-";

assertThat("Circulation settings enabled", circulationSettingsClient.getAll().isEmpty());

// creating 2 different requests and assert request details without enabling printEvent feature
var uuidList = createMultipleRequests(2, itemBarcodePrefix);
JsonObject requestRepresentation1 = requestsClient.getMany(exactMatch("id", uuidList.get(0))).getFirst();
JsonObject requestRepresentation2 = requestsClient.getMany(exactMatch("id", uuidList.get(1))).getFirst();
assertRequestDetails(requestRepresentation1, uuidList.get(0), itemBarcodePrefix+0);
assertRequestDetails(requestRepresentation2, uuidList.get(1), itemBarcodePrefix+1);
assertThat("printDetails should be null for request1 because the print event feature is not enabled",
requestRepresentation1.getJsonObject("printDetails"), Matchers.nullValue());
assertThat("printDetails should be null for request2 because the print event feature is not enabled",
requestRepresentation2.getJsonObject("printDetails"), Matchers.nullValue());

// Enabling printEvent feature and assert request details.
circulationSettingsClient.create(new CirculationSettingBuilder()
.withName("printEventLogFeature")
.withValue(new JsonObject().put("enablePrintLog", true)));
requestRepresentation1 = requestsClient.getMany(exactMatch("id", uuidList.get(0))).getFirst();
requestRepresentation2 = requestsClient.getMany(exactMatch("id", uuidList.get(1))).getFirst();
assertRequestDetails(requestRepresentation1, uuidList.get(0), itemBarcodePrefix+0);
assertRequestDetails(requestRepresentation2, uuidList.get(1), itemBarcodePrefix+1);
assertThat("printDetails should be null for request1 because the request is not printed",
requestRepresentation1.getJsonObject("printDetails"), Matchers.nullValue());
assertThat("printDetails should be null for request2 because the request is not printed",
requestRepresentation2.getJsonObject("printDetails"), Matchers.nullValue());

// Printing both request1 and request2
JsonObject printRequest = getPrintEvent();
printRequest.put("requestIds", uuidList);
printRequest.put("requesterId", userResource.getId());
restAssuredClient.post(printRequest, printEventsUrl("/print-events-entry"), "post-print-event");
requestRepresentation1 = requestsClient.getMany(exactMatch("id", uuidList.get(0))).getFirst();
requestRepresentation2 = requestsClient.getMany(exactMatch("id", uuidList.get(1))).getFirst();
assertRequestDetails(requestRepresentation1, uuidList.get(0), itemBarcodePrefix+0);
assertRequestDetails(requestRepresentation2, uuidList.get(1), itemBarcodePrefix+1);
assertThat("printDetails should not be null for request1 because the request is printed",
requestRepresentation1.getJsonObject("printDetails"), Matchers.notNullValue());
assertThat("printDetails should not be null for request2 because the request is printed",
requestRepresentation2.getJsonObject("printDetails"), Matchers.notNullValue());
assertPrintDetails(requestRepresentation1, 1, "2024-06-25T11:54:07.000Z");
assertPrintDetails(requestRepresentation2, 1, "2024-06-25T11:54:07.000Z");

// Print both the request id again with same user.
// In this case, we will get the latest print event date and the count is increased
printRequest.put("printEventDate", "2024-06-25T14:54:07.000Z");
restAssuredClient.post(printRequest, printEventsUrl("/print-events-entry"), "post-print-event");
requestRepresentation1 = requestsClient.getMany(exactMatch("id", uuidList.get(0))).getFirst();
requestRepresentation2 = requestsClient.getMany(exactMatch("id", uuidList.get(1))).getFirst();
assertRequestDetails(requestRepresentation1, uuidList.get(0), itemBarcodePrefix+0);
assertRequestDetails(requestRepresentation2, uuidList.get(1), itemBarcodePrefix+1);
assertThat("printDetails should not be null for request1 because the request is printed",
requestRepresentation1.getJsonObject("printDetails"), Matchers.notNullValue());
assertThat("printDetails should not be null for request2 because the request is printed",
requestRepresentation2.getJsonObject("printDetails"), Matchers.notNullValue());
assertPrintDetails(requestRepresentation1, 2, "2024-06-25T14:54:07.000Z");
assertPrintDetails(requestRepresentation2, 2, "2024-06-25T14:54:07.000Z");

// Print only request1 with unknown user id.
// In this case, request representation won't contain lastPrintRequester detail
printRequest.put("requesterId", UUID.randomUUID());
printRequest.put("requestIds", List.of(uuidList.get(0)));
printRequest.put("printEventDate", "2024-06-25T14:59:07.000Z");
restAssuredClient.post(printRequest, printEventsUrl("/print-events-entry"), "post-print-event");
requestRepresentation1 = requestsClient.getMany(exactMatch("id", uuidList.get(0))).getFirst();
requestRepresentation2 = requestsClient.getMany(exactMatch("id", uuidList.get(1))).getFirst();
assertRequestDetails(requestRepresentation1, uuidList.get(0), itemBarcodePrefix+0);
assertRequestDetails(requestRepresentation2, uuidList.get(1), itemBarcodePrefix+1);
assertThat("printDetails should not be null for request1 because the request is printed",
requestRepresentation1.getJsonObject("printDetails"), Matchers.notNullValue());
assertThat("printDetails should not be null for request2 because the request is printed",
requestRepresentation2.getJsonObject("printDetails"), Matchers.notNullValue());
assertPrintDetails(requestRepresentation2, 2, "2024-06-25T14:54:07.000Z");
assertThat(requestRepresentation1.getJsonObject("printDetails").getInteger("count"), is(3));
assertThat(requestRepresentation1.getJsonObject("printDetails").getString("lastPrintedDate"),
is("2024-06-25T14:59:07.000Z"));
assertThat("lastPrintRequester object should be null because we used random uuid for user id while printing",
requestRepresentation1.getJsonObject("printDetails").getJsonObject("lastPrintRequester"),
Matchers.nullValue());
}

@Test
void postPrintEventsWhenCirculationSettingIsNotPresentTest() {
JsonObject printRequest = getPrintEvent();
Expand Down Expand Up @@ -174,8 +73,8 @@ void postPrintEventsWithInvalidRequestId() {
.withName("printEventLogFeature")
.withValue(new JsonObject().put("enablePrintLog", true)));
JsonObject printRequest = getPrintEvent();
List<String> requestIds = new ArrayList<>(createMultipleRequests(10, "invalid-"));
requestIds.add(UUID.randomUUID().toString());
List<UUID> requestIds = new ArrayList<>(createOneHundredRequests());
requestIds.add(UUID.randomUUID());
printRequest.put("requestIds", requestIds);
Response response = restAssuredClient.post(printRequest, printEventsUrl("/print-events-entry"), "post-print-event");
assertThat(response, hasStatus(HTTP_UNPROCESSABLE_ENTITY));
Expand Down Expand Up @@ -211,46 +110,19 @@ private JsonObject getPrintEvent() {
return new JsonObject()
.put("requesterId", "5f5751b4-e352-4121-adca-204b0c2aec43")
.put("requesterName", "requester")
.put("printEventDate", "2024-06-25T11:54:07.000Z");
.put("printEventDate", "2024-06-25T14:30:00Z");
}

private List<String> createMultipleRequests(int noOfRequests, String itemBarcode) {
private List<UUID> createOneHundredRequests() {
final UUID pickupServicePointId = servicePointsFixture.cd1().getId();

return IntStream.range(0, noOfRequests).mapToObj(i -> requestsFixture.place(
return IntStream.range(0, 100).mapToObj(notUsed -> requestsFixture.place(
new RequestBuilder()
.open()
.page()
.forItem(itemsFixture.basedUponSmallAngryPlanet(itemBarcode + i))
.by(userResource)
.forItem(itemsFixture.basedUponSmallAngryPlanet())
.by(usersFixture.charlotte())
.fulfillToHoldShelf()
.withRequestExpiration(LocalDate.of(2024, 7, 30))
.withHoldShelfExpiration(LocalDate.of(2024, 8, 15))
.withPickupServicePointId(servicePointResource.getId())).getId().toString()).toList();
}

private void assertRequestDetails(JsonObject representation, String id, String barcodeName) {
assertThat(representation.getString("id"), is(id));
assertThat(representation.getString("requestType"), is("Page"));
assertThat(representation.getString("requestLevel"), is("Item"));
assertThat(representation.getString("requestDate"), is("2017-07-15T09:35:27.000Z"));
assertThat(representation.getJsonObject("item").getString("barcode"), is(barcodeName));
assertThat(representation.getString("fulfillmentPreference"), is("Hold Shelf"));
assertThat(representation.getString("requestExpirationDate"), is("2024-07-30T23:59:59.000Z"));
assertThat(representation.getString("holdShelfExpirationDate"), is("2024-08-15"));
assertThat(representation.getString("status"), is("Open - Not yet filled"));
assertThat(representation.getString("pickupServicePointId"), is(servicePointResource.getId().toString()));
}

private void assertPrintDetails(JsonObject representation, int count, String printEventDate) {
var printDetailObject = representation.getJsonObject("printDetails");
var lastPrintRequesterObject = printDetailObject.getJsonObject("lastPrintRequester");
assertThat(printDetailObject.getInteger("count"), is(count));
assertThat(printDetailObject.getString("lastPrintedDate"), is(printEventDate));
assertThat(lastPrintRequesterObject.getString("middleName"),
is(userResource.getJson().getJsonObject("personal").getString("middleName")));
assertThat(lastPrintRequesterObject.getString("lastName"),
is(userResource.getJson().getJsonObject("personal").getString("lastName")));
assertThat(lastPrintRequesterObject.getString("firstName"),
is(userResource.getJson().getJsonObject("personal").getString("firstName")));
.withPickupServicePointId(pickupServicePointId)).getId()).toList();
}
}
Loading

0 comments on commit dac2a0e

Please sign in to comment.