FINERACT-2042 Configurable CreditAllocations for Loan Product
This commit is contained in:
committed by
Peter Bagrij
parent
02cf6749af
commit
cca520125b
+19
-4
@@ -18,9 +18,24 @@
|
||||
*/
|
||||
package org.apache.fineract.portfolio.loanproduct.domain;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.fineract.infrastructure.core.data.EnumOptionData;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public enum AllocationType {
|
||||
PENALTY, //
|
||||
FEE, //
|
||||
PRINCIPAL, //
|
||||
INTEREST //
|
||||
|
||||
PENALTY("Penalty"), //
|
||||
FEE("Fee"), //
|
||||
PRINCIPAL("Principal"), //
|
||||
INTEREST("Interest"); //
|
||||
|
||||
private final String humanReadableName;
|
||||
|
||||
public static List<EnumOptionData> getValuesAsEnumOptionDataList() {
|
||||
return Arrays.stream(values()).map(v -> new EnumOptionData((long) (v.ordinal() + 1), v.name(), v.getHumanReadableName())).toList();
|
||||
}
|
||||
}
|
||||
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* 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 com.google.common.base.Enums;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.fineract.infrastructure.core.api.JsonCommand;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class CreditAllocationsJsonParser {
|
||||
|
||||
public final CreditAllocationsValidator creditAllocationsValidator;
|
||||
|
||||
public List<LoanProductCreditAllocationRule> assembleLoanProductCreditAllocationRules(final JsonCommand command,
|
||||
String loanTransactionProcessingStrategyCode) {
|
||||
JsonArray creditAllocation = command.arrayOfParameterNamed("creditAllocation");
|
||||
List<LoanProductCreditAllocationRule> productCreditAllocationRules = null;
|
||||
if (creditAllocation != null) {
|
||||
productCreditAllocationRules = creditAllocation.asList().stream().map(json -> {
|
||||
Map<String, JsonElement> map = json.getAsJsonObject().asMap();
|
||||
LoanProductCreditAllocationRule creditAllocationRule = new LoanProductCreditAllocationRule();
|
||||
populateCreditAllocationRules(map, creditAllocationRule);
|
||||
populateTransactionType(map, creditAllocationRule);
|
||||
return creditAllocationRule;
|
||||
}).toList();
|
||||
}
|
||||
creditAllocationsValidator.validate(productCreditAllocationRules, loanTransactionProcessingStrategyCode);
|
||||
return productCreditAllocationRules;
|
||||
}
|
||||
|
||||
private void populateCreditAllocationRules(Map<String, JsonElement> map, LoanProductCreditAllocationRule creditAllocationRule) {
|
||||
JsonArray creditAllocationOrder = asJsonArrayOrNull(map.get("creditAllocationOrder"));
|
||||
if (creditAllocationOrder != null) {
|
||||
creditAllocationRule.setAllocationTypes(getAllocationTypes(creditAllocationOrder));
|
||||
}
|
||||
}
|
||||
|
||||
private void populateTransactionType(Map<String, JsonElement> map, LoanProductCreditAllocationRule creditAllocationRule) {
|
||||
String transactionType = asStringOrNull(map.get("transactionType"));
|
||||
if (transactionType != null) {
|
||||
creditAllocationRule.setTransactionType(Enums.getIfPresent(CreditAllocationTransactionType.class, transactionType).orNull());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<AllocationType> getAllocationTypes(JsonArray allocationOrder) {
|
||||
if (allocationOrder != null) {
|
||||
List<Pair<Integer, AllocationType>> parsedListWithOrder = allocationOrder.asList().stream().map(json -> {
|
||||
Map<String, JsonElement> map = json.getAsJsonObject().asMap();
|
||||
AllocationType allocationType = null;
|
||||
String creditAllocationRule = asStringOrNull(map.get("creditAllocationRule"));
|
||||
if (creditAllocationRule != null) {
|
||||
allocationType = Enums.getIfPresent(AllocationType.class, creditAllocationRule).orNull();
|
||||
}
|
||||
return Pair.of(asIntegerOrNull(map.get("order")), allocationType);
|
||||
}).sorted(Comparator.comparing(Pair::getLeft)).toList();
|
||||
creditAllocationsValidator.validatePairOfOrderAndCreditAllocationType(parsedListWithOrder);
|
||||
return parsedListWithOrder.stream().map(Pair::getRight).toList();
|
||||
} else {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
private Integer asIntegerOrNull(JsonElement element) {
|
||||
if (!element.isJsonNull()) {
|
||||
return element.getAsInt();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String asStringOrNull(JsonElement element) {
|
||||
if (!element.isJsonNull()) {
|
||||
return element.getAsString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private JsonArray asJsonArrayOrNull(JsonElement element) {
|
||||
if (!element.isJsonNull()) {
|
||||
return element.getAsJsonArray();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* 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 java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
|
||||
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CreditAllocationsValidator {
|
||||
|
||||
public static final String ADVANCED_PAYMENT_ALLOCATION_STRATEGY = "advanced-payment-allocation-strategy";
|
||||
|
||||
public void validate(List<LoanProductCreditAllocationRule> rules, String code) {
|
||||
if (isAdvancedPaymentStrategy(code)) {
|
||||
if (hasDuplicateTransactionTypes(rules)) {
|
||||
raiseValidationError("advanced-payment-strategy-with-duplicate-credit-allocation",
|
||||
"The same transaction type must be provided only once");
|
||||
}
|
||||
|
||||
if (rules != null) {
|
||||
for (LoanProductCreditAllocationRule rule : rules) {
|
||||
validateAllocationRule(rule);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if (hasLoanProductCreditAllocationRule(rules)) {
|
||||
raiseValidationError("credit_allocation.must.not.be.provided.when.allocation.strategy.is.not.advanced-payment-strategy",
|
||||
"In case '" + code + "' payment strategy, creditAllocation must not be provided");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void validatePairOfOrderAndCreditAllocationType(List<Pair<Integer, AllocationType>> rules) {
|
||||
if (rules.size() != 4) {
|
||||
raiseValidationError("advanced-payment-strategy.each_credit_allocation_order.must.contain.4.entries",
|
||||
"Each provided credit allocation must contain exactly 4 allocation rules, but " + rules.size() + " were provided");
|
||||
}
|
||||
|
||||
List<AllocationType> deduped = rules.stream().map(Pair::getRight).distinct().toList();
|
||||
if (deduped.size() != 4) {
|
||||
raiseValidationError("advanced-payment-strategy.must.not.have.duplicate.credit.allocation.rule",
|
||||
"The list of provided credit allocation rules must not contain any duplicates");
|
||||
}
|
||||
|
||||
if (!Arrays.equals(IntStream.rangeClosed(1, 4).boxed().toArray(), rules.stream().map(Pair::getLeft).toArray())) {
|
||||
raiseValidationError("advanced-payment-strategy.invalid.order", "The provided orders must be between 1 and 4");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasDuplicateTransactionTypes(List<LoanProductCreditAllocationRule> rules) {
|
||||
return rules != null
|
||||
&& rules.stream().map(LoanProductCreditAllocationRule::getTransactionType).distinct().toList().size() != rules.size();
|
||||
}
|
||||
|
||||
private void validateAllocationRule(LoanProductCreditAllocationRule rule) {
|
||||
if (rule.getTransactionType() == null) {
|
||||
raiseValidationError("advanced-payment-strategy.with.not.valid.transaction.type",
|
||||
"Credit allocation was provided with a not valid transaction type");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAdvancedPaymentStrategy(String code) {
|
||||
return ADVANCED_PAYMENT_ALLOCATION_STRATEGY.equals(code);
|
||||
}
|
||||
|
||||
private boolean hasLoanProductCreditAllocationRule(List<LoanProductCreditAllocationRule> rules) {
|
||||
return rules != null && rules.size() > 0;
|
||||
}
|
||||
|
||||
private void raiseValidationError(String globalisationMessageCode, String msg) {
|
||||
throw new PlatformApiDataValidationException(List.of(ApiParameterError.generalError(globalisationMessageCode, msg)));
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -675,6 +675,13 @@ public class LoanProduct extends AbstractPersistableCustom {
|
||||
}
|
||||
}
|
||||
|
||||
this.creditAllocationRules = creditAllocationRules;
|
||||
if (this.creditAllocationRules != null) {
|
||||
for (LoanProductCreditAllocationRule loanProductCreditAllocationRule : this.creditAllocationRules) {
|
||||
loanProductCreditAllocationRule.setLoanProduct(this);
|
||||
}
|
||||
}
|
||||
|
||||
this.name = name.trim();
|
||||
this.shortName = shortName.trim();
|
||||
if (StringUtils.isNotBlank(description)) {
|
||||
@@ -773,6 +780,13 @@ public class LoanProduct extends AbstractPersistableCustom {
|
||||
"In case '" + transactionProcessingStrategyCode + "' payment strategy, payment_allocation must not be provided");
|
||||
}
|
||||
|
||||
if (this.creditAllocationRules != null && creditAllocationRules.size() > 0
|
||||
&& !transactionProcessingStrategyCode.equals("advanced-payment-allocation-strategy")) {
|
||||
throw new LoanProductGeneralRuleException(
|
||||
"creditAllocation.must.not.be.provided.when.allocation.strategy.is.not.advanced-payment-strategy",
|
||||
"In case '" + transactionProcessingStrategyCode + "' payment strategy, creditAllocation must not be provided");
|
||||
}
|
||||
|
||||
if (this.disallowExpectedDisbursements) {
|
||||
if (!this.isMultiDisburseLoan()) {
|
||||
throw new LoanProductGeneralRuleException("allowMultipleDisbursals.not.set.disallowExpectedDisbursements.cant.be.set",
|
||||
@@ -906,6 +920,10 @@ public class LoanProduct extends AbstractPersistableCustom {
|
||||
return this.paymentAllocationRules;
|
||||
}
|
||||
|
||||
public List<LoanProductCreditAllocationRule> getCreditAllocationRules() {
|
||||
return this.creditAllocationRules;
|
||||
}
|
||||
|
||||
public void update(final LoanProductConfigurableAttributes loanConfigurableAttributes) {
|
||||
this.loanConfigurableAttributes = loanConfigurableAttributes;
|
||||
}
|
||||
@@ -999,6 +1017,14 @@ public class LoanProduct extends AbstractPersistableCustom {
|
||||
}
|
||||
}
|
||||
|
||||
final String creditAllocationParamName = "creditAllocation";
|
||||
if (command.hasParameter(creditAllocationParamName)) {
|
||||
final JsonArray jsonArray = command.arrayOfParameterNamed(creditAllocationParamName);
|
||||
if (jsonArray != null) {
|
||||
actualChanges.put(creditAllocationParamName, command.jsonFragment(creditAllocationParamName));
|
||||
}
|
||||
}
|
||||
|
||||
final String chargesParamName = "charges";
|
||||
if (command.hasParameter(chargesParamName)) {
|
||||
final JsonArray jsonArray = command.arrayOfParameterNamed(chargesParamName);
|
||||
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user