Merge pull request #1017 from rishy/Batch-API

bug fixes, added api docs for Batch API and Client activation command
This commit is contained in:
Vishwas Babu A J
2014-07-17 09:12:30 -07:00
7 changed files with 493 additions and 16 deletions
@@ -203,13 +203,17 @@ public class BatchApiTest {
// Create a createClient Request
final BatchRequest br1 = BatchHelper.createClientRequest(4718L, "");
// Create a activateClient Request
final BatchRequest br2 = BatchHelper.activateClientRequest(4719L, 4718L);
// Create a ApplyLoan Request
final BatchRequest br2 = BatchHelper.applyLoanRequest(4719L, 4718L, productId);
final BatchRequest br3 = BatchHelper.applyLoanRequest(4720L, 4719L, productId);
final List<BatchRequest> batchRequests = new ArrayList<>();
batchRequests.add(br1);
batchRequests.add(br2);
batchRequests.add(br3);
final String jsonifiedRequest = BatchHelper.toJsonString(batchRequests);
@@ -244,13 +248,17 @@ public class BatchApiTest {
// Create a createClient Request
final BatchRequest br1 = BatchHelper.createClientRequest(4720L, "");
// Create a activateClient Request
final BatchRequest br2 = BatchHelper.activateClientRequest(4721L, 4720L);
// Create a applySavings Request
final BatchRequest br2 = BatchHelper.applySavingsRequest(4721L, 4720L, productId);
final BatchRequest br3 = BatchHelper.applySavingsRequest(4722L, 4721L, productId);
final List<BatchRequest> batchRequests = new ArrayList<>();
batchRequests.add(br1);
batchRequests.add(br2);
batchRequests.add(br3);
final String jsonifiedRequest = BatchHelper.toJsonString(batchRequests);
@@ -288,14 +296,14 @@ public class BatchApiTest {
// Create a createClient Request
final BatchRequest br1 = BatchHelper.createClientRequest(4722L, "");
// Create a ApplyLoan Request
final BatchRequest br2 = BatchHelper.applyLoanRequest(4723L, 4722L, productId);
// Create a activateClient Request
final BatchRequest br2 = BatchHelper.activateClientRequest(4723L, 4722L);
// Create a createCharge Request
final BatchRequest br3 = BatchHelper.createChargeRequest(4724L, 4723L);
// Create a ApplyLoan Request
final BatchRequest br3 = BatchHelper.applyLoanRequest(4724L, 4723L, productId);
// Create a Collect Charges Request
final BatchRequest br4 = BatchHelper.collectChargesRequest(4725L, 4723L);
final BatchRequest br4 = BatchHelper.collectChargesRequest(4725L, 4724L);
final List<BatchRequest> batchRequests = new ArrayList<>();
@@ -309,7 +317,36 @@ public class BatchApiTest {
final List<BatchResponse> response = BatchHelper.postBatchRequestsWithoutEnclosingTransaction(this.requestSpec, this.responseSpec,
jsonifiedRequest);
Assert.assertEquals("Verify Status Code 200 for Create Charge", 200L, (long) response.get(2).getStatusCode());
Assert.assertEquals("Verify Status Code 200 for Collect Charges", 200L, (long) response.get(3).getStatusCode());
Assert.assertEquals("Verify Status Code 200 for Create Loan Charge", 200L, (long) response.get(3).getStatusCode());
}
/**
* Test for the successful activation of a pending client using
* 'ActivateClientCommandStrategy'. A '200' status code is expected on
* successful activation.
*
* @see org.mifosplatform.batch.command.internal.ActivateClientCommandStrategy
*/
@Test
public void shouldReturnOkStatusOnSuccessfulClientActivation() {
// Create a createClient Request
final BatchRequest br1 = BatchHelper.createClientRequest(4726L, "");
// Create a activateClient Request
final BatchRequest br2 = BatchHelper.activateClientRequest(4727L, 4726L);
final List<BatchRequest> batchRequests = new ArrayList<>();
batchRequests.add(br1);
batchRequests.add(br2);
final String jsonifiedRequest = BatchHelper.toJsonString(batchRequests);
final List<BatchResponse> response = BatchHelper.postBatchRequestsWithoutEnclosingTransaction(this.requestSpec, this.responseSpec,
jsonifiedRequest);
Assert.assertEquals("Verify Status Code 200 for Create Charge", 200L, (long) response.get(0).getStatusCode());
Assert.assertEquals("Verify Status Code 200 for Collect Charges", 200L, (long) response.get(1).getStatusCode());
}
}
@@ -130,8 +130,7 @@ public class BatchHelper {
}
final String body = "{ \"officeId\": 1, \"firstname\": \"Petra\", \"lastname\": \"Yton\"," + "\"externalId\": " + extId
+ ", \"dateFormat\": \"dd MMMM yyyy\", \"locale\": \"en\","
+ "\"active\": true, \"activationDate\": \"04 March 2009\", \"submittedOnDate\": \"04 March 2009\"}";
+ ", \"dateFormat\": \"dd MMMM yyyy\", \"locale\": \"en\"," + "\"active\": false, \"submittedOnDate\": \"04 March 2009\"}";
br.setBody(body);
@@ -244,7 +243,7 @@ public class BatchHelper {
/**
* Creates and returns a
* {@link org.mifosplatform.batch.command.internal.CollectChargesCommandStrategy}
* Request with given requestId and reference
* Request with given requestId and reference.
*
* @param requestId
* @param reference
@@ -263,6 +262,29 @@ public class BatchHelper {
return br;
}
/**
* Creates and returns a
* {@link org.mifosplatform.batch.command.internal.ActivateClientCommandStrategy}
* Request with given requestId and reference.
*
*
* @param requestId
* @param reference
* @return BatchRequest
*/
public static BatchRequest activateClientRequest(final Long requestId, final Long reference) {
final BatchRequest br = new BatchRequest();
br.setRequestId(requestId);
br.setRelativeUrl("clients/$.clientId?command=activate");
br.setReference(reference);
br.setMethod("POST");
br.setBody("{ \"locale\": \"en\", \"dateFormat\": \"dd MMMM yyyy\", \"activationDate\": \"01 March 2011\"}");
return br;
}
/**
* Checks that the client with given externalId is not created on the
* server.
@@ -74,6 +74,8 @@ public class CommandStrategyProvider {
this.commandStrategies.put(CommandContext.resource("loans\\/\\d+\\/charges").method("POST").build(), "createChargeCommandStrategy");
this.commandStrategies
.put(CommandContext.resource("loans\\/\\d+\\/charges").method("GET").build(), "collectChargesCommandStrategy");
this.commandStrategies.put(CommandContext.resource("clients\\/\\d+\\?command=activate").method("POST").build(),
"activateClientCommandStrategy");
}
}
@@ -0,0 +1,76 @@
package org.mifosplatform.batch.command.internal;
import javax.ws.rs.core.UriInfo;
import org.mifosplatform.batch.command.CommandStrategy;
import org.mifosplatform.batch.domain.BatchRequest;
import org.mifosplatform.batch.domain.BatchResponse;
import org.mifosplatform.batch.exception.ErrorHandler;
import org.mifosplatform.batch.exception.ErrorInfo;
import org.mifosplatform.portfolio.client.api.ClientsApiResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Implements {@link org.mifosplatform.batch.command.CommandStrategy} to handle
* activation of a pending client. It passes the contents of the body from the
* BatchRequest to
* {@link org.mifosplatform.portfolio.client.api.ClientsApiResource} and gets
* back the response. This class will also catch any errors raised by
* {@link org.mifosplatform.portfolio.client.api.ClientsApiResource} and map
* those errors to appropriate status codes in BatchResponse.
*
* @author Rishabh Shukla
*
* @see org.mifosplatform.batch.command.CommandStrategy
* @see org.mifosplatform.batch.domain.BatchRequest
* @see org.mifosplatform.batch.domain.BatchResponse
*/
@Component
public class ActivateClientCommandStrategy implements CommandStrategy {
private final ClientsApiResource clientsApiResource;
@Autowired
public ActivateClientCommandStrategy(final ClientsApiResource clientsApiResource) {
this.clientsApiResource = clientsApiResource;
}
@Override
public BatchResponse execute(final BatchRequest request, @SuppressWarnings("unused") UriInfo uriInfo) {
final BatchResponse response = new BatchResponse();
final String responseBody;
response.setRequestId(request.getRequestId());
response.setHeaders(request.getHeaders());
final String[] pathParameters = request.getRelativeUrl().split("/");
Long clientId = Long.parseLong(pathParameters[1].substring(0, pathParameters[1].indexOf("?")));
// Try-catch blocks to map exceptions to appropriate status codes
try {
// Calls 'activate' function from 'ClientsApiResource' to create a new
// client
responseBody = clientsApiResource.activate(clientId, "activate", request.getBody());
response.setStatusCode(200);
// Sets the body of the response after the successful activation of
// the client
response.setBody(responseBody);
} catch (RuntimeException e) {
// Gets an object of type ErrorInfo, containing information about
// raised exception
ErrorInfo ex = ErrorHandler.handler(e);
response.setStatusCode(ex.getStatusCode());
response.setBody(ex.getMessage());
}
return response;
}
}
@@ -140,7 +140,6 @@ public class BatchApiServiceImpl implements BatchApiService {
childResponse.setRequestId(childRequest.getRequestId());
childResponse.setStatusCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
childResponse.setBody(ex.getMessage());
}
childResponses.add(childResponse);
@@ -141,13 +141,19 @@ public class ResolutionHelper {
if (relativeUrl.contains("$.")) {
String queryParams = "";
if(relativeUrl.contains("?")) {
queryParams = relativeUrl.substring(relativeUrl.indexOf("?"));
relativeUrl = relativeUrl.substring(0, relativeUrl.indexOf("?"));
}
final String[] parameters = relativeUrl.split("/");
for (String parameter : parameters) {
if (parameter.contains("$.")) {
final String resParamValue = responseJsonModel.get(parameter).toString();
relativeUrl = relativeUrl.replace(parameter, resParamValue);
br.setRelativeUrl(relativeUrl);
br.setRelativeUrl(relativeUrl+queryParams);
}
}
}