Skip to content

Commit

Permalink
Enhance import assertion to support multiple currencies
Browse files Browse the repository at this point in the history
Additionally, the check makes sure that only the given currencies are
available for import.

Issue: #3789
  • Loading branch information
buchen committed Feb 19, 2024
1 parent 60c7a64 commit 34dcf58
Showing 1 changed file with 23 additions and 18 deletions.
@@ -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
{
Expand Down Expand Up @@ -61,15 +63,33 @@ public Portfolio getSecondaryPortfolio()
new CheckValidTypesAction(), new CheckSecurityRelatedValuesAction(), new CheckCurrenciesAction(),
new CheckForexGrossValueAction() };

private void check(List<Extractor.Item> items, ImportAction.Context context)
public void check(List<Extractor.Item> 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
// as the transactions most likely has further errors
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);
Expand All @@ -78,19 +98,4 @@ private void check(List<Extractor.Item> items, ImportAction.Context context)
}
}
}

public void check(List<Extractor.Item> items, String currencyCode)
{
check(items, new TestContext(currencyCode));
}

public void check(List<Extractor.Item> 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));
}
}
}

0 comments on commit 34dcf58

Please sign in to comment.