FINERACT-1932: Fineract modularization Fineract progressive loan module

This commit is contained in:
Ruchi Dhamankar
2024-05-24 14:35:34 +02:00
committed by Adam Saghy
parent f59ae64222
commit 53f4864980
53 changed files with 364 additions and 2 deletions
@@ -1,237 +0,0 @@
/**
* 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.loanproduct.domain;
import static org.apache.fineract.portfolio.loanproduct.domain.AdvancedPaymentAllocationsValidator.ADVANCED_PAYMENT_ALLOCATION_STRATEGY;
import static org.apache.fineract.portfolio.loanproduct.domain.FutureInstallmentAllocationRule.NEXT_INSTALLMENT;
import static org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationTransactionType.DEFAULT;
import static org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationType.IN_ADVANCE_PENALTY;
import static org.mockito.Mockito.times;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class AdvancedPaymentAllocationsJsonParserTest {
@Mock
private AdvancedPaymentAllocationsValidator advancedPaymentAllocationsValidator;
@InjectMocks
private AdvancedPaymentAllocationsJsonParser advancedPaymentAllocationsJsonParser;
private FromJsonHelper fromJsonHelper = new FromJsonHelper();
@Test
public void testEmptyJson() throws JsonProcessingException {
Map<String, Object> map = new HashMap<>();
JsonCommand command = createJsonCommand(map);
// when
List<LoanProductPaymentAllocationRule> loanProductPaymentAllocationRules = advancedPaymentAllocationsJsonParser
.assembleLoanProductPaymentAllocationRules(command, "other-strategy");
// then
Assertions.assertNull(loanProductPaymentAllocationRules);
Mockito.verify(advancedPaymentAllocationsValidator, times(1)).validate(null, "other-strategy");
}
@Test
public void testNullAllocationRuleJson() throws JsonProcessingException {
Map<String, Object> map = new HashMap<>();
JsonCommand command = createJsonCommand(map);
// when
List<LoanProductPaymentAllocationRule> loanProductPaymentAllocationRules = advancedPaymentAllocationsJsonParser
.assembleLoanProductPaymentAllocationRules(command, "advanced-payment-allocation-strategy");
// then
Assertions.assertNull(loanProductPaymentAllocationRules);
Mockito.verify(advancedPaymentAllocationsValidator, times(1)).validate(null, "advanced-payment-allocation-strategy");
}
@Test
public void testParseSinglePaymentAllocation() throws JsonProcessingException {
// given
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> paymentAllocations = new ArrayList<>();
map.put("paymentAllocation", paymentAllocations);
List<String> allocationRule = EnumSet.allOf(PaymentAllocationType.class).stream().map(Enum::name).toList();
paymentAllocations.add(createPaymentAllocationEntry("DEFAULT", "NEXT_INSTALLMENT", allocationRule));
JsonCommand command = createJsonCommand(map);
// when
List<LoanProductPaymentAllocationRule> loanProductPaymentAllocationRules = advancedPaymentAllocationsJsonParser
.assembleLoanProductPaymentAllocationRules(command, ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
// then
Assertions.assertEquals(1, loanProductPaymentAllocationRules.size());
Assertions.assertEquals(NEXT_INSTALLMENT, loanProductPaymentAllocationRules.get(0).getFutureInstallmentAllocationRule());
Assertions.assertEquals(DEFAULT, loanProductPaymentAllocationRules.get(0).getTransactionType());
Assertions.assertEquals(12, loanProductPaymentAllocationRules.get(0).getAllocationTypes().size());
Assertions.assertEquals(EnumSet.allOf(PaymentAllocationType.class).stream().toList(),
loanProductPaymentAllocationRules.get(0).getAllocationTypes());
Mockito.verify(advancedPaymentAllocationsValidator, times(1)).validate(loanProductPaymentAllocationRules,
ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
Mockito.verify(advancedPaymentAllocationsValidator, times(1))
.validatePairOfOrderAndPaymentAllocationType(createPaymentAllocationTypeList());
Mockito.verifyNoMoreInteractions(advancedPaymentAllocationsValidator);
}
@Test
public void testInvalidTransactionTypeAndFutureAllocation() throws JsonProcessingException {
// given
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> paymentAllocations = new ArrayList<>();
map.put("paymentAllocation", paymentAllocations);
List<String> allocationRule = EnumSet.allOf(PaymentAllocationType.class).stream().map(Enum::name).toList();
paymentAllocations.add(createPaymentAllocationEntry("INVALID", "INVALID", allocationRule));
JsonCommand command = createJsonCommand(map);
// when
List<LoanProductPaymentAllocationRule> loanProductPaymentAllocationRules = advancedPaymentAllocationsJsonParser
.assembleLoanProductPaymentAllocationRules(command, ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
// then
Assertions.assertEquals(1, loanProductPaymentAllocationRules.size());
Assertions.assertNull(loanProductPaymentAllocationRules.get(0).getFutureInstallmentAllocationRule());
Assertions.assertNull(loanProductPaymentAllocationRules.get(0).getTransactionType());
Assertions.assertEquals(12, loanProductPaymentAllocationRules.get(0).getAllocationTypes().size());
Assertions.assertEquals(EnumSet.allOf(PaymentAllocationType.class).stream().toList(),
loanProductPaymentAllocationRules.get(0).getAllocationTypes());
Mockito.verify(advancedPaymentAllocationsValidator, times(1)).validate(loanProductPaymentAllocationRules,
ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
Mockito.verify(advancedPaymentAllocationsValidator, times(1))
.validatePairOfOrderAndPaymentAllocationType(createPaymentAllocationTypeList());
Mockito.verifyNoMoreInteractions(advancedPaymentAllocationsValidator);
}
@Test
public void testInvalidAndNullAllocationRules() throws JsonProcessingException {
// given
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> paymentAllocations = new ArrayList<>();
map.put("paymentAllocation", paymentAllocations);
List<String> allocationRule = Arrays.asList(new String[] { "invalid", null, "IN_ADVANCE_PENALTY" });
paymentAllocations.add(createPaymentAllocationEntry("DEFAULT", "NEXT_INSTALLMENT", allocationRule));
JsonCommand command = createJsonCommand(map);
// when
List<LoanProductPaymentAllocationRule> loanProductPaymentAllocationRules = advancedPaymentAllocationsJsonParser
.assembleLoanProductPaymentAllocationRules(command, ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
// then
Assertions.assertEquals(1, loanProductPaymentAllocationRules.size());
Assertions.assertEquals(NEXT_INSTALLMENT, loanProductPaymentAllocationRules.get(0).getFutureInstallmentAllocationRule());
Assertions.assertEquals(DEFAULT, loanProductPaymentAllocationRules.get(0).getTransactionType());
Assertions.assertEquals(3, loanProductPaymentAllocationRules.get(0).getAllocationTypes().size());
Assertions.assertNull(loanProductPaymentAllocationRules.get(0).getAllocationTypes().get(0));
Assertions.assertNull(loanProductPaymentAllocationRules.get(0).getAllocationTypes().get(1));
Assertions.assertEquals(IN_ADVANCE_PENALTY, loanProductPaymentAllocationRules.get(0).getAllocationTypes().get(2));
Mockito.verify(advancedPaymentAllocationsValidator, times(1)).validate(loanProductPaymentAllocationRules,
ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
Mockito.verify(advancedPaymentAllocationsValidator, times(1))
.validatePairOfOrderAndPaymentAllocationType(List.of(Pair.of(1, null), Pair.of(2, null), Pair.of(3, IN_ADVANCE_PENALTY)));
Mockito.verifyNoMoreInteractions(advancedPaymentAllocationsValidator);
}
@Test
public void testNullTransactionTypeAndFutureAllocation() throws JsonProcessingException {
// given
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> paymentAllocations = new ArrayList<>();
map.put("paymentAllocation", paymentAllocations);
List<String> allocationRule = EnumSet.allOf(PaymentAllocationType.class).stream().map(Enum::name).toList();
paymentAllocations.add(createPaymentAllocationEntry(null, null, allocationRule));
JsonCommand command = createJsonCommand(map);
// when
List<LoanProductPaymentAllocationRule> loanProductPaymentAllocationRules = advancedPaymentAllocationsJsonParser
.assembleLoanProductPaymentAllocationRules(command, ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
// then
Assertions.assertEquals(1, loanProductPaymentAllocationRules.size());
Assertions.assertNull(loanProductPaymentAllocationRules.get(0).getFutureInstallmentAllocationRule());
Assertions.assertNull(loanProductPaymentAllocationRules.get(0).getTransactionType());
Assertions.assertEquals(12, loanProductPaymentAllocationRules.get(0).getAllocationTypes().size());
Assertions.assertEquals(EnumSet.allOf(PaymentAllocationType.class).stream().toList(),
loanProductPaymentAllocationRules.get(0).getAllocationTypes());
Mockito.verify(advancedPaymentAllocationsValidator, times(1)).validate(loanProductPaymentAllocationRules,
ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
Mockito.verify(advancedPaymentAllocationsValidator, times(1))
.validatePairOfOrderAndPaymentAllocationType(createPaymentAllocationTypeList());
Mockito.verifyNoMoreInteractions(advancedPaymentAllocationsValidator);
}
public Map<String, Object> createPaymentAllocationEntry(String transactionType, String futureInstallmentAllocation,
List<String> orderedRules) {
Map<String, Object> map = new HashMap<>();
map.put("transactionType", transactionType);
map.put("futureInstallmentAllocationRule", futureInstallmentAllocation);
List<Map<String, Object>> paymentAllocationOrder = new ArrayList<>();
map.put("paymentAllocationOrder", paymentAllocationOrder);
for (int i = 0; i < orderedRules.size(); i++) {
HashMap<String, Object> entry = new HashMap<>();
entry.put("paymentAllocationRule", orderedRules.get(i));
entry.put("order", i + 1);
paymentAllocationOrder.add(entry);
}
return map;
}
private static List<Pair<Integer, PaymentAllocationType>> createPaymentAllocationTypeList() {
AtomicInteger i = new AtomicInteger(1);
List<Pair<Integer, PaymentAllocationType>> list = EnumSet.allOf(PaymentAllocationType.class).stream()
.map(p -> Pair.of(i.getAndIncrement(), p)).toList();
return list;
}
@NotNull
private JsonCommand createJsonCommand(Map<String, Object> jsonMap) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonMap);
JsonCommand command = JsonCommand.from(json, JsonParser.parseString(json), fromJsonHelper, null, 1L, 2L, 3L, 4L, null, null, null,
null, null, null, null, null);
return command;
}
}
@@ -1,227 +0,0 @@
/**
* 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.loanproduct.domain;
import static org.apache.fineract.portfolio.loanproduct.domain.AdvancedPaymentAllocationsValidator.ADVANCED_PAYMENT_ALLOCATION_STRATEGY;
import static org.apache.fineract.portfolio.loanproduct.domain.FutureInstallmentAllocationRule.LAST_INSTALLMENT;
import static org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationTransactionType.DEFAULT;
import static org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationTransactionType.REPAYMENT;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
class AdvancedPaymentAllocationsValidatorTest {
private AdvancedPaymentAllocationsValidator underTest = new AdvancedPaymentAllocationsValidator();
@Test
public void testPaymentAllocationsHasNoError() {
underTest.validatePairOfOrderAndPaymentAllocationType(createPaymentAllocationTypeList());
}
@Test
public void testPaymentAllocationsValidationThrowsErrorWhenLessElement() {
PlatformApiDataValidationException validationException = assertThrows(PlatformApiDataValidationException.class,
() -> underTest.validatePairOfOrderAndPaymentAllocationType(createPaymentAllocationTypeList().subList(0, 11)));
assertPlatformException("Each provided payment allocation must contain exactly 12 allocation rules, but 11 were provided",
"advanced-payment-strategy.each_payment_allocation_order.must.contain.12.entries", validationException);
}
@Test
public void testPaymentAllocationsValidationThrowsErrorWhenWithDuplicate() {
ArrayList<Pair<Integer, PaymentAllocationType>> pairs = new ArrayList<>(createPaymentAllocationTypeList().subList(0, 11));
pairs.add(pairs.get(10));
PlatformApiDataValidationException validationException = assertThrows(PlatformApiDataValidationException.class,
() -> underTest.validatePairOfOrderAndPaymentAllocationType(pairs));
assertPlatformException("The list of provided payment allocation rules must not contain any duplicates",
"advanced-payment-strategy.must.not.have.duplicate.payment.allocation.rule", validationException);
}
@Test
public void testPaymentAllocationsValidationThrowsErrorWhenOrderIsNotInRange() {
List<Pair<Integer, PaymentAllocationType>> pairs = createPaymentAllocationTypeList().stream()
.map(p -> Pair.of(p.getLeft() + 1, p.getRight())).toList();
PlatformApiDataValidationException validationException = assertThrows(PlatformApiDataValidationException.class,
() -> underTest.validatePairOfOrderAndPaymentAllocationType(pairs));
assertPlatformException("The provided orders must be between 1 and 12", "advanced-payment-strategy.invalid.order",
validationException);
}
@Test
public void testValidateNoError() {
LoanProductPaymentAllocationRule lppr1 = createLoanProductAllocationRule1();
LoanProductPaymentAllocationRule lppr2 = createLoanProductAllocationRule2();
underTest.validate(List.of(lppr1, lppr2), ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
}
@Test
public void testValidateEmptyRuleList() {
assertPlatformValidationException(
"Advanced-payment-allocation-strategy was selected but no DEFAULT payment allocation was provided",
"advanced-payment-strategy-without-default-payment-allocation",
() -> underTest.validate(List.of(), ADVANCED_PAYMENT_ALLOCATION_STRATEGY));
}
@Test
public void testValidateMissingList() {
assertPlatformValidationException(
"Advanced-payment-allocation-strategy was selected but no DEFAULT payment allocation was provided",
"advanced-payment-strategy-without-default-payment-allocation",
() -> underTest.validate(null, ADVANCED_PAYMENT_ALLOCATION_STRATEGY));
}
@Test
public void testValidatePaymentAllocationThrowsErrorWhenNoDefault() {
LoanProductPaymentAllocationRule lppr2 = createLoanProductAllocationRule2();
assertPlatformValidationException(
"Advanced-payment-allocation-strategy was selected but no DEFAULT payment allocation was provided",
"advanced-payment-strategy-without-default-payment-allocation",
() -> underTest.validate(List.of(lppr2), ADVANCED_PAYMENT_ALLOCATION_STRATEGY));
}
@Test
public void testValidateThrowsErrorWhenDuplicate() {
LoanProductPaymentAllocationRule lppr1 = createLoanProductAllocationRule1();
LoanProductPaymentAllocationRule lppr2 = createLoanProductAllocationRule2();
LoanProductPaymentAllocationRule lppr3 = createLoanProductAllocationRule2();
assertPlatformValidationException("The same transaction type must be provided only once",
"advanced-payment-strategy-with-duplicate-payment-allocation",
() -> underTest.validate(List.of(lppr1, lppr2, lppr3), ADVANCED_PAYMENT_ALLOCATION_STRATEGY));
}
@Test
public void testValidateThrowsErrorWhenPaymentAllocationProvidedWithOtherStrategy() {
LoanProductPaymentAllocationRule lppr1 = createLoanProductAllocationRule1();
LoanProductPaymentAllocationRule lppr2 = createLoanProductAllocationRule2();
assertPlatformValidationException("In case 'some-other-strategy' payment strategy, payment_allocation must not be provided",
"payment_allocation.must.not.be.provided.when.allocation.strategy.is.not.advanced-payment-strategy",
() -> underTest.validate(List.of(lppr1, lppr2), "some-other-strategy"));
}
@Test
public void testValidateThrowsErrorWhenFutureInstallmentIsEmpty() {
LoanProductPaymentAllocationRule lppr1 = createLoanProductAllocationRule1();
lppr1.setFutureInstallmentAllocationRule(null);
assertPlatformValidationException("Payment allocation was provided without a valid future installment allocation rule",
"advanced-payment-strategy.with.not.valid.future.installment.allocation.rule",
() -> underTest.validate(List.of(lppr1), ADVANCED_PAYMENT_ALLOCATION_STRATEGY));
}
@Test
public void testValidateThrowsErrorWhenTransactionTypeEmpty() {
LoanProductPaymentAllocationRule lppr1 = createLoanProductAllocationRule1();
LoanProductPaymentAllocationRule lppr2 = createLoanProductAllocationRule1();
lppr2.setTransactionType(null);
assertPlatformValidationException("Payment allocation was provided with a not valid transaction type",
"advanced-payment-strategy.with.not.valid.transaction.type",
() -> underTest.validate(List.of(lppr1, lppr2), ADVANCED_PAYMENT_ALLOCATION_STRATEGY));
}
@Test
public void checkGroupingOfAllocationRulesGoodOrder() {
List<LoanProductPaymentAllocationRule> allocationRules1 = List.of(createLoanProductAllocationRule1());
Assertions.assertDoesNotThrow(() -> underTest.checkGroupingOfAllocationRules(allocationRules1));
List<LoanProductPaymentAllocationRule> allocationRules2 = List.of(createLoanProductAllocationRule4());
Assertions.assertDoesNotThrow(() -> underTest.checkGroupingOfAllocationRules(allocationRules2));
}
@Test
public void checkGroupingOfAllocationRulesWrongOrder() {
List<LoanProductPaymentAllocationRule> allocationRules = List.of(createLoanProductAllocationRule3());
assertPlatformValidationException("Horizontal repayment schedule processing is not supporting mixed due type allocation rules!",
"mixed.due.type.allocation.rules.are.not.supported.with.horizontal.installment.processing",
() -> underTest.checkGroupingOfAllocationRules(allocationRules));
}
private void assertPlatformValidationException(String message, String code, Executable executable) {
PlatformApiDataValidationException validationException = assertThrows(PlatformApiDataValidationException.class, executable);
assertPlatformException(message, code, validationException);
}
@NotNull
private static LoanProductPaymentAllocationRule createLoanProductAllocationRule1() {
LoanProductPaymentAllocationRule lppr1 = new LoanProductPaymentAllocationRule();
lppr1.setTransactionType(DEFAULT);
lppr1.setFutureInstallmentAllocationRule(LAST_INSTALLMENT);
lppr1.setAllocationTypes(EnumSet.allOf(PaymentAllocationType.class).stream().toList());
return lppr1;
}
@NotNull
private static LoanProductPaymentAllocationRule createLoanProductAllocationRule2() {
LoanProductPaymentAllocationRule lppr2 = new LoanProductPaymentAllocationRule();
lppr2.setTransactionType(REPAYMENT);
lppr2.setFutureInstallmentAllocationRule(LAST_INSTALLMENT);
ArrayList<PaymentAllocationType> allocationTypes = new ArrayList<>(EnumSet.allOf(PaymentAllocationType.class).stream().toList());
Collections.shuffle(allocationTypes);
lppr2.setAllocationTypes(allocationTypes);
return lppr2;
}
@NotNull
private static LoanProductPaymentAllocationRule createLoanProductAllocationRule3() {
LoanProductPaymentAllocationRule lppr = new LoanProductPaymentAllocationRule();
lppr.setTransactionType(REPAYMENT);
lppr.setFutureInstallmentAllocationRule(LAST_INSTALLMENT);
ArrayList<PaymentAllocationType> allocationTypes = new ArrayList<>(EnumSet.allOf(PaymentAllocationType.class).stream().toList());
Collections.swap(allocationTypes, 0, 4);
Collections.swap(allocationTypes, 3, 8);
Collections.swap(allocationTypes, 7, 11);
lppr.setAllocationTypes(allocationTypes);
return lppr;
}
@NotNull
private static LoanProductPaymentAllocationRule createLoanProductAllocationRule4() {
LoanProductPaymentAllocationRule lppr = new LoanProductPaymentAllocationRule();
lppr.setTransactionType(DEFAULT);
lppr.setFutureInstallmentAllocationRule(LAST_INSTALLMENT);
ArrayList<PaymentAllocationType> allocationTypes = new ArrayList<>(EnumSet.allOf(PaymentAllocationType.class).stream().toList());
Collections.swap(allocationTypes, 0, 4);
Collections.swap(allocationTypes, 1, 5);
Collections.swap(allocationTypes, 2, 6);
Collections.swap(allocationTypes, 3, 7);
lppr.setAllocationTypes(allocationTypes);
return lppr;
}
@NotNull
private static List<Pair<Integer, PaymentAllocationType>> createPaymentAllocationTypeList() {
AtomicInteger i = new AtomicInteger(1);
return EnumSet.allOf(PaymentAllocationType.class).stream().map(p -> Pair.of(i.getAndIncrement(), p)).toList();
}
private void assertPlatformException(String expectedMessage, String expectedCode,
PlatformApiDataValidationException platformApiDataValidationException) {
Assertions.assertEquals(expectedMessage, platformApiDataValidationException.getErrors().get(0).getDefaultUserMessage());
Assertions.assertEquals(expectedCode, platformApiDataValidationException.getErrors().get(0).getUserMessageGlobalisationCode());
}
}
@@ -1,125 +0,0 @@
/**
* 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.loanproduct.domain;
import static org.apache.fineract.portfolio.loanproduct.domain.CreditAllocationsValidator.ADVANCED_PAYMENT_ALLOCATION_STRATEGY;
import static org.mockito.Mockito.times;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class CreditAllocationsJsonParserTest {
@InjectMocks
private CreditAllocationsJsonParser creditAllocationsJsonParser;
@Mock
private CreditAllocationsValidator creditAllocationsValidator;
private FromJsonHelper fromJsonHelper = new FromJsonHelper();
@Test
public void testEmptyJson() throws JsonProcessingException {
Map<String, Object> map = new HashMap<>();
JsonCommand command = createJsonCommand(map);
// when
List<LoanProductCreditAllocationRule> loanProductCreditAllocationRules = creditAllocationsJsonParser
.assembleLoanProductCreditAllocationRules(command, "other-strategy");
// then
Assertions.assertNull(loanProductCreditAllocationRules);
Mockito.verify(creditAllocationsValidator, times(1)).validate(null, "other-strategy");
}
@Test
public void testParseSingleChargebackAllocation() throws JsonProcessingException {
// given
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> creditAllocations = new ArrayList<>();
map.put("creditAllocation", creditAllocations);
List<String> allocationRule = EnumSet.allOf(AllocationType.class).stream().map(Enum::name).toList();
creditAllocations.add(createCreditAllocationEntry("CHARGEBACK", allocationRule));
JsonCommand command = createJsonCommand(map);
// when
List<LoanProductCreditAllocationRule> creditAllocationRules = creditAllocationsJsonParser
.assembleLoanProductCreditAllocationRules(command, ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
// then
Assertions.assertEquals(1, creditAllocationRules.size());
Assertions.assertEquals(CreditAllocationTransactionType.CHARGEBACK, creditAllocationRules.get(0).getTransactionType());
Assertions.assertEquals(4, creditAllocationRules.get(0).getAllocationTypes().size());
Assertions.assertEquals(EnumSet.allOf(AllocationType.class).stream().toList(), creditAllocationRules.get(0).getAllocationTypes());
Mockito.verify(creditAllocationsValidator, times(1)).validate(creditAllocationRules, ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
Mockito.verify(creditAllocationsValidator, times(1)).validatePairOfOrderAndCreditAllocationType(createAllocationTypeList());
Mockito.verifyNoMoreInteractions(creditAllocationsValidator);
}
private static List<Pair<Integer, AllocationType>> createAllocationTypeList() {
AtomicInteger i = new AtomicInteger(1);
List<Pair<Integer, AllocationType>> list = EnumSet.allOf(AllocationType.class).stream().map(p -> Pair.of(i.getAndIncrement(), p))
.toList();
return list;
}
public Map<String, Object> createCreditAllocationEntry(String transactionType, List<String> orderedRules) {
Map<String, Object> map = new HashMap<>();
map.put("transactionType", transactionType);
List<Map<String, Object>> creditAllocationOrder = new ArrayList<>();
map.put("creditAllocationOrder", creditAllocationOrder);
for (int i = 0; i < orderedRules.size(); i++) {
HashMap<String, Object> entry = new HashMap<>();
entry.put("creditAllocationRule", orderedRules.get(i));
entry.put("order", i + 1);
creditAllocationOrder.add(entry);
}
return map;
}
@NotNull
private JsonCommand createJsonCommand(Map<String, Object> jsonMap) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonMap);
JsonCommand command = JsonCommand.from(json, JsonParser.parseString(json), fromJsonHelper, null, 1L, 2L, 3L, 4L, null, null, null,
null, null, null, null, null);
return command;
}
}
@@ -1,132 +0,0 @@
/**
* 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.loanproduct.domain;
import static org.apache.fineract.portfolio.loanproduct.domain.AdvancedPaymentAllocationsValidator.ADVANCED_PAYMENT_ALLOCATION_STRATEGY;
import static org.apache.fineract.portfolio.loanproduct.domain.CreditAllocationTransactionType.CHARGEBACK;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
public class CreditAllocationsValidatorTest {
private CreditAllocationsValidator underTest = new CreditAllocationsValidator();
@Test
public void testCreditAllocationsHasNoError() {
underTest.validatePairOfOrderAndCreditAllocationType(createCreditAllocationTypeList());
}
@Test
public void testCreditAllocationsValidationThrowsErrorWhenLessElement() {
PlatformApiDataValidationException validationException = assertThrows(PlatformApiDataValidationException.class,
() -> underTest.validatePairOfOrderAndCreditAllocationType(createCreditAllocationTypeList().subList(0, 3)));
assertPlatformException("Each provided credit allocation must contain exactly 4 allocation rules, but 3 were provided",
"advanced-payment-strategy.each_credit_allocation_order.must.contain.4.entries", validationException);
}
@Test
public void testCreditAllocationsValidationThrowsErrorWhenWithDuplicate() {
ArrayList<Pair<Integer, AllocationType>> pairs = new ArrayList<>(createCreditAllocationTypeList().subList(0, 3));
pairs.add(pairs.get(2));
PlatformApiDataValidationException validationException = assertThrows(PlatformApiDataValidationException.class,
() -> underTest.validatePairOfOrderAndCreditAllocationType(pairs));
assertPlatformException("The list of provided credit allocation rules must not contain any duplicates",
"advanced-payment-strategy.must.not.have.duplicate.credit.allocation.rule", validationException);
}
@Test
public void testCreditAllocationsValidationThrowsErrorWhenOrderIsNotInRange() {
List<Pair<Integer, AllocationType>> pairs = createCreditAllocationTypeList().stream()
.map(p -> Pair.of(p.getLeft() + 1, p.getRight())).toList();
PlatformApiDataValidationException validationException = assertThrows(PlatformApiDataValidationException.class,
() -> underTest.validatePairOfOrderAndCreditAllocationType(pairs));
assertPlatformException("The provided orders must be between 1 and 4", "advanced-payment-strategy.invalid.order",
validationException);
}
@Test
public void testValidateThrowsErrorWhenPaymentAllocationProvidedWithOtherStrategy() {
assertPlatformValidationException("In case 'some-other-strategy' payment strategy, creditAllocation must not be provided",
"credit_allocation.must.not.be.provided.when.allocation.strategy.is.not.advanced-payment-strategy",
() -> underTest.validate(List.of(createLoanProductCreditAllocationRule1()), "some-other-strategy"));
}
@Test
public void testValidateThrowsErrorWhenTransactionTypeEmpty() {
LoanProductCreditAllocationRule lpcar = createLoanProductCreditAllocationRule1();
lpcar.setTransactionType(null);
assertPlatformValidationException("Credit allocation was provided with a not valid transaction type",
"advanced-payment-strategy.with.not.valid.transaction.type",
() -> underTest.validate(List.of(lpcar), ADVANCED_PAYMENT_ALLOCATION_STRATEGY));
}
@Test
public void testValidateNoError() {
underTest.validate(List.of(createLoanProductCreditAllocationRule1()), ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
}
@Test
public void testValidateCreditAllocationIsOptional() {
underTest.validate(List.of(), ADVANCED_PAYMENT_ALLOCATION_STRATEGY);
}
@Test
public void testValidateThrowsErrorWhenDuplicate() {
assertPlatformValidationException("The same transaction type must be provided only once",
"advanced-payment-strategy-with-duplicate-credit-allocation",
() -> underTest.validate(List.of(createLoanProductCreditAllocationRule1(), createLoanProductCreditAllocationRule1()),
ADVANCED_PAYMENT_ALLOCATION_STRATEGY));
}
@NotNull
private static List<Pair<Integer, AllocationType>> createCreditAllocationTypeList() {
AtomicInteger i = new AtomicInteger(1);
return EnumSet.allOf(AllocationType.class).stream().map(p -> Pair.of(i.getAndIncrement(), p)).toList();
}
@NotNull
private static LoanProductCreditAllocationRule createLoanProductCreditAllocationRule1() {
LoanProductCreditAllocationRule lpcr1 = new LoanProductCreditAllocationRule();
lpcr1.setTransactionType(CHARGEBACK);
lpcr1.setAllocationTypes(EnumSet.allOf(AllocationType.class).stream().toList());
return lpcr1;
}
private void assertPlatformValidationException(String message, String code, Executable executable) {
PlatformApiDataValidationException validationException = assertThrows(PlatformApiDataValidationException.class, executable);
assertPlatformException(message, code, validationException);
}
private void assertPlatformException(String expectedMessage, String expectedCode,
PlatformApiDataValidationException platformApiDataValidationException) {
Assertions.assertEquals(expectedMessage, platformApiDataValidationException.getErrors().get(0).getDefaultUserMessage());
Assertions.assertEquals(expectedCode, platformApiDataValidationException.getErrors().get(0).getUserMessageGlobalisationCode());
}
}