Search in sources :

Example 6 with LoanRequestBuilder

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

the class LoansApiTest method returnDateIsMandatoryForClosedLoans.

@Test
@Ignore("Should conditional field validation be done in a storage module?")
public void returnDateIsMandatoryForClosedLoans() throws InterruptedException, MalformedURLException, TimeoutException, ExecutionException {
    DateTime loanDate = new DateTime(2017, 3, 1, 13, 25, 46, 232, DateTimeZone.UTC);
    IndividualResource loan = loansClient.create(new LoanRequestBuilder().withLoanDate(loanDate).withDueDate(loanDate.plus(Period.days(14))).create());
    LoanRequestBuilder returnedLoan = LoanRequestBuilder.from(loan.copyJson()).closed();
    CompletableFuture<TextResponse> putCompleted = new CompletableFuture<>();
    client.put(InterfaceUrls.loanStorageUrl(String.format("/%s", loan.getId())), returnedLoan.create(), StorageTestSuite.TENANT_ID, ResponseHandler.text(putCompleted));
    TextResponse response = putCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Should have failed to update loan: %s", response.getBody()), response.getStatusCode(), is(HttpURLConnection.HTTP_BAD_REQUEST));
    assertThat(response.getBody(), containsString("return date is mandatory to close a loan"));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) TextResponse(org.folio.rest.support.TextResponse) IndividualResource(org.folio.rest.support.IndividualResource) DateTime(org.joda.time.DateTime) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with LoanRequestBuilder

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

the class LoansApiTest method canCreateMultipleOpenLoansForDifferentItems.

@Test
public void canCreateMultipleOpenLoansForDifferentItems() throws InterruptedException, MalformedURLException, TimeoutException, ExecutionException {
    UUID firstItemId = UUID.randomUUID();
    UUID secondItemId = UUID.randomUUID();
    JsonObject firstLoanRequest = new LoanRequestBuilder().withItemId(firstItemId).open().create();
    loansClient.create(firstLoanRequest);
    JsonObject firstLoan = loansClient.getById(firstLoanRequest.getString("id")).getJson();
    JsonObject secondLoanRequest = new LoanRequestBuilder().withItemId(secondItemId).open().create();
    loansClient.create(secondLoanRequest);
    JsonObject secondLoan = loansClient.getById(secondLoanRequest.getString("id")).getJson();
    assertCreateEventForLoan(firstLoan);
    assertCreateEventForLoan(secondLoan);
}
Also used : LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) JsonObject(io.vertx.core.json.JsonObject) UUID(java.util.UUID) Test(org.junit.Test)

Example 8 with LoanRequestBuilder

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

the class LoansApiTest method canSearchByLoanStatus.

@Test
public void canSearchByLoanStatus() throws Exception {
    final IndividualResource openLoan = loansClient.create(new LoanRequestBuilder().checkedOut());
    loansClient.create(new LoanRequestBuilder().checkedIn());
    final List<String> openLoans = loansClient.getMany("status.name==Open").getRecords().stream().map(json -> json.getString("id")).collect(Collectors.toList());
    assertThat(openLoans, hasSize(1));
    assertThat(openLoans, hasItem(openLoan.getId()));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ResponseHandler(org.folio.rest.support.ResponseHandler) ValidationResponseMatchers.isValidationResponseWhich(org.folio.rest.support.matchers.ValidationResponseMatchers.isValidationResponseWhich) JsonResponse(org.folio.rest.support.JsonResponse) DateTimeZone(org.joda.time.DateTimeZone) DomainEventAssertions.assertLoanEventCount(org.folio.rest.support.matchers.DomainEventAssertions.assertLoanEventCount) Date(java.util.Date) AssertingRecordClient(org.folio.rest.support.http.AssertingRecordClient) TimeoutException(java.util.concurrent.TimeoutException) LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) IsNull.nullValue(org.hamcrest.core.IsNull.nullValue) After(org.junit.After) Is.is(org.hamcrest.core.Is.is) JsonObject(io.vertx.core.json.JsonObject) Metadata(org.folio.rest.jaxrs.model.Metadata) HttpResponseStatusCodeMatchers.isNotFound(org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isNotFound) LoanMatchers.isClosed(org.folio.rest.support.matchers.LoanMatchers.isClosed) DateFormat(java.text.DateFormat) IsNot.not(org.hamcrest.core.IsNot.not) MatcherAssert.assertThat(org.hamcrest.junit.MatcherAssert.assertThat) ISODateTimeFormat(org.joda.time.format.ISODateTimeFormat) ValidationErrorMatchers.hasMessageContaining(org.folio.rest.support.matchers.ValidationErrorMatchers.hasMessageContaining) Matchers.allOf(org.hamcrest.Matchers.allOf) TimeZone(java.util.TimeZone) DomainEventAssertions.assertUpdateEventForLoan(org.folio.rest.support.matchers.DomainEventAssertions.assertUpdateEventForLoan) DomainEventAssertions.assertCreateEventForLoan(org.folio.rest.support.matchers.DomainEventAssertions.assertCreateEventForLoan) TextResponse(org.folio.rest.support.TextResponse) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) Matchers.anyOf(org.hamcrest.Matchers.anyOf) TRUE(java.lang.Boolean.TRUE) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime.parse(org.joda.time.DateTime.parse) SimpleDateFormat(java.text.SimpleDateFormat) DomainEventAssertions.assertRemoveEventForLoan(org.folio.rest.support.matchers.DomainEventAssertions.assertRemoveEventForLoan) CompletableFuture(java.util.concurrent.CompletableFuture) IndividualResource(org.folio.rest.support.IndividualResource) DomainEventAssertions.assertNoLoanEvent(org.folio.rest.support.matchers.DomainEventAssertions.assertNoLoanEvent) HttpResponseStatusCodeMatchers.isBadRequest(org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isBadRequest) ValidationErrorMatchers.hasParameter(org.folio.rest.support.matchers.ValidationErrorMatchers.hasParameter) Matchers.hasSize(org.hamcrest.Matchers.hasSize) InterfaceUrls(org.folio.rest.support.http.InterfaceUrls) Before(org.junit.Before) Period(org.joda.time.Period) MalformedURLException(java.net.MalformedURLException) DateTime(org.joda.time.DateTime) Test(org.junit.Test) ValidationErrorMatchers.hasMessage(org.folio.rest.support.matchers.ValidationErrorMatchers.hasMessage) ApiTests(org.folio.rest.support.ApiTests) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) LoanMatchers.isOpen(org.folio.rest.support.matchers.LoanMatchers.isOpen) JsonArray(io.vertx.core.json.JsonArray) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Ignore(org.junit.Ignore) IsNull.notNullValue(org.hamcrest.core.IsNull.notNullValue) DateTimeConstants(org.joda.time.DateTimeConstants) LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) IndividualResource(org.folio.rest.support.IndividualResource) Test(org.junit.Test)

Example 9 with LoanRequestBuilder

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

the class LoansApiTest method cannotCreateOpenLoanWithoutUserIdViaPut.

@Test
public void cannotCreateOpenLoanWithoutUserIdViaPut() throws InterruptedException, MalformedURLException, TimeoutException, ExecutionException {
    final UUID loanId = UUID.randomUUID();
    final LoanRequestBuilder loanRequest = new LoanRequestBuilder().withId(loanId).open().withNoUserId();
    final JsonResponse putResponse = loansClient.attemptCreateOrReplace(loanId.toString(), loanRequest);
    assertThat(putResponse, isValidationResponseWhich(hasMessage("Open loan must have a user ID")));
    assertNoLoanEvent(loanId.toString());
}
Also used : LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) UUID(java.util.UUID) JsonResponse(org.folio.rest.support.JsonResponse) Test(org.junit.Test)

Example 10 with LoanRequestBuilder

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

the class LoansApiTest method canSearchByUserId.

@Test
public void canSearchByUserId() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
    UUID firstUserId = UUID.randomUUID();
    UUID secondUserId = UUID.randomUUID();
    String queryTemplate = InterfaceUrls.loanStorageUrl() + "?query=userId='%s'";
    loansClient.create(new LoanRequestBuilder().withUserId(firstUserId).create());
    loansClient.create(new LoanRequestBuilder().withUserId(firstUserId).create());
    loansClient.create(new LoanRequestBuilder().withUserId(firstUserId).create());
    loansClient.create(new LoanRequestBuilder().withUserId(firstUserId).create());
    loansClient.create(new LoanRequestBuilder().withUserId(secondUserId).create());
    loansClient.create(new LoanRequestBuilder().withUserId(secondUserId).create());
    loansClient.create(new LoanRequestBuilder().withUserId(secondUserId).create());
    CompletableFuture<JsonResponse> firstUserSearchCompleted = new CompletableFuture<>();
    CompletableFuture<JsonResponse> secondUserSeatchCompleted = new CompletableFuture<>();
    client.get(String.format(queryTemplate, firstUserId), StorageTestSuite.TENANT_ID, ResponseHandler.json(firstUserSearchCompleted));
    client.get(String.format(queryTemplate, secondUserId), StorageTestSuite.TENANT_ID, ResponseHandler.json(secondUserSeatchCompleted));
    JsonResponse firstPageResponse = firstUserSearchCompleted.get(5, TimeUnit.SECONDS);
    JsonResponse secondPageResponse = secondUserSeatchCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to get loans for first user: %s", firstPageResponse.getBody()), firstPageResponse.getStatusCode(), is(200));
    assertThat(String.format("Failed to get loans for second user: %s", secondPageResponse.getBody()), secondPageResponse.getStatusCode(), is(200));
    JsonObject firstPage = firstPageResponse.getJson();
    JsonObject secondPage = secondPageResponse.getJson();
    JsonArray firstPageLoans = firstPage.getJsonArray("loans");
    JsonArray secondPageLoans = secondPage.getJsonArray("loans");
    assertThat(firstPageLoans.size(), is(4));
    assertThat(firstPage.getInteger("totalRecords"), is(4));
    assertThat(secondPageLoans.size(), is(3));
    assertThat(secondPage.getInteger("totalRecords"), is(3));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) CompletableFuture(java.util.concurrent.CompletableFuture) LoanRequestBuilder(org.folio.rest.support.builders.LoanRequestBuilder) JsonObject(io.vertx.core.json.JsonObject) Matchers.containsString(org.hamcrest.Matchers.containsString) UUID(java.util.UUID) 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