This is an automated email from the ASF dual-hosted git repository. myrle pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/fineract-cn-accounting.git
commit 49af79c4fd4ecb36bd7327182237e92fa599e3eb Author: Myrle Krantz <[email protected]> AuthorDate: Tue Sep 19 11:01:22 2017 +0200 Adding streaming of ledger pages of accounts. --- .../accounting/api/v1/client/LedgerManager.java | 49 ++++++++++++++-------- .../io/mifos/accounting/api/v1/domain/Account.java | 5 ++- .../main/java/io/mifos/accounting/TestLedger.java | 29 +++++++++++++ 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/api/src/main/java/io/mifos/accounting/api/v1/client/LedgerManager.java b/api/src/main/java/io/mifos/accounting/api/v1/client/LedgerManager.java index 0848559..34f7290 100644 --- a/api/src/main/java/io/mifos/accounting/api/v1/client/LedgerManager.java +++ b/api/src/main/java/io/mifos/accounting/api/v1/client/LedgerManager.java @@ -15,29 +15,14 @@ */ package io.mifos.accounting.api.v1.client; -import io.mifos.accounting.api.v1.domain.Account; -import io.mifos.accounting.api.v1.domain.AccountCommand; -import io.mifos.accounting.api.v1.domain.AccountEntry; -import io.mifos.accounting.api.v1.domain.AccountEntryPage; -import io.mifos.accounting.api.v1.domain.AccountPage; -import io.mifos.accounting.api.v1.domain.ChartOfAccountEntry; -import io.mifos.accounting.api.v1.domain.JournalEntry; -import io.mifos.accounting.api.v1.domain.Ledger; -import io.mifos.accounting.api.v1.domain.LedgerPage; -import io.mifos.accounting.api.v1.domain.TransactionType; -import io.mifos.accounting.api.v1.domain.TransactionTypePage; -import io.mifos.accounting.api.v1.domain.TrialBalance; +import io.mifos.accounting.api.v1.domain.*; import io.mifos.core.api.annotation.ThrowsException; import io.mifos.core.api.annotation.ThrowsExceptions; import io.mifos.core.api.util.CustomFeignClientsConfiguration; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.math.BigDecimal; @@ -208,6 +193,36 @@ public interface LedgerManager { // These helper functions are implemented here rather than in the client because it is easier to test // and mock if it's part of the accounting interface, rather than part of the client calling it. + default Stream<Account> streamAccountsOfLedger( + final String ledgerIdentifer, + final String sortDirection) { + final AccountPage firstPage = this.fetchAccountsOfLedger( + ledgerIdentifer, + 0, + 10, + null, + null); + final Integer pageCount = firstPage.getTotalPages(); + switch (sortDirection) { + case "ASC": + // Sort column is always date and order always ascending so that the order and adjacency of account + // entries is always stable. This has the advantage that the set of account entries included in the + // stream is set the moment the first call to fetchAccountEntries (above) is made. + return Stream.iterate(0, (i) -> i + 1).limit(pageCount) + .map(i -> this.fetchAccountsOfLedger(ledgerIdentifer, i, 10, "lastModifiedOn", "ASC")) + .flatMap(pageI -> pageI.getAccounts().stream()); + case "DESC": + return Stream.iterate(pageCount - 1, (i) -> i - 1).limit(pageCount) + .map(i -> this.fetchAccountsOfLedger(ledgerIdentifer, i, 10, "lastModifiedOn", "DESC")) + .flatMap(pageI -> { + Collections.reverse(pageI.getAccounts()); + return pageI.getAccounts().stream(); + }); + default: + throw new IllegalArgumentException(); + } + } + default Stream<AccountEntry> fetchAccountEntriesStream( final String accountIdentifier, final String dateRange, diff --git a/api/src/main/java/io/mifos/accounting/api/v1/domain/Account.java b/api/src/main/java/io/mifos/accounting/api/v1/domain/Account.java index dbe7720..b3c466a 100644 --- a/api/src/main/java/io/mifos/accounting/api/v1/domain/Account.java +++ b/api/src/main/java/io/mifos/accounting/api/v1/domain/Account.java @@ -118,7 +118,10 @@ public final class Account { } public void setState(final String state) { - this.state = State.valueOf(state); + if (state == null) + this.state = null; + else + this.state = State.valueOf(state); } public String getCreatedOn() { diff --git a/component-test/src/main/java/io/mifos/accounting/TestLedger.java b/component-test/src/main/java/io/mifos/accounting/TestLedger.java index 67e8ee9..c8654c5 100644 --- a/component-test/src/main/java/io/mifos/accounting/TestLedger.java +++ b/component-test/src/main/java/io/mifos/accounting/TestLedger.java @@ -20,6 +20,7 @@ import io.mifos.accounting.api.v1.client.LedgerAlreadyExistsException; import io.mifos.accounting.api.v1.client.LedgerNotFoundException; import io.mifos.accounting.api.v1.client.LedgerReferenceExistsException; import io.mifos.accounting.api.v1.domain.Account; +import io.mifos.accounting.api.v1.domain.AccountType; import io.mifos.accounting.api.v1.domain.Ledger; import io.mifos.accounting.api.v1.domain.LedgerPage; import io.mifos.accounting.util.AccountGenerator; @@ -29,6 +30,9 @@ import org.junit.Assert; import org.junit.Test; import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class TestLedger extends AbstractAccountingTest { @Test @@ -328,4 +332,29 @@ public class TestLedger extends AbstractAccountingTest { Assert.assertFalse(failed); } + + @Test + public void shouldStreamAllAccountsBelongingToLedger() throws InterruptedException { + final Ledger assetLedger = LedgerGenerator.createRandomLedger(); + assetLedger.setType(AccountType.ASSET.name()); + this.testSubject.createLedger(assetLedger); + this.eventRecorder.wait(EventConstants.POST_LEDGER, assetLedger.getIdentifier()); + + final List<Account> createdAssetAccounts = Stream.generate(() -> AccountGenerator.createRandomAccount(assetLedger.getIdentifier())).limit(1) + .peek(account -> { + account.setType(AccountType.ASSET.name()); + this.testSubject.createAccount(account); + }) + .collect(Collectors.toList()); + + for (final Account account : createdAssetAccounts) { + this.eventRecorder.wait(EventConstants.POST_ACCOUNT, account.getIdentifier()); + } + + final List<Account> foundAccounts = testSubject.streamAccountsOfLedger(assetLedger.getIdentifier(), "ASC") + .peek(account -> account.setState(null)) + .collect(Collectors.toList()); + + Assert.assertEquals(createdAssetAccounts, foundAccounts); + } } -- To stop receiving notification emails like this one, please contact [email protected].
