|
| 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.fail; |
| 22 | +import static org.apache.fineract.client.feign.util.FeignCalls.ok; |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +import io.cucumber.java.en.Then; |
| 26 | +import io.cucumber.java.en.When; |
| 27 | +import java.math.BigDecimal; |
| 28 | +import lombok.RequiredArgsConstructor; |
| 29 | +import lombok.extern.slf4j.Slf4j; |
| 30 | +import org.apache.fineract.client.feign.FineractFeignClient; |
| 31 | +import org.apache.fineract.client.feign.util.CallFailedRuntimeException; |
| 32 | +import org.apache.fineract.client.models.CommandProcessingResult; |
| 33 | +import org.apache.fineract.client.models.StringEnumOptionData; |
| 34 | +import org.apache.fineract.client.models.WorkingCapitalBreachData; |
| 35 | +import org.apache.fineract.client.models.WorkingCapitalBreachRequest; |
| 36 | +import org.apache.fineract.client.models.WorkingCapitalBreachTemplateResponse; |
| 37 | +import org.apache.fineract.test.factory.WorkingCapitalRequestFactory; |
| 38 | +import org.apache.fineract.test.helper.ErrorMessageHelper; |
| 39 | +import org.apache.fineract.test.stepdef.AbstractStepDef; |
| 40 | +import org.apache.fineract.test.support.TestContext; |
| 41 | +import org.apache.fineract.test.support.TestContextKey; |
| 42 | +import org.assertj.core.api.SoftAssertions; |
| 43 | +import org.springframework.beans.factory.annotation.Autowired; |
| 44 | + |
| 45 | +@Slf4j |
| 46 | +@RequiredArgsConstructor |
| 47 | +public class WorkingCapitalBreachConfigStepDef extends AbstractStepDef { |
| 48 | + |
| 49 | + @Autowired |
| 50 | + private WorkingCapitalRequestFactory workingCapitalRequestFactory; |
| 51 | + |
| 52 | + private final FineractFeignClient fineractFeignClient; |
| 53 | + |
| 54 | + @When("Admin Calls Breach Template") |
| 55 | + public void adminCallsBreachTemplate() { |
| 56 | + final WorkingCapitalBreachTemplateResponse template = ok( |
| 57 | + () -> fineractFeignClient.workingCapitalBreaches().retrieveWorkingCapitalBreachTemplate()); |
| 58 | + assertThat(template).isNotNull(); |
| 59 | + assertThat(template.getBreachFrequencyTypeOptions()).isNotNull().isNotEmpty(); |
| 60 | + assertThat(template.getBreachAmountCalculationTypeOptions()).isNotNull().isNotEmpty(); |
| 61 | + log.info("Template WorkingCapitalBreach: {}", template); |
| 62 | + } |
| 63 | + |
| 64 | + @When("Admin creates WC Breach With Values") |
| 65 | + public void adminCreatesWCBreachWithValues() { |
| 66 | + final WorkingCapitalBreachRequest request = workingCapitalRequestFactory.defaultWorkingCapitalBreachRequest(); |
| 67 | + final CommandProcessingResult response = ok(() -> fineractFeignClient.workingCapitalBreaches().createWorkingCapitalBreach(request)); |
| 68 | + assertThat(response).isNotNull(); |
| 69 | + assertThat(response.getResourceId()).isNotNull(); |
| 70 | + TestContext.GLOBAL.set(TestContextKey.WORKING_CAPITAL_BREACH_ID, response.getResourceId()); |
| 71 | + TestContext.GLOBAL.set(TestContextKey.WORKING_CAPITAL_BREACH_CREATE_REQUEST, request); |
| 72 | + } |
| 73 | + |
| 74 | + @When("Admin creates WC Breach With Values for update") |
| 75 | + public void adminCreatesWCBreachWithValuesForUpdate() { |
| 76 | + final WorkingCapitalBreachRequest request = workingCapitalRequestFactory.defaultWorkingCapitalBreachRequest() |
| 77 | + .breachAmount(new BigDecimal("2.34")); |
| 78 | + final CommandProcessingResult response = ok(() -> fineractFeignClient.workingCapitalBreaches().createWorkingCapitalBreach(request)); |
| 79 | + assertThat(response).isNotNull(); |
| 80 | + assertThat(response.getResourceId()).isNotNull(); |
| 81 | + TestContext.GLOBAL.set(TestContextKey.WORKING_CAPITAL_BREACH_ID_FOR_UPDATE, response.getResourceId()); |
| 82 | + TestContext.GLOBAL.set(TestContextKey.WORKING_CAPITAL_BREACH_CREATE_REQUEST_FOR_UPDATE, request); |
| 83 | + } |
| 84 | + |
| 85 | + @Then("Check created Breach has the following values") |
| 86 | + public void checkCreatedBreachHasTheFollowingValues() { |
| 87 | + final Long id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID); |
| 88 | + final WorkingCapitalBreachData data = ok(() -> fineractFeignClient.workingCapitalBreaches().retrieveWorkingCapitalBreach(id)); |
| 89 | + final WorkingCapitalBreachRequest request = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_CREATE_REQUEST); |
| 90 | + checkBreachData(request, data); |
| 91 | + } |
| 92 | + |
| 93 | + @Then("Get Breach With Template has the following values") |
| 94 | + public void getBreachWithTemplateHasTheFollowingValues() { |
| 95 | + final Long id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID); |
| 96 | + final WorkingCapitalBreachData data = ok(() -> fineractFeignClient.workingCapitalBreaches().retrieveWorkingCapitalBreach(id)); |
| 97 | + final WorkingCapitalBreachTemplateResponse template = ok( |
| 98 | + () -> fineractFeignClient.workingCapitalBreaches().retrieveWorkingCapitalBreachTemplate()); |
| 99 | + assertThat(data).isNotNull(); |
| 100 | + assertThat(template).isNotNull(); |
| 101 | + assertThat(template.getBreachFrequencyTypeOptions()).isNotNull().isNotEmpty(); |
| 102 | + assertThat(template.getBreachAmountCalculationTypeOptions()).isNotNull().isNotEmpty(); |
| 103 | + } |
| 104 | + |
| 105 | + @When("Admin modifies WC Breach With Values") |
| 106 | + public void adminModifiesWCBreachWithValues() { |
| 107 | + final Long id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID); |
| 108 | + final WorkingCapitalBreachRequest request = workingCapitalRequestFactory.defaultWorkingCapitalBreachRequest() // |
| 109 | + .breachFrequency(4) // |
| 110 | + .breachFrequencyType("MONTHS") // |
| 111 | + .breachAmountCalculationType("FLAT") // |
| 112 | + .breachAmount(new BigDecimal("7.89")); |
| 113 | + ok(() -> fineractFeignClient.workingCapitalBreaches().updateWorkingCapitalBreach(id, request)); |
| 114 | + TestContext.GLOBAL.set(TestContextKey.WORKING_CAPITAL_BREACH_UPDATE_REQUEST, request); |
| 115 | + } |
| 116 | + |
| 117 | + @Then("Check updated Breach has the following values") |
| 118 | + public void checkUpdatedBreachHasTheFollowingValues() { |
| 119 | + final Long id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID); |
| 120 | + final WorkingCapitalBreachData data = ok(() -> fineractFeignClient.workingCapitalBreaches().retrieveWorkingCapitalBreach(id)); |
| 121 | + final WorkingCapitalBreachRequest request = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_UPDATE_REQUEST); |
| 122 | + checkBreachData(request, data); |
| 123 | + } |
| 124 | + |
| 125 | + @When("Admin deletes WC Breach With Values") |
| 126 | + public void adminDeletesWCBreachWithValues() { |
| 127 | + final Long id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID); |
| 128 | + ok(() -> fineractFeignClient.workingCapitalBreaches().deleteWorkingCapitalBreach(id)); |
| 129 | + } |
| 130 | + |
| 131 | + @When("Admin deletes WC Breach With Values for update") |
| 132 | + public void adminDeletesWCBreachWithValuesForUpdate() { |
| 133 | + final Long id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID_FOR_UPDATE); |
| 134 | + ok(() -> fineractFeignClient.workingCapitalBreaches().deleteWorkingCapitalBreach(id)); |
| 135 | + } |
| 136 | + |
| 137 | + @Then("Admin failed to create a new WC Breach for field {string} with invalid data {string} results with an error {}") |
| 138 | + public void createWCBreachWithInvalidDataFailed(final String fieldName, final String value, final String errorMessage) { |
| 139 | + final WorkingCapitalBreachRequest request = setWCBreachFieldValue(workingCapitalRequestFactory.defaultWorkingCapitalBreachRequest(), |
| 140 | + fieldName, value); |
| 141 | + checkCreateWCBreachWithInvalidDataFailure(request, errorMessage, 400); |
| 142 | + } |
| 143 | + |
| 144 | + @Then("Admin failed to update WC Breach for field {string} with invalid data {string} results with an error {}") |
| 145 | + public void updateWCBreachWithInvalidDataFailed(final String fieldName, final String value, final String errorMessage) { |
| 146 | + Long id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID_FOR_UPDATE); |
| 147 | + if (id == null) { |
| 148 | + adminCreatesWCBreachWithValuesForUpdate(); |
| 149 | + id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID_FOR_UPDATE); |
| 150 | + } |
| 151 | + final WorkingCapitalBreachRequest request = setWCBreachFieldValue(workingCapitalRequestFactory.defaultWorkingCapitalBreachRequest(), |
| 152 | + fieldName, value); |
| 153 | + checkUpdateWCBreachWithInvalidDataFailure(id, request, errorMessage, 400); |
| 154 | + } |
| 155 | + |
| 156 | + @Then("Admin failed to delete WC Breach that is already deleted") |
| 157 | + public void adminDeleteWCBreachAlreadyDeletedFailure() { |
| 158 | + final Long id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID_FOR_UPDATE); |
| 159 | + checkDeleteWCBreachNotFoundFailure(id); |
| 160 | + } |
| 161 | + |
| 162 | + @Then("Admin failed to retrieve WC Breach that is already deleted") |
| 163 | + public void adminRetrieveWCBreachAlreadyDeletedFailure() { |
| 164 | + final Long id = TestContext.GLOBAL.get(TestContextKey.WORKING_CAPITAL_BREACH_ID_FOR_UPDATE); |
| 165 | + checkRetrieveWCBreachNotFoundFailure(id); |
| 166 | + } |
| 167 | + |
| 168 | + @Then("Admin failed to delete WC Breach with id {int} that doesn't exist") |
| 169 | + public void adminDeleteWCBreachWithIncorrectIdFailure(final Integer id) { |
| 170 | + checkDeleteWCBreachNotFoundFailure(Long.valueOf(id)); |
| 171 | + } |
| 172 | + |
| 173 | + @Then("Admin failed to retrieve WC Breach with id {int} that is not found") |
| 174 | + public void adminRetrieveWCBreachWithIncorrectIdFailure(final Integer id) { |
| 175 | + checkRetrieveWCBreachNotFoundFailure(Long.valueOf(id)); |
| 176 | + } |
| 177 | + |
| 178 | + private void checkCreateWCBreachWithInvalidDataFailure(final WorkingCapitalBreachRequest request, final String errorMessage, |
| 179 | + final int errorCode) { |
| 180 | + final CallFailedRuntimeException exception = fail( |
| 181 | + () -> fineractFeignClient.workingCapitalBreaches().createWorkingCapitalBreach(request)); |
| 182 | + assertThat(exception.getStatus()).as(ErrorMessageHelper.incorrectExpectedValueInResponse()).isEqualTo(errorCode); |
| 183 | + assertThat(exception.getDeveloperMessage()).contains(errorMessage); |
| 184 | + } |
| 185 | + |
| 186 | + private void checkUpdateWCBreachWithInvalidDataFailure(final Long id, final WorkingCapitalBreachRequest request, |
| 187 | + final String errorMessage, final int errorCode) { |
| 188 | + final CallFailedRuntimeException exception = fail( |
| 189 | + () -> fineractFeignClient.workingCapitalBreaches().updateWorkingCapitalBreach(id, request)); |
| 190 | + assertThat(exception.getStatus()).as(ErrorMessageHelper.incorrectExpectedValueInResponse()).isEqualTo(errorCode); |
| 191 | + assertThat(exception.getDeveloperMessage()).contains(errorMessage); |
| 192 | + } |
| 193 | + |
| 194 | + private void checkDeleteWCBreachNotFoundFailure(final Long id) { |
| 195 | + final CallFailedRuntimeException exception = fail( |
| 196 | + () -> fineractFeignClient.workingCapitalBreaches().deleteWorkingCapitalBreach(id)); |
| 197 | + assertThat(exception.getStatus()).isEqualTo(404); |
| 198 | + assertThat(exception.getDeveloperMessage()).contains(ErrorMessageHelper.workingCapitalBreachNotFoundFailure(id)); |
| 199 | + } |
| 200 | + |
| 201 | + private void checkRetrieveWCBreachNotFoundFailure(final Long id) { |
| 202 | + final CallFailedRuntimeException exception = fail( |
| 203 | + () -> fineractFeignClient.workingCapitalBreaches().retrieveWorkingCapitalBreach(id)); |
| 204 | + assertThat(exception.getStatus()).isEqualTo(404); |
| 205 | + assertThat(exception.getDeveloperMessage()).contains(ErrorMessageHelper.workingCapitalBreachNotFoundFailure(id)); |
| 206 | + } |
| 207 | + |
| 208 | + private WorkingCapitalBreachRequest setWCBreachFieldValue(final WorkingCapitalBreachRequest request, final String fieldName, |
| 209 | + String fieldValue) { |
| 210 | + if ("null".equals(fieldValue)) { |
| 211 | + fieldValue = null; |
| 212 | + } |
| 213 | + final Integer intValue = fieldValue != null && "breachFrequency".equals(fieldName) ? Integer.valueOf(fieldValue) : null; |
| 214 | + final BigDecimal decimalValue = fieldValue != null && "breachAmount".equals(fieldName) ? new BigDecimal(fieldValue) : null; |
| 215 | + switch (fieldName) { |
| 216 | + case "breachFrequency": |
| 217 | + request.setBreachFrequency(intValue); |
| 218 | + break; |
| 219 | + case "breachFrequencyType": |
| 220 | + request.setBreachFrequencyType(fieldValue); |
| 221 | + break; |
| 222 | + case "breachAmountCalculationType": |
| 223 | + request.setBreachAmountCalculationType(fieldValue); |
| 224 | + break; |
| 225 | + case "breachAmount": |
| 226 | + request.setBreachAmount(decimalValue); |
| 227 | + break; |
| 228 | + default: |
| 229 | + break; |
| 230 | + } |
| 231 | + return request; |
| 232 | + } |
| 233 | + |
| 234 | + private void checkBreachData(final WorkingCapitalBreachRequest request, final WorkingCapitalBreachData response) { |
| 235 | + final SoftAssertions assertions = new SoftAssertions(); |
| 236 | + assertions.assertThat(response.getBreachFrequency()).isEqualTo(request.getBreachFrequency()); |
| 237 | + assertions.assertThat(enumId(response.getBreachFrequencyType())).isEqualTo(request.getBreachFrequencyType()); |
| 238 | + assertions.assertThat(enumId(response.getBreachAmountCalculationType())).isEqualTo(request.getBreachAmountCalculationType()); |
| 239 | + assert response.getBreachAmount() != null; |
| 240 | + assertions.assertThat(response.getBreachAmount().compareTo(request.getBreachAmount())).isEqualTo(0); |
| 241 | + assertions.assertAll(); |
| 242 | + } |
| 243 | + |
| 244 | + private String enumId(final StringEnumOptionData option) { |
| 245 | + return option != null ? option.getId() : null; |
| 246 | + } |
| 247 | +} |
0 commit comments