Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public void validateForCreate(final JsonCommand command) {
baseDataValidator.reset().parameter(StandingInstructionApiConstants.instructionTypeParamName).value(standingInstructionType)
.notNull().inMinMaxRange(1, 2);

if (isAmountRequired(standingInstructionType)) {
baseDataValidator.reset().parameter(StandingInstructionApiConstants.amountParamName).value(transferAmount).notNull();
}

final Integer recurrenceType = this.fromApiJsonHelper.extractIntegerNamed(StandingInstructionApiConstants.recurrenceTypeParamName,
element, Locale.getDefault());
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceTypeParamName).value(recurrenceType).notNull()
Expand All @@ -151,21 +155,26 @@ public void validateForCreate(final JsonCommand command) {
if (frequencyType.isMonthly() || frequencyType.isYearly()) {
final MonthDay monthDay = this.fromApiJsonHelper
.extractMonthDayNamed(StandingInstructionApiConstants.recurrenceOnMonthDayParamName, element);
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceOnMonthDayParamName).value(monthDay)
.notNull();

if (isRecurrenceRequired(standingInstructionType)) {
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceOnMonthDayParamName).value(monthDay)
.notNull();
}
}
}

final Integer recurrenceInterval = this.fromApiJsonHelper
.extractIntegerNamed(StandingInstructionApiConstants.recurrenceIntervalParamName, element, Locale.getDefault());
if (isPeriodic) {
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceIntervalParamName).value(recurrenceInterval)
.notNull();
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceFrequencyParamName).value(recurrenceFrequency)
.notNull();
if (isRecurrenceRequired(standingInstructionType)) {
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceIntervalParamName).value(recurrenceInterval)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a little bit complicated. and if conditions can be merged, also of there is any business rule that is implemented make sure to document that helper function

Business rules should be explicit and self-documenting

while the Github Action is not run here is a pro tip you can run the check on your forked repo and add testcase for more coverage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aman-Mittal Thank you so much for your guidance and suggestions. I will improve the PR and update shortly.

.notNull();
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceFrequencyParamName).value(recurrenceFrequency)
.notNull();
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceIntervalParamName).value(recurrenceInterval)
.integerGreaterThanZero();
}
}
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceIntervalParamName).value(recurrenceInterval)
.integerGreaterThanZero();

final String name = this.fromApiJsonHelper.extractStringNamed(StandingInstructionApiConstants.nameParamName, element);
baseDataValidator.reset().parameter(StandingInstructionApiConstants.nameParamName).value(name).notNull();
Expand All @@ -178,7 +187,7 @@ public void validateForCreate(final JsonCommand command) {
.inMinMaxRange(1, 1);

}
if (standingInstructionType != null && StandingInstructionType.fromInt(standingInstructionType).isFixedAmoutTransfer()) {
if (isAmountRequired(standingInstructionType)) {
baseDataValidator.reset().parameter(StandingInstructionApiConstants.amountParamName).value(transferAmount).notNull();
}

Expand Down Expand Up @@ -230,10 +239,22 @@ public void validateForUpdate(final JsonCommand command) {
baseDataValidator.reset().parameter(StandingInstructionApiConstants.validTillParamName).value(validTill).notNull();
}

Integer standingInstructionType = null;
if (this.fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element)) {
standingInstructionType = this.fromApiJsonHelper.extractIntegerNamed(StandingInstructionApiConstants.instructionTypeParamName,
element, Locale.getDefault());
baseDataValidator.reset().parameter(StandingInstructionApiConstants.instructionTypeParamName).value(standingInstructionType)
.notNull().inMinMaxRange(1, 2);
}

if (this.fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.amountParamName, element)) {
final BigDecimal transferAmount = this.fromApiJsonHelper
.extractBigDecimalWithLocaleNamed(StandingInstructionApiConstants.amountParamName, element);
baseDataValidator.reset().parameter(StandingInstructionApiConstants.amountParamName).value(transferAmount).positiveAmount();

if (isAmountRequired(standingInstructionType)) {
baseDataValidator.reset().parameter(StandingInstructionApiConstants.amountParamName).value(transferAmount).notNull();
}
}

if (this.fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.statusParamName, element)) {
Expand All @@ -250,13 +271,6 @@ public void validateForUpdate(final JsonCommand command) {
.inMinMaxRange(1, 4);
}

if (this.fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element)) {
final Integer standingInstructionType = this.fromApiJsonHelper
.extractIntegerNamed(StandingInstructionApiConstants.instructionTypeParamName, element, Locale.getDefault());
baseDataValidator.reset().parameter(StandingInstructionApiConstants.instructionTypeParamName).value(standingInstructionType)
.notNull().inMinMaxRange(1, 2);
}

if (this.fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceTypeParamName, element)) {
final Integer recurrenceType = this.fromApiJsonHelper
.extractIntegerNamed(StandingInstructionApiConstants.recurrenceTypeParamName, element, Locale.getDefault());
Expand All @@ -274,8 +288,11 @@ public void validateForUpdate(final JsonCommand command) {
if (this.fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceIntervalParamName, element)) {
final Integer recurrenceInterval = this.fromApiJsonHelper
.extractIntegerNamed(StandingInstructionApiConstants.recurrenceIntervalParamName, element, Locale.getDefault());
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceIntervalParamName).value(recurrenceInterval)
.integerGreaterThanZero();

if (isRecurrenceRequired(standingInstructionType)) {
baseDataValidator.reset().parameter(StandingInstructionApiConstants.recurrenceIntervalParamName).value(recurrenceInterval)
.integerGreaterThanZero();
}
}

if (this.fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.nameParamName, element)) {
Expand All @@ -291,4 +308,32 @@ private void throwExceptionIfValidationWarningsExist(final List<ApiParameterErro
throw new PlatformApiDataValidationException(dataValidationErrors);
}
}

// ============================================
// PRIVATE HELPER METHODS
// ============================================

/**
* Determines if the standing instruction type requires amount validation. Amount is required only for
* FIXED_AMOUNT_TRANSFER type. For DUES type, amount is calculated based on outstanding dues.
*
* @param standingInstructionType
* the instruction type code
* @return true if amount validation is required, false otherwise
*/
private boolean isAmountRequired(Integer standingInstructionType) {
return standingInstructionType != null && StandingInstructionType.fromInt(standingInstructionType).isFixedAmoutTransfer();
}

/**
* Determines if recurrence fields should be validated. Recurrence is required for all types except DUES. DUES
* instructions are processed based on loan due dates, not recurrence pattern.
*
* @param standingInstructionType
* the instruction type code
* @return true if recurrence validation is required, false otherwise
*/
private boolean isRecurrenceRequired(Integer standingInstructionType) {
return standingInstructionType == null || !StandingInstructionType.fromInt(standingInstructionType).isDuesAmoutTransfer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.account.data;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.fineract.portfolio.account.domain.StandingInstructionType;
import org.junit.jupiter.api.Test;

class StandingInstructionDataValidatorTest {

@Test
void isAmountRequired_fixedAmountTransferType_returnsTrue() {
assertTrue(isAmountRequired(1));
}

@Test
void isAmountRequired_duesType_returnsFalse() {
assertFalse(isAmountRequired(2));
}

@Test
void isAmountRequired_nullType_returnsFalse() {
assertFalse(isAmountRequired(null));
}

@Test
void isRecurrenceRequired_fixedAmountTransferType_returnsTrue() {
assertTrue(isRecurrenceRequired(1));
}

@Test
void isRecurrenceRequired_duesType_returnsFalse() {
assertFalse(isRecurrenceRequired(2));
}

@Test
void isRecurrenceRequired_nullType_returnsTrue() {
assertTrue(isRecurrenceRequired(null));
}

@Test
void standingInstructionType_fixedAmountTransfer_isFixedAmountTransfer() {
StandingInstructionType type = StandingInstructionType.fromInt(1);
assertTrue(type.isFixedAmoutTransfer());
assertFalse(type.isDuesAmoutTransfer());
}

@Test
void standingInstructionType_dues_isDuesAmountTransfer() {
StandingInstructionType type = StandingInstructionType.fromInt(2);
assertTrue(type.isDuesAmoutTransfer());
assertFalse(type.isFixedAmoutTransfer());
}

private boolean isAmountRequired(Integer standingInstructionType) {
return standingInstructionType != null && StandingInstructionType.fromInt(standingInstructionType).isFixedAmoutTransfer();
}

private boolean isRecurrenceRequired(Integer standingInstructionType) {
return standingInstructionType == null || !StandingInstructionType.fromInt(standingInstructionType).isDuesAmoutTransfer();
}
}
Loading