|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.fineract.test.stepdef.loan; |
| 20 | + |
| 21 | +import static org.apache.fineract.client.feign.util.FeignCalls.ok; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +import io.cucumber.datatable.DataTable; |
| 25 | +import io.cucumber.java.en.Then; |
| 26 | +import java.math.BigDecimal; |
| 27 | +import java.time.LocalDate; |
| 28 | +import java.util.List; |
| 29 | +import lombok.RequiredArgsConstructor; |
| 30 | +import lombok.extern.slf4j.Slf4j; |
| 31 | +import org.apache.fineract.client.feign.FineractFeignClient; |
| 32 | +import org.apache.fineract.client.models.PostWorkingCapitalLoansResponse; |
| 33 | +import org.apache.fineract.client.models.WorkingCapitalLoanDelinquencyRangeScheduleData; |
| 34 | +import org.apache.fineract.test.stepdef.AbstractStepDef; |
| 35 | +import org.apache.fineract.test.support.TestContextKey; |
| 36 | + |
| 37 | +@Slf4j |
| 38 | +@RequiredArgsConstructor |
| 39 | +public class WorkingCapitalDelinquencyStepDef extends AbstractStepDef { |
| 40 | + |
| 41 | + private final FineractFeignClient fineractClient; |
| 42 | + |
| 43 | + @Then("Working Capital loan delinquency range schedule has no data on a not yet disbursed loan") |
| 44 | + public void verifyRangeScheduleIsEmpty() { |
| 45 | + Long loanId = extractLoanId(); |
| 46 | + List<WorkingCapitalLoanDelinquencyRangeScheduleData> actualRangeSchedule = retrieveRangeSchedule(loanId); |
| 47 | + |
| 48 | + assertThat(actualRangeSchedule).as("Range schedule should be empty when loan is not yet disbursed").isEmpty(); |
| 49 | + |
| 50 | + log.info("Verified that loan {} has no delinquency range schedule on a not yet disbursed loan", loanId); |
| 51 | + } |
| 52 | + |
| 53 | + @Then("Working Capital loan delinquency range schedule has the following data:") |
| 54 | + public void verifyRangeSchedule(DataTable dataTable) { |
| 55 | + Long loanId = extractLoanId(); |
| 56 | + List<WorkingCapitalLoanDelinquencyRangeScheduleData> actualRangeSchedule = retrieveRangeSchedule(loanId); |
| 57 | + |
| 58 | + // If no data rows provided (only header), just log and return |
| 59 | + if (dataTable.height() <= 1) { |
| 60 | + log.info("No expected data provided for verification, skipping validation"); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + List<List<String>> rows = dataTable.asLists(); |
| 65 | + List<String> headers = rows.get(0); |
| 66 | + List<List<String>> expectedData = rows.subList(1, rows.size()); |
| 67 | + |
| 68 | + verifyRangeScheduleSize(actualRangeSchedule, expectedData.size()); |
| 69 | + verifyAllRangeScheduleFields(actualRangeSchedule, headers, expectedData); |
| 70 | + |
| 71 | + log.info("Successfully verified {} range schedule entries", actualRangeSchedule.size()); |
| 72 | + } |
| 73 | + |
| 74 | + private Long extractLoanId() { |
| 75 | + final PostWorkingCapitalLoansResponse loanResponse = testContext().get(TestContextKey.LOAN_CREATE_RESPONSE); |
| 76 | + return loanResponse.getLoanId(); |
| 77 | + } |
| 78 | + |
| 79 | + private List<WorkingCapitalLoanDelinquencyRangeScheduleData> retrieveRangeSchedule(Long loanId) { |
| 80 | + List<WorkingCapitalLoanDelinquencyRangeScheduleData> rangeSchedule = ok( |
| 81 | + () -> fineractClient.workingCapitalLoanDelinquencyRangeSchedule().retrieveDelinquencyRangeSchedule(loanId)); |
| 82 | + log.debug("Delinquency Range Schedule for loan {}: {}", loanId, rangeSchedule); |
| 83 | + return rangeSchedule; |
| 84 | + } |
| 85 | + |
| 86 | + private void verifyRangeScheduleSize(List<WorkingCapitalLoanDelinquencyRangeScheduleData> actualRangeSchedule, int expectedSize) { |
| 87 | + assertThat(actualRangeSchedule).as("Range schedule size should match expected data").hasSize(expectedSize); |
| 88 | + } |
| 89 | + |
| 90 | + private void verifyAllRangeScheduleFields(List<WorkingCapitalLoanDelinquencyRangeScheduleData> actualRangeSchedule, |
| 91 | + List<String> headers, List<List<String>> expectedData) { |
| 92 | + for (int i = 0; i < expectedData.size(); i++) { |
| 93 | + List<String> expectedRow = expectedData.get(i); |
| 94 | + WorkingCapitalLoanDelinquencyRangeScheduleData actualRow = actualRangeSchedule.get(i); |
| 95 | + |
| 96 | + for (int j = 0; j < headers.size(); j++) { |
| 97 | + String header = headers.get(j); |
| 98 | + String expectedValue = expectedRow.get(j); |
| 99 | + verifyRangeScheduleField(actualRow, header, expectedValue, i + 1); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void verifyRangeScheduleField(WorkingCapitalLoanDelinquencyRangeScheduleData actual, String fieldName, String expectedValue, |
| 105 | + int rowNumber) { |
| 106 | + switch (fieldName) { |
| 107 | + case "periodNumber" -> |
| 108 | + assertThat(actual.getPeriodNumber()).as("Period number for row %d", rowNumber).isEqualTo(Integer.parseInt(expectedValue)); |
| 109 | + case "fromDate" -> |
| 110 | + assertThat(actual.getFromDate()).as("From date for row %d", rowNumber).isEqualTo(LocalDate.parse(expectedValue)); |
| 111 | + case "toDate" -> assertThat(actual.getToDate()).as("To date for row %d", rowNumber).isEqualTo(LocalDate.parse(expectedValue)); |
| 112 | + case "expectedAmount" -> assertThat(actual.getExpectedAmount()).as("Expected amount for row %d", rowNumber) |
| 113 | + .isEqualByComparingTo(new BigDecimal(expectedValue)); |
| 114 | + case "paidAmount" -> assertThat(actual.getPaidAmount()).as("Paid amount for row %d", rowNumber) |
| 115 | + .isEqualByComparingTo(new BigDecimal(expectedValue)); |
| 116 | + case "outstandingAmount" -> assertThat(actual.getOutstandingAmount()).as("Outstanding amount for row %d", rowNumber) |
| 117 | + .isEqualByComparingTo(new BigDecimal(expectedValue)); |
| 118 | + case "minPaymentCriteriaMet" -> |
| 119 | + verifyNullableBoolean(actual.getMinPaymentCriteriaMet(), expectedValue, "Min payment criteria met", rowNumber); |
| 120 | + case "delinquentAmount" -> |
| 121 | + verifyNullableBigDecimal(actual.getDelinquentAmount(), expectedValue, "Delinquent amount", rowNumber); |
| 122 | + case "delinquentDays" -> verifyNullableLong(actual.getDelinquentDays(), expectedValue, "Delinquent days", rowNumber); |
| 123 | + default -> throw new IllegalArgumentException("Unknown field name: " + fieldName); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private void verifyNullableBoolean(Boolean actualValue, String expectedValue, String fieldDescription, int rowNumber) { |
| 128 | + if ("null".equals(expectedValue)) { |
| 129 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isNull(); |
| 130 | + } else { |
| 131 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isEqualTo(Boolean.parseBoolean(expectedValue)); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private void verifyNullableBigDecimal(BigDecimal actualValue, String expectedValue, String fieldDescription, int rowNumber) { |
| 136 | + if ("null".equals(expectedValue)) { |
| 137 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isNull(); |
| 138 | + } else { |
| 139 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isEqualByComparingTo(new BigDecimal(expectedValue)); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private void verifyNullableLong(Long actualValue, String expectedValue, String fieldDescription, int rowNumber) { |
| 144 | + if ("null".equals(expectedValue)) { |
| 145 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isNull(); |
| 146 | + } else { |
| 147 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isEqualTo(Long.parseLong(expectedValue)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments