package name.abuchen.portfolio.datatransfer.pdf;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
import org.junit.Test;
import name.abuchen.portfolio.datatransfer.Extractor.BuySellEntryItem;
import name.abuchen.portfolio.datatransfer.Extractor.Item;
import name.abuchen.portfolio.datatransfer.Extractor.SecurityItem;
import name.abuchen.portfolio.datatransfer.Extractor.TransactionItem;
import name.abuchen.portfolio.datatransfer.actions.AssertImportActions;
import name.abuchen.portfolio.model.AccountTransaction;
import name.abuchen.portfolio.model.BuySellEntry;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.PortfolioTransaction;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.model.Transaction.Unit;
import name.abuchen.portfolio.money.Money;
import name.abuchen.portfolio.money.Values;
@SuppressWarnings("nls")
public class BankSLMPDFExtractorTest
{
@Test
public void testKauf_Inland1() throws IOException
{
BankSLMPDFExctractor extractor = new BankSLMPDFExctractor(new Client())
{
@Override
protected String strip(File file) throws IOException
{
return from(file.getName());
}
};
List<Exception> errors = new ArrayList<>();
List<Item> results = extractor.extract(Arrays.asList(new File("BankSLM_Kauf_Inland1.txt")), errors);
assertThat(errors, empty());
assertThat(results.size(), is(2));
new AssertImportActions().check(results, "CHF");
// check security
Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
assertThat(security.getWkn(), is("135186"));
assertThat(security.getName(), is("Bank SLM AG"));
// check buy sell transaction
Optional<Item> item = results.stream().filter(i -> i instanceof BuySellEntryItem).findFirst();
assertThat(item.isPresent(), is(true));
assertThat(item.get().getSubject(), instanceOf(BuySellEntry.class));
BuySellEntry entry = (BuySellEntry) item.get().getSubject();
assertThat(entry.getPortfolioTransaction().getType(), is(PortfolioTransaction.Type.BUY));
assertThat(entry.getAccountTransaction().getType(), is(AccountTransaction.Type.BUY));
assertThat(entry.getPortfolioTransaction().getMonetaryAmount(),
is(Money.of("CHF", Values.Amount.factorize(1481.10))));
assertThat(entry.getPortfolioTransaction().getDate(), is(LocalDate.parse("2016-06-21")));
assertThat(entry.getPortfolioTransaction().getShares(), is(Values.Share.factorize(1)));
assertThat(entry.getPortfolioTransaction().getUnitSum(Unit.Type.FEE), is(Money.of("CHF", 1_10L)));
}
@Test
public void testKauf_Inland2() throws IOException
{
BankSLMPDFExctractor extractor = new BankSLMPDFExctractor(new Client())
{
@Override
protected String strip(File file) throws IOException
{
return from(file.getName());
}
};
List<Exception> errors = new ArrayList<>();
List<Item> results = extractor.extract(Arrays.asList(new File("BankSLM_Kauf_Inland2.txt")), errors);
assertThat(errors, empty());
assertThat(results.size(), is(2));
new AssertImportActions().check(results, "CHF");
// check security
Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
assertThat(security.getWkn(), is("24476758"));
assertThat(security.getName(), is("UBS Group AG"));
// check buy sell transaction
Optional<Item> item = results.stream().filter(i -> i instanceof BuySellEntryItem).findFirst();
assertThat(item.isPresent(), is(true));
assertThat(item.get().getSubject(), instanceOf(BuySellEntry.class));
BuySellEntry entry = (BuySellEntry) item.get().getSubject();
assertThat(entry.getPortfolioTransaction().getType(), is(PortfolioTransaction.Type.BUY));
assertThat(entry.getAccountTransaction().getType(), is(AccountTransaction.Type.BUY));
assertThat(entry.getPortfolioTransaction().getMonetaryAmount(),
is(Money.of("CHF", Values.Amount.factorize(43412.10))));
assertThat(entry.getPortfolioTransaction().getDate(), is(LocalDate.parse("2016-02-10")));
assertThat(entry.getPortfolioTransaction().getShares(), is(Values.Share.factorize(3000)));
assertThat(entry.getPortfolioTransaction().getUnitSum(Unit.Type.FEE),
is(Money.of("CHF", 32_40L + 3_50 + 176_20)));
}
@Test
public void testKauf_Ausland1() throws IOException
{
BankSLMPDFExctractor extractor = new BankSLMPDFExctractor(new Client())
{
@Override
protected String strip(File file) throws IOException
{
return from(file.getName());
}
};
List<Exception> errors = new ArrayList<>();
List<Item> results = extractor.extract(Arrays.asList(new File("BankSLM_Kauf_Ausland1.txt")), errors);
assertThat(errors, empty());
assertThat(results.size(), is(2));
new AssertImportActions().check(results, "CHF");
// check security
Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
assertThat(security.getWkn(), is("472672"));
assertThat(security.getName(), is("Nokia Corp"));
// check buy sell transaction
Optional<Item> item = results.stream().filter(i -> i instanceof BuySellEntryItem).findFirst();
assertThat(item.isPresent(), is(true));
assertThat(item.get().getSubject(), instanceOf(BuySellEntry.class));
BuySellEntry entry = (BuySellEntry) item.get().getSubject();
assertThat(entry.getPortfolioTransaction().getType(), is(PortfolioTransaction.Type.BUY));
assertThat(entry.getAccountTransaction().getType(), is(AccountTransaction.Type.BUY));
assertThat(entry.getPortfolioTransaction().getMonetaryAmount(),
is(Money.of("CHF", Values.Amount.factorize(92658.45))));
assertThat(entry.getPortfolioTransaction().getDate(), is(LocalDate.parse("2013-09-03")));
assertThat(entry.getPortfolioTransaction().getShares(), is(Values.Share.factorize(17000)));
assertThat(entry.getPortfolioTransaction().getUnitSum(Unit.Type.FEE),
is(Money.of("CHF", 138_05L + 489_15L)));
}
@Test
public void testVerkauf_Inland1() throws IOException
{
BankSLMPDFExctractor extractor = new BankSLMPDFExctractor(new Client())
{
@Override
protected String strip(File file) throws IOException
{
return from(file.getName());
}
};
List<Exception> errors = new ArrayList<>();
List<Item> results = extractor.extract(Arrays.asList(new File("BankSLM_Verkauf_Inland1.txt")), errors);
assertThat(errors, empty());
assertThat(results.size(), is(2));
new AssertImportActions().check(results, "CHF");
// check security
Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
assertThat(security.getWkn(), is("2489948"));
assertThat(security.getName(), is("UBS AG"));
// check buy sell transaction
Optional<Item> item = results.stream().filter(i -> i instanceof BuySellEntryItem).findFirst();
assertThat(item.isPresent(), is(true));
assertThat(item.get().getSubject(), instanceOf(BuySellEntry.class));
BuySellEntry entry = (BuySellEntry) item.get().getSubject();
assertThat(entry.getPortfolioTransaction().getType(), is(PortfolioTransaction.Type.SELL));
assertThat(entry.getAccountTransaction().getType(), is(AccountTransaction.Type.SELL));
assertThat(entry.getPortfolioTransaction().getMonetaryAmount(),
is(Money.of("CHF", Values.Amount.factorize(142359.40))));
assertThat(entry.getPortfolioTransaction().getDate(), is(LocalDate.parse("2013-08-22")));
assertThat(entry.getPortfolioTransaction().getShares(), is(Values.Share.factorize(7798)));
assertThat(entry.getPortfolioTransaction().getUnitSum(Unit.Type.FEE),
is(Money.of("CHF", 107_15L + 3_50L + 389_30L)));
}
@Test
public void testVerkauf_Ausland1() throws IOException
{
BankSLMPDFExctractor extractor = new BankSLMPDFExctractor(new Client())
{
@Override
protected String strip(File file) throws IOException
{
return from(file.getName());
}
};
List<Exception> errors = new ArrayList<>();
List<Item> results = extractor.extract(Arrays.asList(new File("BankSLM_Verkauf_Ausland1.txt")), errors);
assertThat(errors, empty());
assertThat(results.size(), is(2));
new AssertImportActions().check(results, "CHF");
// check security
Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
assertThat(security.getWkn(), is("472672"));
assertThat(security.getName(), is("Nokia Corp"));
// check buy sell transaction
Optional<Item> item = results.stream().filter(i -> i instanceof BuySellEntryItem).findFirst();
assertThat(item.isPresent(), is(true));
assertThat(item.get().getSubject(), instanceOf(BuySellEntry.class));
BuySellEntry entry = (BuySellEntry) item.get().getSubject();
assertThat(entry.getPortfolioTransaction().getType(), is(PortfolioTransaction.Type.SELL));
assertThat(entry.getAccountTransaction().getType(), is(AccountTransaction.Type.SELL));
assertThat(entry.getPortfolioTransaction().getMonetaryAmount(),
is(Money.of("CHF", Values.Amount.factorize(43180.00))));
assertThat(entry.getPortfolioTransaction().getDate(), is(LocalDate.parse("2013-01-24")));
assertThat(entry.getPortfolioTransaction().getShares(), is(Values.Share.factorize(11500)));
assertThat(entry.getPortfolioTransaction().getUnitSum(Unit.Type.FEE),
is(Money.of("CHF", 65_25L + 264_30L)));
}
@Test
public void testDividende_Inland1() throws IOException
{
BankSLMPDFExctractor extractor = new BankSLMPDFExctractor(new Client())
{
@Override
protected String strip(File file) throws IOException
{
return from(file.getName());
}
};
List<Exception> errors = new ArrayList<>();
List<Item> results = extractor.extract(Arrays.asList(new File("BankSLM_Dividende_Inland1.txt")), errors);
assertThat(errors, empty());
assertThat(results.size(), is(2));
new AssertImportActions().check(results, "CHF");
// check security
Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
assertThat(security.getWkn(), is("135186"));
assertThat(security.getName(), is("Bank SLM AG"));
// check transaction
Optional<Item> item = results.stream().filter(i -> i instanceof TransactionItem).findFirst();
assertThat(item.isPresent(), is(true));
assertThat(item.get().getSubject(), instanceOf(AccountTransaction.class));
AccountTransaction transaction = (AccountTransaction) item.get().getSubject();
assertThat(transaction.getType(), is(AccountTransaction.Type.DIVIDENDS));
assertThat(transaction.getSecurity(), is(security));
assertThat(transaction.getDate(), is(LocalDate.parse("2016-05-02")));
assertThat(transaction.getMonetaryAmount(), is(Money.of("CHF", 18_20L)));
assertThat(transaction.getUnitSum(Unit.Type.TAX), is(Money.of("CHF", 9_80L)));
assertThat(transaction.getGrossValue(), is(Money.of("CHF", 28_00L)));
assertThat(transaction.getShares(), is(Values.Share.factorize(1)));
}
@Test
public void testDividende_Ausland1() throws IOException
{
BankSLMPDFExctractor extractor = new BankSLMPDFExctractor(new Client())
{
@Override
protected String strip(File file) throws IOException
{
return from(file.getName());
}
};
List<Exception> errors = new ArrayList<>();
List<Item> results = extractor.extract(Arrays.asList(new File("BankSLM_Dividende_Ausland1.txt")), errors);
assertThat(errors, empty());
assertThat(results.size(), is(2));
new AssertImportActions().check(results, "CHF");
// check security
Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
assertThat(security.getWkn(), is("472672"));
assertThat(security.getName(), is("Nokia Corp"));
// check transaction
Optional<Item> item = results.stream().filter(i -> i instanceof TransactionItem).findFirst();
assertThat(item.isPresent(), is(true));
assertThat(item.get().getSubject(), instanceOf(AccountTransaction.class));
AccountTransaction transaction = (AccountTransaction) item.get().getSubject();
assertThat(transaction.getType(), is(AccountTransaction.Type.DIVIDENDS));
assertThat(transaction.getSecurity(), is(security));
assertThat(transaction.getDate(), is(LocalDate.parse("2016-07-05")));
assertThat(transaction.getMonetaryAmount(), is(Money.of("CHF", 3302_00L)));
assertThat(transaction.getUnitSum(Unit.Type.TAX), is(Money.of("CHF", 1415_14L)));
assertThat(transaction.getGrossValue(), is(Money.of("CHF", 4717_14L)));
assertThat(transaction.getShares(), is(Values.Share.factorize(17000)));
}
private String from(String resource)
{
try (Scanner scanner = new Scanner(getClass().getResourceAsStream(resource), StandardCharsets.UTF_8.name()))
{
return scanner.useDelimiter("\\A").next();
}
}
}