[FINERACT-1678] Set a limit to the request body list size on Inline Loan COB API
This commit is contained in:
+10
@@ -45,6 +45,7 @@ import org.apache.fineract.infrastructure.core.config.FineractProperties;
|
|||||||
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
|
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
|
||||||
import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder;
|
import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder;
|
||||||
import org.apache.fineract.infrastructure.core.exception.PlatformInternalServerException;
|
import org.apache.fineract.infrastructure.core.exception.PlatformInternalServerException;
|
||||||
|
import org.apache.fineract.infrastructure.core.exception.PlatformRequestBodyItemLimitValidationException;
|
||||||
import org.apache.fineract.infrastructure.core.serialization.GoogleGsonSerializerHelper;
|
import org.apache.fineract.infrastructure.core.serialization.GoogleGsonSerializerHelper;
|
||||||
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
|
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
|
||||||
import org.apache.fineract.infrastructure.jobs.domain.CustomJobParameter;
|
import org.apache.fineract.infrastructure.jobs.domain.CustomJobParameter;
|
||||||
@@ -95,6 +96,7 @@ public class InlineLoanCOBExecutorServiceImpl implements InlineExecutorService<L
|
|||||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
public CommandProcessingResult executeInlineJob(JsonCommand command, String jobName) throws LoanAccountLockCannotBeOverruledException {
|
public CommandProcessingResult executeInlineJob(JsonCommand command, String jobName) throws LoanAccountLockCannotBeOverruledException {
|
||||||
List<Long> loanIds = dataParser.parseExecution(command);
|
List<Long> loanIds = dataParser.parseExecution(command);
|
||||||
|
validateLoanIdsListSize(loanIds);
|
||||||
execute(loanIds, jobName);
|
execute(loanIds, jobName);
|
||||||
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).build();
|
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).build();
|
||||||
}
|
}
|
||||||
@@ -231,4 +233,12 @@ public class InlineLoanCOBExecutorServiceImpl implements InlineExecutorService<L
|
|||||||
private boolean isBypassUser() {
|
private boolean isBypassUser() {
|
||||||
return context.getAuthenticatedUserIfPresent().isBypassUser();
|
return context.getAuthenticatedUserIfPresent().isBypassUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateLoanIdsListSize(List<Long> loanIds) {
|
||||||
|
int inlineLoanCobRequestItemLimit = fineractProperties.getApi().getBodyItemSizeLimit().getInlineLoanCob();
|
||||||
|
if (loanIds.size() > inlineLoanCobRequestItemLimit) {
|
||||||
|
String userMessage = "Size of the loan IDs list cannot be over " + inlineLoanCobRequestItemLimit;
|
||||||
|
throw new PlatformRequestBodyItemLimitValidationException(userMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
@@ -56,6 +56,7 @@ public class FineractProperties {
|
|||||||
private FineractJpaProperties jpa;
|
private FineractJpaProperties jpa;
|
||||||
|
|
||||||
private FineractQueryProperties query;
|
private FineractQueryProperties query;
|
||||||
|
private FineractApiProperties api;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@@ -265,4 +266,18 @@ public class FineractProperties {
|
|||||||
private int inClauseParameterSizeLimit;
|
private int inClauseParameterSizeLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public static class FineractApiProperties {
|
||||||
|
|
||||||
|
private FineractBodyItemSizeLimitProperties bodyItemSizeLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public static class FineractBodyItemSizeLimitProperties {
|
||||||
|
|
||||||
|
private int inlineLoanCob;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+5
@@ -21,6 +21,7 @@ package org.apache.fineract.infrastructure.core.data;
|
|||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.ws.rs.core.Response.Status;
|
import javax.ws.rs.core.Response.Status;
|
||||||
|
|
||||||
@@ -176,6 +177,10 @@ public class ApiGlobalErrorResponse {
|
|||||||
return globalErrorResponse;
|
return globalErrorResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ApiGlobalErrorResponse badClientRequest(final String globalisationMessageCode, final String defaultUserMessage) {
|
||||||
|
return badClientRequest(globalisationMessageCode, defaultUserMessage, Collections.emptyList());
|
||||||
|
}
|
||||||
|
|
||||||
public static ApiGlobalErrorResponse serverSideError(final String globalisationMessageCode, final String defaultUserMessage,
|
public static ApiGlobalErrorResponse serverSideError(final String globalisationMessageCode, final String defaultUserMessage,
|
||||||
final Object... defaultUserMessageArgs) {
|
final Object... defaultUserMessageArgs) {
|
||||||
|
|
||||||
|
|||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
/**
|
||||||
|
* 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.infrastructure.core.exception;
|
||||||
|
|
||||||
|
public class PlatformRequestBodyItemLimitValidationException extends RuntimeException {
|
||||||
|
|
||||||
|
public PlatformRequestBodyItemLimitValidationException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* 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.infrastructure.core.exceptionmapper;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.ext.ExceptionMapper;
|
||||||
|
import javax.ws.rs.ext.Provider;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
|
||||||
|
import org.apache.fineract.infrastructure.core.exception.PlatformRequestBodyItemLimitValidationException;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Provider
|
||||||
|
@Component
|
||||||
|
@Scope("singleton")
|
||||||
|
@Slf4j
|
||||||
|
public class PlatformRequestBodyItemLimitValidationExceptionMapper
|
||||||
|
implements ExceptionMapper<PlatformRequestBodyItemLimitValidationException> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response toResponse(PlatformRequestBodyItemLimitValidationException exception) {
|
||||||
|
String globalisationMessage = "error.msg.validation.request.body.item.limit.validation";
|
||||||
|
log.warn("Exception: {}, Message: {}", exception.getClass().getName(), exception.getMessage());
|
||||||
|
final ApiGlobalErrorResponse badRequestErrorResponse = ApiGlobalErrorResponse.badClientRequest(globalisationMessage,
|
||||||
|
exception.getMessage());
|
||||||
|
return Response.status(Response.Status.BAD_REQUEST).entity(badRequestErrorResponse).type(MediaType.APPLICATION_JSON).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-2
@@ -45,7 +45,7 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
@Tag(name = "Inline Job", description = "")
|
@Tag(name = "Inline Job", description = "")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class InlineJobResource {
|
public class InlineJobApiResource {
|
||||||
|
|
||||||
private final PortfolioCommandSourceWritePlatformService commandWritePlatformService;
|
private final PortfolioCommandSourceWritePlatformService commandWritePlatformService;
|
||||||
private final DefaultToApiJsonSerializer<LoanIdsResponseDTO> serializer;
|
private final DefaultToApiJsonSerializer<LoanIdsResponseDTO> serializer;
|
||||||
@@ -57,7 +57,8 @@ public class InlineJobResource {
|
|||||||
@Operation(summary = "Starts an inline Job", description = "Starts an inline Job")
|
@Operation(summary = "Starts an inline Job", description = "Starts an inline Job")
|
||||||
@RequestBody(content = @Content(schema = @Schema(implementation = InlineJobResourceSwagger.InlineJobRequest.class)))
|
@RequestBody(content = @Content(schema = @Schema(implementation = InlineJobResourceSwagger.InlineJobRequest.class)))
|
||||||
@ApiResponses({
|
@ApiResponses({
|
||||||
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = InlineJobResourceSwagger.InlineJobResponse.class))) })
|
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = InlineJobResourceSwagger.InlineJobResponse.class))),
|
||||||
|
@ApiResponse(responseCode = "400", description = "Request body item size validation error") })
|
||||||
public String executeInlineJob(@PathParam("jobName") @Parameter(description = "jobName") final String jobName,
|
public String executeInlineJob(@PathParam("jobName") @Parameter(description = "jobName") final String jobName,
|
||||||
@Parameter(hidden = true) final String jsonRequestBody) {
|
@Parameter(hidden = true) final String jsonRequestBody) {
|
||||||
|
|
||||||
@@ -44,6 +44,8 @@ fineract.mode.batch-manager-enabled=${FINERACT_MODE_BATCH_MANAGER_ENABLED:true}
|
|||||||
|
|
||||||
fineract.query.in-clause-parameter-size-limit=${FINERACT_QUERY_PARAMETER_SIZE:1000}
|
fineract.query.in-clause-parameter-size-limit=${FINERACT_QUERY_PARAMETER_SIZE:1000}
|
||||||
|
|
||||||
|
fineract.api.body-item-size-limit.inline-loan-cob=${FINERACT_API_REQUEST_BODY_SIZE_LIMIT_INLINE_COB:1000}
|
||||||
|
|
||||||
fineract.correlation.enabled=${FINERACT_LOGGING_HTTP_CORRELATION_ID_ENABLED:false}
|
fineract.correlation.enabled=${FINERACT_LOGGING_HTTP_CORRELATION_ID_ENABLED:false}
|
||||||
fineract.correlation.header-name=${FINERACT_LOGGING_HTTP_CORRELATION_ID_HEADER_NAME:X-Correlation-ID}
|
fineract.correlation.header-name=${FINERACT_LOGGING_HTTP_CORRELATION_ID_HEADER_NAME:X-Correlation-ID}
|
||||||
|
|
||||||
|
|||||||
+10
@@ -66,6 +66,10 @@ class InlineLoanCOBExecutorServiceImplTest {
|
|||||||
private FineractProperties fineractProperties;
|
private FineractProperties fineractProperties;
|
||||||
@Mock
|
@Mock
|
||||||
private FineractProperties.FineractQueryProperties fineractQueryProperties;
|
private FineractProperties.FineractQueryProperties fineractQueryProperties;
|
||||||
|
@Mock
|
||||||
|
private FineractProperties.FineractApiProperties fineractApiProperties;
|
||||||
|
@Mock
|
||||||
|
private FineractProperties.FineractBodyItemSizeLimitProperties fineractBodyItemSizeLimitProperties;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldExceptionThrownIfLoanIsAlreadyLocked() {
|
void shouldExceptionThrownIfLoanIsAlreadyLocked() {
|
||||||
@@ -80,8 +84,11 @@ class InlineLoanCOBExecutorServiceImplTest {
|
|||||||
|
|
||||||
when(transactionTemplate.execute(any())).thenThrow(new LoanAccountLockCannotBeOverruledException(""));
|
when(transactionTemplate.execute(any())).thenThrow(new LoanAccountLockCannotBeOverruledException(""));
|
||||||
when(fineractProperties.getQuery()).thenReturn(fineractQueryProperties);
|
when(fineractProperties.getQuery()).thenReturn(fineractQueryProperties);
|
||||||
|
when(fineractProperties.getApi()).thenReturn(fineractApiProperties);
|
||||||
when(dataParser.parseExecution(any())).thenReturn(List.of(1L));
|
when(dataParser.parseExecution(any())).thenReturn(List.of(1L));
|
||||||
when(fineractQueryProperties.getInClauseParameterSizeLimit()).thenReturn(65000);
|
when(fineractQueryProperties.getInClauseParameterSizeLimit()).thenReturn(65000);
|
||||||
|
when(fineractApiProperties.getBodyItemSizeLimit()).thenReturn(fineractBodyItemSizeLimitProperties);
|
||||||
|
when(fineractBodyItemSizeLimitProperties.getInlineLoanCob()).thenReturn(1000);
|
||||||
when(loanRepository.findAllNonClosedLoansBehindOrNullByLoanIds(any(), anyList())).thenReturn(List.of(loan));
|
when(loanRepository.findAllNonClosedLoansBehindOrNullByLoanIds(any(), anyList())).thenReturn(List.of(loan));
|
||||||
assertThrows(LoanAccountLockCannotBeOverruledException.class, () -> testObj.executeInlineJob(command, "INLINE_LOAN_COB"));
|
assertThrows(LoanAccountLockCannotBeOverruledException.class, () -> testObj.executeInlineJob(command, "INLINE_LOAN_COB"));
|
||||||
}
|
}
|
||||||
@@ -101,8 +108,11 @@ class InlineLoanCOBExecutorServiceImplTest {
|
|||||||
|
|
||||||
when(transactionTemplate.execute(any())).thenThrow(new LoanAccountLockCannotBeOverruledException(""));
|
when(transactionTemplate.execute(any())).thenThrow(new LoanAccountLockCannotBeOverruledException(""));
|
||||||
when(fineractProperties.getQuery()).thenReturn(fineractQueryProperties);
|
when(fineractProperties.getQuery()).thenReturn(fineractQueryProperties);
|
||||||
|
when(fineractProperties.getApi()).thenReturn(fineractApiProperties);
|
||||||
when(dataParser.parseExecution(any())).thenReturn(List.of(1L, 2L, 3L));
|
when(dataParser.parseExecution(any())).thenReturn(List.of(1L, 2L, 3L));
|
||||||
when(fineractQueryProperties.getInClauseParameterSizeLimit()).thenReturn(2);
|
when(fineractQueryProperties.getInClauseParameterSizeLimit()).thenReturn(2);
|
||||||
|
when(fineractApiProperties.getBodyItemSizeLimit()).thenReturn(fineractBodyItemSizeLimitProperties);
|
||||||
|
when(fineractBodyItemSizeLimitProperties.getInlineLoanCob()).thenReturn(1000);
|
||||||
when(loanRepository.findAllNonClosedLoansBehindOrNullByLoanIds(any(), anyList())).thenReturn(List.of(loan1, loan2, loan3));
|
when(loanRepository.findAllNonClosedLoansBehindOrNullByLoanIds(any(), anyList())).thenReturn(List.of(loan1, loan2, loan3));
|
||||||
assertThrows(LoanAccountLockCannotBeOverruledException.class, () -> testObj.executeInlineJob(command, "INLINE_LOAN_COB"));
|
assertThrows(LoanAccountLockCannotBeOverruledException.class, () -> testObj.executeInlineJob(command, "INLINE_LOAN_COB"));
|
||||||
verify(loanRepository, times(2)).findAllNonClosedLoansBehindOrNullByLoanIds(any(), anyList());
|
verify(loanRepository, times(2)).findAllNonClosedLoansBehindOrNullByLoanIds(any(), anyList());
|
||||||
|
|||||||
+8
@@ -45,6 +45,14 @@ public class InlineLoanCOBHelper extends IntegrationTest {
|
|||||||
return Utils.performServerPost(requestSpec, responseSpec, EXECUTE_INLINE_COB_API, buildInlineCOBRequest(loanIds));
|
return Utils.performServerPost(requestSpec, responseSpec, EXECUTE_INLINE_COB_API, buildInlineCOBRequest(loanIds));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String executeInlineCOB(List<Long> loanIds, String responseAttribute) {
|
||||||
|
final String EXECUTE_INLINE_COB_API = "/fineract-provider/api/v1/jobs/LOAN_COB/inline";
|
||||||
|
log.info("------------------EXECUTE INLINE COB----------------------");
|
||||||
|
log.info("------------------Loan IDs: {}----------------------", loanIds);
|
||||||
|
return Utils.performServerPost(requestSpec, responseSpec, EXECUTE_INLINE_COB_API, buildInlineCOBRequest(loanIds),
|
||||||
|
responseAttribute);
|
||||||
|
}
|
||||||
|
|
||||||
private static String buildInlineCOBRequest(List<Long> loanIds) {
|
private static String buildInlineCOBRequest(List<Long> loanIds) {
|
||||||
final HashMap<String, List<Long>> map = new HashMap<>();
|
final HashMap<String, List<Long>> map = new HashMap<>();
|
||||||
map.put("loanIds", loanIds);
|
map.put("loanIds", loanIds);
|
||||||
|
|||||||
+11
@@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.fineract.integrationtests.inlinecob;
|
package org.apache.fineract.integrationtests.inlinecob;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
import io.restassured.builder.RequestSpecBuilder;
|
import io.restassured.builder.RequestSpecBuilder;
|
||||||
@@ -31,6 +32,7 @@ import java.time.LocalDate;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.LongStream;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.fineract.batch.domain.BatchRequest;
|
import org.apache.fineract.batch.domain.BatchRequest;
|
||||||
import org.apache.fineract.batch.domain.BatchResponse;
|
import org.apache.fineract.batch.domain.BatchResponse;
|
||||||
@@ -390,6 +392,15 @@ public class InlineLoanCOBTest {
|
|||||||
Assertions.assertEquals(LocalDate.of(2020, 3, 9), loan.getLastClosedBusinessDate());
|
Assertions.assertEquals(LocalDate.of(2020, 3, 9), loan.getLastClosedBusinessDate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInlineCOBRequestBodyItemLimitValidation() {
|
||||||
|
responseSpec = new ResponseSpecBuilder().expectStatusCode(400).build();
|
||||||
|
inlineLoanCOBHelper = new InlineLoanCOBHelper(requestSpec, responseSpec);
|
||||||
|
List<Long> loanIds = LongStream.rangeClosed(1, 1001).boxed().toList();
|
||||||
|
String responseUserMessage = inlineLoanCOBHelper.executeInlineCOB(loanIds, "defaultUserMessage");
|
||||||
|
assertEquals("Size of the loan IDs list cannot be over 1000", responseUserMessage);
|
||||||
|
}
|
||||||
|
|
||||||
private void createNewSimpleUserWithoutBypassPermission() {
|
private void createNewSimpleUserWithoutBypassPermission() {
|
||||||
GetOfficesResponse headOffice = OfficeHelper.getHeadOffice(requestSpec, responseSpec);
|
GetOfficesResponse headOffice = OfficeHelper.getHeadOffice(requestSpec, responseSpec);
|
||||||
String username = Utils.uniqueRandomStringGenerator("NotificationUser", 4);
|
String username = Utils.uniqueRandomStringGenerator("NotificationUser", 4);
|
||||||
|
|||||||
Reference in New Issue
Block a user