@Test
  public void testErtragsgutschrift() throws IOException {
    DeutscheBankPDFExctractor extractor =
        new DeutscheBankPDFExctractor(new Client()) {
          @Override
          String strip(File file) throws IOException {
            return from("DeutscheBankErtragsgutschrift.txt");
          }
        };
    List<Exception> errors = new ArrayList<Exception>();

    List<Item> results = extractor.extract(Arrays.asList(new File("t")), errors);

    assertThat(errors, empty());
    assertThat(results.size(), is(2));
    new AssertImportActions().check(results, CurrencyUnit.EUR);

    // check security
    Security security = assertSecurity(results);

    // 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("2014-12-15")));
    assertThat(transaction.getMonetaryAmount(), is(Money.of(CurrencyUnit.EUR, 1495L)));
    assertThat(transaction.getShares(), is(Values.Share.factorize(123)));
  }
  private AccountTransaction convertTo(AccountTransaction t, AccountTransaction.Type type) {
    AccountTransaction clone = new AccountTransaction();
    clone.setType(type);
    clone.setDate(t.getDate());
    clone.setCurrencyCode(t.getCurrencyCode());
    clone.setSecurity(null); // no security for REMOVAL or DEPOSIT
    clone.setAmount(t.getAmount());
    clone.setShares(t.getShares());

    // do *not* copy units as REMOVAL and DEPOSIT have never units
    return clone;
  }
  @Test
  public void testErtragsgutschrift2() throws IOException {
    DeutscheBankPDFExctractor extractor =
        new DeutscheBankPDFExctractor(new Client()) {
          @Override
          String strip(File file) throws IOException {
            return from(file.getName());
          }
        };
    List<Exception> errors = new ArrayList<Exception>();

    List<Item> results =
        extractor.extract(Arrays.asList(new File("DeutscheBankErtragsgutschrift2.txt")), errors);

    assertThat(errors, empty());
    assertThat(results.size(), is(2));
    new AssertImportActions().check(results, CurrencyUnit.EUR);

    // check security
    Security security =
        results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
    assertThat(security.getName(), is("ISHS-MSCI N. AMERIC.UCITS ETF BE.SH.(DT.ZT.)"));
    assertThat(security.getIsin(), is("DE000A0J2060"));
    assertThat(security.getWkn(), is("A0J206"));
    assertThat(security.getCurrencyCode(), is("USD"));

    // 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("2015-03-24")));
    assertThat(transaction.getMonetaryAmount(), is(Money.of(CurrencyUnit.EUR, 16_17L)));
    assertThat(transaction.getShares(), is(Values.Share.factorize(123)));

    Optional<Unit> grossValue = transaction.getUnit(Unit.Type.GROSS_VALUE);
    assertThat(grossValue.isPresent(), is(true));
    assertThat(grossValue.get().getAmount(), is(Money.of("EUR", 16_17L)));
    assertThat(grossValue.get().getForex(), is(Money.of("USD", 17_38L)));
    assertThat(
        grossValue.get().getExchangeRate().doubleValue(), IsCloseTo.closeTo(0.930578, 0.000001));
  }
  @Test
  public void testErtragsgutschrift7_USD_Freibetrag_nicht_ausgeschoepft() throws IOException {
    ConsorsbankPDFExctractor extractor = new ConsorsbankPDFExctractor(new Client());
    List<Exception> errors = new ArrayList<Exception>();
    URL url =
        FileLocator.resolve(
            getClass()
                .getResource(
                    "ConsorsbankErtragsgutschrift7_USD_Freibetrag_nicht_ausgeschoepft.pdf"));
    List<Item> results = extractor.extract(Arrays.asList(new File(url.getPath())), errors);

    assertThat(errors, empty());
    assertThat(results.size(), is(2));

    // check security
    Security security =
        results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
    assertThat(security.getWkn(), is("200417"));
    assertThat(security.getName(), is("ALTRIA GROUP INC."));

    // check dividend transaction
    AccountTransaction t =
        (AccountTransaction)
            results
                .stream()
                .filter(i -> i instanceof TransactionItem)
                .filter(
                    i ->
                        ((AccountTransaction) i.getSubject()).getType()
                            == AccountTransaction.Type.DIVIDENDS)
                .findFirst()
                .get()
                .getSubject();
    assertThat(t.getDate(), is(LocalDate.parse("2016-01-11")));
    assertThat(t.getShares(), is(Values.Share.factorize(650)));
    assertThat(t.getMonetaryAmount(), is(Money.of("EUR", 285_60)));
    assertThat(t.getUnit(Unit.Type.GROSS_VALUE).get().getForex(), is(Money.of("USD", 367_25)));

    // check tax
    assertThat(t.getUnitSum(Type.TAX), is(Money.of("EUR", 50_40))); // QUEST
  }
  private static void extractSecurityRelatedAccountTransactions(
      Account account, ReportingPeriod period, Map<Security, SecurityPerformanceRecord> records) {
    for (AccountTransaction t : account.getTransactions()) {
      if (t.getSecurity() == null) continue;

      if (!period.containsTransaction().test(t)) continue;

      if (t.getType() == AccountTransaction.Type.DIVIDENDS //
          || t.getType() == AccountTransaction.Type.INTEREST) {
        DividendTransaction dt = new DividendTransaction();
        dt.setDate(t.getDate());
        dt.setSecurity(t.getSecurity());
        dt.setAccount(account);
        dt.setCurrencyCode(t.getCurrencyCode());
        dt.setAmount(t.getAmount());
        dt.setShares(t.getShares());
        dt.setNote(t.getNote());
        records.get(t.getSecurity()).addTransaction(dt);
      } else if (t.getType() == AccountTransaction.Type.TAX_REFUND) {
        records.get(t.getSecurity()).addTransaction(t);
      }
    }
  }