FINERACT-2421: Improved Spring Batch context handling

This commit is contained in:
Adam Saghy
2026-01-07 14:08:51 +01:00
parent 5c04216841
commit 7ee0f7f4e4
16 changed files with 190 additions and 138 deletions
@@ -32,8 +32,6 @@ import java.time.LocalDate;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import org.apache.fineract.cob.common.CustomJobParameterResolver;
import org.apache.fineract.cob.data.COBParameter;
import org.apache.fineract.cob.domain.LoanAccountLock;
import org.apache.fineract.cob.domain.LockOwner;
@@ -44,6 +42,7 @@ import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.mockito.ArgumentCaptor;
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.item.ExecutionContext;
@@ -62,9 +61,8 @@ public class ApplyLoanLockTaskletStepDefinitions implements En {
private RetrieveLoanIdService retrieveLoanIdService = mock(RetrieveLoanIdService.class);
private TransactionTemplate transactionTemplate = spy(TransactionTemplate.class);
private CustomJobParameterResolver customJobParameterResolver = mock(CustomJobParameterResolver.class);
private ApplyLoanLockTasklet applyLoanLockTasklet = new ApplyLoanLockTasklet(fineractProperties, loanLockingService,
retrieveLoanIdService, customJobParameterResolver, transactionTemplate);
retrieveLoanIdService, transactionTemplate);
private RepeatStatus resultItem;
private StepContribution stepContribution;
@@ -74,7 +72,8 @@ public class ApplyLoanLockTaskletStepDefinitions implements En {
HashMap<BusinessDateType, LocalDate> businessDateMap = new HashMap<>();
businessDateMap.put(BusinessDateType.COB_DATE, LocalDate.now(ZoneId.systemDefault()));
ThreadLocalContextUtil.setBusinessDates(businessDateMap);
StepExecution stepExecution = new StepExecution("test", null);
JobExecution jobExecution = new JobExecution(1L, null);
StepExecution stepExecution = new StepExecution("test", jobExecution);
ExecutionContext executionContext = new ExecutionContext();
COBParameter loanCOBParameter = new COBParameter(1L, 4L);
executionContext.put(LoanCOBConstant.LOAN_COB_PARAMETER, loanCOBParameter);
@@ -117,7 +116,6 @@ public class ApplyLoanLockTaskletStepDefinitions implements En {
lenient().when(loanLockingService.findAllByLoanIdIn(Mockito.anyList())).thenReturn(accountLocks);
}
transactionTemplate.setTransactionManager(mock(PlatformTransactionManager.class));
lenient().when(customJobParameterResolver.getCustomJobParameterSet(any())).thenReturn(Optional.empty());
});
@@ -30,16 +30,14 @@ import org.apache.fineract.cob.COBBusinessStepService;
import org.apache.fineract.cob.data.BusinessStepNameAndOrder;
import org.apache.fineract.cob.data.COBParameter;
import org.apache.fineract.cob.data.COBPartition;
import org.apache.fineract.infrastructure.jobs.service.JobName;
import org.apache.fineract.infrastructure.springbatch.PropertyService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.launch.JobExecutionNotRunningException;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.launch.NoSuchJobExecutionException;
@@ -59,7 +57,11 @@ class LoanCOBPartitionerTest {
@Mock
private JobOperator jobOperator;
@Mock
private JobExplorer jobExplorer;
private StepExecution stepExecution;
@Mock
private JobExecution jobExecution;
@Mock
private ExecutionContext executionContext;
@Test
public void testLoanCOBPartitioner() {
@@ -69,17 +71,19 @@ class LoanCOBPartitionerTest {
.thenReturn(BUSINESS_STEP_SET);
when(retrieveLoanIdService.retrieveLoanCOBPartitions(1L, BUSINESS_DATE, false, 5))
.thenReturn(List.of(new COBPartition(1L,10L, 1L, 5L), new COBPartition(11L,20L, 2L, 4L)));
LoanCOBPartitioner loanCOBPartitioner = new LoanCOBPartitioner(propertyService, cobBusinessStepService, retrieveLoanIdService, jobOperator, jobExplorer, 1L);
loanCOBPartitioner.setBusinessDate(BUSINESS_DATE);
loanCOBPartitioner.setIsCatchUp(false);
when(stepExecution.getJobExecution()).thenReturn(jobExecution);
when(jobExecution.getExecutionContext()).thenReturn(executionContext);
when(executionContext.get(LoanCOBConstant.BUSINESS_DATE_PARAMETER_NAME)).thenReturn(BUSINESS_DATE);
when(executionContext.get(LoanCOBConstant.IS_CATCH_UP_PARAMETER_NAME)).thenReturn(false);
LoanCOBPartitioner loanCOBPartitioner = new LoanCOBPartitioner(propertyService, cobBusinessStepService, retrieveLoanIdService, jobOperator,stepExecution, 1L);
//when
Map<String, ExecutionContext> partitions = loanCOBPartitioner.partition(1);
//then
Assertions.assertEquals(2, partitions.size());
validatePartitions(partitions, 1, 1, 10);
validatePartitions(partitions, 2, 11, 20);
validatePartitions(partitions, 1, 1, 10, BUSINESS_DATE.toString(), "false");
validatePartitions(partitions, 2, 11, 20, BUSINESS_DATE.toString(), "false");
}
@Test
@@ -88,19 +92,16 @@ class LoanCOBPartitionerTest {
when(propertyService.getPartitionSize(LoanCOBConstant.JOB_NAME)).thenReturn(5);
when(cobBusinessStepService.getCOBBusinessSteps(LoanCOBBusinessStep.class, LoanCOBConstant.LOAN_COB_JOB_NAME))
.thenReturn(Set.of());
JobExecution jobExecution = Mockito.mock(JobExecution.class);
when(stepExecution.getJobExecution()).thenReturn(jobExecution);
when(jobExecution.getId()).thenReturn(123L);
when(jobExplorer.findRunningJobExecutions(JobName.LOAN_COB.name())).thenReturn(Set.of(jobExecution));
LoanCOBPartitioner loanCOBPartitioner = new LoanCOBPartitioner(propertyService, cobBusinessStepService, retrieveLoanIdService, jobOperator, jobExplorer, 1L);
loanCOBPartitioner.setBusinessDate(BUSINESS_DATE);
loanCOBPartitioner.setIsCatchUp(false);
LoanCOBPartitioner loanCOBPartitioner = new LoanCOBPartitioner(propertyService, cobBusinessStepService, retrieveLoanIdService, jobOperator, stepExecution, 1L);
//when
Map<String, ExecutionContext> partitions = loanCOBPartitioner.partition(1);
//then
Assertions.assertEquals(0, partitions.size());
verify(jobExplorer, times(1)).findRunningJobExecutions(JobName.LOAN_COB.name());
verify(jobOperator, times(1)).stop(123L);
}
@@ -112,24 +113,30 @@ class LoanCOBPartitionerTest {
.thenReturn(BUSINESS_STEP_SET);
when(retrieveLoanIdService.retrieveLoanCOBPartitions(1L, BUSINESS_DATE, false, 5))
.thenReturn(List.of());
LoanCOBPartitioner loanCOBPartitioner = new LoanCOBPartitioner(propertyService, cobBusinessStepService, retrieveLoanIdService, jobOperator, jobExplorer, 1L);
loanCOBPartitioner.setBusinessDate(BUSINESS_DATE);
loanCOBPartitioner.setBusinessDate(BUSINESS_DATE);
loanCOBPartitioner.setIsCatchUp(false);
when(stepExecution.getJobExecution()).thenReturn(jobExecution);
when(jobExecution.getExecutionContext()).thenReturn(executionContext);
when(executionContext.get(LoanCOBConstant.BUSINESS_DATE_PARAMETER_NAME)).thenReturn(BUSINESS_DATE);
when(executionContext.get(LoanCOBConstant.IS_CATCH_UP_PARAMETER_NAME)).thenReturn(false);
LoanCOBPartitioner loanCOBPartitioner = new LoanCOBPartitioner(propertyService, cobBusinessStepService, retrieveLoanIdService, jobOperator,stepExecution, 1L);
//when
Map<String, ExecutionContext> partitions = loanCOBPartitioner.partition(1);
//then
Assertions.assertEquals(1, partitions.size());
validatePartitions(partitions, 1, 0, 0);
validatePartitions(partitions, 1, 0, 0, BUSINESS_DATE.toString(), "false");
}
private void validatePartitions(Map<String, ExecutionContext> partitions, int index, long min, long max) {
private void validatePartitions(Map<String, ExecutionContext> partitions, int index, long min, long max, String businessDate,
String isCatchUp) {
Assertions.assertEquals(BUSINESS_STEP_SET,
partitions.get(LoanCOBPartitioner.PARTITION_PREFIX + index).get(LoanCOBConstant.BUSINESS_STEPS));
Assertions.assertEquals(new COBParameter(min, max),
partitions.get(LoanCOBPartitioner.PARTITION_PREFIX + index).get(LoanCOBConstant.LOAN_COB_PARAMETER));
Assertions.assertEquals("partition_" + index, partitions.get(LoanCOBPartitioner.PARTITION_PREFIX + index).get("partition"));
Assertions.assertEquals(businessDate,
partitions.get(LoanCOBPartitioner.PARTITION_PREFIX + index).get(LoanCOBConstant.BUSINESS_DATE_PARAMETER_NAME));
Assertions.assertEquals(isCatchUp,
partitions.get(LoanCOBPartitioner.PARTITION_PREFIX + index).get(LoanCOBConstant.IS_CATCH_UP_PARAMETER_NAME));
}
}
@@ -59,8 +59,7 @@ public class LoanItemReaderStepDefinitions implements En {
private LoanLockingService lockingService = mock(LoanLockingService.class);
private LoanItemReader loanItemReader = new LoanItemReader(loanRepository, retrieveLoanIdService, customJobParameterResolver,
lockingService);
private LoanItemReader loanItemReader = new LoanItemReader(loanRepository, retrieveLoanIdService, lockingService);
private Loan loan = mock(Loan.class);
@@ -32,7 +32,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.apache.fineract.cob.common.CustomJobParameterResolver;
import java.util.stream.Stream;
import org.apache.fineract.cob.data.COBParameter;
import org.apache.fineract.cob.domain.LoanAccountLock;
import org.apache.fineract.cob.domain.LockOwner;
@@ -47,6 +47,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
@@ -59,14 +60,13 @@ class LoanItemReaderTest {
@Mock
private RetrieveLoanIdService retrieveLoanIdService;
@Mock
private CustomJobParameterResolver customJobParameterResolver;
@Mock
private LoanLockingService loanLockingService;
@Mock
private StepExecution stepExecution;
@Mock
private JobExecution jobExecution;
@Mock
private ExecutionContext executionContext;
@@ -83,14 +83,15 @@ class LoanItemReaderTest {
public void testLoanItemReaderSimple() throws Exception {
// given
ThreadLocalContextUtil.setTenant(new FineractPlatformTenant(1L, "test", "test", "UTC", null));
LoanItemReader loanItemReader = new LoanItemReader(loanRepository, retrieveLoanIdService, customJobParameterResolver,
loanLockingService);
LoanItemReader loanItemReader = new LoanItemReader(loanRepository, retrieveLoanIdService, loanLockingService);
when(stepExecution.getExecutionContext()).thenReturn(executionContext);
when(stepExecution.getJobExecution()).thenReturn(jobExecution);
when(jobExecution.getExecutionContext()).thenReturn(executionContext);
COBParameter loanCOBParameter = new COBParameter(1L, 5L);
when(executionContext.get(LoanCOBConstant.LOAN_COB_PARAMETER)).thenReturn(loanCOBParameter);
when(retrieveLoanIdService.retrieveAllNonClosedLoansByLastClosedBusinessDateAndMinAndMaxLoanId(loanCOBParameter, false))
.thenReturn(new ArrayList<>(List.of(1L, 2L, 3L, 4L, 5L)));
List<LoanAccountLock> accountLocks = List.of(1L, 2L, 3L, 4L, 5L).stream()
List<LoanAccountLock> accountLocks = Stream.of(1L, 2L, 3L, 4L, 5L)
.map(l -> new LoanAccountLock(l, LockOwner.LOAN_COB_CHUNK_PROCESSING, LocalDate.of(2023, 7, 25))).toList();
when(loanLockingService.findAllByLoanIdInAndLockOwner(List.of(1L, 2L, 3L, 4L, 5L), LockOwner.LOAN_COB_CHUNK_PROCESSING))
.thenReturn(accountLocks);
@@ -111,9 +112,10 @@ class LoanItemReaderTest {
public void testLoanItemReadNoOpenLoansFound() throws Exception {
// given
ThreadLocalContextUtil.setTenant(new FineractPlatformTenant(1L, "test", "test", "UTC", null));
LoanItemReader loanItemReader = new LoanItemReader(loanRepository, retrieveLoanIdService, customJobParameterResolver,
loanLockingService);
LoanItemReader loanItemReader = new LoanItemReader(loanRepository, retrieveLoanIdService, loanLockingService);
when(stepExecution.getExecutionContext()).thenReturn(executionContext);
when(stepExecution.getJobExecution()).thenReturn(jobExecution);
when(jobExecution.getExecutionContext()).thenReturn(executionContext);
COBParameter loanCOBParameter = new COBParameter(1L, 5L);
when(executionContext.get(LoanCOBConstant.LOAN_COB_PARAMETER)).thenReturn(loanCOBParameter);
when(retrieveLoanIdService.retrieveAllNonClosedLoansByLastClosedBusinessDateAndMinAndMaxLoanId(loanCOBParameter, false))
@@ -132,9 +134,10 @@ class LoanItemReaderTest {
public void testLoanItemReaderMultiThreadRead() throws Exception {
// given
ThreadLocalContextUtil.setTenant(new FineractPlatformTenant(1L, "test", "test", "UTC", null));
LoanItemReader loanItemReader = new LoanItemReader(loanRepository, retrieveLoanIdService, customJobParameterResolver,
loanLockingService);
LoanItemReader loanItemReader = new LoanItemReader(loanRepository, retrieveLoanIdService, loanLockingService);
when(stepExecution.getExecutionContext()).thenReturn(executionContext);
when(stepExecution.getJobExecution()).thenReturn(jobExecution);
when(jobExecution.getExecutionContext()).thenReturn(executionContext);
COBParameter loanCOBParameter = new COBParameter(1L, 100L);
when(executionContext.get(LoanCOBConstant.LOAN_COB_PARAMETER)).thenReturn(loanCOBParameter);
when(retrieveLoanIdService.retrieveAllNonClosedLoansByLastClosedBusinessDateAndMinAndMaxLoanId(loanCOBParameter, false))
@@ -160,7 +163,7 @@ class LoanItemReaderTest {
}
executorService.shutdown();
boolean b = executorService.awaitTermination(5L, TimeUnit.SECONDS);
Assertions.assertEquals(true, b, "Executor did not terminate successfully");
Assertions.assertTrue(b, "Executor did not terminate successfully");
// verify that this was called 100times, and for each loan it was called exactly once
for (long i = 1; i <= 100; i++) {