public void shouldListAccountEntries()

in component-test/src/main/java/org/apache/fineract/cn/accounting/TestAccount.java [310:388]


  public void shouldListAccountEntries() throws InterruptedException {
    final Ledger ledger = LedgerGenerator.createRandomLedger();

    this.testSubject.createLedger(ledger);

    this.eventRecorder.wait(EventConstants.POST_LEDGER, ledger.getIdentifier());

    final Account debtorAccount = AccountGenerator.createRandomAccount(ledger.getIdentifier());
    this.testSubject.createAccount(debtorAccount);

    this.eventRecorder.wait(EventConstants.POST_ACCOUNT, debtorAccount.getIdentifier());

    final Account creditorAccount = AccountGenerator.createRandomAccount(ledger.getIdentifier());
    this.testSubject.createAccount(creditorAccount);

    this.eventRecorder.wait(EventConstants.POST_ACCOUNT, creditorAccount.getIdentifier());

    final int journaEntryCount = 58;
    final List<JournalEntry> randomJournalEntries = Stream.generate(() -> JournalEntryGenerator.createRandomJournalEntry(debtorAccount, "50.00", creditorAccount, "50.00"))
        .limit(journaEntryCount)
        .collect(Collectors.toList());

    randomJournalEntries.stream()
        .map(randomJournalEntry -> {
            this.testSubject.createJournalEntry(randomJournalEntry);
            return randomJournalEntry.getTransactionIdentifier();
        })
        .forEach(transactionIdentifier -> {
          try {
            this.eventRecorder.wait(EventConstants.POST_JOURNAL_ENTRY, transactionIdentifier);
            this.eventRecorder.wait(EventConstants.RELEASE_JOURNAL_ENTRY, transactionIdentifier);
          }
          catch (final InterruptedException e) {
            throw new RuntimeException(e);
          }
        });

    Thread.sleep(300L); // Short pause to make sure it really is last.
    final JournalEntry lastRandomJournalEntry = JournalEntryGenerator.createRandomJournalEntry(debtorAccount, "50.00", creditorAccount, "50.00");
    this.testSubject.createJournalEntry(lastRandomJournalEntry);
    this.eventRecorder.wait(EventConstants.POST_JOURNAL_ENTRY, lastRandomJournalEntry.getTransactionIdentifier());
    this.eventRecorder.wait(EventConstants.RELEASE_JOURNAL_ENTRY, lastRandomJournalEntry.getTransactionIdentifier());

    final Set<String> journalEntryMessages
        = randomJournalEntries.stream().map(JournalEntry::getMessage).collect(Collectors.toSet());
    journalEntryMessages.add(lastRandomJournalEntry.getMessage());

    final LocalDate today = LocalDate.now(Clock.systemUTC());
    final String todayDateRange = new DateRange(today, today).toString();
    final List<AccountEntry> accountEntriesForward = this.testSubject.fetchAccountEntriesStream(creditorAccount.getIdentifier(),
        todayDateRange, null, "ASC")
        .collect(Collectors.toList());
    final Set<String> accountEntryMessages = accountEntriesForward.stream()
        .map(AccountEntry::getMessage)
        .collect(Collectors.toSet());

    Assert.assertEquals(journalEntryMessages, accountEntryMessages);
    Assert.assertEquals(journaEntryCount + 1, accountEntryMessages.size());

    final String oneMessage = accountEntryMessages.iterator().next();
    final List<AccountEntry> oneAccountEntry = this.testSubject.fetchAccountEntriesStream(creditorAccount.getIdentifier(),
        todayDateRange, oneMessage, "ASC").collect(Collectors.toList());
    Assert.assertEquals(1, oneAccountEntry.size());
    Assert.assertEquals(oneMessage, oneAccountEntry.get(0).getMessage());

    final List<AccountEntry> accountEntriesBackward = this.testSubject
        .fetchAccountEntriesStream(
            creditorAccount.getIdentifier(),
            todayDateRange,
            null, "DESC")
        .collect(Collectors.toList());

    final Optional<AccountEntry> lastAccountEntry = accountEntriesBackward.stream().findFirst();
    Assert.assertTrue(lastAccountEntry.isPresent());
    Assert.assertEquals(lastRandomJournalEntry.getMessage(), lastAccountEntry.get().getMessage());

    Collections.reverse(accountEntriesBackward);
    Assert.assertEquals(accountEntriesBackward, accountEntriesForward);
  }