Search in sources :

Example 1 with JsonResponse

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

the class LoanPoliciesApiTest method canGetAllPolicies.

@Test
public void canGetAllPolicies() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
    String firstPolicyId = createLoanPolicy(defaultRollingPolicy().create()).getId();
    String secondPolicyId = createLoanPolicy(defaultRollingPolicy().create()).getId();
    String thirdPolicyId = createLoanPolicy(defaultRollingPolicy().create()).getId();
    String fourthPolicyId = createLoanPolicy(defaultRollingPolicy().create()).getId();
    CompletableFuture<JsonResponse> getAllCompleted = new CompletableFuture<>();
    client.get(loanPolicyStorageUrl(), StorageTestSuite.TENANT_ID, ResponseHandler.json(getAllCompleted));
    JsonResponse getAllResponse = getAllCompleted.get(5, TimeUnit.SECONDS);
    MatcherAssert.assertThat(String.format("Failed to get policies: %s", getAllResponse.getBody()), getAllResponse.getStatusCode(), is(200));
    JsonObject firstPage = getAllResponse.getJson();
    List<JsonObject> firstPolicyResults = JsonArrayHelper.toList(firstPage.getJsonArray("loanPolicies"));
    assertThat(firstPolicyResults.size(), is(4));
    assertThat(firstPage.getInteger("totalRecords"), is(4));
    assertThat(hasRecordWithId(firstPolicyResults, firstPolicyId), is(true));
    assertThat(hasRecordWithId(firstPolicyResults, secondPolicyId), is(true));
    assertThat(hasRecordWithId(firstPolicyResults, thirdPolicyId), is(true));
    assertThat(hasRecordWithId(firstPolicyResults, fourthPolicyId), is(true));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject) JsonResponse(org.folio.rest.support.JsonResponse) Test(org.junit.Test)

Example 2 with JsonResponse

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

the class LoanPoliciesApiTest method canCreateLoanPolicyWithItemLimitWithinBounds.

@Test
public void canCreateLoanPolicyWithItemLimitWithinBounds() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
    CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
    UUID id = UUID.randomUUID();
    JsonObject loanPolicyRequest = defaultRollingPolicy().withId(id).withName("Example Loan Policy").withDescription("An example loan policy").create();
    loanPolicyRequest.getJsonObject("loansPolicy").put("itemLimit", 1000);
    client.post(loanPolicyStorageUrl(), loanPolicyRequest, StorageTestSuite.TENANT_ID, ResponseHandler.json(createCompleted));
    JsonResponse response = createCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Failed to create loan policy: %s", response.getBody()), response, matchesCreated());
    assertThat(response.getJson().getJsonObject("loansPolicy").getInteger("itemLimit"), is(1000));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject) UUID(java.util.UUID) JsonResponse(org.folio.rest.support.JsonResponse) Test(org.junit.Test)

Example 3 with JsonResponse

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

the class LoanPoliciesApiTest method cannotCreateALoanPolicyWithInvalidPeriodInterval.

@Test
public void cannotCreateALoanPolicyWithInvalidPeriodInterval() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
    CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
    UUID id = UUID.randomUUID();
    JsonObject loanPolicyRequest = defaultRollingPolicy().withId(id).withName("Example Loan Policy").withDescription("An example loan policy").create();
    JsonObject period = new JsonObject();
    period.put("duration", 1);
    period.put("intervalId", "Foo");
    loanPolicyRequest.getJsonObject("loansPolicy").put("period", period);
    client.post(loanPolicyStorageUrl(), loanPolicyRequest, StorageTestSuite.TENANT_ID, ResponseHandler.json(createCompleted));
    JsonResponse response = createCompleted.get(5, TimeUnit.SECONDS);
    assertThat(String.format("Should fail to create loan policy: %s", response.getBody()), response, matchesBadRequest());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject) UUID(java.util.UUID) JsonResponse(org.folio.rest.support.JsonResponse) Test(org.junit.Test)

Example 4 with JsonResponse

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

the class LoanPoliciesApiTest method canSearchForLoanPolicyById.

@Test
public void canSearchForLoanPolicyById() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
    String firstPolicyId = createLoanPolicy(defaultRollingPolicy().create()).getId();
    String secondPolicyId = createLoanPolicy(defaultRollingPolicy().create()).getId();
    String queryTemplate = "id=\"%s\"";
    CompletableFuture<JsonResponse> searchForFirstPolicyCompleted = new CompletableFuture<>();
    CompletableFuture<JsonResponse> searchForSecondPolicyCompleted = new CompletableFuture<>();
    client.get(loanPolicyStorageUrl() + "?query=" + URLEncoder.encode(String.format(queryTemplate, firstPolicyId), UTF_8), StorageTestSuite.TENANT_ID, ResponseHandler.json(searchForFirstPolicyCompleted));
    client.get(loanPolicyStorageUrl() + "?query=" + URLEncoder.encode(String.format(queryTemplate, secondPolicyId), UTF_8), StorageTestSuite.TENANT_ID, ResponseHandler.json(searchForSecondPolicyCompleted));
    JsonResponse firstPolicySearchResponse = searchForFirstPolicyCompleted.get(5, TimeUnit.SECONDS);
    JsonResponse secondPolicySearchResponse = searchForSecondPolicyCompleted.get(5, TimeUnit.SECONDS);
    MatcherAssert.assertThat(String.format("Failed to get policy by id: %s", firstPolicySearchResponse.getBody()), firstPolicySearchResponse.getStatusCode(), is(200));
    MatcherAssert.assertThat(String.format("Failed to get policy by id: %s", secondPolicySearchResponse.getBody()), secondPolicySearchResponse.getStatusCode(), is(200));
    JsonObject firstPage = firstPolicySearchResponse.getJson();
    JsonObject secondPage = secondPolicySearchResponse.getJson();
    List<JsonObject> firstPolicyResults = JsonArrayHelper.toList(firstPage.getJsonArray("loanPolicies"));
    List<JsonObject> secondPolicyResults = JsonArrayHelper.toList(secondPage.getJsonArray("loanPolicies"));
    assertThat(firstPolicyResults.size(), is(1));
    assertThat(firstPage.getInteger("totalRecords"), is(1));
    assertThat(hasRecordWithId(firstPolicyResults, firstPolicyId), is(true));
    assertThat(secondPolicyResults.size(), is(1));
    assertThat(secondPage.getInteger("totalRecords"), is(1));
    assertThat(hasRecordWithId(firstPolicyResults, firstPolicyId), is(true));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject) JsonResponse(org.folio.rest.support.JsonResponse) Test(org.junit.Test)

Example 5 with JsonResponse

use of org.folio.rest.support.JsonResponse 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)

Aggregations

JsonResponse (org.folio.rest.support.JsonResponse)156 Test (org.junit.Test)133 JsonObject (io.vertx.core.json.JsonObject)91 CompletableFuture (java.util.concurrent.CompletableFuture)91 UUID (java.util.UUID)52 RequestRequestBuilder (org.folio.rest.support.builders.RequestRequestBuilder)20 JsonArray (io.vertx.core.json.JsonArray)19 RequestPolicy (org.folio.rest.jaxrs.model.RequestPolicy)18 IndividualResource (org.folio.rest.support.IndividualResource)15 LoanRequestBuilder (org.folio.rest.support.builders.LoanRequestBuilder)12 URL (java.net.URL)11 RequestType (org.folio.rest.jaxrs.model.RequestType)10 RequestPreference (org.folio.rest.jaxrs.model.RequestPreference)9 StaffSlipRequestBuilder (org.folio.rest.support.builders.StaffSlipRequestBuilder)9 Response (org.folio.rest.support.Response)8 ResponseHandler (org.folio.rest.support.ResponseHandler)8 DateTime (org.joda.time.DateTime)7 MalformedURLException (java.net.MalformedURLException)6 ExecutionException (java.util.concurrent.ExecutionException)6 TimeoutException (java.util.concurrent.TimeoutException)6