|
| 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.WorkingCapitalLoanBreachScheduleData; |
| 34 | +import org.apache.fineract.test.stepdef.AbstractStepDef; |
| 35 | +import org.apache.fineract.test.support.TestContextKey; |
| 36 | + |
| 37 | +@Slf4j |
| 38 | +@RequiredArgsConstructor |
| 39 | +public class WorkingCapitalBreachScheduleStepDef extends AbstractStepDef { |
| 40 | + |
| 41 | + private final FineractFeignClient fineractClient; |
| 42 | + |
| 43 | + @Then("Working Capital loan breach schedule has no data") |
| 44 | + public void verifyBreachScheduleIsEmpty() { |
| 45 | + final Long loanId = extractLoanId(); |
| 46 | + final List<WorkingCapitalLoanBreachScheduleData> schedule = retrieveBreachSchedule(loanId); |
| 47 | + |
| 48 | + assertThat(schedule).as("Breach schedule should be empty").isEmpty(); |
| 49 | + |
| 50 | + log.info("Verified that loan {} has no breach schedule", loanId); |
| 51 | + } |
| 52 | + |
| 53 | + @Then("Working Capital loan breach schedule has the following data:") |
| 54 | + public void verifyBreachSchedule(final DataTable dataTable) { |
| 55 | + final Long loanId = extractLoanId(); |
| 56 | + final List<WorkingCapitalLoanBreachScheduleData> schedule = retrieveBreachSchedule(loanId); |
| 57 | + |
| 58 | + final List<List<String>> rows = dataTable.asLists(); |
| 59 | + final List<String> headers = rows.getFirst(); |
| 60 | + final List<List<String>> expectedData = rows.subList(1, rows.size()); |
| 61 | + |
| 62 | + assertThat(schedule).as("Breach schedule size should match expected data").hasSize(expectedData.size()); |
| 63 | + |
| 64 | + for (int i = 0; i < expectedData.size(); i++) { |
| 65 | + final List<String> expectedRow = expectedData.get(i); |
| 66 | + final WorkingCapitalLoanBreachScheduleData actualRow = schedule.get(i); |
| 67 | + |
| 68 | + for (int j = 0; j < headers.size(); j++) { |
| 69 | + final String header = headers.get(j); |
| 70 | + final String expectedValue = expectedRow.get(j); |
| 71 | + verifyField(actualRow, header, expectedValue, i + 1); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + log.info("Successfully verified {} breach schedule entries for loan {}", schedule.size(), loanId); |
| 76 | + } |
| 77 | + |
| 78 | + private Long extractLoanId() { |
| 79 | + final PostWorkingCapitalLoansResponse loanResponse = testContext().get(TestContextKey.LOAN_CREATE_RESPONSE); |
| 80 | + return loanResponse.getLoanId(); |
| 81 | + } |
| 82 | + |
| 83 | + private List<WorkingCapitalLoanBreachScheduleData> retrieveBreachSchedule(final Long loanId) { |
| 84 | + final List<WorkingCapitalLoanBreachScheduleData> schedule = ok( |
| 85 | + () -> fineractClient.workingCapitalLoanBreachSchedule().retrieveBreachSchedule(loanId)); |
| 86 | + log.debug("Breach schedule for loan {}: {}", loanId, schedule); |
| 87 | + return schedule; |
| 88 | + } |
| 89 | + |
| 90 | + private void verifyField(final WorkingCapitalLoanBreachScheduleData actual, final String fieldName, final String expectedValue, |
| 91 | + final int rowNumber) { |
| 92 | + switch (fieldName) { |
| 93 | + case "periodNumber" -> |
| 94 | + assertThat(actual.getPeriodNumber()).as("Period number for row %d", rowNumber).isEqualTo(Integer.parseInt(expectedValue)); |
| 95 | + case "fromDate" -> |
| 96 | + assertThat(actual.getFromDate()).as("From date for row %d", rowNumber).isEqualTo(LocalDate.parse(expectedValue)); |
| 97 | + case "toDate" -> assertThat(actual.getToDate()).as("To date for row %d", rowNumber).isEqualTo(LocalDate.parse(expectedValue)); |
| 98 | + case "numberOfDays" -> verifyNullableInteger(actual.getNumberOfDays(), expectedValue, "Number of days", rowNumber); |
| 99 | + case "minPaymentAmount" -> |
| 100 | + verifyNullableBigDecimal(actual.getMinPaymentAmount(), expectedValue, "Min payment amount", rowNumber); |
| 101 | + case "outstandingAmount" -> |
| 102 | + verifyNullableBigDecimal(actual.getOutstandingAmount(), expectedValue, "Outstanding amount", rowNumber); |
| 103 | + case "nearBreach" -> verifyNullableBoolean(actual.getNearBreach(), expectedValue, "Near breach", rowNumber); |
| 104 | + case "breach" -> verifyNullableBoolean(actual.getBreach(), expectedValue, "Breach", rowNumber); |
| 105 | + default -> throw new IllegalArgumentException("Unknown field name: " + fieldName); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private void verifyNullableBoolean(final Boolean actualValue, final String expectedValue, final String fieldDescription, |
| 110 | + final int rowNumber) { |
| 111 | + if ("null".equals(expectedValue)) { |
| 112 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isNull(); |
| 113 | + } else { |
| 114 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isEqualTo(Boolean.parseBoolean(expectedValue)); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private void verifyNullableBigDecimal(final BigDecimal actualValue, final String expectedValue, final String fieldDescription, |
| 119 | + final int rowNumber) { |
| 120 | + if ("null".equals(expectedValue)) { |
| 121 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isNull(); |
| 122 | + } else { |
| 123 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isEqualByComparingTo(new BigDecimal(expectedValue)); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private void verifyNullableInteger(final Integer actualValue, final String expectedValue, final String fieldDescription, |
| 128 | + final int rowNumber) { |
| 129 | + if ("null".equals(expectedValue)) { |
| 130 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isNull(); |
| 131 | + } else { |
| 132 | + assertThat(actualValue).as("%s for row %d", fieldDescription, rowNumber).isEqualTo(Integer.parseInt(expectedValue)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments