FINERACT-1724 - Optimistic locking fix

- [x] ReloadService for lead entities
- [x] ReloaderService iterating over ReloadService and try to find and reload the entity
- [x] Fix COBBusinessStepServiceImpl to reload the entity before business step
This commit is contained in:
Janos Haber
2023-03-08 14:27:48 +01:00
committed by Arnold Gálovics
parent baa71f6484
commit 367550007c
7 changed files with 129 additions and 4 deletions
@@ -23,6 +23,7 @@ import static org.mockito.Mockito.mock;
import org.apache.fineract.cob.COBBusinessStepService;
import org.apache.fineract.cob.COBBusinessStepServiceImpl;
import org.apache.fineract.cob.domain.BatchBusinessStepRepository;
import org.apache.fineract.cob.service.ReloaderService;
import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService;
@@ -48,9 +49,9 @@ public class TestDefaultConfiguration {
@Bean
public COBBusinessStepService cobBusinessStepService(BatchBusinessStepRepository batchBusinessStepRepository,
ApplicationContext context, ListableBeanFactory beanFactory, BusinessEventNotifierService businessEventNotifierService,
ConfigurationDomainService configurationDomainService) {
ConfigurationDomainService configurationDomainService, ReloaderService reloaderService) {
return new COBBusinessStepServiceImpl(batchBusinessStepRepository, context, beanFactory, businessEventNotifierService,
configurationDomainService);
configurationDomainService, reloaderService);
}
@Bean
@@ -30,6 +30,7 @@ import org.apache.fineract.cob.data.BusinessStepNameAndOrder;
import org.apache.fineract.cob.domain.BatchBusinessStep;
import org.apache.fineract.cob.domain.BatchBusinessStepRepository;
import org.apache.fineract.cob.exceptions.BusinessStepException;
import org.apache.fineract.cob.service.ReloaderService;
import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
import org.apache.fineract.infrastructure.core.domain.ActionContext;
@@ -51,6 +52,8 @@ public class COBBusinessStepServiceImpl implements COBBusinessStepService {
private final BusinessEventNotifierService businessEventNotifierService;
private final ConfigurationDomainService configurationDomainService;
private final ReloaderService reloaderService;
@Override
public <T extends COBBusinessStep<S>, S extends AbstractPersistableCustom> S run(TreeMap<Long, String> executionMap, S item) {
if (executionMap == null || executionMap.isEmpty()) {
@@ -67,6 +70,7 @@ public class COBBusinessStepServiceImpl implements COBBusinessStepService {
try {
ThreadLocalContextUtil.setActionContext(ActionContext.COB);
COBBusinessStep<S> businessStepBean = (COBBusinessStep<S>) applicationContext.getBean(businessStep);
item = reloaderService.reload(item);
item = businessStepBean.execute(item);
} catch (Exception e) {
throw new BusinessStepException("Error happened during business step execution", e);
@@ -0,0 +1,43 @@
/**
* 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.service;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class LoanReloadService implements ReloadService<Loan> {
private final LoanRepository loanRepository;
@Override
public <S extends AbstractPersistableCustom> boolean canReload(S input) {
return input instanceof Loan;
}
@Override
public Loan reload(Loan input) {
return loanRepository.findById(input.getId()).get();
}
}
@@ -0,0 +1,28 @@
/**
* 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.service;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
public interface ReloadService<S extends AbstractPersistableCustom> {
<X extends AbstractPersistableCustom> boolean canReload(X input);
S reload(S input);
}
@@ -0,0 +1,40 @@
/**
* 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.service;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class ReloaderService {
private final List<ReloadService> reloadServices;
public <S extends AbstractPersistableCustom> S reload(S input) {
for (ReloadService reloadService : reloadServices) {
if (reloadService.canReload(input)) {
return (S) reloadService.reload(input);
}
}
return input;
}
}
@@ -20,6 +20,7 @@ package org.apache.fineract.cob;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -37,6 +38,7 @@ import org.apache.fineract.cob.domain.BatchBusinessStep;
import org.apache.fineract.cob.domain.BatchBusinessStepRepository;
import org.apache.fineract.cob.exceptions.BusinessStepException;
import org.apache.fineract.cob.loan.LoanCOBBusinessStep;
import org.apache.fineract.cob.service.ReloaderService;
import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService;
import org.apache.fineract.infrastructure.core.domain.AbstractAuditableCustom;
import org.apache.fineract.infrastructure.core.domain.ActionContext;
@@ -55,8 +57,10 @@ public class COBBusinessStepServiceStepDefinitions implements En {
private BatchBusinessStepRepository batchBusinessStepRepository = mock(BatchBusinessStepRepository.class);
private BusinessEventNotifierService businessEventNotifierService = mock(BusinessEventNotifierService.class);
private ConfigurationDomainService configurationDomainService = mock(ConfigurationDomainService.class);
private ReloaderService reloaderService = mock(ReloaderService.class);
private final COBBusinessStepService businessStepService = new COBBusinessStepServiceImpl(batchBusinessStepRepository,
applicationContext, beanFactory, businessEventNotifierService, configurationDomainService);
applicationContext, beanFactory, businessEventNotifierService, configurationDomainService, reloaderService);
private COBBusinessStep cobBusinessStep = mock(COBBusinessStep.class);
private COBBusinessStep notRegistereCobBusinessStep = mock(COBBusinessStep.class);
private TreeMap<Long, String> executionMap;
@@ -92,6 +96,7 @@ public class COBBusinessStepServiceStepDefinitions implements En {
lenient().when(this.applicationContext.getBean("test")).thenReturn(cobBusinessStep);
lenient().when(this.applicationContext.getBean("notExist")).thenThrow(BeanCreationException.class);
lenient().when(this.cobBusinessStep.execute(this.item)).thenReturn(outputItem);
lenient().when(this.reloaderService.reload(any())).thenAnswer(invocation -> invocation.getArgument(0));
lenient().when(this.configurationDomainService.isCOBBulkEventEnabled()).thenReturn(true);
ThreadLocalContextUtil.setActionContext(ActionContext.DEFAULT);
@@ -114,7 +119,7 @@ public class COBBusinessStepServiceStepDefinitions implements En {
lenient().when(this.batchBusinessStepRepository.findAllByJobName("exist"))
.thenReturn(Collections.singletonList(this.batchBusinessStep));
lenient().when(this.batchBusinessStepRepository.findAllByJobName("notExist")).thenReturn(Collections.emptyList());
lenient().when(this.reloaderService.reload(any())).thenAnswer(invocation -> invocation.getArgument(0));
lenient().when(this.beanFactory.getBeanNamesForType((Class<?>) null)).thenReturn(new String[] { "notExist" });
lenient().when(this.beanFactory.getBeanNamesForType(Object.class)).thenReturn(new String[] { "testNotRegistered" });
lenient().when(this.beanFactory.getBeanNamesForType(String.class)).thenReturn(new String[] {});
@@ -69,11 +69,15 @@ public class COBBulkEventConfigurationTest {
@InjectMocks
private COBBusinessStepServiceImpl underTest;
@Mock
private ReloaderService reloaderService;
@BeforeEach
public void setUp() {
ThreadLocalContextUtil.setTenant(new FineractPlatformTenant(1L, "default", "Default", "Asia/Kolkata", null));
ThreadLocalContextUtil
.setBusinessDates(new HashMap<>(Map.of(BusinessDateType.BUSINESS_DATE, LocalDate.now(ZoneId.systemDefault()))));
when(reloaderService.reload(any())).thenAnswer(invocation -> invocation.getArgument(0));
}
@AfterEach