Skip to content

Commit

Permalink
FINERACT-1678: Fix Apply Penalty to overdue loan COB job
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsaghy authored and galovics committed Aug 30, 2022
1 parent aa12de3 commit 4808bb3
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.apache.fineract.cob.domain.BatchBusinessStep;
import org.apache.fineract.cob.domain.BatchBusinessStepRepository;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
import org.apache.fineract.infrastructure.core.domain.ActionContext;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.context.ApplicationContext;
Expand All @@ -42,8 +44,13 @@ public class COBBusinessStepServiceImpl implements COBBusinessStepService {
@Override
public <T extends COBBusinessStep<S>, S extends AbstractPersistableCustom> S run(TreeMap<Long, String> executionMap, S item) {
for (String businessStep : executionMap.values()) {
COBBusinessStep<S> businessStepBean = (COBBusinessStep<S>) applicationContext.getBean(businessStep);
item = businessStepBean.execute(item);
try {
COBBusinessStep<S> businessStepBean = (COBBusinessStep<S>) applicationContext.getBean(businessStep);
item = businessStepBean.execute(item);
} finally {
// Fallback to COB action context after each business step
ThreadLocalContextUtil.setActionContext(ActionContext.COB);
}
}
return item;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.fineract.cob;

import org.apache.fineract.infrastructure.core.domain.ActionContext;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
Expand All @@ -31,6 +32,7 @@ public class COBInputChannelInterceptor implements ExecutorChannelInterceptor {
public Message<?> beforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
COBMessage castedMessage = COBMessage.class.cast(message.getPayload());
ThreadLocalContextUtil.init(castedMessage.getContext());
ThreadLocalContextUtil.setActionContext(ActionContext.COB);
return new GenericMessage<>(castedMessage.getStepExecutionRequest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public HashMap<BusinessDateType, LocalDate> getBusinessDates() {
ZoneId zone = DateUtils.getDateTimeZoneOfTenant();
LocalDate tenantDate = LocalDate.now(zone);
businessDateMap.put(BusinessDateType.BUSINESS_DATE, tenantDate);
businessDateMap.put(BusinessDateType.COB_DATE, tenantDate);
if (configurationDomainService.isBusinessDateEnabled()) {
final List<BusinessDateData> businessDateDataList = this.findAll();
for (BusinessDateData businessDateData : businessDateDataList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ public GlobalConfigurationWritePlatformServiceJpaRepositoryImpl(final PlatformSe
@Transactional
@Override
public CommandProcessingResult update(final Long configId, final JsonCommand command) {

this.context.authenticatedUser();

try {
this.globalConfigurationDataValidator.validateForUpdate(command);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1576,8 +1576,8 @@ public Collection<OverdueLoanScheduleData> retrieveAllOverdueInstallmentsForLoan
continue;
}

boolean isPenaltyDue = installment.isOverdueOn(DateUtils.getBusinessLocalDate().plusDays(penaltyWaitPeriod + 1));
boolean isDueToday = installment.getDueDate().equals(DateUtils.getBusinessLocalDate().plusDays(penaltyWaitPeriod));
boolean isPenaltyDue = installment.isOverdueOn(DateUtils.getBusinessLocalDate().minusDays(penaltyWaitPeriod).plusDays(1));
boolean isDueToday = installment.getDueDate().equals(DateUtils.getBusinessLocalDate().minusDays(penaltyWaitPeriod));

if (isPenaltyDue) {
if (!backdatePenalties && !isDueToday) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.apache.fineract.integrationtests.common.BusinessDateHelper;
import org.apache.fineract.integrationtests.common.ClientHelper;
import org.apache.fineract.integrationtests.common.CollateralManagementHelper;
import org.apache.fineract.integrationtests.common.GlobalConfigurationHelper;
Expand Down Expand Up @@ -646,6 +648,82 @@ public void testLoanCOBJobOutcome() {
}
}

@Test
public void testLoanCOBApplyPenaltyOnDue() {
GlobalConfigurationHelper.updateIsBusinessDateEnabled(requestSpec, responseSpec, Boolean.TRUE);
// set penalty wait period to 0
GlobalConfigurationHelper.updateValueForGlobalConfiguration(this.requestSpec, this.responseSpec, "10", "0");
this.loanTransactionHelper = new LoanTransactionHelper(requestSpec, responseSpec);

final Integer clientID = ClientHelper.createClient(requestSpec, responseSpec);
Assertions.assertNotNull(clientID);

Integer overdueFeeChargeId = ChargesHelper.createCharges(requestSpec, responseSpec,
ChargesHelper.getLoanOverdueFeeJSONWithCalculationTypePercentage("1"));
Assertions.assertNotNull(overdueFeeChargeId);

final Integer loanProductID = createLoanProduct(overdueFeeChargeId.toString());
Assertions.assertNotNull(loanProductID);
HashMap loanStatusHashMap;

final Integer loanID = applyForLoanApplication(clientID.toString(), loanProductID.toString(), null, "10 January 2020");

Assertions.assertNotNull(loanID);

loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(requestSpec, responseSpec, loanID);
LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap);

loanStatusHashMap = this.loanTransactionHelper.approveLoan("01 March 2020", loanID);
LoanStatusChecker.verifyLoanIsApproved(loanStatusHashMap);

String loanDetails = this.loanTransactionHelper.getLoanDetails(requestSpec, responseSpec, loanID);
loanStatusHashMap = this.loanTransactionHelper.disburseLoanWithNetDisbursalAmount("02 March 2020", loanID,
JsonPath.from(loanDetails).get("netDisbursalAmount").toString());
LoanStatusChecker.verifyLoanIsActive(loanStatusHashMap);
BusinessDateHelper.updateBusinessDate(requestSpec, responseSpec, BusinessDateType.COB_DATE, LocalDate.of(2020, 4, 1));
String jobName = "Loan COB";

this.schedulerJobHelper.executeAndAwaitJob(jobName);
List<HashMap> repaymentScheduleDataAfter = this.loanTransactionHelper.getLoanRepaymentSchedule(requestSpec, responseSpec, loanID);
Assertions.assertEquals(0, (Integer) repaymentScheduleDataAfter.get(1).get("penaltyChargesDue"),
"Verifying From Penalty Charges due fot first Repayment after Successful completion of Scheduler Job");

BusinessDateHelper.updateBusinessDate(requestSpec, responseSpec, BusinessDateType.COB_DATE, LocalDate.of(2020, 4, 2));
this.schedulerJobHelper.executeAndAwaitJob(jobName);
repaymentScheduleDataAfter = this.loanTransactionHelper.getLoanRepaymentSchedule(requestSpec, responseSpec, loanID);
Assertions.assertEquals(39.39f, (Float) repaymentScheduleDataAfter.get(1).get("penaltyChargesDue"),
"Verifying From Penalty Charges due fot first Repayment after Successful completion of Scheduler Job");

GlobalConfigurationHelper.updateValueForGlobalConfiguration(this.requestSpec, this.responseSpec, "10", "1");

BusinessDateHelper.updateBusinessDate(requestSpec, responseSpec, BusinessDateType.COB_DATE, LocalDate.of(2020, 5, 2));
this.schedulerJobHelper.executeAndAwaitJob(jobName);
repaymentScheduleDataAfter = this.loanTransactionHelper.getLoanRepaymentSchedule(requestSpec, responseSpec, loanID);
Assertions.assertEquals(0, (Integer) repaymentScheduleDataAfter.get(2).get("penaltyChargesDue"),
"Verifying From Penalty Charges due fot first Repayment after Successful completion of Scheduler Job");

BusinessDateHelper.updateBusinessDate(requestSpec, responseSpec, BusinessDateType.COB_DATE, LocalDate.of(2020, 5, 3));
this.schedulerJobHelper.executeAndAwaitJob(jobName);
repaymentScheduleDataAfter = this.loanTransactionHelper.getLoanRepaymentSchedule(requestSpec, responseSpec, loanID);
Assertions.assertEquals(39.39f, (Float) repaymentScheduleDataAfter.get(2).get("penaltyChargesDue"),
"Verifying From Penalty Charges due fot first Repayment after Successful completion of Scheduler Job");

List<Map> transactions = this.loanTransactionHelper.getLoanTransactions(this.requestSpec, this.responseSpec, loanID);
Assertions.assertEquals(39.39f, (Float) transactions.get(2).get("amount"));
Assertions.assertEquals(2020, ((List) transactions.get(2).get("date")).get(0));
Assertions.assertEquals(4, ((List) transactions.get(2).get("date")).get(1));
Assertions.assertEquals(2, ((List) transactions.get(2).get("date")).get(2));

transactions = this.loanTransactionHelper.getLoanTransactions(this.requestSpec, this.responseSpec, loanID);
Assertions.assertEquals(39.39f, (Float) transactions.get(3).get("amount"));
Assertions.assertEquals(2020, ((List) transactions.get(3).get("date")).get(0));
Assertions.assertEquals(5, ((List) transactions.get(3).get("date")).get(1));
Assertions.assertEquals(2, ((List) transactions.get(3).get("date")).get(2));

GlobalConfigurationHelper.updateIsBusinessDateEnabled(requestSpec, responseSpec, Boolean.FALSE);
GlobalConfigurationHelper.updateValueForGlobalConfiguration(this.requestSpec, this.responseSpec, "10", "2");
}

@Test
public void testAvoidUnncessaryPenaltyWhenAmountZeroForOverdueLoansJobOutcome() throws InterruptedException {
this.savingsAccountHelper = new SavingsAccountHelper(requestSpec, responseSpec);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 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.integrationtests.common;

import com.google.gson.Gson;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import java.time.LocalDate;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;

@Slf4j
public final class BusinessDateHelper {

private BusinessDateHelper() {}

public static HashMap updateBusinessDate(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final BusinessDateType type, final LocalDate date) {
final String BUSINESS_DATE_API = "/fineract-provider/api/v1/businessdate?" + Utils.TENANT_IDENTIFIER;
log.info("------------------UPDATE BUSINESS DATE----------------------");
log.info("------------------Type: {}, date: {}----------------------", type, date);
return Utils.performServerPost(requestSpec, responseSpec, BUSINESS_DATE_API, buildBusinessDateRequest(type, date), "changes");
}

private static String buildBusinessDateRequest(BusinessDateType type, LocalDate date) {
final HashMap<String, String> map = new HashMap<>();
map.put("type", type.name());
map.put("date", Utils.dateFormatter.format(date));
map.put("dateFormat", Utils.DATE_FORMAT);
map.put("locale", "en");
log.info("map : {}", map);
return new Gson().toJson(map);
}

}

0 comments on commit 4808bb3

Please sign in to comment.