Search in sources :

Example 1 with TextResponse

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

the class LoanPoliciesApiTest method loanPolicyWithFixedDate.

@Test
public void loanPolicyWithFixedDate() throws InterruptedException, MalformedURLException, TimeoutException, ExecutionException {
    UUID fddId = UUID.randomUUID();
    // /////////// create fixed due date to use as foreign key
    JsonObject fdd = FixedDueDateApiTest.createFixedDueDate(fddId.toString(), "semester_test", "desc");
    JsonObject fddSchedule = FixedDueDateApiTest.createSchedule("2017-01-01T10:00:00.000+0000", "2017-03-03T10:00:00.000+0000", "2017-04-04T10:00:00.000+0000");
    fdd.put(FixedDueDateApiTest.SCHEDULE_SECTION, new JsonArray().add(fddSchedule));
    CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
    client.post(FixedDueDateApiTest.dueDateURL(), fdd, StorageTestSuite.TENANT_ID, ResponseHandler.json(createCompleted));
    JsonResponse response = createCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to create due date: %s", response.getBody()), response, matchesCreated());
    JsonObject representation = response.getJson();
    assertThat(representation.getString("id"), is(fddId.toString()));
    // //////////////////////////////////////////////////////////
    UUID id1 = UUID.randomUUID();
    // ////////// create loan policy with foreign key to fdd
    JsonObject loanPolicyRequest = defaultRollingPolicy().withId(id1).withName("Example Loan Policy").withDescription("An example loan policy").create();
    loanPolicyRequest.getJsonObject("loansPolicy").put("fixedDueDateScheduleId", fddId.toString());
    CompletableFuture<JsonResponse> createLPCompleted = new CompletableFuture<>();
    client.post(loanPolicyStorageUrl(), loanPolicyRequest, StorageTestSuite.TENANT_ID, ResponseHandler.json(createLPCompleted));
    JsonResponse lpResponse = createLPCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to create loan policy: %s", lpResponse.getBody()), lpResponse, matchesCreated());
    // //////////////////////////////////////////////////////
    // /////////non-existent foreign key
    UUID id2 = UUID.randomUUID();
    JsonObject badLoanPolicyRequest = defaultRollingPolicy().withId(id2).withName("Example Loan Policy").withDescription("An example loan policy").create();
    badLoanPolicyRequest.getJsonObject("loansPolicy").put("fixedDueDateScheduleId", "cb201cd6-296c-4457-9ac4-617d9584e27b");
    CompletableFuture<JsonResponse> createBadLPCompleted = new CompletableFuture<>();
    client.post(loanPolicyStorageUrl(), badLoanPolicyRequest, StorageTestSuite.TENANT_ID, ResponseHandler.json(createBadLPCompleted));
    JsonResponse badlpResponse = createBadLPCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Non-existent foreign key: %s", badlpResponse.getBody()), badlpResponse, matchesUnprocessableEntity());
    // ////////////////////////////////////////
    // /////////bad foreign key
    id2 = UUID.randomUUID();
    JsonObject bad2LoanPolicyRequest = defaultRollingPolicy().withId(id2).withName("Example Loan Policy").withDescription("An example loan policy").create();
    bad2LoanPolicyRequest.getJsonObject("loansPolicy").put("fixedDueDateScheduleId", UUID.randomUUID().toString());
    CompletableFuture<JsonResponse> createBadLP2Completed = new CompletableFuture<>();
    client.post(loanPolicyStorageUrl(), bad2LoanPolicyRequest, StorageTestSuite.TENANT_ID, ResponseHandler.json(createBadLP2Completed));
    JsonResponse badlpResponse2 = createBadLP2Completed.get(5, TimeUnit.SECONDS);
    assertThat("Bad foreign key", badlpResponse2, matchesUnprocessableEntity());
    // ////////////////////////////////////////
    id2 = UUID.randomUUID();
    // ////////// create loan policy with fk to jsonb->'renewalsPolicy'->>'alternateFixedDueDateScheduleId'
    JsonObject loanPolicyRequest4 = defaultRollingPolicy().withId(id2).withName("Example Loan Policy").withDescription("An example loan policy").create();
    JsonObject renewalsPolicy = loanPolicyRequest4.getJsonObject("renewalsPolicy");
    renewalsPolicy.put("alternateFixedDueDateScheduleId", fddId.toString());
    CompletableFuture<JsonResponse> createLPHeirarchyCompleted = new CompletableFuture<>();
    client.post(loanPolicyStorageUrl(), loanPolicyRequest4, StorageTestSuite.TENANT_ID, ResponseHandler.json(createLPHeirarchyCompleted));
    JsonResponse lpHierarchyResponse = createLPHeirarchyCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to create loan policy: %s", lpHierarchyResponse.getBody()), lpHierarchyResponse, matchesCreated());
    // /validation, fk to fixedDueDateScheduleId required once profileid = fixed
    CompletableFuture<JsonResponse> createpdateV2Completed = new CompletableFuture<>();
    JsonObject loanPolicyRequest8 = defaultRollingPolicy().withId(id2).withName("Example Loan Policy").withDescription("An example loan policy").create();
    loanPolicyRequest8.getJsonObject("loansPolicy").put("profileId", "Fixed");
    loanPolicyRequest8.put("renewable", false);
    client.put(loanPolicyStorageUrl("/" + id2), loanPolicyRequest8, StorageTestSuite.TENANT_ID, ResponseHandler.json(createpdateV2Completed));
    JsonResponse updateV2response = createpdateV2Completed.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to create due date: %s", updateV2response.getBody()), updateV2response, matchesUnprocessableEntity());
    // update alternateFixedDueDateScheduleId with a bad (non existent) id
    CompletableFuture<TextResponse> updateCompleted = new CompletableFuture<>();
    renewalsPolicy.put("alternateFixedDueDateScheduleId", "ab201cd6-296c-4457-9ac4-617d9584e27b");
    client.put(loanPolicyStorageUrl("/" + id2), loanPolicyRequest4, StorageTestSuite.TENANT_ID, ResponseHandler.text(updateCompleted));
    TextResponse updateResponse = updateCompleted.get(5, TimeUnit.SECONDS);
    assertThat("update loanPolicy: set alternateFixedDueDateScheduleId = non existent id", updateResponse, matchesBadRequest());
    // delete loan policy //////////////////
    System.out.println("Running: DELETE " + loanPolicyStorageUrl("/" + id2));
    CompletableFuture<Response> delCompleted2 = new CompletableFuture<>();
    client.delete(loanPolicyStorageUrl("/" + id2), StorageTestSuite.TENANT_ID, ResponseHandler.empty(delCompleted2));
    Response delCompleted4Response = delCompleted2.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to delete due date: %s", loanPolicyStorageUrl("/" + id2)), delCompleted4Response.getStatusCode(), isNoContent());
    // /////////////////////////////////////
    // delete loan policy //////////////////
    System.out.println("Running: DELETE " + loanPolicyStorageUrl("/" + id1));
    CompletableFuture<Response> delCompleted9 = new CompletableFuture<>();
    client.delete(loanPolicyStorageUrl("/" + id1), StorageTestSuite.TENANT_ID, ResponseHandler.empty(delCompleted9));
    Response delCompleted5Response = delCompleted9.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to delete due date: %s", loanPolicyStorageUrl("/" + id1)), delCompleted5Response.getStatusCode(), isNoContent());
    // /////////////////////////////////////
    // // try to delete the fdd - not allowed since referenced by loan policy ///////////////////////
    System.out.println("Running: DELETE " + FixedDueDateApiTest.dueDateURL("/" + fddId.toString()));
    CompletableFuture<Response> delCompleted3 = new CompletableFuture<>();
    client.delete(FixedDueDateApiTest.dueDateURL("/" + fddId.toString()), StorageTestSuite.TENANT_ID, ResponseHandler.empty(delCompleted3));
    Response delCompleted6Response = delCompleted3.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to delete due date: %s", FixedDueDateApiTest.dueDateURL("/" + fddId.toString())), delCompleted6Response.getStatusCode(), isNoContent());
    // ////////////////////////////////////////////////////////////////////////////////////////
    // // try to delete all fdds (uses cascade so will succeed) ///////////////////////
    System.out.println("Running: DELETE " + FixedDueDateApiTest.dueDateURL());
    CompletableFuture<Response> delAllCompleted = new CompletableFuture<>();
    client.delete(FixedDueDateApiTest.dueDateURL(), StorageTestSuite.TENANT_ID, ResponseHandler.empty(delAllCompleted));
    Response delAllCompleted4Response = delAllCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to delete due date: %s", FixedDueDateApiTest.dueDateURL()), delAllCompleted4Response.getStatusCode(), isNoContent());
// ////////////////////////////////////////////////////////////////////////////////////////
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonResponse(org.folio.rest.support.JsonResponse) Response(org.folio.rest.support.Response) TextResponse(org.folio.rest.support.TextResponse) CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject) TextResponse(org.folio.rest.support.TextResponse) UUID(java.util.UUID) JsonResponse(org.folio.rest.support.JsonResponse) Test(org.junit.Test)

Example 2 with TextResponse

use of org.folio.rest.support.TextResponse 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 3 with TextResponse

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

the class PatronNoticePoliciesApiTest method cannotDeleteInUsePatronNoticePolicy.

@Test
public void cannotDeleteInUsePatronNoticePolicy() throws InterruptedException, MalformedURLException, TimeoutException, ExecutionException {
    String inUsePolicyId = "16b88363-0d93-464a-967a-ad5ad0f9187c";
    String rulesAsText = "priority: t, s, c, b, a, m, g" + "fallback-policy: l 43198de5-f56a-4a53-a0bd-5a324418967a r 4c6e1fb0-2ef1-4666-bd15-f9190ff89060 " + "n 122b3d2b-4788-4f1e-9117-56daa91cb75c m 1a54b431-2e4f-452d-9cae-9cee66c9a892: " + "l d9cd0bed-1b49-4b5e-a7bd-064b8d177231 r 334e5a9e-94f9-4673-8d1d-ab552863886b n " + inUsePolicyId;
    JsonObject circulationRules = new JsonObject().put("rulesAsText", rulesAsText);
    CompletableFuture<TextResponse> putCompleted = new CompletableFuture<>();
    client.put(rulesStorageUrl(), circulationRules, TENANT_ID, ResponseHandler.text(putCompleted));
    putCompleted.get(5, TimeUnit.SECONDS);
    JsonResponse response = deletePatronNoticePolicy(inUsePolicyId);
    String message = response.getBody();
    assertThat(response.getStatusCode(), is(400));
    assertThat(message, is("Cannot delete in use notice policy"));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject) TextResponse(org.folio.rest.support.TextResponse) JsonResponse(org.folio.rest.support.JsonResponse) Test(org.junit.Test)

Example 4 with TextResponse

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

the class RequestBatchAPITest method willAbortBatchUpdateOnNullPointerExceptionDueToNoIdInRequest.

@Test
public void willAbortBatchUpdateOnNullPointerExceptionDueToNoIdInRequest() throws Exception {
    UUID itemId = UUID.randomUUID();
    JsonObject firstRequest = createRequestForItemAtPosition(itemId, 1);
    JsonObject secondRequest = createRequestForItemAtPosition(itemId, 2);
    JsonObject firstRequestCopy = firstRequest.copy();
    firstRequestCopy.remove("id");
    TextResponse reorderResponse = attemptReorderRequests(ResponseHandler::text, new ReorderRequest(firstRequestCopy, 3), new ReorderRequest(secondRequest, 10));
    assertThat(reorderResponse.getStatusCode(), is(500));
    assertRequestsNotUpdated(itemId, firstRequest, secondRequest);
}
Also used : ResponseHandler(org.folio.rest.support.ResponseHandler) JsonObject(io.vertx.core.json.JsonObject) TextResponse(org.folio.rest.support.TextResponse) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with TextResponse

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

the class RequestBatchAPITest method willAbortBatchUpdateOnPopulateMetadataException.

@Test
public void willAbortBatchUpdateOnPopulateMetadataException() throws Exception {
    CompletableFuture<TextResponse> postCompleted = new CompletableFuture<>();
    new RequestsBatchAPI().postRequestStorageBatchRequests(null, null, result -> postCompleted.complete(new TextResponse(result.result().getStatus(), result.result().getEntity().toString())), null);
    TextResponse response = postCompleted.get(5, TimeUnit.SECONDS);
    assertThat(response.getStatusCode(), is(500));
    assertThat(response.getBody(), containsString("Cannot populate metadata"));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) TextResponse(org.folio.rest.support.TextResponse) RequestsBatchAPI(org.folio.rest.impl.RequestsBatchAPI) Test(org.junit.Test)

Aggregations

TextResponse (org.folio.rest.support.TextResponse)13 Test (org.junit.Test)10 CompletableFuture (java.util.concurrent.CompletableFuture)9 JsonObject (io.vertx.core.json.JsonObject)8 UUID (java.util.UUID)6 DateTime (org.joda.time.DateTime)5 RequestRequestBuilder (org.folio.rest.support.builders.RequestRequestBuilder)4 IndividualResource (org.folio.rest.support.IndividualResource)3 JsonResponse (org.folio.rest.support.JsonResponse)3 Parameters (junitparams.Parameters)2 JsonArray (io.vertx.core.json.JsonArray)1 RequestsBatchAPI (org.folio.rest.impl.RequestsBatchAPI)1 Response (org.folio.rest.support.Response)1 ResponseHandler (org.folio.rest.support.ResponseHandler)1 LoanRequestBuilder (org.folio.rest.support.builders.LoanRequestBuilder)1 Description (org.hamcrest.Description)1 TypeSafeDiagnosingMatcher (org.hamcrest.TypeSafeDiagnosingMatcher)1 Ignore (org.junit.Ignore)1