Skip to content

Commit 416ec19

Browse files
Fix StandingInstructionDataValidatorTest mocks and formatting
1 parent 2c71d2e commit 416ec19

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

fineract-provider/src/test/java/org/apache/fineract/portfolio/account/data/StandingInstructionDataValidatorTest.java

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
2424
import static org.mockito.ArgumentMatchers.any;
2525
import static org.mockito.ArgumentMatchers.eq;
26+
import static org.mockito.Mockito.doNothing;
2627
import static org.mockito.Mockito.mock;
2728
import static org.mockito.Mockito.when;
2829

2930
import com.google.gson.JsonElement;
3031
import com.google.gson.JsonObject;
3132
import java.math.BigDecimal;
32-
import java.util.Locale;
33+
import java.time.MonthDay;
34+
import java.util.HashSet;
3335
import java.util.Set;
3436
import org.apache.fineract.infrastructure.core.api.JsonCommand;
3537
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
36-
import java.time.MonthDay;
3738
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
38-
import org.apache.fineract.infrastructure.core.serialization.FromApiJsonDeserializerHelper;
39+
import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
3940
import org.apache.fineract.portfolio.account.api.StandingInstructionApiConstants;
4041
import org.junit.jupiter.api.BeforeEach;
4142
import org.junit.jupiter.api.Nested;
@@ -49,7 +50,7 @@
4950
class StandingInstructionDataValidatorTest {
5051

5152
@Mock
52-
private FromApiJsonDeserializerHelper fromApiJsonHelper;
53+
private FromJsonHelper fromJsonHelper;
5354

5455
@Mock
5556
private AccountTransfersDetailDataValidator accountTransfersDetailDataValidator;
@@ -63,8 +64,8 @@ class StandingInstructionDataValidatorTest {
6364
@BeforeEach
6465
void setUp() {
6566
MockitoAnnotations.openMocks(this);
66-
when(accountTransfersDetailDataValidator.validate(any(), any())).thenReturn(null);
67-
when(fromApiJsonHelper.extractMonthDayNamed(any(), any())).thenReturn(null);
67+
doNothing().when(accountTransfersDetailDataValidator).validate(any(), any());
68+
when(fromJsonHelper.extractMonthDayNamed(any(), any())).thenReturn(null);
6869

6970
command = mock(JsonCommand.class);
7071
element = new JsonObject();
@@ -110,10 +111,10 @@ void fixedInvalidRecurrenceType_shouldFail(int invalidType) {
110111
setupInstructionType(1);
111112
setupAmount(BigDecimal.valueOf(1000));
112113

113-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceTypeParamName, element)).thenReturn(true);
114+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceTypeParamName, element)).thenReturn(true);
114115

115-
when(fromApiJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceTypeParamName), eq(element),
116-
any(Locale.class))).thenReturn(invalidType);
116+
when(fromJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceTypeParamName), eq(element),
117+
org.mockito.ArgumentMatchers.<Set<String>>any())).thenReturn(invalidType);
117118

118119
assertThrows(PlatformApiDataValidationException.class, () -> validator.validateForCreate(command));
119120
}
@@ -213,7 +214,7 @@ void zeroInterval_shouldFail() {
213214
@Test
214215
void missingAmountParam_shouldPass() {
215216
setupInstructionType(2);
216-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.amountParamName, element)).thenReturn(false);
217+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.amountParamName, element)).thenReturn(false);
217218

218219
assertDoesNotThrow(() -> validator.validateForCreate(command));
219220
}
@@ -232,10 +233,10 @@ class InstructionTypeEdgeCases {
232233

233234
@Test
234235
void nullType_shouldFail() {
235-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element))
236+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element))
236237
.thenReturn(true);
237-
when(fromApiJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.instructionTypeParamName),
238-
eq(element), any(Locale.class))).thenReturn(null);
238+
when(fromJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.instructionTypeParamName),
239+
eq(element), org.mockito.ArgumentMatchers.<Set<String>>any())).thenReturn(null);
239240

240241
assertThrows(PlatformApiDataValidationException.class,
241242
() -> validator.validateForCreate(command));
@@ -250,7 +251,7 @@ void invalidTypeValues_shouldFail(int invalidType) {
250251

251252
@Test
252253
void missingTypeParam_shouldFail() {
253-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element))
254+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element))
254255
.thenReturn(false);
255256

256257
PlatformApiDataValidationException ex = assertThrows(
@@ -267,9 +268,9 @@ class PartialDataTests {
267268
void fixedPartialRecurrence_shouldFail() {
268269
setupInstructionType(1);
269270
setupAmount(BigDecimal.valueOf(1000));
270-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceIntervalParamName, element)).thenReturn(true);
271-
when(fromApiJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceIntervalParamName), eq(element),
272-
any(Locale.class))).thenReturn(30);
271+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceIntervalParamName, element)).thenReturn(true);
272+
when(fromJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceIntervalParamName), eq(element),
273+
org.mockito.ArgumentMatchers.<Set<String>>any())).thenReturn(30);
273274

274275
PlatformApiDataValidationException ex = assertThrows(PlatformApiDataValidationException.class,
275276
() -> validator.validateForCreate(command));
@@ -298,7 +299,7 @@ void fixedOnlyRecurrence_shouldFail() {
298299

299300
@Test
300301
void emptyJson_shouldFail() {
301-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element))
302+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element))
302303
.thenReturn(false);
303304

304305
assertThrows(PlatformApiDataValidationException.class,
@@ -317,7 +318,7 @@ void fixedMultipleErrors_shouldAccumulate() {
317318

318319
PlatformApiDataValidationException ex = assertThrows(PlatformApiDataValidationException.class,
319320
() -> validator.validateForCreate(command));
320-
Set<ApiParameterError> errors = ex.getErrors();
321+
Set<ApiParameterError> errors = new HashSet<>(ex.getErrors());
321322
assertTrue(errors.size() > 1);
322323
}
323324

@@ -364,54 +365,54 @@ void duesPartialUpdate_shouldPass() {
364365
}
365366

366367
private void setupInstructionType(int type) {
367-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element))
368+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.instructionTypeParamName, element))
368369
.thenReturn(true);
369-
when(fromApiJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.instructionTypeParamName),
370-
eq(element), any(Locale.class))).thenReturn(type);
370+
when(fromJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.instructionTypeParamName),
371+
eq(element), org.mockito.ArgumentMatchers.<Set<String>>any())).thenReturn(type);
371372
}
372373

373374
private void setupAmount(BigDecimal amount) {
374-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.amountParamName, element))
375+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.amountParamName, element))
375376
.thenReturn(true);
376-
when(fromApiJsonHelper.extractBigDecimalWithLocaleNamed(eq(StandingInstructionApiConstants.amountParamName),
377-
eq(element), any(Locale.class))).thenReturn(amount);
377+
when(fromJsonHelper.extractBigDecimalWithLocaleNamed(eq(StandingInstructionApiConstants.amountParamName),
378+
eq(element), org.mockito.ArgumentMatchers.<Set<String>>any())).thenReturn(amount);
378379
}
379380

380381
private void setupRecurrence(int recurrenceType, int interval, int frequency, String monthDay) {
381-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceTypeParamName, element))
382+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceTypeParamName, element))
382383
.thenReturn(true);
383-
when(fromApiJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceTypeParamName),
384-
eq(element), any(Locale.class))).thenReturn(recurrenceType);
384+
when(fromJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceTypeParamName),
385+
eq(element), org.mockito.ArgumentMatchers.<Set<String>>any())).thenReturn(recurrenceType);
385386

386-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceIntervalParamName, element))
387+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceIntervalParamName, element))
387388
.thenReturn(true);
388-
when(fromApiJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceIntervalParamName),
389-
eq(element), any(Locale.class))).thenReturn(interval);
389+
when(fromJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceIntervalParamName),
390+
eq(element), org.mockito.ArgumentMatchers.<Set<String>>any())).thenReturn(interval);
390391

391-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceFrequencyParamName, element))
392+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceFrequencyParamName, element))
392393
.thenReturn(true);
393-
when(fromApiJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceFrequencyParamName),
394-
eq(element), any(Locale.class))).thenReturn(frequency);
394+
when(fromJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceFrequencyParamName),
395+
eq(element), org.mockito.ArgumentMatchers.<Set<String>>any())).thenReturn(frequency);
395396

396-
when(fromApiJsonHelper.extractMonthDayNamed(eq(StandingInstructionApiConstants.recurrenceOnMonthDayParamName),
397+
when(fromJsonHelper.extractMonthDayNamed(eq(StandingInstructionApiConstants.recurrenceOnMonthDayParamName),
397398
eq(element))).thenReturn(monthDay != null ? MonthDay.parse("--" + monthDay) : null);
398399
}
399400

400401
private void setupRecurrenceInterval(int interval) {
401-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceIntervalParamName, element))
402+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceIntervalParamName, element))
402403
.thenReturn(true);
403-
when(fromApiJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceIntervalParamName),
404-
eq(element), any(Locale.class))).thenReturn(interval);
404+
when(fromJsonHelper.extractIntegerNamed(eq(StandingInstructionApiConstants.recurrenceIntervalParamName),
405+
eq(element), org.mockito.ArgumentMatchers.<Set<String>>any())).thenReturn(interval);
405406
}
406407

407408
private void setupRecurrenceMissing() {
408-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceTypeParamName, element)).thenReturn(false);
409-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceIntervalParamName, element)).thenReturn(false);
410-
when(fromApiJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceFrequencyParamName, element)).thenReturn(false);
409+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceTypeParamName, element)).thenReturn(false);
410+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceIntervalParamName, element)).thenReturn(false);
411+
when(fromJsonHelper.parameterExists(StandingInstructionApiConstants.recurrenceFrequencyParamName, element)).thenReturn(false);
411412
}
412413

413414
private void assertHasError(PlatformApiDataValidationException ex, String paramName) {
414-
boolean found = ex.getErrors().stream().anyMatch(e -> e.getParameter().equals(paramName));
415+
boolean found = ex.getErrors().stream().anyMatch(e -> paramName.equals(e.getParameterName()));
415416
assertTrue(found, "Expected validation error for parameter: " + paramName);
416417
}
417418
}

0 commit comments

Comments
 (0)