|
| 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.common; |
| 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.And; |
| 26 | +import io.cucumber.java.en.Then; |
| 27 | +import io.cucumber.java.en.When; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import lombok.RequiredArgsConstructor; |
| 33 | +import org.apache.fineract.client.feign.FineractFeignClient; |
| 34 | +import org.apache.fineract.client.models.CurrencyConfigurationData; |
| 35 | +import org.apache.fineract.client.models.CurrencyData; |
| 36 | +import org.apache.fineract.client.models.CurrencyUpdateRequest; |
| 37 | +import org.apache.fineract.client.models.CurrencyUpdateResponse; |
| 38 | +import org.apache.fineract.test.helper.ErrorMessageHelper; |
| 39 | +import org.apache.fineract.test.stepdef.AbstractStepDef; |
| 40 | +import org.apache.fineract.test.support.TestContextKey; |
| 41 | + |
| 42 | +@RequiredArgsConstructor |
| 43 | +public class CurrencyStepDef extends AbstractStepDef { |
| 44 | + |
| 45 | + private static final List<String> DEFAULT_CURRENCIES = Arrays.asList("EUR", "USD"); |
| 46 | + private final FineractFeignClient fineractClient; |
| 47 | + |
| 48 | + @When("Admin retrieves currency configuration") |
| 49 | + public void adminRetrievesCurrencyConfiguration() { |
| 50 | + |
| 51 | + CurrencyConfigurationData response = ok(() -> fineractClient.currency().retrieveCurrencies()); |
| 52 | + testContext().set(TestContextKey.GET_CURRENCIES_RESPONSE, response); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + @Then("Currency {string} has the following properties:") |
| 57 | + public void currencyHasFollowingProperties(String currencyCode, DataTable table) { |
| 58 | + |
| 59 | + CurrencyConfigurationData config = testContext().get(TestContextKey.GET_CURRENCIES_RESPONSE); |
| 60 | + |
| 61 | + List<CurrencyData> allCurrencies = new ArrayList<>(); |
| 62 | + if (config.getSelectedCurrencyOptions() != null) { |
| 63 | + allCurrencies.addAll(config.getSelectedCurrencyOptions()); |
| 64 | + } |
| 65 | + if (config.getCurrencyOptions() != null) { |
| 66 | + allCurrencies.addAll(config.getCurrencyOptions()); |
| 67 | + } |
| 68 | + |
| 69 | + Map<String, String> expected = table.asMaps().get(0); |
| 70 | + String expectedName = expected.get("name"); |
| 71 | + String expectedSymbol = expected.get("symbol"); |
| 72 | + int expectedDecimalPlaces = Integer.parseInt(expected.get("decimalPlaces")); |
| 73 | + |
| 74 | + CurrencyData currency = allCurrencies.stream().filter(c -> currencyCode.equals(c.getCode())).findFirst().orElse(null); |
| 75 | + |
| 76 | + assertThat(currency).as(ErrorMessageHelper.currencyNotFound(currencyCode)).isNotNull(); |
| 77 | + assertThat(currency.getName()).as(ErrorMessageHelper.wrongCurrencyField(currencyCode, "name", currency.getName(), expectedName)) |
| 78 | + .isEqualTo(expectedName); |
| 79 | + assertThat(currency.getDisplaySymbol()) |
| 80 | + .as(ErrorMessageHelper.wrongCurrencyField(currencyCode, "displaySymbol", currency.getDisplaySymbol(), expectedSymbol)) |
| 81 | + .isEqualTo(expectedSymbol); |
| 82 | + assertThat(currency.getDecimalPlaces()).as( |
| 83 | + ErrorMessageHelper.wrongCurrencyField(currencyCode, "decimalPlaces", currency.getDecimalPlaces(), expectedDecimalPlaces)) |
| 84 | + .isEqualTo(expectedDecimalPlaces); |
| 85 | + assertThat(currency.getNameCode()) |
| 86 | + .as(ErrorMessageHelper.wrongCurrencyField(currencyCode, "nameCode", currency.getNameCode(), "currency." + currencyCode)) |
| 87 | + .isEqualTo("currency." + currencyCode); |
| 88 | + assertThat(currency.getDisplayLabel()).as(ErrorMessageHelper.wrongCurrencyField(currencyCode, "displayLabel", |
| 89 | + currency.getDisplayLabel(), expectedName + " (" + expectedSymbol + ")")) |
| 90 | + .isEqualTo(expectedName + " (" + expectedSymbol + ")"); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + @When("Admin updates selected currencies to {string}") |
| 95 | + public void adminUpdatesSelectedCurrencies(String currencyCodes) { |
| 96 | + |
| 97 | + List<String> currencies = Arrays.asList(currencyCodes.split(",")); |
| 98 | + CurrencyUpdateRequest request = new CurrencyUpdateRequest().currencies(currencies); |
| 99 | + CurrencyUpdateResponse response = ok(() -> fineractClient.currency().updateCurrencies(request, Map.of())); |
| 100 | + testContext().set(TestContextKey.PUT_CURRENCIES_RESPONSE, response); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + @Then("The returned currency list matches {string}") |
| 105 | + public void returnedCurrencyListMatches(String expectedCodes) { |
| 106 | + |
| 107 | + List<String> expected = Arrays.asList(expectedCodes.split(",")); |
| 108 | + CurrencyUpdateResponse response = testContext().get(TestContextKey.PUT_CURRENCIES_RESPONSE); |
| 109 | + |
| 110 | + assertThat(response).as(ErrorMessageHelper.idNull()).isNotNull(); |
| 111 | + assertThat(response.getCurrencies()).as(ErrorMessageHelper.wrongSelectedCurrencies(response.getCurrencies(), expected)) |
| 112 | + .isEqualTo(expected); |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + @Then("The selected currencies contain {string}") |
| 117 | + public void selectedCurrenciesContain(String expectedCodes) { |
| 118 | + |
| 119 | + List<String> expected = Arrays.asList(expectedCodes.split(",")); |
| 120 | + CurrencyConfigurationData config = testContext().get(TestContextKey.GET_CURRENCIES_RESPONSE); |
| 121 | + |
| 122 | + assertThat(config.getSelectedCurrencyOptions()).as(ErrorMessageHelper.idNull()).isNotNull(); |
| 123 | + |
| 124 | + List<String> selectedCodes = config.getSelectedCurrencyOptions().stream().map(CurrencyData::getCode).sorted().toList(); |
| 125 | + List<String> expectedSorted = expected.stream().sorted().toList(); |
| 126 | + |
| 127 | + assertThat(selectedCodes).as(ErrorMessageHelper.wrongSelectedCurrencies(selectedCodes, expectedSorted)).isEqualTo(expectedSorted); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + @And("Admin resets selected currencies to default") |
| 132 | + public void adminResetsSelectedCurrenciesToDefault() { |
| 133 | + |
| 134 | + CurrencyUpdateRequest request = new CurrencyUpdateRequest().currencies(DEFAULT_CURRENCIES); |
| 135 | + ok(() -> fineractClient.currency().updateCurrencies(request, Map.of())); |
| 136 | + |
| 137 | + } |
| 138 | +} |
0 commit comments