Skip to content

Commit

Permalink
Merge pull request #221 from folio-org/CIRCSTORE-170
Browse files Browse the repository at this point in the history
[CIRCSTORE-170] Add Item Limit to Loan Policy
  • Loading branch information
OleksandrVidinieiev committed Nov 18, 2019
2 parents 94f96f6 + 977fd9c commit aed73ca
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 3 deletions.
2 changes: 1 addition & 1 deletion descriptors/ModuleDescriptor-template.json
Expand Up @@ -80,7 +80,7 @@
},
{
"id": "loan-policy-storage",
"version": "2.0",
"version": "2.1",
"handlers": [
{
"methods": ["GET"],
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -52,6 +52,11 @@
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>

<properties>
Expand Down
3 changes: 2 additions & 1 deletion ramls/examples/loan-policy.json
Expand Up @@ -13,7 +13,8 @@
"gracePeriod": {
"duration": 7,
"intervalId": "Days"
}
},
"itemLimit": 1000
},
"renewable": true,
"renewalsPolicy": {
Expand Down
2 changes: 1 addition & 1 deletion ramls/loan-policy-storage.raml
@@ -1,6 +1,6 @@
#%RAML 1.0
title: Loan Policy Storage
version: v2.0
version: v2.1
protocols: [ HTTP, HTTPS ]
baseUri: http://localhost:9130

Expand Down
6 changes: 6 additions & 0 deletions ramls/loan-policy.json
Expand Up @@ -44,6 +44,12 @@
"fixedDueDateScheduleId": {
"type": "string",
"description": "Fixed due date schedule (due date limit)"
},
"itemLimit": {
"type": "integer",
"description": "Number of items allowed",
"minimum": 1,
"maximum": 9999
}
}
},
Expand Down
106 changes: 106 additions & 0 deletions src/test/java/org/folio/rest/api/LoanPoliciesApiTest.java
Expand Up @@ -314,6 +314,108 @@ public void cannotCreateALoanPolicyWithAdditionalPropertiesInLoanPolicy()
response.getStatusCode(), is(HttpStatus.HTTP_VALIDATION_ERROR.toInt()));
}

@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.getStatusCode(), is(HttpURLConnection.HTTP_CREATED));
assertThat(response.getJson().getJsonObject("loansPolicy").getInteger("itemLimit"), is(1000));
}

@Test
public void cannotCreateLoanPolicyWithItemLimitBelowMinimum()
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", 0);

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.getStatusCode(), is(HttpStatus.HTTP_VALIDATION_ERROR.toInt()));

JsonObject error = getErrorFromResponse(response);
JsonObject parameters = error.getJsonArray("parameters").getJsonObject(0);

assertThat(error.getString("message"), is("must be greater than or equal to 1"));
assertThat(parameters.getString("key"), is("loansPolicy.itemLimit"));
assertThat(parameters.getString("value"), is("0"));
}

@Test
public void cannotCreateLoanPolicyWithItemLimitAboveMaximum()
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", 10_000);

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.getStatusCode(), is(HttpStatus.HTTP_VALIDATION_ERROR.toInt()));

JsonObject error = getErrorFromResponse(response);
JsonObject parameters = error.getJsonArray("parameters").getJsonObject(0);

assertThat(error.getString("message"), is("must be less than or equal to 9999"));
assertThat(parameters.getString("key"), is("loansPolicy.itemLimit"));
assertThat(parameters.getString("value"), is("10000"));
}

@Test
public void cannotCreateALoanPolicyWithInvalidPeriodInterval()
throws MalformedURLException,
Expand Down Expand Up @@ -887,4 +989,8 @@ private IndividualResource createFixedDueDateSchedule(String name,

return new IndividualResource(response);
}

private JsonObject getErrorFromResponse(JsonResponse response) {
return response.getJson().getJsonArray("errors").getJsonObject(0);
}
}

0 comments on commit aed73ca

Please sign in to comment.