forked from apache/fineract
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINERACT-1971: Tiny refactoring to be able to customize down payment …
…logic in disbursement
- Loading branch information
Showing
5 changed files
with
208 additions
and
10 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...java/org/apache/fineract/portfolio/loanaccount/service/LoanDownPaymentHandlerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* 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.portfolio.loanaccount.service; | ||
|
||
import org.apache.fineract.infrastructure.core.api.JsonCommand; | ||
import org.apache.fineract.organisation.monetary.domain.Money; | ||
import org.apache.fineract.portfolio.loanaccount.data.ScheduleGeneratorDTO; | ||
import org.apache.fineract.portfolio.loanaccount.domain.Loan; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; | ||
|
||
public interface LoanDownPaymentHandlerService { | ||
|
||
LoanTransaction handleDownPayment(ScheduleGeneratorDTO scheduleGeneratorDTO, JsonCommand command, Money amountToDisburse, Loan loan); | ||
} |
51 changes: 51 additions & 0 deletions
51
.../org/apache/fineract/portfolio/loanaccount/service/LoanDownPaymentHandlerServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* 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.portfolio.loanaccount.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.fineract.infrastructure.core.api.JsonCommand; | ||
import org.apache.fineract.infrastructure.event.business.domain.loan.LoanBalanceChangedBusinessEvent; | ||
import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanTransactionDownPaymentPostBusinessEvent; | ||
import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanTransactionDownPaymentPreBusinessEvent; | ||
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService; | ||
import org.apache.fineract.organisation.monetary.domain.Money; | ||
import org.apache.fineract.portfolio.loanaccount.data.ScheduleGeneratorDTO; | ||
import org.apache.fineract.portfolio.loanaccount.domain.Loan; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepository; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class LoanDownPaymentHandlerServiceImpl implements LoanDownPaymentHandlerService { | ||
|
||
private final LoanTransactionRepository loanTransactionRepository; | ||
private final BusinessEventNotifierService businessEventNotifierService; | ||
|
||
@Override | ||
public LoanTransaction handleDownPayment(ScheduleGeneratorDTO scheduleGeneratorDTO, JsonCommand command, Money amountToDisburse, | ||
Loan loan) { | ||
businessEventNotifierService.notifyPreBusinessEvent(new LoanTransactionDownPaymentPreBusinessEvent(loan)); | ||
LoanTransaction downPaymentTransaction = loan.handleDownPayment(amountToDisburse.getAmount(), command, scheduleGeneratorDTO); | ||
LoanTransaction savedLoanTransaction = loanTransactionRepository.saveAndFlush(downPaymentTransaction); | ||
businessEventNotifierService.notifyPostBusinessEvent(new LoanTransactionDownPaymentPostBusinessEvent(savedLoanTransaction)); | ||
businessEventNotifierService.notifyPostBusinessEvent(new LoanBalanceChangedBusinessEvent(loan)); | ||
return savedLoanTransaction; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
.../apache/fineract/portfolio/loanaccount/service/LoanDownPaymentHandlerServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* 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.portfolio.loanaccount.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import org.apache.fineract.infrastructure.core.api.JsonCommand; | ||
import org.apache.fineract.infrastructure.event.business.domain.loan.LoanBalanceChangedBusinessEvent; | ||
import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanTransactionDownPaymentPostBusinessEvent; | ||
import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanTransactionDownPaymentPreBusinessEvent; | ||
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService; | ||
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency; | ||
import org.apache.fineract.organisation.monetary.domain.Money; | ||
import org.apache.fineract.organisation.monetary.domain.MoneyHelper; | ||
import org.apache.fineract.portfolio.loanaccount.data.ScheduleGeneratorDTO; | ||
import org.apache.fineract.portfolio.loanaccount.domain.Loan; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepository; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.junit.jupiter.MockitoSettings; | ||
import org.mockito.quality.Strictness; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
public class LoanDownPaymentHandlerServiceImplTest { | ||
|
||
private final MockedStatic<MoneyHelper> moneyHelper = Mockito.mockStatic(MoneyHelper.class); | ||
|
||
@Mock | ||
private BusinessEventNotifierService businessEventNotifierService; | ||
|
||
@Mock | ||
private LoanTransactionRepository loanTransactionRepository; | ||
|
||
@Mock | ||
private LoanTransaction loanTransaction; | ||
|
||
@Mock | ||
private ScheduleGeneratorDTO scheduleGeneratorDTO; | ||
|
||
@Mock | ||
private JsonCommand command; | ||
|
||
private LoanDownPaymentHandlerServiceImpl underTest; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
underTest = new LoanDownPaymentHandlerServiceImpl(loanTransactionRepository, businessEventNotifierService); | ||
moneyHelper.when(() -> MoneyHelper.getRoundingMode()).thenReturn(RoundingMode.UP); | ||
} | ||
|
||
@AfterEach | ||
public void reset() { | ||
moneyHelper.close(); | ||
} | ||
|
||
@Test | ||
public void testDownPaymentHandler() { | ||
// given | ||
Loan loanForProcessing = Mockito.mock(Loan.class); | ||
MonetaryCurrency loanCurrency = Mockito.mock(MonetaryCurrency.class); | ||
doNothing().when(businessEventNotifierService).notifyPreBusinessEvent(any(LoanTransactionDownPaymentPreBusinessEvent.class)); | ||
doNothing().when(businessEventNotifierService).notifyPostBusinessEvent(any(LoanTransactionDownPaymentPostBusinessEvent.class)); | ||
doNothing().when(businessEventNotifierService).notifyPostBusinessEvent(any(LoanBalanceChangedBusinessEvent.class)); | ||
when(loanTransactionRepository.saveAndFlush(any(LoanTransaction.class))).thenReturn(loanTransaction); | ||
when(loanForProcessing.handleDownPayment(any(BigDecimal.class), eq(command), eq(scheduleGeneratorDTO))).thenReturn(loanTransaction); | ||
when(loanForProcessing.getCurrency()).thenReturn(loanCurrency); | ||
when(loanCurrency.getCode()).thenReturn("CODE"); | ||
when(loanCurrency.getCurrencyInMultiplesOf()).thenReturn(1); | ||
when(loanCurrency.getDigitsAfterDecimal()).thenReturn(1); | ||
Money amount = Money.of(loanCurrency, BigDecimal.TEN); | ||
// when | ||
LoanTransaction actual = underTest.handleDownPayment(scheduleGeneratorDTO, command, amount, loanForProcessing); | ||
|
||
// then | ||
assertNotNull(actual); | ||
verify(businessEventNotifierService, Mockito.times(1)) | ||
.notifyPreBusinessEvent(Mockito.any(LoanTransactionDownPaymentPreBusinessEvent.class)); | ||
verify(businessEventNotifierService, Mockito.times(1)) | ||
.notifyPostBusinessEvent(Mockito.any(LoanTransactionDownPaymentPostBusinessEvent.class)); | ||
verify(businessEventNotifierService, Mockito.times(1)).notifyPostBusinessEvent(Mockito.any(LoanBalanceChangedBusinessEvent.class)); | ||
verify(loanForProcessing, Mockito.times(1)).handleDownPayment(any(BigDecimal.class), eq(command), eq(scheduleGeneratorDTO)); | ||
} | ||
} |