FINERACT-1678: Soft-lock Loan COB relevant loan accounts upon startup

This commit is contained in:
Adam Saghy
2022-09-13 16:47:12 +02:00
committed by Arnold Gálovics
parent f258aa258c
commit 44db1d7b94
15 changed files with 325 additions and 36 deletions
@@ -73,8 +73,8 @@ public class ApplyLoanLockTaskletStepDefinitions implements En {
Then("ApplyLoanLockTasklet.execute result should match", () -> {
assertEquals(RepeatStatus.FINISHED, resultItem);
assertEquals(3L, ((List) stepContribution.getStepExecution().getExecutionContext()
.get(LoanCOBWorkerConfiguration.ALREADY_LOCKED_LOAN_IDS)).get(0));
assertEquals(3L,
((List) stepContribution.getStepExecution().getExecutionContext().get(LoanCOBConstant.ALREADY_LOCKED_LOAN_IDS)).get(0));
verify(this.accountLockRepository, Mockito.times(2)).save(valueCaptor.capture());
List<LoanAccountLock> values = valueCaptor.getAllValues();
assertEquals(2L, values.get(0).getLoanId());
@@ -0,0 +1,146 @@
/**
* 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.cob.loan;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import io.cucumber.java8.En;
import java.util.Collections;
import java.util.List;
import org.apache.fineract.cob.domain.LoanAccountLock;
import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.cob.domain.LockOwner;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.mockito.Mockito;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.repeat.RepeatStatus;
public class FetchAndLockLoanStepDefinitions implements En {
private final LoanAccountLockRepository loanAccountLockRepository = mock(LoanAccountLockRepository.class);
private final RetrieveLoanIdService retrieveLoanIdService = mock(RetrieveLoanIdService.class);
StepContribution contribution;
private FetchAndLockLoanTasklet fetchAndLockLoanTasklet;
private String action;
private RepeatStatus result;
public FetchAndLockLoanStepDefinitions() {
Given("/^The FetchAndLockLoanTasklet.execute method with action (.*)$/", (String action) -> {
ThreadLocalContextUtil.setTenant(new FineractPlatformTenant(1L, "default", "Default", "Asia/Kolkata", null));
this.action = action;
if ("empty loanIds".equals(action)) {
lenient().when(retrieveLoanIdService.retrieveLoanIds()).thenReturn(Collections.emptyList());
} else if ("good".equals(action)) {
lenient().when(retrieveLoanIdService.retrieveLoanIds()).thenReturn(List.of(1L, 2L, 3L));
lenient().when(loanAccountLockRepository.findAllByLoanIdIn(Mockito.anyList())).thenReturn(Collections.emptyList());
} else if ("soft lock".equals(action)) {
lenient().when(retrieveLoanIdService.retrieveLoanIds()).thenReturn(List.of(1L, 2L, 3L));
lenient().when(loanAccountLockRepository.findAllByLoanIdIn(Mockito.anyList()))
.thenReturn(List.of(new LoanAccountLock(1L, LockOwner.LOAN_COB_PARTITIONING)));
} else if ("inline cob".equals(action)) {
lenient().when(retrieveLoanIdService.retrieveLoanIds()).thenReturn(List.of(1L, 2L, 3L));
lenient().when(loanAccountLockRepository.findAllByLoanIdIn(Mockito.anyList()))
.thenReturn(List.of(new LoanAccountLock(2L, LockOwner.LOAN_INLINE_COB_PROCESSING)));
} else if ("chunk processing".equals(action)) {
lenient().when(retrieveLoanIdService.retrieveLoanIds()).thenReturn(List.of(1L, 2L, 3L));
lenient().when(loanAccountLockRepository.findAllByLoanIdIn(Mockito.anyList()))
.thenReturn(List.of(new LoanAccountLock(3L, LockOwner.LOAN_COB_CHUNK_PROCESSING)));
}
JobExecution jobExecution = new JobExecution(1L);
StepExecution stepExecution = new StepExecution("step", jobExecution);
contribution = new StepContribution(stepExecution);
fetchAndLockLoanTasklet = new FetchAndLockLoanTasklet(loanAccountLockRepository, retrieveLoanIdService);
});
When("FetchAndLockLoanTasklet.execute method executed", () -> {
result = this.fetchAndLockLoanTasklet.execute(contribution, null);
});
Then("FetchAndLockLoanTasklet.execute result should match", () -> {
if ("empty steps".equals(action)) {
assertEquals(RepeatStatus.FINISHED, result);
} else if ("good".equals(action)) {
verify(loanAccountLockRepository, Mockito.times(3)).save(Mockito.any());
assertEquals(RepeatStatus.FINISHED, result);
assertEquals(3,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.size());
assertEquals(1L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(0));
assertEquals(2L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(1));
assertEquals(3L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(2));
} else if ("soft lock".equals(action)) {
verify(loanAccountLockRepository, Mockito.times(2)).save(Mockito.any());
assertEquals(RepeatStatus.FINISHED, result);
assertEquals(3,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.size());
assertEquals(1L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(0));
assertEquals(2L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(1));
assertEquals(3L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(2));
} else if ("inline cob".equals(action)) {
verify(loanAccountLockRepository, Mockito.times(2)).save(Mockito.any());
assertEquals(RepeatStatus.FINISHED, result);
assertEquals(2,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.size());
assertEquals(1L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(0));
assertEquals(3L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(1));
} else if ("chunk processing".equals(action)) {
verify(loanAccountLockRepository, Mockito.times(2)).save(Mockito.any());
assertEquals(RepeatStatus.FINISHED, result);
assertEquals(2,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.size());
assertEquals(1L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(0));
assertEquals(2L,
((List) contribution.getStepExecution().getJobExecution().getExecutionContext().get(LoanCOBConstant.LOAN_IDS))
.get(1));
}
});
}
}
@@ -26,7 +26,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import io.cucumber.java8.En;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -46,9 +46,9 @@ public class LoanCOBPartitionerStepDefinitions implements En {
COBBusinessStepService cobBusinessStepService = mock(COBBusinessStepService.class);
JobOperator jobOperator = mock(JobOperator.class);
JobExplorer jobExplorer = mock(JobExplorer.class);
RetrieveLoanIdService retrieveLoanIdService = mock(RetrieveLoanIdService.class);
private LoanCOBPartitioner loanCOBPartitioner = new LoanCOBPartitioner(propertyService, cobBusinessStepService, jobOperator,
jobExplorer, retrieveLoanIdService);
List<Long> loanIds;
private LoanCOBPartitioner loanCOBPartitioner;
private TreeMap<Long, String> cobBusinessMap = new TreeMap<>();
@@ -69,14 +69,15 @@ public class LoanCOBPartitionerStepDefinitions implements En {
cobBusinessMap.put(1L, "Business step");
lenient().when(cobBusinessStepService.getCOBBusinessStepMap(LoanCOBBusinessStep.class, LoanCOBConstant.LOAN_COB_JOB_NAME))
.thenReturn(cobBusinessMap);
lenient().when(retrieveLoanIdService.retrieveLoanIds()).thenReturn(Collections.emptyList());
loanIds = new ArrayList<>();
lenient().when(jobExplorer.findRunningJobExecutions(JobName.LOAN_COB.name())).thenThrow(new RuntimeException("fail"));
} else if ("good".equals(action)) {
cobBusinessMap.put(1L, "Business step");
lenient().when(cobBusinessStepService.getCOBBusinessStepMap(LoanCOBBusinessStep.class, LoanCOBConstant.LOAN_COB_JOB_NAME))
.thenReturn(cobBusinessMap);
lenient().when(retrieveLoanIdService.retrieveLoanIds()).thenReturn(List.of(1L, 2L, 3L));
loanIds = List.of(1L, 2L, 3L);
}
loanCOBPartitioner = new LoanCOBPartitioner(propertyService, cobBusinessStepService, jobOperator, jobExplorer, loanIds);
});
When("LoanCOBPartitioner.partition method executed", () -> {
@@ -57,7 +57,7 @@ public class LoanItemReaderStepDefinitions implements En {
List<String> splitLockedAccountsStr = Splitter.on(',').splitToList(lockedAccounts);
splitLockedAccounts = splitLockedAccountsStr.stream().map(Long::parseLong).toList();
}
jobExecutionContext.put(LoanCOBWorkerConfiguration.ALREADY_LOCKED_LOAN_IDS, new ArrayList<>(splitLockedAccounts));
jobExecutionContext.put(LoanCOBConstant.ALREADY_LOCKED_LOAN_IDS, new ArrayList<>(splitLockedAccounts));
jobExecution.setExecutionContext(jobExecutionContext);
StepExecution stepExecution = new StepExecution("test", jobExecution);
ExecutionContext stepExecutionContext = new ExecutionContext();
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.springframework.data.auditing;
package org.apache.fineract.infrastructure.core.auditing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -28,7 +28,6 @@ import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.apache.fineract.infrastructure.core.auditing.CustomAuditingHandler;
import org.apache.fineract.infrastructure.core.domain.AbstractAuditableCustom;
import org.apache.fineract.infrastructure.core.domain.AbstractAuditableWithUTCDateTimeCustom;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
@@ -0,0 +1,34 @@
#
# 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.
#
Feature: COB Loan fetch and (soft) lock Step
@cob
Scenario Outline: FetchAndLockLoanTasklet - run test
Given The FetchAndLockLoanTasklet.execute method with action <action>
When FetchAndLockLoanTasklet.execute method executed
Then FetchAndLockLoanTasklet.execute result should match
Examples:
|action|
|empty loanIds|
|good|
|soft lock|
|inline cob|
|chunk processing|