Skip to content

Commit edc833b

Browse files
FINERACT-2048: Improve StandingInstructionDataValidator validation and tests
1 parent a98c143 commit edc833b

2 files changed

Lines changed: 588 additions & 16 deletions

File tree

fineract-provider/src/main/java/org/apache/fineract/portfolio/account/data/StandingInstructionDataValidator.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ public void validateForCreate(final JsonCommand command) {
115115
baseDataValidator.reset().parameter(StandingInstructionApiConstants.validTillParamName).value(validTill)
116116
.validateDateAfter(validFrom);
117117

118-
final BigDecimal transferAmount = this.fromApiJsonHelper
119-
.extractBigDecimalWithLocaleNamed(StandingInstructionApiConstants.amountParamName, element);
120-
baseDataValidator.reset().parameter(StandingInstructionApiConstants.amountParamName).value(transferAmount).positiveAmount();
121-
122118
final Integer transferType = this.fromApiJsonHelper.extractIntegerNamed(transferTypeParamName, element, Locale.getDefault());
123119
baseDataValidator.reset().parameter(transferTypeParamName).value(transferType).notNull().inMinMaxRange(1, 3);
124120

@@ -129,43 +125,70 @@ public void validateForCreate(final JsonCommand command) {
129125

130126
final Integer standingInstructionType = this.fromApiJsonHelper
131127
.extractIntegerNamed(StandingInstructionApiConstants.instructionTypeParamName, element, Locale.getDefault());
128+
132129
baseDataValidator.reset().parameter(StandingInstructionApiConstants.instructionTypeParamName).value(standingInstructionType)
133130
.notNull().inMinMaxRange(1, 2);
134131

132+
boolean isDues = false;
133+
134+
if (standingInstructionType != null) {
135+
StandingInstructionType type = StandingInstructionType.fromInt(standingInstructionType);
136+
isDues = type.isDuesAmoutTransfer();
137+
}
138+
139+
final BigDecimal transferAmount = this.fromApiJsonHelper
140+
.extractBigDecimalWithLocaleNamed(StandingInstructionApiConstants.amountParamName, element);
141+
142+
if (!isDues) {
143+
baseDataValidator.reset().parameter(StandingInstructionApiConstants.amountParamName).value(transferAmount).notNull()
144+
.positiveAmount();
145+
}
146+
135147
final Integer recurrenceType = this.fromApiJsonHelper.extractIntegerNamed(StandingInstructionApiConstants.recurrenceTypeParamName,
136148
element, Locale.getDefault());
137-
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceTypeParamName).value(recurrenceType).notNull()
138-
.inMinMaxRange(1, 2);
149+
if (!isDues) {
150+
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceTypeParamName).value(recurrenceType).notNull()
151+
.inMinMaxRange(1, 2);
152+
}
153+
139154
boolean isPeriodic = false;
140155
if (recurrenceType != null) {
141156
isPeriodic = AccountTransferRecurrenceType.fromInt(recurrenceType).isPeriodicRecurrence();
142157
}
143158

144159
final Integer recurrenceFrequency = this.fromApiJsonHelper
145160
.extractIntegerNamed(StandingInstructionApiConstants.recurrenceFrequencyParamName, element, Locale.getDefault());
146-
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceFrequencyParamName).value(recurrenceFrequency)
147-
.inMinMaxRange(0, 3);
161+
if (!isDues) {
162+
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceFrequencyParamName).value(recurrenceFrequency)
163+
.inMinMaxRange(0, 3);
164+
}
148165

149166
if (recurrenceFrequency != null) {
150167
PeriodFrequencyType frequencyType = PeriodFrequencyType.fromInt(recurrenceFrequency);
151168
if (frequencyType.isMonthly() || frequencyType.isYearly()) {
152169
final MonthDay monthDay = this.fromApiJsonHelper
153170
.extractMonthDayNamed(StandingInstructionApiConstants.recurrenceOnMonthDayParamName, element);
154-
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceOnMonthDayParamName).value(monthDay)
155-
.notNull();
171+
172+
if (!isDues) {
173+
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceOnMonthDayParamName).value(monthDay)
174+
.notNull();
175+
}
156176
}
157177
}
158178

159179
final Integer recurrenceInterval = this.fromApiJsonHelper
160180
.extractIntegerNamed(StandingInstructionApiConstants.recurrenceIntervalParamName, element, Locale.getDefault());
161-
if (isPeriodic) {
181+
if (!isDues && isPeriodic) {
162182
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceIntervalParamName).value(recurrenceInterval)
163183
.notNull();
164184
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceFrequencyParamName).value(recurrenceFrequency)
165185
.notNull();
166186
}
167-
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceIntervalParamName).value(recurrenceInterval)
168-
.integerGreaterThanZero();
187+
188+
if (!isDues) {
189+
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceIntervalParamName).value(recurrenceInterval)
190+
.integerGreaterThanZero();
191+
}
169192

170193
final String name = this.fromApiJsonHelper.extractStringNamed(StandingInstructionApiConstants.nameParamName, element);
171194
baseDataValidator.reset().parameter(StandingInstructionApiConstants.nameParamName).value(name).notNull();
@@ -178,9 +201,6 @@ public void validateForCreate(final JsonCommand command) {
178201
.inMinMaxRange(1, 1);
179202

180203
}
181-
if (standingInstructionType != null && StandingInstructionType.fromInt(standingInstructionType).isFixedAmoutTransfer()) {
182-
baseDataValidator.reset().parameter(StandingInstructionApiConstants.amountParamName).value(transferAmount).notNull();
183-
}
184204

185205
String errorCode = null;
186206
AccountTransferType accountTransferType = AccountTransferType.fromInt(transferType);
@@ -211,6 +231,7 @@ public void validateForUpdate(final JsonCommand command) {
211231
}
212232

213233
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
234+
214235
this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, UPDATE_REQUEST_DATA_PARAMETERS);
215236

216237
final List<ApiParameterError> dataValidationErrors = new ArrayList<>();

0 commit comments

Comments
 (0)