FINERACT-1760: Batch API for datatable query update

This commit is contained in:
Adam Saghy
2022-12-07 22:46:16 +01:00
committed by Arnold Gálovics
parent 7dfbc2534d
commit 9999e08de2
6 changed files with 428 additions and 7 deletions
@@ -114,9 +114,14 @@ public class CommandStrategyProvider {
"createDatatableEntryCommandStrategy");
commandStrategies.put(CommandContext.resource("datatables\\/[a-zA-Z0-9_]*\\/\\d+\\/\\d+").method("PUT").build(),
"updateDatatableEntryOneToManyCommandStrategy");
commandStrategies.put(CommandContext.resource("datatables\\/[a-zA-Z0-9_]*\\/\\d+").method("PUT").build(),
"updateDatatableEntryOneToOneCommandStrategy");
commandStrategies.put(
CommandContext.resource("datatables\\/[a-zA-Z0-9_]*\\/\\d+(\\?(\\w+(?:\\=[\\w,]+|&)+)+)").method("GET").build(),
"getDatatableEntryByAppTableIdCommandStrategy");
commandStrategies.put(
CommandContext.resource("datatables\\/[a-zA-Z0-9_]*\\/query(\\?(\\w+(?:\\=[\\w,]+|&)+)+)").method("GET").build(),
"getDatatableEntryByQueryCommandStrategy");
}
}
@@ -0,0 +1,99 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.batch.command.internal;
import java.util.Map;
import javax.ws.rs.core.UriInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.batch.command.CommandStrategy;
import org.apache.fineract.batch.command.CommandStrategyUtils;
import org.apache.fineract.batch.domain.BatchRequest;
import org.apache.fineract.batch.domain.BatchResponse;
import org.apache.fineract.infrastructure.core.api.MutableUriInfo;
import org.apache.fineract.infrastructure.dataqueries.api.DatatablesApiResource;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
/**
* Implements {@link CommandStrategy} and get datatable entry by query. It passes the contents of the body from the
* BatchRequest to {@link DatatablesApiResource} and gets back the response. This class will also catch any errors
* raised by {@link DatatablesApiResource} and map those errors to appropriate status codes in BatchResponse.
*/
@Component
@RequiredArgsConstructor
public class GetDatatableEntryByQueryCommandStrategy implements CommandStrategy {
/**
* Data table api resource {@link DatatablesApiResource}.
*/
private final DatatablesApiResource dataTablesApiResource;
@Override
public BatchResponse execute(final BatchRequest request, @SuppressWarnings("unused") UriInfo uriInfo) {
final MutableUriInfo parameterizedUriInfo = new MutableUriInfo(uriInfo);
final BatchResponse response = new BatchResponse();
final String responseBody;
response.setRequestId(request.getRequestId());
response.setHeaders(request.getHeaders());
final String relativeUrl = request.getRelativeUrl();
final String relativeUrlSubString = StringUtils.substringAfter(relativeUrl, "/");
// uriInfo will contain the query parameter value(s) that are sent in the actual batch uri.
// for example: batches?enclosingTransaction=true
// But the query parameters that are sent in the batch relative url has to be sent to
// datatablesApiResource.getDatatable
// To use the relative url query parameters
// - Parse and fetch the query parameters sent in the relative url
// (datatables/dt_123/1?genericResultSet=true)
// - Add them to the MutableUriInfo query parameters list
// - Call datatablesApiResource.getDatatable(dataTable, appTableId, null, uriInfo)
String columnFilter = null;
String valueFilter = null;
String resultColumns = null;
if (relativeUrl.indexOf('?') > 0) {
Map<String, String> queryParameters = CommandStrategyUtils.getQueryParameters(relativeUrl);
// Add the query parameters sent in the relative URL to MutableUriInfo
CommandStrategyUtils.addQueryParametersToUriInfo(parameterizedUriInfo, queryParameters);
for (Map.Entry<String, String> entry : queryParameters.entrySet()) {
switch (entry.getKey()) {
case "columnFilter" -> columnFilter = entry.getValue();
case "valueFilter" -> valueFilter = entry.getValue();
case "resultColumns" -> resultColumns = entry.getValue();
}
}
}
String dataTableName = relativeUrlSubString.substring(0, relativeUrlSubString.indexOf("/"));
// Calls 'queryValues' function from 'DatatablesApiResource' to
// get the datatable details based on the filters
responseBody = dataTablesApiResource.queryValues(dataTableName, columnFilter, valueFilter, resultColumns, parameterizedUriInfo);
response.setStatusCode(HttpStatus.SC_OK);
// Sets the response after retrieving the datatable
response.setBody(responseBody);
return response;
}
}
@@ -29,9 +29,9 @@ import org.apache.fineract.infrastructure.dataqueries.api.DatatablesApiResource;
import org.springframework.stereotype.Component;
/**
* Implements {@link CommandStrategy} and updates a datatable entry for a given loan. It passes the contents of the body
* from the BatchRequest to {@link DatatablesApiResource} and gets back the response. This class will also catch any
* errors raised by {@link DatatablesApiResource} and map those errors to appropriate status codes in BatchResponse.
* Implements {@link CommandStrategy} and updates a datatable entry for a given entity. It passes the contents of the
* body from the BatchRequest to {@link DatatablesApiResource} and gets back the response. This class will also catch
* any errors raised by {@link DatatablesApiResource} and map those errors to appropriate status codes in BatchResponse.
*
* @see CommandStrategy
* @see BatchRequest
@@ -53,14 +53,14 @@ public class UpdateDatatableEntryOneToManyCommandStrategy implements CommandStra
response.setHeaders(request.getHeaders());
final List<String> pathParameters = Splitter.on('/').splitToList(request.getRelativeUrl());
// Pluck out the datatable name, loanId & datatable entry id out of the relative path
// Pluck out the datatable name, entity id & datatable entry id out of the relative path
final String datatableName = pathParameters.get(1);
final Long loanId = Long.parseLong(pathParameters.get(2));
final Long entityId = Long.parseLong(pathParameters.get(2));
final Long datatableEntryId = Long.parseLong(pathParameters.get(3));
// Calls 'updateDatatableEntryOneToMany' function from
// 'DatatablesApiResource' to update a datatable entry on an existing loan in a one-many relationship
responseBody = datatablesApiResource.updateDatatableEntryOneToMany(datatableName, loanId, datatableEntryId, request.getBody());
// 'DatatablesApiResource' to update a datatable entry on an existing entity in a one-many relationship
responseBody = datatablesApiResource.updateDatatableEntryOneToMany(datatableName, entityId, datatableEntryId, request.getBody());
response.setStatusCode(200);
// Sets the body of the response after datatable entry is successfully
@@ -0,0 +1,71 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.batch.command.internal;
import com.google.common.base.Splitter;
import java.util.List;
import javax.ws.rs.core.UriInfo;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.batch.command.CommandStrategy;
import org.apache.fineract.batch.domain.BatchRequest;
import org.apache.fineract.batch.domain.BatchResponse;
import org.apache.fineract.infrastructure.dataqueries.api.DatatablesApiResource;
import org.springframework.stereotype.Component;
/**
* Implements {@link CommandStrategy} and updates a datatable entry for a given entity. It passes the contents of the
* body from the BatchRequest to {@link DatatablesApiResource} and gets back the response. This class will also catch
* any errors raised by {@link DatatablesApiResource} and map those errors to appropriate status codes in BatchResponse.
*
* @see CommandStrategy
* @see BatchRequest
* @see BatchResponse
*/
@Component
@RequiredArgsConstructor
public class UpdateDatatableEntryOneToOneCommandStrategy implements CommandStrategy {
private final DatatablesApiResource datatablesApiResource;
@Override
public BatchResponse execute(BatchRequest request, @SuppressWarnings("unused") UriInfo uriInfo) {
final BatchResponse response = new BatchResponse();
final String responseBody;
response.setRequestId(request.getRequestId());
response.setHeaders(request.getHeaders());
final List<String> pathParameters = Splitter.on('/').splitToList(request.getRelativeUrl());
// Pluck out the datatable name, entity id & datatable entry id out of the relative path
final String datatableName = pathParameters.get(1);
final Long entityId = Long.parseLong(pathParameters.get(2));
// Calls 'updateDatatableEntryOneToMany' function from
// 'DatatablesApiResource' to update a datatable entry on an existing entity in a one-one relationship
responseBody = datatablesApiResource.updateDatatableEntryOnetoOne(datatableName, entityId, request.getBody());
response.setStatusCode(200);
// Sets the body of the response after datatable entry is successfully
// updated
response.setBody(responseBody);
return response;
}
}
@@ -1631,6 +1631,173 @@ public class BatchApiTest {
Assertions.assertEquals(date, payoutRefundDate);
}
/**
* Test for finding datatable entry by the query API and update its value
*/
@Test
public void shouldFindOneToOneDatatableEntryByQueryAPIAndUpdateOneOfItsColumn() {
final String datatableName = Utils.randomNameGenerator(LOAN_APP_TABLE_NAME + "_", 5).toLowerCase();
final FromJsonHelper jsonHelper = new FromJsonHelper();
final Long loanId = jsonHelper.extractLongNamed("loanId", jsonHelper.parse(setupAccount()).getAsJsonObject());
// creating datatable with m_loan association
final Map<String, Object> columnMap = new HashMap<>();
final List<HashMap<String, Object>> datatableColumnsList = new ArrayList<>();
final String columnName1 = Utils.randomNameGenerator("COL1_", 5).toLowerCase();
final String columnName2 = Utils.randomNameGenerator("COL2_", 5).toLowerCase();
columnMap.put("datatableName", datatableName);
columnMap.put("apptableName", LOAN_APP_TABLE_NAME);
columnMap.put("entitySubType", "PERSON");
columnMap.put("multiRow", false);
DatatableHelper.addDatatableColumns(datatableColumnsList, columnName1, "String", true, 10, null);
DatatableHelper.addDatatableColumns(datatableColumnsList, columnName2, "String", false, 10, null);
columnMap.put("columns", datatableColumnsList);
final String datatableRequestJsonString = new Gson().toJson(columnMap);
LOG.info("CreateDataTable map : {}", datatableRequestJsonString);
this.datatableHelper.createDatatable(datatableRequestJsonString, "");
// Create a datatable entry so that it can be updated using BatchApi
final Map<String, Object> datatableEntryMap = new HashMap<>();
String columnValue1 = Utils.randomNameGenerator("VAL1_", 3);
String columnValue2 = Utils.randomNameGenerator("VAL2_", 3);
datatableEntryMap.put(columnName1, columnValue1);
datatableEntryMap.put(columnName2, columnValue2);
final String datatableEntryRequestJsonString = new Gson().toJson(datatableEntryMap);
LOG.info("CreateDataTableEntry map : {}", datatableEntryRequestJsonString);
final Map<String, Object> datatableEntryResponse = this.datatableHelper.createDatatableEntry(datatableName, loanId.intValue(),
false, datatableEntryRequestJsonString);
final Integer datatableEntryResourceId = (Integer) datatableEntryResponse.get("resourceId");
assertNotNull(datatableEntryResourceId, "ERROR IN CREATING THE ENTITY DATATABLE RECORD");
final BatchRequest queryDatatableEntriesRequest = BatchHelper.queryDatatableEntries(datatableName, columnName1, columnValue1,
"loan_id");
final BatchRequest updateDatatableEntry = BatchHelper.updateDatatableEntry(datatableName, "$.[0].loan_id", columnName2,
columnValue2 + "1");
final List<BatchRequest> batchRequestsToQueryAndUpdateDatatableEntries = Arrays.asList(queryDatatableEntriesRequest,
updateDatatableEntry);
LOG.info("Batch Request : {}", BatchHelper.toJsonString(batchRequestsToQueryAndUpdateDatatableEntries));
final List<BatchResponse> responseOfQuertAndUpdateDatatableBatch = BatchHelper.postBatchRequestsWithEnclosingTransaction(
this.requestSpec, this.responseSpec, BatchHelper.toJsonString(batchRequestsToQueryAndUpdateDatatableEntries));
LOG.info("Batch Response : {}", new Gson().toJson(responseOfQuertAndUpdateDatatableBatch));
final BatchResponse batchQueryAndUpdateResponse1 = responseOfQuertAndUpdateDatatableBatch.get(0);
final BatchResponse batchQueryAndUpdateResponse2 = responseOfQuertAndUpdateDatatableBatch.get(1);
Assertions.assertEquals(HttpStatus.SC_OK, batchQueryAndUpdateResponse1.getStatusCode(),
"Verify Status Code 200 for create datatable entry");
Assertions.assertEquals(HttpStatus.SC_OK, batchQueryAndUpdateResponse2.getStatusCode(),
"Verify Status Code 200 for update datatable entry");
final JsonObject changes = jsonHelper.parse(batchQueryAndUpdateResponse2.getBody()).getAsJsonObject().get("changes")
.getAsJsonObject();
Assertions.assertEquals(changes.get(columnName2).getAsString(), columnValue2 + "1");
}
/**
* Test for finding datatable entry by the query API and update its value
*/
@Test
public void shouldFindOneToManyDatatableEntryByQueryAPIAndUpdateOneOfItsColumn() {
final String datatableName = Utils.randomNameGenerator(LOAN_APP_TABLE_NAME + "_", 5).toLowerCase();
final FromJsonHelper jsonHelper = new FromJsonHelper();
final Long loanId = jsonHelper.extractLongNamed("loanId", jsonHelper.parse(setupAccount()).getAsJsonObject());
// creating datatable with m_loan association
final Map<String, Object> columnMap = new HashMap<>();
final List<HashMap<String, Object>> datatableColumnsList = new ArrayList<>();
final String columnName1 = Utils.randomNameGenerator("COL1_", 5).toLowerCase();
final String columnName2 = Utils.randomNameGenerator("COL2_", 5).toLowerCase();
columnMap.put("datatableName", datatableName);
columnMap.put("apptableName", LOAN_APP_TABLE_NAME);
columnMap.put("entitySubType", "PERSON");
columnMap.put("multiRow", true);
DatatableHelper.addDatatableColumns(datatableColumnsList, columnName1, "String", true, 10, null);
DatatableHelper.addDatatableColumns(datatableColumnsList, columnName2, "String", false, 10, null);
columnMap.put("columns", datatableColumnsList);
final String datatableRequestJsonString = new Gson().toJson(columnMap);
LOG.info("CreateDataTable map : {}", datatableRequestJsonString);
this.datatableHelper.createDatatable(datatableRequestJsonString, "");
// Create a datatable entry so that it can be updated using BatchApi
final Map<String, Object> datatableEntryMap = new HashMap<>();
String columnValue1 = Utils.randomNameGenerator("VAL1_", 3);
String columnValue2 = Utils.randomNameGenerator("VAL2_", 3);
datatableEntryMap.put(columnName1, columnValue1);
datatableEntryMap.put(columnName2, columnValue2);
final String datatableEntryRequestJsonString = new Gson().toJson(datatableEntryMap);
LOG.info("CreateDataTableEntry map : {}", datatableEntryRequestJsonString);
final Map<String, Object> datatableEntryResponse = this.datatableHelper.createDatatableEntry(datatableName, loanId.intValue(),
false, datatableEntryRequestJsonString);
final Integer datatableEntryResourceId = (Integer) datatableEntryResponse.get("resourceId");
assertNotNull(datatableEntryResourceId, "ERROR IN CREATING THE ENTITY DATATABLE RECORD");
// Create datatable entry batch request
final BatchRequest createDatatableEntryRequest = BatchHelper.createDatatableEntryRequest(loanId, datatableName,
Arrays.asList(columnName1, columnName2));
// Get datatable entries batch request
final BatchRequest getDatatableEntriesRequest = BatchHelper.getDatatableByIdRequest(loanId, datatableName, null,
createDatatableEntryRequest.getReference());
final List<BatchRequest> batchRequestsDatatableEntries = Arrays.asList(createDatatableEntryRequest, getDatatableEntriesRequest);
LOG.info("Batch Request : {}", BatchHelper.toJsonString(batchRequestsDatatableEntries));
final List<BatchResponse> responseDatatableBatch = BatchHelper.postBatchRequestsWithEnclosingTransaction(this.requestSpec,
this.responseSpec, BatchHelper.toJsonString(batchRequestsDatatableEntries));
LOG.info("Batch Response : {}", new Gson().toJson(responseDatatableBatch));
final BatchResponse batchResponse1 = responseDatatableBatch.get(0);
final BatchResponse batchResponse2 = responseDatatableBatch.get(1);
Assertions.assertEquals(HttpStatus.SC_OK, batchResponse1.getStatusCode(), "Verify Status Code 200 for create datatable entry");
Assertions.assertEquals(HttpStatus.SC_OK, batchResponse2.getStatusCode(), "Verify Status Code 200 for update datatable entry");
final String getDatatableEntriesResponse = batchResponse2.getBody();
final Long createDatatableEntryId = jsonHelper.extractLongNamed("resourceId",
jsonHelper.parse(batchResponse1.getBody()).getAsJsonObject());
final JsonArray datatableEntries = jsonHelper.parse(getDatatableEntriesResponse).getAsJsonArray();
Assertions.assertEquals(2, datatableEntries.size());
// Ensure both resourceIds are available in response
Assertions.assertTrue(getDatatableEntriesResponse.contains(String.format("\"id\": %d", createDatatableEntryId)));
Assertions.assertTrue(getDatatableEntriesResponse.contains(String.format("\"id\": %d", datatableEntryResourceId)));
final BatchRequest queryDatatableEntriesRequest = BatchHelper.queryDatatableEntries(datatableName, columnName1, columnValue1,
"id,loan_id");
final BatchRequest updateDatatableEntry = BatchHelper.updateDatatableEntry(datatableName, "$.[0].loan_id", "$.[0].id", columnName2,
columnValue2 + "1");
final List<BatchRequest> batchRequestsToQueryAndUpdateDatatableEntries = Arrays.asList(queryDatatableEntriesRequest,
updateDatatableEntry);
LOG.info("Batch Request : {}", BatchHelper.toJsonString(batchRequestsToQueryAndUpdateDatatableEntries));
final List<BatchResponse> responseOfQuertAndUpdateDatatableBatch = BatchHelper.postBatchRequestsWithEnclosingTransaction(
this.requestSpec, this.responseSpec, BatchHelper.toJsonString(batchRequestsToQueryAndUpdateDatatableEntries));
LOG.info("Batch Response : {}", new Gson().toJson(responseOfQuertAndUpdateDatatableBatch));
final BatchResponse batchQueryAndUpdateResponse1 = responseOfQuertAndUpdateDatatableBatch.get(0);
final BatchResponse batchQueryAndUpdateResponse2 = responseOfQuertAndUpdateDatatableBatch.get(1);
Assertions.assertEquals(HttpStatus.SC_OK, batchQueryAndUpdateResponse1.getStatusCode(),
"Verify Status Code 200 for create datatable entry");
Assertions.assertEquals(HttpStatus.SC_OK, batchQueryAndUpdateResponse2.getStatusCode(),
"Verify Status Code 200 for update datatable entry");
final JsonObject changes = jsonHelper.parse(batchQueryAndUpdateResponse2.getBody()).getAsJsonObject().get("changes")
.getAsJsonObject();
Assertions.assertEquals(changes.get(columnName2).getAsString(), columnValue2 + "1");
}
/**
* Delete datatable
*
@@ -936,4 +936,83 @@ public final class BatchHelper {
return br;
}
/**
* Creates and returns a batch request to query datatable entry.
*
* @param datatableName
* @param columnName
* @param columnValue
* @param columnResult
* @return
*/
public static BatchRequest queryDatatableEntries(final String datatableName, final String columnName, final String columnValue,
final String columnResult) {
final BatchRequest br = new BatchRequest();
String relativeUrl = String.format("datatables/%s/query", datatableName);
relativeUrl += "?columnFilter=" + columnName + "&" + "valueFilter=" + columnValue + "&" + "resultColumns=" + columnResult;
br.setRequestId(1L);
br.setRelativeUrl(relativeUrl);
br.setMethod(HttpMethod.GET);
br.setBody("{}");
return br;
}
/**
* Creates and returns a batch request to update datatable entry.
*
* @param datatableName
* @param resourceId
* @param subResourceId
* @param columnName
* @param columnValue
* @return
*/
public static BatchRequest updateDatatableEntry(final String datatableName, final String resourceId, final String columnName,
final String columnValue) {
final BatchRequest br = new BatchRequest();
final String relativeUrl = String.format("datatables/%s/%s", datatableName, resourceId);
final Map<String, Object> datatableEntryMap = new HashMap<>();
datatableEntryMap.put(columnName, columnValue);
final String datatableEntryRequestJsonString = new Gson().toJson(datatableEntryMap);
LOG.info("UpdateDataTableEntry map : {}", datatableEntryRequestJsonString);
br.setRequestId(2L);
br.setReference(1L);
br.setRelativeUrl(relativeUrl);
br.setMethod(HttpMethod.PUT);
br.setBody(datatableEntryRequestJsonString);
return br;
}
/**
* Creates and returns a batch request to update datatable entry.
*
* @param datatableName
* @param resourceId
* @param subResourceId
* @param columnName
* @param columnValue
* @return
*/
public static BatchRequest updateDatatableEntry(final String datatableName, final String resourceId, final String subResourceId,
final String columnName, final String columnValue) {
final BatchRequest br = new BatchRequest();
final String relativeUrl = String.format("datatables/%s/%s/%s", datatableName, resourceId, subResourceId);
final Map<String, Object> datatableEntryMap = new HashMap<>();
datatableEntryMap.put(columnName, columnValue);
final String datatableEntryRequestJsonString = new Gson().toJson(datatableEntryMap);
LOG.info("UpdateDataTableEntry map : {}", datatableEntryRequestJsonString);
br.setRequestId(2L);
br.setReference(1L);
br.setRelativeUrl(relativeUrl);
br.setMethod(HttpMethod.PUT);
br.setBody(datatableEntryRequestJsonString);
return br;
}
}