From 92b1a2f4ee572a7da13406627bd116d83a6cf4c3 Mon Sep 17 00:00:00 2001 From: Kristof Jozsa Date: Sun, 4 Aug 2024 12:32:48 +0200 Subject: [PATCH] FINERACT-2081: fix loan creation validations --- .../core/data/DataValidatorBuilder.java | 551 ++++++++---------- .../workingdays/service/WorkingDaysUtil.java | 2 +- .../portfolio/calendar/data/CalendarData.java | 2 +- .../portfolio/calendar/domain/Calendar.java | 4 +- .../calendar/service/CalendarUtils.java | 10 +- .../domain/LoanApplicationTerms.java | 2 +- .../service/LoanScheduleAssembler.java | 2 +- .../LoanApplicationValidator.java | 59 +- .../LoanTransactionValidator.java | 2 +- .../LoanProductDataValidator.java | 12 +- .../LoanValidationIntegrationTest.java | 135 +++++ 11 files changed, 410 insertions(+), 371 deletions(-) create mode 100644 integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanValidationIntegrationTest.java diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/data/DataValidatorBuilder.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/data/DataValidatorBuilder.java index da2a1abd585..6a718e16e62 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/data/DataValidatorBuilder.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/data/DataValidatorBuilder.java @@ -38,7 +38,7 @@ public class DataValidatorBuilder { - public static final String VALID_INPUT_SEPERATOR = "_"; + public static final String VALID_INPUT_SEPARATOR = "_"; private final List dataValidationErrors; private String resource; private String parameter; @@ -113,31 +113,29 @@ public DataValidatorBuilder andNotBlank(final String linkedParameterName, final } if (StringUtils.isBlank(linkedValue)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(linkedParameterName).append(".cannot.be.empty.when.").append(this.parameter).append(".is.populated"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(linkedParameterName) - .append("` cannot be empty when ").append(this.parameter).append(" is populated."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), linkedParameterName, linkedValue, this.value); + String validationErrorCode = "validation.msg." + this.resource + "." + linkedParameterName + ".cannot.be.empty.when." + + this.parameter + ".is.populated"; + String defaultEnglishMessage = "The parameter `" + linkedParameterName + "` cannot be empty when " + this.parameter + + " is populated."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, + linkedParameterName, linkedValue, this.value); this.dataValidationErrors.add(error); } return this; } public void failWithCode(final String errorCode, final Object... defaultUserMessageArgs) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".").append(errorCode); - final StringBuilder defaultEnglishMessage = new StringBuilder("Failed data validation due to: ").append(errorCode).append("."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), - this.parameter, this.value, defaultUserMessageArgs); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + "." + errorCode; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, + "Failed data validation due to: " + errorCode + ".", this.parameter, this.value, defaultUserMessageArgs); this.dataValidationErrors.add(error); } public void failWithCodeNoParameterAddedToErrorCode(final String errorCode, final Object... defaultUserMessageArgs) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".").append(errorCode); - final StringBuilder defaultEnglishMessage = new StringBuilder("Failed data validation due to: ").append(errorCode).append("."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), - this.parameter, this.value, defaultUserMessageArgs); + String validationErrorCode = "validation.msg." + this.resource + "." + errorCode; + String defaultEnglishMessage = "Failed data validation due to: " + errorCode + "."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value, defaultUserMessageArgs); this.dataValidationErrors.add(error); } @@ -147,12 +145,10 @@ public DataValidatorBuilder equalToParameter(final String linkedParameterName, f } if (this.value != null && !this.value.equals(linkedValue)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(linkedParameterName).append(".not.equal.to.").append(this.parameter); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(linkedParameterName) - .append("` is not equal to ").append(this.parameter).append("."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), linkedParameterName, linkedValue, this.value); + String validationErrorCode = "validation.msg." + this.resource + "." + linkedParameterName + ".not.equal.to." + this.parameter; + String defaultEnglishMessage = "The parameter `" + linkedParameterName + "` is not equal to " + this.parameter + "."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, + linkedParameterName, linkedValue, this.value); this.dataValidationErrors.add(error); } return this; @@ -164,12 +160,10 @@ public DataValidatorBuilder notSameAsParameter(final String linkedParameterName, } if (this.value != null && this.value.equals(linkedValue)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(linkedParameterName).append(".same.as.").append(this.parameter); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(linkedParameterName) - .append("` is same as ").append(this.parameter).append("."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), linkedParameterName, linkedValue, this.value); + String validationErrorCode = "validation.msg." + this.resource + "." + linkedParameterName + ".same.as." + this.parameter; + String defaultEnglishMessage = "The parameter `" + linkedParameterName + "` is same as " + this.parameter + "."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, + linkedParameterName, linkedValue, this.value); this.dataValidationErrors.add(error); } return this; @@ -179,12 +173,9 @@ public DataValidatorBuilder trueOrFalseRequired(final Object trueOfFalseField) { if (trueOfFalseField != null && !trueOfFalseField.toString().equalsIgnoreCase("true") && !trueOfFalseField.toString().equalsIgnoreCase("false")) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".must.be.true.or.false"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be set as true or false."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".must.be.true.or.false"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be set as true or false."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter); this.dataValidationErrors.add(error); } @@ -199,15 +190,13 @@ public DataValidatorBuilder notNull() { .append(this.parameter); if (this.arrayIndex != null && StringUtils.isNotBlank(this.arrayPart)) { validationErrorCode.append(".").append(this.arrayPart); - realParameterName = new StringBuilder(this.parameter).append('[').append(this.arrayIndex).append("][") - .append(this.arrayPart).append(']').toString(); + realParameterName = this.parameter + '[' + this.arrayIndex + "][" + this.arrayPart + ']'; } validationErrorCode.append(".cannot.be.blank"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(realParameterName) - .append("` is mandatory."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), realParameterName, this.arrayIndex); + String defaultEnglishMessage = "The parameter `" + realParameterName + "` is mandatory."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage, + realParameterName, this.arrayIndex); this.dataValidationErrors.add(error); } return this; @@ -224,15 +213,13 @@ public DataValidatorBuilder notBlank() { .append(this.parameter); if (this.arrayIndex != null && StringUtils.isNotBlank(this.arrayPart)) { validationErrorCode.append(".").append(this.arrayPart); - realParameterName = new StringBuilder(this.parameter).append('[').append(this.arrayIndex).append("][") - .append(this.arrayPart).append(']').toString(); + realParameterName = this.parameter + '[' + this.arrayIndex + "][" + this.arrayPart + ']'; } validationErrorCode.append(".cannot.be.blank"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(realParameterName) - .append("` is mandatory."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), realParameterName, this.arrayIndex); + String defaultEnglishMessage = "The parameter `" + realParameterName + "` is mandatory."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage, + realParameterName, this.arrayIndex); this.dataValidationErrors.add(error); } return this; @@ -244,12 +231,10 @@ public DataValidatorBuilder notExceedingLengthOf(final Integer maxLength) { } if (this.value != null && this.value.toString().trim().length() > maxLength) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".exceeds.max.length"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` exceeds max length of ").append(maxLength).append("."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, maxLength, this.value.toString()); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".exceeds.max.length"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` exceeds max length of " + maxLength + "."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + maxLength, this.value.toString()); this.dataValidationErrors.add(error); } return this; @@ -261,12 +246,9 @@ public DataValidatorBuilder notExceedingListLengthOf(final Integer maxLength) { } if (this.value instanceof List && ((List) this.value).size() > maxLength) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".exceeds.max.length.allowed"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` exceeds allowed max length of ").append(maxLength).append("."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".exceeds.max.length.allowed"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` exceeds allowed max length of " + maxLength + "."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter); this.dataValidationErrors.add(error); } return this; @@ -280,12 +262,10 @@ public DataValidatorBuilder inMinMaxRange(final Integer min, final Integer max) if (this.value != null) { final Integer number = Integer.valueOf(this.value.toString()); if (number < min || number > max) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.not.within.expected.range"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be between ").append(min).append(" and ").append(max).append("."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, number, min, max); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.not.within.expected.range"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be between " + min + " and " + max + "."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + number, min, max); this.dataValidationErrors.add(error); } } @@ -302,13 +282,11 @@ public DataValidatorBuilder isOneOfTheseValues(final Object... values) { if (this.value == null || !rawValuesList.contains(this.value)) { final List valuesList = Arrays.stream(values).map(Object::toString).toList(); final String valuesListStr = String.join(", ", valuesList); - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.not.one.of.expected.enumerations"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be one of [ ").append(valuesListStr).append(" ] ").append("."); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.not.one.of.expected.enumerations"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be one of [ " + valuesListStr + " ] " + "."; - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, this.value, values); + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value, values); this.dataValidationErrors.add(error); } @@ -331,13 +309,11 @@ public DataValidatorBuilder isOneOfTheseStringValues(final Object... values) { if (this.value == null || !rawValuesList.contains(this.value.toString().toLowerCase())) { final List valuesList = Arrays.stream(values).map(Object::toString).toList(); final String valuesListStr = String.join(", ", valuesList); - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.not.one.of.expected.enumerations"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be one of [ ").append(valuesListStr).append(" ] ").append("."); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.not.one.of.expected.enumerations"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be one of [ " + valuesListStr + " ] " + "."; - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, this.value, values); + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value, values); this.dataValidationErrors.add(error); } @@ -354,13 +330,11 @@ public DataValidatorBuilder isOneOfTheseStringValues(final List valuesLi if (this.value == null || !valuesListLowercase.contains(this.value.toString().toLowerCase())) { final String valuesListStr = String.join(", ", valuesList); - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.not.one.of.expected.enumerations"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be one of [ ").append(valuesListStr).append(" ] ").append("."); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.not.one.of.expected.enumerations"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be one of [ " + valuesListStr + " ] " + "."; - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, this.value, valuesList); + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value, valuesList); this.dataValidationErrors.add(error); } @@ -379,13 +353,11 @@ public DataValidatorBuilder isNotOneOfTheseValues(final Object... values) { if (rawValuesList.contains(this.value)) { final List valuesList = Arrays.stream(values).map(Object::toString).toList(); final String valuesListStr = String.join(", ", valuesList); - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.one.of.unwanted.enumerations"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must not be any of [ ").append(valuesListStr).append(" ] ").append("."); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.one.of.unwanted.enumerations"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must not be any of [ " + valuesListStr + " ] " + "."; - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, this.value, values); + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value, values); this.dataValidationErrors.add(error); } @@ -401,12 +373,10 @@ public DataValidatorBuilder positiveAmount() { if (this.value != null) { final BigDecimal number = BigDecimal.valueOf(Double.parseDouble(this.value.toString())); if (number.compareTo(BigDecimal.ZERO) <= 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.greater.than.zero"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be greater than 0."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, number, 0); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".not.greater.than.zero"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be greater than 0."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + number, 0); this.dataValidationErrors.add(error); } } @@ -424,12 +394,10 @@ public DataValidatorBuilder zeroOrPositiveAmount() { if (this.value != null) { final BigDecimal number = BigDecimal.valueOf(Double.parseDouble(this.value.toString())); if (number.compareTo(BigDecimal.ZERO) < 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.zero.or.greater"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be greater than or equal to 0."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, number, 0); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".not.zero.or.greater"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be greater than or equal to 0."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + number, 0); this.dataValidationErrors.add(error); } } @@ -445,14 +413,12 @@ public DataValidatorBuilder integerZeroOrGreater() { } if (this.value != null) { - final Integer number = Integer.valueOf(this.value.toString()); + final int number = Integer.parseInt(this.value.toString()); if (number < 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.zero.or.greater"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be zero or greater."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, number, 0); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".not.zero.or.greater"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be zero or greater."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + number, 0); this.dataValidationErrors.add(error); } } @@ -468,14 +434,12 @@ public DataValidatorBuilder integerGreaterThanZero() { } if (this.value != null) { - final Integer number = Integer.valueOf(this.value.toString()); + final int number = Integer.parseInt(this.value.toString()); if (number < 1) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.greater.than.zero"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be greater than 0."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, number, 0); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".not.greater.than.zero"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be greater than 0."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + number, 0); this.dataValidationErrors.add(error); } } @@ -488,14 +452,13 @@ public DataValidatorBuilder integerGreaterThanNumber(Integer number) { } if (this.value != null) { - final Integer intValue = Integer.valueOf(this.value.toString()); + final int intValue = Integer.parseInt(this.value.toString()); if (intValue < number + 1) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.greater.than.specified.number"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be greater than ").append(number); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, intValue, number); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + + ".not.greater.than.specified.number"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be greater than " + number; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + intValue, number); this.dataValidationErrors.add(error); } } @@ -510,12 +473,11 @@ public DataValidatorBuilder integerEqualToOrGreaterThanNumber(Integer number) { if (this.value != null) { final Integer intValue = Integer.valueOf(this.value.toString()); if (intValue < number) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.equal.to.or.greater.than.specified.number"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be equal to or greater than").append(number); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, intValue, number); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + + ".not.equal.to.or.greater.than.specified.number"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be equal to or greater than" + number; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + intValue, number); this.dataValidationErrors.add(error); } } @@ -530,12 +492,10 @@ public DataValidatorBuilder integerSameAsNumber(Integer number) { if (this.value != null) { final Integer intValue = Integer.valueOf(this.value.toString()); if (!intValue.equals(number)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.equal.to.specified.number"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be same as").append(number); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, intValue, number); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".not.equal.to.specified.number"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be same as" + number; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + intValue, number); this.dataValidationErrors.add(error); } } @@ -550,12 +510,11 @@ public DataValidatorBuilder integerInMultiplesOfNumber(Integer number) { if (this.value != null) { final Integer intValue = Integer.valueOf(this.value.toString()); if (intValue < number || intValue % number != 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.in.multiples.of.specified.number"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be multiples of ").append(number); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, intValue, number); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + + ".not.in.multiples.of.specified.number"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be multiples of " + number; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + intValue, number); this.dataValidationErrors.add(error); } } @@ -571,14 +530,12 @@ public DataValidatorBuilder longGreaterThanZero() { } if (this.value != null) { - final Long number = Long.valueOf(this.value.toString()); + final long number = Long.parseLong(this.value.toString()); if (number < 1) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.greater.than.zero"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be greater than 0."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, number, 0); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".not.greater.than.zero"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be greater than 0."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + number, 0); this.dataValidationErrors.add(error); } } @@ -594,14 +551,12 @@ public DataValidatorBuilder longZeroOrGreater() { } if (this.value != null) { - final Long number = Long.valueOf(this.value.toString()); + final long number = Long.parseLong(this.value.toString()); if (number < 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.equal.or.greater.than.zero"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be equal or greater than 0."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, number, 0); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".not.equal.or.greater.than.zero"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be equal or greater than 0."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + number, 0); this.dataValidationErrors.add(error); } } @@ -614,14 +569,13 @@ public DataValidatorBuilder longGreaterThanNumber(Long number) { } if (this.value != null) { - final Long longValue = Long.valueOf(this.value.toString()); + final long longValue = Long.parseLong(this.value.toString()); if (longValue < number + 1) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.greater.than.specified.number"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be greater than ").append(number); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, longValue, number); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + + ".not.greater.than.specified.number"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be greater than " + number; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + longValue, number); this.dataValidationErrors.add(error); } } @@ -634,14 +588,13 @@ public DataValidatorBuilder longGreaterThanNumber(String paramName, Long number, } if (this.value != null) { - final Long longValue = Long.valueOf(this.value.toString()); + final long longValue = Long.parseLong(this.value.toString()); if (longValue < number + 1) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".not.greater.than.specified.").append(paramName).append(".at Index.").append(index); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be greater than ").append(number); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, longValue, number); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".not.greater.than.specified." + + paramName + ".at Index." + index; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be greater than " + number; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + longValue, number); this.dataValidationErrors.add(error); } } @@ -655,12 +608,9 @@ public DataValidatorBuilder arrayNotEmpty() { final Object[] array = (Object[]) this.value; if (ObjectUtils.isEmpty(array)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".cannot.be.empty"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` cannot be empty. You must select at least one."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".cannot.be.empty"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` cannot be empty. You must select at least one."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter); this.dataValidationErrors.add(error); } return this; @@ -673,24 +623,18 @@ public DataValidatorBuilder jsonArrayNotEmpty() { final JsonArray array = (JsonArray) this.value; if (this.value != null && !array.iterator().hasNext()) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".cannot.be.empty"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` cannot be empty. You must select at least one."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".cannot.be.empty"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` cannot be empty. You must select at least one."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter); this.dataValidationErrors.add(error); } return this; } public void expectedArrayButIsNot() { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.not.an.array"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` is not an array."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), - this.parameter); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.not.an.array"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` is not an array."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter); this.dataValidationErrors.add(error); } @@ -704,23 +648,18 @@ public DataValidatorBuilder anyOfNotNull(final Object... object) { } if (!hasData) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource) - .append(".no.parameters.for.update"); - final StringBuilder defaultEnglishMessage = new StringBuilder("No parameters passed for update."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), "id"); + String validationErrorCode = "validation.msg." + this.resource + ".no.parameters.for.update"; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, "No parameters passed for update.", "id"); this.dataValidationErrors.add(error); } return this; } public DataValidatorBuilder inValidValue(final String parameterValueCode, final Object invalidValue) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".invalid.").append(parameterValueCode); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` has an invalid value."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), - this.parameter, invalidValue); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".invalid." + parameterValueCode; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` has an invalid value."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + invalidValue); this.dataValidationErrors.add(error); return this; } @@ -739,12 +678,12 @@ public DataValidatorBuilder mustBeBlankWhenParameterProvided(final String parame return this; } - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".cannot.also.be.provided.when.").append(parameterName).append(".is.populated"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` cannot also be provided when `").append(parameterName).append("` is populated."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), - this.parameter, this.value, parameterName, parameterValue); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".cannot.also.be.provided.when." + + parameterName + ".is.populated"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` cannot also be provided when `" + parameterName + + "` is populated."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value, parameterName, parameterValue); this.dataValidationErrors.add(error); return this; } @@ -763,13 +702,12 @@ public DataValidatorBuilder mustBeBlankWhenParameterProvidedIs(final String para return this; } - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".cannot.also.be.provided.when.").append(parameterName).append(".is.") - .append(parameterValue); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` cannot also be provided when `").append(parameterName).append("` is ").append(parameterValue); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), - this.parameter, this.value, parameterName, parameterValue); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".cannot.also.be.provided.when." + + parameterName + ".is." + parameterValue; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` cannot also be provided when `" + parameterName + "` is " + + parameterValue; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value, parameterName, parameterValue); this.dataValidationErrors.add(error); return this; } @@ -779,12 +717,12 @@ public DataValidatorBuilder cantBeBlankWhenParameterProvidedIs(final String para return this; } - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".must.be.provided.when.").append(parameterName).append(".is.").append(parameterValue); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be provided when `").append(parameterName).append("` is ").append(parameterValue); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), - this.parameter, this.value, parameterName, parameterValue); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".must.be.provided.when." + parameterName + + ".is." + parameterValue; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be provided when `" + parameterName + "` is " + + parameterValue; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value, parameterName, parameterValue); this.dataValidationErrors.add(error); return this; } @@ -792,13 +730,11 @@ public DataValidatorBuilder cantBeBlankWhenParameterProvidedIs(final String para public DataValidatorBuilder compareMinimumAndMaximumAmounts(final BigDecimal minimumBalance, final BigDecimal maximumBalance) { if (minimumBalance != null && maximumBalance != null) { if (maximumBalance.compareTo(minimumBalance) < 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.not.within.expected.range"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` minimum amount ").append(minimumBalance).append(" should less than the maximum amount ") - .append(maximumBalance).append("."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, minimumBalance, maximumBalance); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.not.within.expected.range"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` minimum amount " + minimumBalance + + " should less than the maximum amount " + maximumBalance + "."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + minimumBalance, maximumBalance); this.dataValidationErrors.add(error); return this; } @@ -810,13 +746,12 @@ public DataValidatorBuilder inMinAndMaxAmountRange(final BigDecimal minimumAmoun if (minimumAmount != null && maximumAmount != null && this.value != null) { final BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(this.value.toString())); if (amount.compareTo(minimumAmount) < 0 || amount.compareTo(maximumAmount) > 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".amount.is.not.within.min.max.range"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter).append("` amount ") - .append(amount).append(" must be between ").append(minimumAmount).append(" and ").append(maximumAmount) - .append(" ."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, amount, minimumAmount, maximumAmount); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + + ".amount.is.not.within.min.max.range"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` amount " + amount + " must be between " + + minimumAmount + " and " + maximumAmount + " ."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + amount, minimumAmount, maximumAmount); this.dataValidationErrors.add(error); return this; } @@ -828,12 +763,11 @@ public DataValidatorBuilder notLessThanMin(final BigDecimal min) { if (min != null && this.value != null) { final BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(this.value.toString())); if (amount.compareTo(min) < 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.less.than.min"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter).append("` value ") - .append(amount).append(" must not be less than the minimum value ").append(min); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, amount, min); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.less.than.min"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` value " + amount + + " must not be less than the minimum value " + min; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + amount, min); this.dataValidationErrors.add(error); return this; } @@ -845,12 +779,11 @@ public DataValidatorBuilder notGreaterThanMax(final BigDecimal max) { if (max != null && this.value != null) { final BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(this.value.toString())); if (amount.compareTo(max) > 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.greater.than.max"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter).append("` value ") - .append(amount).append(" must not be more than maximum value ").append(max); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, amount, max); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.greater.than.max"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` value " + amount + + " must not be more than maximum value " + max; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + amount, max); this.dataValidationErrors.add(error); return this; } @@ -860,12 +793,10 @@ public DataValidatorBuilder notGreaterThanMax(final BigDecimal max) { public DataValidatorBuilder compareMinAndMaxOfTwoBigDecmimalNos(final BigDecimal min, final BigDecimal max) { if (min != null && max != null && max.compareTo(min) < 0) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.not.within.expected.range"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The ").append(" min number ").append(min) - .append(" should less than max number ").append(max).append("."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, min, max); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.not.within.expected.range"; + String defaultEnglishMessage = "The " + " min number " + min + " should less than max number " + max + "."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + min, max); this.dataValidationErrors.add(error); return this; } @@ -900,12 +831,10 @@ public DataValidatorBuilder notLessThanMin(final Integer min) { if (this.value != null && min != null) { final Integer number = Integer.valueOf(this.value.toString()); if (number < min) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.less.than.min"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be greater than the minimum value ").append(min); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, number, min); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.less.than.min"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be greater than the minimum value " + min; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + number, min); this.dataValidationErrors.add(error); } } @@ -920,12 +849,10 @@ public DataValidatorBuilder notGreaterThanMax(final Integer max) { if (this.value != null && max != null) { final Integer number = Integer.valueOf(this.value.toString()); if (number > max) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.greater.than.max"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be less than the maximum value ").append(max); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, number, max); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.greater.than.max"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be less than the maximum value " + max; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + number, max); this.dataValidationErrors.add(error); } } @@ -938,13 +865,12 @@ public DataValidatorBuilder matchesRegularExpression(final String expression) { } if (this.value != null && !this.value.toString().matches(expression)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".does.not.match.regexp"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must match the provided regular expression [ ").append(expression).append(" ] ").append("."); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".does.not.match.regexp"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must match the provided regular expression [ " + + expression + " ] " + "."; - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, this.value, expression); + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value, expression); this.dataValidationErrors.add(error); } @@ -958,12 +884,10 @@ public DataValidatorBuilder matchesRegularExpression(final String expression, fi } if (this.value != null && !this.value.toString().matches(expression)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".does.not.match.regexp"); - final StringBuilder defaultEnglishMessage = new StringBuilder(Message); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".does.not.match.regexp"; - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, this.value, expression); + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, Message, this.parameter, this.value, + expression); this.dataValidationErrors.add(error); } @@ -975,7 +899,7 @@ private DataValidatorBuilder validateStringFor(final String validInputs) { if (this.value == null && this.ignoreNullValue) { return this; } - final Iterable inputs = Splitter.onPattern(VALID_INPUT_SEPERATOR).split(validInputs); + final Iterable inputs = Splitter.onPattern(VALID_INPUT_SEPARATOR).split(validInputs); boolean validationErr = true; for (final String input : inputs) { if (input.equalsIgnoreCase(this.value.toString().trim())) { @@ -984,19 +908,17 @@ private DataValidatorBuilder validateStringFor(final String validInputs) { } } if (validationErr) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".value.should.true.or.false"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` value should true or false "); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, this.value); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".value.should.true.or.false"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` value should true or false "; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value); this.dataValidationErrors.add(error); } return this; } public DataValidatorBuilder validateForBooleanValue() { - return validateStringFor("TRUE" + VALID_INPUT_SEPERATOR + "FALSE"); + return validateStringFor("TRUE" + VALID_INPUT_SEPARATOR + "FALSE"); } public DataValidatorBuilder validatePhoneNumber() { @@ -1015,12 +937,11 @@ public DataValidatorBuilder validatePhoneNumber() { validationErr = false; } if (validationErr) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".format.is.invalid"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` is in invalid format, should contain '-','+','()' and numbers only."); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, this.value); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".format.is.invalid"; + String defaultEnglishMessage = "The parameter `" + this.parameter + + "` is in invalid format, should contain '-','+','()' and numbers only."; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value); this.dataValidationErrors.add(error); } return this; @@ -1028,12 +949,10 @@ public DataValidatorBuilder validatePhoneNumber() { public DataValidatorBuilder validateCronExpression() { if (this.value != null && !CronExpression.isValidExpression(this.value.toString().trim())) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".invalid"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` value is not a valid cron expression"); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, this.value); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".invalid"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` value is not a valid cron expression"; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + this.value); this.dataValidationErrors.add(error); } return this; @@ -1047,12 +966,10 @@ public DataValidatorBuilder validateDateAfter(final LocalDate date) { if (this.value != null && date != null) { final LocalDate dateVal = (LocalDate) this.value; if (DateUtils.isAfter(date, dateVal)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.less.than.date"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be greater than the provided date").append(date); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, dateVal, date); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.less.than.date"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be greater than the provided date" + date; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + dateVal, date); this.dataValidationErrors.add(error); } } @@ -1067,12 +984,10 @@ public DataValidatorBuilder validateDateBefore(final LocalDate date) { if (this.value != null && date != null) { final LocalDate dateVal = (LocalDate) this.value; if (DateUtils.isBefore(date, dateVal)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.greater.than.date"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be less than the provided date").append(date); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, dateVal, date); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.greater.than.date"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be less than the provided date" + date; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + dateVal, date); this.dataValidationErrors.add(error); } } @@ -1087,12 +1002,11 @@ public DataValidatorBuilder validateDateBeforeOrEqual(final LocalDate date) { if (this.value != null && date != null) { final LocalDate dateVal = (LocalDate) this.value; if (DateUtils.isAfter(dateVal, date)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.greater.than.date"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be less than or equal to the provided date: ").append(date); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, dateVal, date); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.greater.than.date"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be less than or equal to the provided date: " + + date; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + dateVal, date); this.dataValidationErrors.add(error); } } @@ -1107,12 +1021,10 @@ public DataValidatorBuilder validateDateForEqual(final LocalDate date) { if (this.value != null && date != null) { final LocalDate dateVal = (LocalDate) this.value; if (!DateUtils.isEqual(dateVal, date)) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".is.not.equal.to.date"); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter) - .append("` must be equal to the provided date").append(date); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, dateVal, date); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".is.not.equal.to.date"; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` must be equal to the provided date" + date; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + dateVal, date); this.dataValidationErrors.add(error); } } @@ -1122,12 +1034,11 @@ public DataValidatorBuilder validateDateForEqual(final LocalDate date) { public DataValidatorBuilder scaleNotGreaterThan(Integer scale) { final BigDecimal value = BigDecimal.valueOf(Double.parseDouble(this.value.toString())); if (value.scale() > scale.intValue()) { - final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") - .append(this.parameter).append(".scale.is.greater.than.").append(scale); - final StringBuilder defaultEnglishMessage = new StringBuilder("The parameter `").append(this.parameter).append("` value ") - .append(value).append(" decimal place must not be more than ").append(scale).append(" places"); - final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), - defaultEnglishMessage.toString(), this.parameter, value, scale); + String validationErrorCode = "validation.msg." + this.resource + "." + this.parameter + ".scale.is.greater.than." + scale; + String defaultEnglishMessage = "The parameter `" + this.parameter + "` value " + value + " decimal place must not be more than " + + scale + " places"; + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, this.parameter, + value, scale); this.dataValidationErrors.add(error); return this; } diff --git a/fineract-core/src/main/java/org/apache/fineract/organisation/workingdays/service/WorkingDaysUtil.java b/fineract-core/src/main/java/org/apache/fineract/organisation/workingdays/service/WorkingDaysUtil.java index fec8fc3204f..989e76d9eca 100644 --- a/fineract-core/src/main/java/org/apache/fineract/organisation/workingdays/service/WorkingDaysUtil.java +++ b/fineract-core/src/main/java/org/apache/fineract/organisation/workingdays/service/WorkingDaysUtil.java @@ -57,7 +57,7 @@ public static LocalDate getOffSetDateIfNonWorkingDay(final LocalDate date, final } public static boolean isWorkingDay(final WorkingDays workingDays, final LocalDate date) { - return CalendarUtils.isValidRedurringDate(workingDays.getRecurrence(), date, date); + return CalendarUtils.isValidRecurringDate(workingDays.getRecurrence(), date, date); } public static boolean isNonWorkingDay(final WorkingDays workingDays, final LocalDate date) { diff --git a/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/data/CalendarData.java b/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/data/CalendarData.java index 8426c3b971d..b8c1fd4638f 100644 --- a/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/data/CalendarData.java +++ b/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/data/CalendarData.java @@ -391,7 +391,7 @@ public boolean isBetweenStartAndEndDate(final LocalDate compareDate) { public boolean isValidRecurringDate(final LocalDate compareDate, final Boolean isSkipMeetingOnFirstDay, final Integer numberOfDays) { if (isBetweenStartAndEndDate(compareDate)) { - return CalendarUtils.isValidRedurringDate(this.getRecurrence(), this.getStartDate(), compareDate, isSkipMeetingOnFirstDay, + return CalendarUtils.isValidRecurringDate(this.getRecurrence(), this.getStartDate(), compareDate, isSkipMeetingOnFirstDay, numberOfDays); } return false; diff --git a/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/domain/Calendar.java b/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/domain/Calendar.java index 54ed8ae669f..de4ac71d292 100644 --- a/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/domain/Calendar.java +++ b/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/domain/Calendar.java @@ -558,14 +558,14 @@ private static String constructRecurrence(final CalendarFrequencyType frequencyT public boolean isValidRecurringDate(final LocalDate compareDate, Boolean isSkipRepaymentOnFirstMonth, Integer numberOfDays) { if (isBetweenStartAndEndDate(compareDate)) { - return CalendarUtils.isValidRedurringDate(getRecurrence(), getStartDateLocalDate(), compareDate, isSkipRepaymentOnFirstMonth, + return CalendarUtils.isValidRecurringDate(getRecurrence(), getStartDateLocalDate(), compareDate, isSkipRepaymentOnFirstMonth, numberOfDays); } // validate with history details. for (CalendarHistory history : history()) { if (history.isBetweenStartAndEndDate(compareDate)) { - return CalendarUtils.isValidRedurringDate(history.getRecurrence(), history.getStartDate(), compareDate, + return CalendarUtils.isValidRecurringDate(history.getRecurrence(), history.getStartDate(), compareDate, isSkipRepaymentOnFirstMonth, numberOfDays); } } diff --git a/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/service/CalendarUtils.java b/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/service/CalendarUtils.java index 1b4f568b534..f1eac5c7368 100644 --- a/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/service/CalendarUtils.java +++ b/fineract-core/src/main/java/org/apache/fineract/portfolio/calendar/service/CalendarUtils.java @@ -355,7 +355,7 @@ public static String getRRuleReadable(final LocalDate startDate, final String re return humanReadable; } - public static boolean isValidRedurringDate(final String recurringRule, final LocalDate seedDate, final LocalDate date) { + public static boolean isValidRecurringDate(final String recurringRule, final LocalDate seedDate, final LocalDate date) { final Recur recur = CalendarUtils.getICalRecur(recurringRule); if (recur == null) { return false; @@ -365,7 +365,7 @@ public static boolean isValidRedurringDate(final String recurringRule, final Loc return isValidRecurringDate(recur, seedDate, date, isSkipRepaymentonFirstDayOfMonth, numberOfDays); } - public static boolean isValidRedurringDate(final String recurringRule, final LocalDate seedDate, final LocalDate date, + public static boolean isValidRecurringDate(final String recurringRule, final LocalDate seedDate, final LocalDate date, boolean isSkipRepaymentonFirstDayOfMonth, final Integer numberOfDays) { final Recur recur = CalendarUtils.getICalRecur(recurringRule); @@ -529,7 +529,7 @@ public static LocalDate getFirstRepaymentMeetingDate(final Calendar calendar, fi } LocalDate startDate = disbursementDate; final LocalDate seedDate = calendar.getStartDateLocalDate(); - if (isValidRedurringDate(calendar.getRecurrence(), seedDate, startDate, isSkipRepaymentOnFirstDayOfMonth, numberOfDays) + if (isValidRecurringDate(calendar.getRecurrence(), seedDate, startDate, isSkipRepaymentOnFirstDayOfMonth, numberOfDays) && !frequency.equals(Recur.Frequency.DAILY.name())) { startDate = startDate.plusDays(1); } @@ -728,10 +728,6 @@ public static LocalDate getNextScheduleDate(final Calendar calendar, final Local return null; } final LocalDate seedDate = calendar.getStartDateLocalDate(); - /** - * if (isValidRedurringDate(calendar.getRecurrence(), seedDate, date)) { date = date.plusDays(1); } - **/ - return getNextRecurringDate(recur, seedDate, startDate); } diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/domain/LoanApplicationTerms.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/domain/LoanApplicationTerms.java index ed0813cc50a..a9a08f33524 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/domain/LoanApplicationTerms.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/domain/LoanApplicationTerms.java @@ -797,7 +797,7 @@ public BigDecimal calculatePeriodsBetweenDates(final LocalDate startDate, final this.repaymentPeriodFrequencyType); } else { LocalDate expectedStartDate = startDate; - if (!CalendarUtils.isValidRedurringDate(loanCalendar.getRecurrence(), + if (!CalendarUtils.isValidRecurringDate(loanCalendar.getRecurrence(), loanCalendar.getStartDateLocalDate().minusMonths(getRepaymentEvery()), startDate)) { expectedStartDate = CalendarUtils.getNewRepaymentMeetingDate(loanCalendar.getRecurrence(), startDate.minusMonths(getRepaymentEvery()), startDate.minusMonths(getRepaymentEvery()), getRepaymentEvery(), diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/service/LoanScheduleAssembler.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/service/LoanScheduleAssembler.java index 2bca76f2d1b..6e8b22d9c3b 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/service/LoanScheduleAssembler.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/service/LoanScheduleAssembler.java @@ -616,7 +616,7 @@ private List fetchDisbursementData(final JsonObject command) { private void validateRepaymentsStartDateWithMeetingDates(final LocalDate repaymentsStartingFromDate, final Calendar calendar, boolean isSkipRepaymentOnFirstDayOfMonth, final Integer numberOfDays) { - if (repaymentsStartingFromDate != null && !CalendarUtils.isValidRedurringDate(calendar.getRecurrence(), + if (repaymentsStartingFromDate != null && !CalendarUtils.isValidRecurringDate(calendar.getRecurrence(), calendar.getStartDateLocalDate(), repaymentsStartingFromDate, isSkipRepaymentOnFirstDayOfMonth, numberOfDays)) { final String errorMessage = "First repayment date '" + repaymentsStartingFromDate + "' do not fall on a meeting date"; throw new LoanApplicationDateException("first.repayment.date.do.not.match.meeting.date", errorMessage, diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationValidator.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationValidator.java index 2405288a8d2..5542f8b366d 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationValidator.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationValidator.java @@ -356,10 +356,6 @@ private void validateForCreate(final JsonElement element) { .integerGreaterThanZero(); } - final BigDecimal principal = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed(LoanApiConstants.principalParamName, - element); - baseDataValidator.reset().parameter(LoanApiConstants.principalParamName).value(principal).notNull().positiveAmount(); - final Integer loanTermFrequency = this.fromApiJsonHelper .extractIntegerWithLocaleNamed(LoanApiConstants.loanTermFrequencyParameterName, element); baseDataValidator.reset().parameter(LoanApiConstants.loanTermFrequencyParameterName).value(loanTermFrequency).notNull() @@ -422,7 +418,7 @@ private void validateForCreate(final JsonElement element) { baseDataValidator.reset().parameter(LoanApiConstants.isFloatingInterestRate).trueOrFalseRequired(false); } - if (interestType != null && interestType.equals(InterestMethod.FLAT.getValue())) { + if (InterestMethod.FLAT.getValue().equals(interestType)) { baseDataValidator.reset().parameter(LoanApiConstants.interestTypeParameterName).failWithCode( "should.be.0.for.selected.loan.product", "interestType should be DECLINING_BALANCE for selected Loan Product as it is linked to floating rates."); @@ -452,7 +448,7 @@ private void validateForCreate(final JsonElement element) { .extractBigDecimalWithLocaleNamed(LoanApiConstants.interestRatePerPeriodParameterName, element); baseDataValidator.reset().parameter(LoanApiConstants.interestRatePerPeriodParameterName).value(interestRatePerPeriod) .notNull().zeroOrPositiveAmount(); - isInterestBearing = interestRatePerPeriod.compareTo(BigDecimal.ZERO) > 0; + isInterestBearing = interestRatePerPeriod != null && interestRatePerPeriod.compareTo(BigDecimal.ZERO) > 0; } final Integer amortizationType = this.fromApiJsonHelper @@ -622,6 +618,9 @@ private void validateForCreate(final JsonElement element) { .ignoreIfNull().positiveAmount(); } + final BigDecimal principal = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed(LoanApiConstants.principalParamName, + element); + if (loanProduct.isCanUseForTopup() && this.fromApiJsonHelper.parameterExists(LoanApiConstants.isTopup, element)) { final Boolean isTopup = this.fromApiJsonHelper.extractBooleanNamed(LoanApiConstants.isTopup, element); baseDataValidator.reset().parameter(LoanApiConstants.isTopup).value(isTopup).validateForBooleanValue(); @@ -745,7 +744,7 @@ private void validateForCreate(final JsonElement element) { loanScheduleValidator.validateDownPaymentAttribute(loanProduct.getLoanProductRelatedDetail().isEnableDownPayment(), element); checkForProductMixRestrictions(element); - validateSubmittedOnDate(element, submittedOnDate, loanProduct); + validateSubmittedOnDate(element, null, null, loanProduct); validateDisbursementDetails(loanProduct, element); validateCollateral(element); // validate if disbursement date is a holiday or a non-working day @@ -754,8 +753,11 @@ private void validateForCreate(final JsonElement element) { validateDisbursementDateIsOnHoliday(expectedDisbursementDate, officeId); final Integer recurringMoratoriumOnPrincipalPeriods = this.fromApiJsonHelper .extractIntegerWithLocaleNamed("recurringMoratoriumOnPrincipalPeriods", element); - loanProductDataValidator.validateRepaymentPeriodWithGraceSettings(numberOfRepayments, graceOnPrincipalPayment, - graceOnInterestPayment, graceOnInterestCharged, recurringMoratoriumOnPrincipalPeriods, baseDataValidator); + + if (numberOfRepayments != null) { + loanProductDataValidator.validateRepaymentPeriodWithGraceSettings(numberOfRepayments, graceOnPrincipalPayment, + graceOnInterestPayment, graceOnInterestCharged, recurringMoratoriumOnPrincipalPeriods, baseDataValidator); + } }); } @@ -777,7 +779,8 @@ private void validateBorrowerCycle(JsonElement element, LoanProduct loanProduct, private void validateDisbursementDateIsOnNonWorkingDay(final LocalDate expectedDisbursementDate) { final WorkingDays workingDays = this.workingDaysRepository.findOne(); final boolean allowTransactionsOnNonWorkingDay = this.configurationDomainService.allowTransactionsOnNonWorkingDayEnabled(); - if (!allowTransactionsOnNonWorkingDay && !WorkingDaysUtil.isWorkingDay(workingDays, expectedDisbursementDate)) { + if (expectedDisbursementDate != null && !allowTransactionsOnNonWorkingDay + && !WorkingDaysUtil.isWorkingDay(workingDays, expectedDisbursementDate)) { final String errorMessage = "Expected disbursement date cannot be on a non working day"; throw new LoanApplicationDateException("disbursement.date.on.non.working.day", errorMessage, expectedDisbursementDate); } @@ -1032,7 +1035,7 @@ public void validateForModify(final JsonCommand command, final Loan loan) { if (interestType == null) { interestType = loan.getLoanProductRelatedDetail().getInterestMethod().getValue(); } - if (interestType != null && interestType.equals(InterestMethod.FLAT.getValue())) { + if (InterestMethod.FLAT.getValue().equals(interestType)) { baseDataValidator.reset().parameter(LoanApiConstants.interestTypeParameterName).failWithCode( "should.be.0.for.selected.loan.product", "interestType should be DECLINING_BALANCE for selected Loan Product as it is linked to floating rates."); @@ -1402,7 +1405,7 @@ public void validateForModify(final JsonCommand command, final Loan loan) { loanScheduleValidator.validateDownPaymentAttribute(loanProduct.getLoanProductRelatedDetail().isEnableDownPayment(), element); validateDisbursementDetails(loanProduct, element); - validateSubmittedOnDate(element, loan.getSubmittedOnDate(), loanProduct); + validateSubmittedOnDate(element, loan.getSubmittedOnDate(), loan.getExpectedDisbursementDate(), loanProduct); validateClientOrGroup(client, group, productId); // validate if disbursement date is a holiday or a non-working day @@ -1759,16 +1762,16 @@ private void validateTransactionProcessingStrategy(final String transactionProce "Loan transaction processing strategy cannot be Advanced Payment Allocation Strategy if it's not configured on loan product"); } else { // PROGRESSIVE: Repayment strategy MUST be only "advanced payment allocation" - if (loanProduct.getLoanProductRelatedDetail().getLoanScheduleType().equals(LoanScheduleType.PROGRESSIVE)) { - if (!transactionProcessingStrategy.equals(LoanProductConstants.ADVANCED_PAYMENT_ALLOCATION_STRATEGY)) { + if (LoanScheduleType.PROGRESSIVE.equals(loanProduct.getLoanProductRelatedDetail().getLoanScheduleType())) { + if (!LoanProductConstants.ADVANCED_PAYMENT_ALLOCATION_STRATEGY.equals(transactionProcessingStrategy)) { // TODO: GeneralPlatformDomainRuleException vs PlatformApiDataValidationException throw new GeneralPlatformDomainRuleException( "error.msg.loan.repayment.strategy.can.not.be.different.than.advanced.payment.allocation", "Loan repayment strategy can not be different than Advanced Payment Allocation"); } // CUMULATIVE: Repayment strategy CANNOT be "advanced payment allocation" - } else if (loanProduct.getLoanProductRelatedDetail().getLoanScheduleType().equals(LoanScheduleType.CUMULATIVE)) { - if (transactionProcessingStrategy.equals(LoanProductConstants.ADVANCED_PAYMENT_ALLOCATION_STRATEGY)) { + } else if (LoanScheduleType.CUMULATIVE.equals(loanProduct.getLoanProductRelatedDetail().getLoanScheduleType())) { + if (LoanProductConstants.ADVANCED_PAYMENT_ALLOCATION_STRATEGY.equals(transactionProcessingStrategy)) { // TODO: GeneralPlatformDomainRuleException vs PlatformApiDataValidationException throw new GeneralPlatformDomainRuleException( "error.msg.loan.repayment.strategy.can.not.be.equal.to.advanced.payment.allocation", @@ -1781,7 +1784,6 @@ private void validateTransactionProcessingStrategy(final String transactionProce } public void checkForProductMixRestrictions(final JsonElement element) { - final List activeLoansLoanProductIds; final Long productId = this.fromApiJsonHelper.extractLongNamed(LoanApiConstants.productIdParameterName, element); final Long groupId = this.fromApiJsonHelper.extractLongNamed(LoanApiConstants.groupIdParameterName, element); @@ -1813,7 +1815,8 @@ private void checkForProductMixRestrictions(final List activeLoansLoanProd } } - private void validateSubmittedOnDate(final JsonElement element, LocalDate originalSubmittedOnDate, LoanProduct loanProduct) { + private void validateSubmittedOnDate(final JsonElement element, LocalDate originalSubmittedOnDate, + LocalDate originalExpectedDisbursementDate, LoanProduct loanProduct) { final LocalDate startDate = loanProduct.getStartDate(); final LocalDate closeDate = loanProduct.getCloseDate(); final LocalDate submittedOnDate = this.fromApiJsonHelper.parameterExists(LoanApiConstants.submittedOnDateParameterName, element) @@ -1822,7 +1825,9 @@ private void validateSubmittedOnDate(final JsonElement element, LocalDate origin final Long clientId = this.fromApiJsonHelper.extractLongNamed(LoanApiConstants.clientIdParameterName, element); final Long groupId = this.fromApiJsonHelper.extractLongNamed(LoanApiConstants.groupIdParameterName, element); final LocalDate expectedDisbursementDate = this.fromApiJsonHelper - .extractLocalDateNamed(LoanApiConstants.expectedDisbursementDateParameterName, element); + .parameterExists(LoanApiConstants.expectedDisbursementDateParameterName, element) + ? this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.expectedDisbursementDateParameterName, element) + : originalExpectedDisbursementDate; String defaultUserMessage = ""; if (DateUtils.isBefore(submittedOnDate, startDate)) { @@ -1863,17 +1868,17 @@ private void validateSubmittedOnDate(final JsonElement element, LocalDate origin Group group = groupRepository.findOneWithNotFoundDetection(groupId); if (group != null && group.isActivatedAfter(submittedOnDate)) { - final String errorMessage = "The date on which a loan is submitted cannot be earlier than groups's activation date."; + final String errorMessage = "The date on which a loan is submitted cannot be earlier than group's activation date."; throw new InvalidLoanStateTransitionException("submittal", "cannot.be.before.group.activation.date", errorMessage, submittedOnDate, group.getActivationDate()); } + } - if (DateUtils.isAfter(submittedOnDate, expectedDisbursementDate)) { - final String errorMessage = "The date on which a loan is submitted cannot be after its expected disbursement date: " - + expectedDisbursementDate; - throw new InvalidLoanStateTransitionException("submittal", "cannot.be.after.expected.disbursement.date", errorMessage, - submittedOnDate, expectedDisbursementDate); - } + if (DateUtils.isAfter(submittedOnDate, expectedDisbursementDate)) { + final String errorMessage = "The date on which a loan is submitted cannot be after its expected disbursement date: " + + expectedDisbursementDate; + throw new InvalidLoanStateTransitionException("submittal", "cannot.be.after.expected.disbursement.date", errorMessage, + submittedOnDate, expectedDisbursementDate); } } @@ -1975,7 +1980,7 @@ public void validateApproval(JsonCommand command, Long loanId) { expectedDisbursementDate = loan.getExpectedDisbursedOnLocalDate(); } - if (DateUtils.isBefore(approvedOnDate, loan.getSubmittedOnDate())) { + if (approvedOnDate != null && DateUtils.isBefore(approvedOnDate, loan.getSubmittedOnDate())) { final String errorMessage = "Loan approval date " + approvedOnDate + " can not be before its submittal date: " + loan.getSubmittedOnDate(); throw new InvalidLoanStateTransitionException("approval", "cannot.be.before.submittal.date", errorMessage, approvedOnDate, diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanTransactionValidator.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanTransactionValidator.java index de1f979221d..41ceb75902a 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanTransactionValidator.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanTransactionValidator.java @@ -442,7 +442,7 @@ public void validateRepaymentDateWithMeetingDate(final LocalDate repaymentDate, final Calendar calendar = calendarInstance.getCalendar(); if (calendar != null && repaymentDate != null) { // Disbursement date should fall on a meeting date - if (!CalendarUtils.isValidRedurringDate(calendar.getRecurrence(), calendar.getStartDateLocalDate(), repaymentDate)) { + if (!CalendarUtils.isValidRecurringDate(calendar.getRecurrence(), calendar.getStartDateLocalDate(), repaymentDate)) { final String errorMessage = "Transaction date '" + repaymentDate.toString() + "' does not fall on a meeting date."; throw new NotValidRecurringDateException("loan.transaction.date", errorMessage, repaymentDate.toString(), calendar.getTitle()); diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanproduct/serialization/LoanProductDataValidator.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanproduct/serialization/LoanProductDataValidator.java index a3840007476..30ae80d3dbe 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanproduct/serialization/LoanProductDataValidator.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanproduct/serialization/LoanProductDataValidator.java @@ -1942,11 +1942,8 @@ private void validateChargeToIncomeAccountMappings(final DataValidatorBuilder ba public void validateMinMaxConstraints(final JsonElement element, final DataValidatorBuilder baseDataValidator, final LoanProduct loanProduct) { - validatePrincipalMinMaxConstraint(element, loanProduct, baseDataValidator); - validateNumberOfRepaymentsMinMaxConstraint(element, loanProduct, baseDataValidator); - validateNominalInterestRatePerPeriodMinMaxConstraint(element, loanProduct, baseDataValidator); } @@ -2029,7 +2026,6 @@ public void validateMinMaxConstraints(final JsonElement element, final DataValid private void validatePrincipalMinMaxConstraint(final JsonElement element, final LoanProduct loanProduct, final DataValidatorBuilder baseDataValidator) { - boolean principalUpdated = false; boolean minPrincipalUpdated = false; boolean maxPrincipalUpdated = false; @@ -2555,11 +2551,7 @@ public void validateRepaymentPeriodWithGraceSettings(final Integer numberOfRepay } } - private Integer defaultToZeroIfNull(final Integer value) { - Integer result = value; - if (value == null) { - result = 0; - } - return result; + private Integer defaultToZeroIfNull(Integer value) { + return value != null ? value : 0; } } diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanValidationIntegrationTest.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanValidationIntegrationTest.java new file mode 100644 index 00000000000..abbc9279a60 --- /dev/null +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanValidationIntegrationTest.java @@ -0,0 +1,135 @@ +/** + * 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.integrationtests; + +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import io.restassured.builder.RequestSpecBuilder; +import io.restassured.builder.ResponseSpecBuilder; +import io.restassured.http.ContentType; +import io.restassured.specification.RequestSpecification; +import io.restassured.specification.ResponseSpecification; +import java.util.Collections; +import net.minidev.json.JSONArray; +import org.apache.fineract.integrationtests.common.ClientHelper; +import org.apache.fineract.integrationtests.common.Utils; +import org.apache.fineract.integrationtests.common.accounting.Account; +import org.apache.fineract.integrationtests.common.accounting.AccountHelper; +import org.apache.fineract.integrationtests.common.loans.LoanApplicationTestBuilder; +import org.apache.fineract.integrationtests.common.loans.LoanProductTestBuilder; +import org.apache.fineract.integrationtests.common.loans.LoanTestLifecycleExtension; +import org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper; +import org.apache.fineract.integrationtests.common.organisation.StaffHelper; +import org.apache.fineract.integrationtests.useradministration.users.UserHelper; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@ExtendWith(LoanTestLifecycleExtension.class) +public class LoanValidationIntegrationTest { + + private static final Logger LOG = LoggerFactory.getLogger(LoanValidationIntegrationTest.class); + + private RequestSpecification requestSpec; + private ResponseSpecification responseSpec; + private LoanTransactionHelper loanTransactionHelper; + private AccountHelper accountHelper; + + @BeforeEach + public void setup() { + Utils.initializeRESTAssured(); + this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); + this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); + this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + this.loanTransactionHelper = new LoanTransactionHelper(this.requestSpec, this.responseSpec); + this.accountHelper = new AccountHelper(this.requestSpec, this.responseSpec); + } + + @Test + public void checkPrincipalErrors() { + final Integer staffId = StaffHelper.createStaff(this.requestSpec, this.responseSpec); + String username = Utils.uniqueRandomStringGenerator("user", 8); + UserHelper.createUser(this.requestSpec, this.responseSpec, 1, staffId, username, "P4ssw0rd", "resourceId"); + + LOG.info("-------------------------Creating Client---------------------------"); + final Integer clientID = ClientHelper.createClient(requestSpec, responseSpec); + ClientHelper.verifyClientCreatedOnServer(requestSpec, responseSpec, clientID); + + LOG.info("-------------------------Creating Loan---------------------------"); + final Account assetAccount = this.accountHelper.createAssetAccount(); + final Account incomeAccount = this.accountHelper.createIncomeAccount(); + final Account expenseAccount = this.accountHelper.createExpenseAccount(); + final Account overpaymentAccount = this.accountHelper.createLiabilityAccount(); + + LOG.info("------------------------------CREATING NEW LOAN PRODUCT ---------------------------------------"); + final String loanProductJSON = new LoanProductTestBuilder() // + .withPrincipal("10000000.00") // + .withNumberOfRepayments("24") // + .withRepaymentAfterEvery("1") // + .withRepaymentTypeAsMonth() // + .withinterestRatePerPeriod("2") // + .withInterestRateFrequencyTypeAsMonths() // + .withRepaymentStrategy(LoanProductTestBuilder.DEFAULT_STRATEGY) // + .withAmortizationTypeAsEqualPrincipalPayment() // + .withInterestTypeAsDecliningBalance() // + .currencyDetails("0", "0") + .withAccounting("2", new Account[] { assetAccount, incomeAccount, expenseAccount, overpaymentAccount }).build(null); + final Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJSON); + + LOG.info("--------------------------------APPLYING FOR LOAN APPLICATION--------------------------------"); + final String loanApplicationJSON = new LoanApplicationTestBuilder() // + .withPrincipal("-1") // + .withLoanTermFrequency("6") // + .withLoanTermFrequencyAsMonths() // + .withNumberOfRepayments("6") // + .withRepaymentEveryAfter("1") // + .withRepaymentFrequencyTypeAsMonths() // + .withInterestRatePerPeriod("2") // + .withAmortizationTypeAsEqualInstallments() // + .withInterestTypeAsFlatBalance() // + .withInterestCalculationPeriodTypeSameAsRepaymentPeriod() // + .withExpectedDisbursementDate("12 July 2022") // + .withSubmittedOnDate("10 July 2022") // + .withRepaymentStrategy(LoanApplicationTestBuilder.DEFAULT_STRATEGY) // + .withCharges(Collections.emptyList()) // + .build(clientID.toString(), loanProductID.toString(), null); + + ResponseSpecification failedResponseSpec = new ResponseSpecBuilder().expectStatusCode(400).expectBody(new BaseMatcher() { + + @Override + public boolean matches(Object body) { + DocumentContext json = JsonPath.parse(body.toString()); + LOG.error(body.toString()); + JSONArray errors = json.read("$.errors[*].developerMessage"); + LOG.info("errors: {}", errors); + return errors.size() == 1; + } + + @Override + public void describeTo(Description description) { + + } + }).build(); + final Integer loanID = this.loanTransactionHelper.getLoanId(loanApplicationJSON, requestSpec, failedResponseSpec); + } +}