From 34dcf581276eea9caa160cfe640b80c3c3de8c3c Mon Sep 17 00:00:00 2001 From: Andreas Buchen Date: Mon, 19 Feb 2024 10:34:24 +0100 Subject: [PATCH] Enhance import assertion to support multiple currencies Additionally, the check makes sure that only the given currencies are available for import. Issue: #3789 --- .../actions/AssertImportActions.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/name.abuchen.portfolio.tests/src/name/abuchen/portfolio/datatransfer/actions/AssertImportActions.java b/name.abuchen.portfolio.tests/src/name/abuchen/portfolio/datatransfer/actions/AssertImportActions.java index cda5ac10ac..8ab8f68f3a 100644 --- a/name.abuchen.portfolio.tests/src/name/abuchen/portfolio/datatransfer/actions/AssertImportActions.java +++ b/name.abuchen.portfolio.tests/src/name/abuchen/portfolio/datatransfer/actions/AssertImportActions.java @@ -1,17 +1,19 @@ package name.abuchen.portfolio.datatransfer.actions; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.nullValue; +import java.text.MessageFormat; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import name.abuchen.portfolio.datatransfer.Extractor; import name.abuchen.portfolio.datatransfer.ImportAction; import name.abuchen.portfolio.model.Account; import name.abuchen.portfolio.model.Portfolio; -import name.abuchen.portfolio.money.CurrencyUnit; -import name.abuchen.portfolio.money.Money; public class AssertImportActions { @@ -61,8 +63,11 @@ public Portfolio getSecondaryPortfolio() new CheckValidTypesAction(), new CheckSecurityRelatedValuesAction(), new CheckCurrenciesAction(), new CheckForexGrossValueAction() }; - private void check(List items, ImportAction.Context context) + public void check(List items, String... currencyCode) { + var contexts = Arrays.asList(currencyCode).stream() + .collect(Collectors.toMap((c) -> c, (c) -> new TestContext(c))); + for (Extractor.Item item : items) { // do not apply further checks if the item is a (permanent) failure @@ -70,6 +75,21 @@ private void check(List items, ImportAction.Context context) if (item.isFailure()) continue; + // items that have no amount (e.g. a security) are checked against a + // pseudo currency for the account to make sure that no attempt is + // made to import into an account + ImportAction.Context context; + if (item.getAmount() == null) + { + context = new TestContext("XYZ"); //$NON-NLS-1$ + } + else + { + context = contexts.get(item.getAmount().getCurrencyCode()); + assertThat(MessageFormat.format("No account available for currency ''{0}''", //$NON-NLS-1$ + item.getAmount().getCurrencyCode()), context, is(not(nullValue()))); + } + for (ImportAction action : actions) { ImportAction.Status status = item.apply(action, context); @@ -78,19 +98,4 @@ private void check(List items, ImportAction.Context context) } } } - - public void check(List items, String currencyCode) - { - check(items, new TestContext(currencyCode)); - } - - public void check(List items) - { - for (Extractor.Item item : items) - { - Money money = item.getAmount(); - String currencyCode = money != null ? money.getCurrencyCode() : CurrencyUnit.EUR; - check(Arrays.asList(item), new TestContext(currencyCode)); - } - } }