Search in sources :

Example 1 with LoanRequestBuilder

use of org.folio.rest.support.builders.LoanRequestBuilder in project mod-circulation-storage by folio-org.

the class LoansApiHistoryTest method replacingALoanCreatesHistoryRecord.

@Test
public void replacingALoanCreatesHistoryRecord() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
    UUID id = UUID.randomUUID();
    CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
    client.post(loanStorageUrl(), new LoanRequestBuilder().withId(id).create(), StorageTestSuite.TENANT_ID, ResponseHandler.json(createCompleted));
    JsonResponse createResponse = createCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to create loan: %s", createResponse.getBody()), createResponse.getStatusCode(), is(HttpURLConnection.HTTP_CREATED));
    JsonObject updatedLoanRequest = createResponse.getJson().copy();
    updatedLoanRequest.put("dueDate", new DateTime(2017, 3, 30, 13, 25, 46, DateTimeZone.UTC).toString(ISODateTimeFormat.dateTime())).put("action", "renewed").put("itemStatus", "Checked out").put("renewalCount", 1);
    CompletableFuture<JsonResponse> putCompleted = new CompletableFuture<>();
    client.put(loanStorageUrl(String.format("/%s", id)), updatedLoanRequest, StorageTestSuite.TENANT_ID, ResponseHandler.json(putCompleted));
    JsonResponse putResponse = putCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to update loan: %s", putResponse.getBody()), putResponse.getStatusCode(), is(HttpURLConnection.HTTP_NO_CONTENT));
    CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
    client.get(loanStorageHistoryUrl("", "query", "loan.id==" + id.toString()), StorageTestSuite.TENANT_ID, ResponseHandler.json(getCompleted));
    JsonResponse historyResponse = getCompleted.get(5, TimeUnit.SECONDS);
    List<JsonObject> entries = JsonArrayHelper.toList(historyResponse.getJson().getJsonArray("loansHistory"));
    assertThat("Incorrect number of entries in loan history for id: " + id.toString(), entries.size(), is(2));
    JsonObject entry = entries.get(0).getJsonObject("loan");
    assertThat(entry.getString("dueDate"), is("2017-03-30T13:25:46.000+00:00"));
    assertThat("status is not open", entry.getJsonObject("status").getString("name"), is("Open"));
    assertThat("action is not renewed", entry.getString("action"), is("renewed"));
    assertThat("renewal count is not 1", entry.getInteger("renewalCount"), is(1));
    assertThat("item status is not checked out", entry.getString("itemStatus"), is("Checked out"));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) JsonObject(io.vertx.core.json.JsonObject) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with LoanRequestBuilder

use of org.folio.rest.support.builders.LoanRequestBuilder in project mod-circulation-storage by folio-org.

the class LoansApiHistoryTest method creatingALoanCreatesHistoryEntry.

@Test
public void creatingALoanCreatesHistoryEntry() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
    UUID id = UUID.randomUUID();
    UUID itemId = UUID.randomUUID();
    UUID userId = UUID.randomUUID();
    UUID proxyUserId = UUID.randomUUID();
    JsonObject loanRequest = new LoanRequestBuilder().withId(id).withItemId(itemId).withUserId(userId).withProxyUserId(proxyUserId).withLoanDate(new DateTime(2017, 6, 27, 10, 23, 43, DateTimeZone.UTC)).withStatus("Open").withAction("checkedout").withItemStatus("Checked out").withDueDate(new DateTime(2017, 7, 27, 10, 23, 43, DateTimeZone.UTC)).create();
    CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
    client.post(loanStorageUrl(), loanRequest, StorageTestSuite.TENANT_ID, ResponseHandler.json(createCompleted));
    JsonResponse createResponse = createCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to create loan: %s", createResponse.getBody()), createResponse.getStatusCode(), is(HttpURLConnection.HTTP_CREATED));
    CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
    client.get(loanStorageHistoryUrl("", "query", "loan.id==" + id.toString()), StorageTestSuite.TENANT_ID, ResponseHandler.json(getCompleted));
    JsonResponse historyResponse = getCompleted.get(5, TimeUnit.SECONDS);
    assertThat(historyResponse.getStatusCode(), is(HTTP_OK));
    List<JsonObject> entries = JsonArrayHelper.toList(historyResponse.getJson().getJsonArray("loansHistory"));
    assertThat("Incorrect number of entries in loan history for id: " + id.toString(), entries.size(), is(1));
    JsonObject loan = entries.get(0).getJsonObject("loan");
    assertThat("id does not match", loan.getString("id"), is(id.toString()));
    assertThat("user id does not match", loan.getString("userId"), is(userId.toString()));
    assertThat("proxy user id does not match", loan.getString("proxyUserId"), is(proxyUserId.toString()));
    assertThat("item id does not match", loan.getString("itemId"), is(itemId.toString()));
    assertThat("loan date does not match", loan.getString("loanDate"), is("2017-06-27T10:23:43.000Z"));
    assertThat("status is not open", loan.getJsonObject("status").getString("name"), is("Open"));
    assertThat("action is not checked out", loan.getString("action"), is("checkedout"));
    assertThat("item status is not checked out", loan.getString("itemStatus"), is("Checked out"));
    assertThat("due date does not match", loan.getString("dueDate"), is("2017-07-27T10:23:43.000+00:00"));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) JsonObject(io.vertx.core.json.JsonObject) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with LoanRequestBuilder

use of org.folio.rest.support.builders.LoanRequestBuilder in project mod-circulation-storage by folio-org.

the class LoansApiTest method canRemoveUserIdFromClosedLoan.

@Test
public void canRemoveUserIdFromClosedLoan() throws InterruptedException, MalformedURLException, TimeoutException, ExecutionException {
    DateTime loanDate = new DateTime(2017, 3, 1, 13, 25, 46, 232, DateTimeZone.UTC);
    final UUID loanId = UUID.randomUUID();
    final UUID userId = UUID.randomUUID();
    IndividualResource loan = loansClient.create(new LoanRequestBuilder().withId(loanId).withUserId(userId).withLoanDate(loanDate).withDueDate(loanDate.plus(Period.days(14))).open().create());
    final LoanRequestBuilder closedLoanRequest = LoanRequestBuilder.from(loan.getJson()).closed();
    loansClient.replace(loanId.toString(), closedLoanRequest);
    final IndividualResource closedLoan = loansClient.getById(loanId);
    final LoanRequestBuilder anonymisedLoanRequest = LoanRequestBuilder.from(closedLoan.getJson()).withNoUserId();
    loansClient.replace(loanId.toString(), anonymisedLoanRequest);
    final IndividualResource anonymisedLoan = loansClient.getById(loanId);
    assertThat(anonymisedLoan.getJson(), isClosed());
    assertThat("Should not have a user ID", anonymisedLoan.getJson().containsKey("userId"), is(false));
    assertCreateEventForLoan(loan.getJson());
    assertUpdateEventForLoan(closedLoan.getJson(), anonymisedLoan.getJson());
}
Also used : LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) UUID(java.util.UUID) IndividualResource(org.folio.rest.support.IndividualResource) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with LoanRequestBuilder

use of org.folio.rest.support.builders.LoanRequestBuilder in project mod-circulation-storage by folio-org.

the class LoansApiTest method canCreateALoanAtSpecificLocationWithOnlyRequiredProperties.

@Test
public void canCreateALoanAtSpecificLocationWithOnlyRequiredProperties() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
    UUID id = UUID.randomUUID();
    JsonObject loanRequest = new LoanRequestBuilder().withId(id).withNoStatus().withNoItemStatus().withLoanDate(new DateTime(2017, 3, 5, 14, 23, 41, DateTimeZone.UTC)).withAction("checkedout").create();
    loansClient.createAtSpecificLocation(id, loanRequest);
    JsonObject loan = loansClient.getById(id).getJson();
    assertThat("id does not match", loan.getString("id"), is(id.toString()));
    assertThat(loan, isOpen());
    assertCreateEventForLoan(loan);
}
Also used : LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) JsonObject(io.vertx.core.json.JsonObject) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 5 with LoanRequestBuilder

use of org.folio.rest.support.builders.LoanRequestBuilder in project mod-circulation-storage by folio-org.

the class LoansApiTest method cannotRemoveUserIdFromOpenLoan.

@Test
public void cannotRemoveUserIdFromOpenLoan() throws InterruptedException, MalformedURLException, TimeoutException, ExecutionException {
    final UUID loanId = UUID.randomUUID();
    final UUID userId = UUID.randomUUID();
    IndividualResource loan = loansClient.create(new LoanRequestBuilder().withId(loanId).withUserId(userId).open().create());
    final LoanRequestBuilder loanRequestWithoutId = LoanRequestBuilder.from(loan.getJson()).withNoUserId();
    final JsonResponse putResponse = loansClient.attemptCreateOrReplace(loanId.toString(), loanRequestWithoutId);
    assertThat(putResponse, isValidationResponseWhich(hasMessage("Open loan must have a user ID")));
    assertCreateEventForLoan(loan.getJson());
    assertLoanEventCount(loanId.toString(), 1);
}
Also used : LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) UUID(java.util.UUID) IndividualResource(org.folio.rest.support.IndividualResource) JsonResponse(org.folio.rest.support.JsonResponse) Test(org.junit.Test)

Aggregations

LoanRequestBuilder (org.folio.rest.support.builders.LoanRequestBuilder)46 Test (org.junit.Test)44 UUID (java.util.UUID)39 JsonObject (io.vertx.core.json.JsonObject)34 DateTime (org.joda.time.DateTime)18 IndividualResource (org.folio.rest.support.IndividualResource)17 JsonResponse (org.folio.rest.support.JsonResponse)16 CompletableFuture (java.util.concurrent.CompletableFuture)9 UUIDMatchers.isUUID (org.folio.rest.support.matchers.UUIDMatchers.isUUID)8 Matchers.containsString (org.hamcrest.Matchers.containsString)6 JsonArray (io.vertx.core.json.JsonArray)5 TextResponse (org.folio.rest.support.TextResponse)5 TRUE (java.lang.Boolean.TRUE)4 HttpURLConnection (java.net.HttpURLConnection)4 MalformedURLException (java.net.MalformedURLException)4 DateFormat (java.text.DateFormat)4 SimpleDateFormat (java.text.SimpleDateFormat)4 Date (java.util.Date)4 List (java.util.List)4 TimeZone (java.util.TimeZone)4