Skip to content

Commit

Permalink
FINERACT-2067: NPE business date partition step COB
Browse files Browse the repository at this point in the history
  • Loading branch information
kulminsky authored and adamsaghy committed Sep 18, 2024
1 parent c511385 commit 7b058d6
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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.listener;

import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ExecutionContext;

/**
* {@link StepExecutionListener} to copy values from Job execution context into Step execution context.
*/
@Slf4j
public class JobExecutionContextCopyListener implements StepExecutionListener {

private final List<String> stepExecutionKeys;

public JobExecutionContextCopyListener(List<String> stepExecutionKeys) {
this.stepExecutionKeys = stepExecutionKeys;
}

/**
* Method to copy values from Job execution context into Step execution context before step execution.
*
* @param stepExecution
* the step to be executed.
*/
@Override
public void beforeStep(final StepExecution stepExecution) {
log.debug("Before step: copying job execution context to step [{}]", stepExecution.getStepName());

final ExecutionContext stepExecutionContext = stepExecution.getExecutionContext();
final ExecutionContext jobExecutionContext = stepExecution.getJobExecution().getExecutionContext();

jobExecutionContext.entrySet().forEach(jobExecutionContextEntry -> {
if (stepExecutionKeys.contains(jobExecutionContextEntry.getKey())
&& BooleanUtils.isFalse(stepExecutionContext.containsKey(jobExecutionContextEntry.getKey()))) {
stepExecutionContext.put(jobExecutionContextEntry.getKey(), jobExecutionContextEntry.getValue());
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@

import static org.apache.fineract.cob.loan.LoanCOBConstant.JOB_NAME;

import java.util.List;
import org.apache.fineract.cob.COBBusinessStepService;
import org.apache.fineract.cob.common.CustomJobParameterResolver;
import org.apache.fineract.cob.conditions.BatchManagerCondition;
import org.apache.fineract.cob.listener.COBExecutionListenerRunner;
import org.apache.fineract.cob.listener.JobExecutionContextCopyListener;
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService;
import org.apache.fineract.infrastructure.jobs.service.JobName;
import org.apache.fineract.infrastructure.springbatch.PropertyService;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.JobOperator;
Expand Down Expand Up @@ -78,7 +81,7 @@ public class LoanCOBManagerConfiguration {
private CustomJobParameterResolver customJobParameterResolver;

@Bean
@JobScope
@StepScope
public LoanCOBPartitioner partitioner() {
return new LoanCOBPartitioner(propertyService, cobBusinessStepService, retrieveLoanIdService, jobOperator, jobExplorer,
LoanCOBConstant.NUMBER_OF_DAYS_BEHIND);
Expand All @@ -88,7 +91,8 @@ public LoanCOBPartitioner partitioner() {
public Step loanCOBStep() {
return stepBuilderFactory.get(LoanCOBConstant.LOAN_COB_PARTITIONER_STEP)
.partitioner(LoanCOBConstant.LOAN_COB_WORKER_STEP, partitioner()).pollInterval(propertyService.getPollInterval(JOB_NAME))
.outputChannel(outboundRequests).build();
.listener(new JobExecutionContextCopyListener(List.of("BusinessDate", "IS_CATCH_UP"))).outputChannel(outboundRequests)
.build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public class LoanCOBPartitioner implements Partitioner {

private final Long numberOfDays;

@Value("#{jobExecutionContext['BusinessDate']}")
@Value("#{stepExecutionContext['BusinessDate']}")
@Setter
private LocalDate businessDate;
@Value("#{jobExecutionContext['IS_CATCH_UP']}")
@Value("#{stepExecutionContext['IS_CATCH_UP']}")
@Setter
private Boolean isCatchUp;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.cob.listener;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;

@ExtendWith(MockitoExtension.class)
public class JobExecutionContextCopyListenerTest {

private static final List<String> STEP_EXECUTION_KEYS = List.of("BusinessDate", "IS_CATCH_UP");

private final JobExecutionContextCopyListener jobExecutionContextCopyListener = new JobExecutionContextCopyListener(
List.of("BusinessDate", "IS_CATCH_UP"));

@Test
public void verifyJobExecutionContextMapIsCopiedToEmptyStepExecutionContextMap() {
Map<String, Object> jobExecutionContextMap = createRandomHashmap();
jobExecutionContextMap.put("BusinessDate", "value");
jobExecutionContextMap.put("IS_CATCH_UP", "value");
final JobExecution jobExecution = new JobExecution(1L);
jobExecution.setExecutionContext(new ExecutionContext(jobExecutionContextMap));
final StepExecution stepExecution = new StepExecution("someStep", jobExecution);

jobExecutionContextCopyListener.beforeStep(stepExecution);

assertEquals(stepExecution.getExecutionContext().size(), 2);
stepExecution.getExecutionContext().toMap().forEach((key, value) -> {
assertTrue(STEP_EXECUTION_KEYS.contains(key));
assertEquals(value, jobExecutionContextMap.get(key));
});
}

@Test
public void verifyStepExecutionContextMapNotOverwritten() {
Map<String, Object> jobExecutionContextMap = new HashMap<>();
jobExecutionContextMap.put("BusinessDate", "BusinessDate value");
jobExecutionContextMap.put("IS_CATCH_UP", "IS_CATCH_UP value");
Map<String, Object> expectedStepExecutionContextMap = new HashMap<>();
expectedStepExecutionContextMap.put("BusinessDate", "BusinessDate anotherValue");
expectedStepExecutionContextMap.put("IS_CATCH_UP", "IS_CATCH_UP anotherValue");

final JobExecution jobExecution = new JobExecution(1L);
jobExecution.setExecutionContext(new ExecutionContext(jobExecutionContextMap));
final StepExecution actualStepExecution = new StepExecution("someStep", jobExecution);
actualStepExecution.setExecutionContext(new ExecutionContext(expectedStepExecutionContextMap));

jobExecutionContextCopyListener.beforeStep(actualStepExecution);

assertEquals(actualStepExecution.getExecutionContext().size(), STEP_EXECUTION_KEYS.size());
actualStepExecution.getExecutionContext().toMap().forEach((key, value) -> {
assertTrue(STEP_EXECUTION_KEYS.contains(key));
assertEquals(value, expectedStepExecutionContextMap.get(key));
});
}

/**
* Helper method to create a random Hash Map.
*
* @return the random Hash Map.
*/
private Map<String, Object> createRandomHashmap() {
final Random random = new Random();
final Map<String, Object> map = new HashMap<>();
for (int i = 0; i < random.nextInt(50); i++) {
map.put(RandomStringUtils.randomAlphanumeric(5), RandomStringUtils.randomAlphanumeric(5));
}

return map;
}
}

0 comments on commit 7b058d6

Please sign in to comment.