Search in sources :

Example 1 with TransferMoneyContext2

use of org.qi4j.dci.moneytransfer.context.TransferMoneyContext2 in project qi4j-sdk by Qi4j.

the class TransferMoneyTest2 method transferHalfOfMoneyFromSavingsToChecking.

@Test
public void transferHalfOfMoneyFromSavingsToChecking() throws Exception {
    UnitOfWork uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("Transfer from savings to checking"));
    try {
        // Select source and destination
        BalanceData source = uow.get(BalanceData.class, SAVINGS_ACCOUNT_ID);
        BalanceData destination = uow.get(BalanceData.class, CHECKING_ACCOUNT_ID);
        // Instantiate context and execute enactments with that context
        TransferMoneyContext2 context = module.newTransient(TransferMoneyContext2.class).bind(source, destination);
        // Query for half the balance
        final Integer amountToTransfer = context.availableFunds() / 2;
        // Transfer from savings to checking
        context.transfer(amountToTransfer);
    } finally {
        uow.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) BalanceData(org.qi4j.dci.moneytransfer.domain.data.BalanceData) TransferMoneyContext2(org.qi4j.dci.moneytransfer.context.TransferMoneyContext2) Test(org.junit.Test)

Example 2 with TransferMoneyContext2

use of org.qi4j.dci.moneytransfer.context.TransferMoneyContext2 in project qi4j-sdk by Qi4j.

the class TransferMoneyTest2 method transferTwiceOfMoneyFromSavingsToChecking.

@Test(expected = IllegalArgumentException.class)
public void transferTwiceOfMoneyFromSavingsToChecking() throws Exception {
    UnitOfWork uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("Transfer from savings to checking"));
    try {
        // Select source and destination
        BalanceData source = uow.get(BalanceData.class, SAVINGS_ACCOUNT_ID);
        BalanceData destination = uow.get(BalanceData.class, CHECKING_ACCOUNT_ID);
        // Instantiate context and execute enactments with that context
        TransferMoneyContext2 context = module.newTransient(TransferMoneyContext2.class).bind(source, destination);
        // Query for double the balance
        final Integer amountToTransfer = context.availableFunds() * 2;
        // Transfer from savings to checking
        context.transfer(amountToTransfer);
    } finally {
        uow.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) BalanceData(org.qi4j.dci.moneytransfer.domain.data.BalanceData) TransferMoneyContext2(org.qi4j.dci.moneytransfer.context.TransferMoneyContext2) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)2 TransferMoneyContext2 (org.qi4j.dci.moneytransfer.context.TransferMoneyContext2)2 BalanceData (org.qi4j.dci.moneytransfer.domain.data.BalanceData)2