|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * license agreements; and to You under the Apache License, version 2.0: |
| 4 | + * |
| 5 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * This file is part of the Apache Pekko project, which was derived from Akka. |
| 8 | + */ |
| 9 | + |
| 10 | +/* |
| 11 | + * Copyright (C) 2022 Lightbend Inc. <https://www.lightbend.com> |
| 12 | + */ |
| 13 | + |
| 14 | +package jdocs.org.apache.pekko.cluster.sharding.typed; |
| 15 | + |
| 16 | +import org.apache.pekko.Done; |
| 17 | +import org.scalatestplus.junit.JUnitSuite; |
| 18 | + |
| 19 | +import static jdocs.org.apache.pekko.cluster.sharding.typed.AccountExampleWithEventHandlersInState.AccountEntity; |
| 20 | +import static org.junit.Assert.*; |
| 21 | + |
| 22 | +// #test |
| 23 | +import java.math.BigDecimal; |
| 24 | + |
| 25 | +import org.apache.pekko.actor.testkit.typed.javadsl.BehaviorTestKit; |
| 26 | +import org.apache.pekko.actor.testkit.typed.javadsl.ReplyInbox; |
| 27 | +import org.apache.pekko.actor.testkit.typed.javadsl.StatusReplyInbox; |
| 28 | +import org.apache.pekko.persistence.testkit.javadsl.UnpersistentBehavior; |
| 29 | +import org.apache.pekko.persistence.testkit.javadsl.PersistenceEffect; |
| 30 | +import org.apache.pekko.persistence.typed.PersistenceId; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class AccountExampleUnpersistentDocTest |
| 34 | + // #test |
| 35 | + extends JUnitSuite |
| 36 | +// #test |
| 37 | +{ |
| 38 | + @Test |
| 39 | + public void createWithEmptyBalance() { |
| 40 | + UnpersistentBehavior<AccountEntity.Command, AccountEntity.Event, AccountEntity.Account> |
| 41 | + unpersistent = emptyAccount(); |
| 42 | + |
| 43 | + BehaviorTestKit<AccountEntity.Command> testkit = unpersistent.getBehaviorTestKit(); |
| 44 | + |
| 45 | + StatusReplyInbox<Done> ackInbox = testkit.runAskWithStatus(AccountEntity.CreateAccount::new); |
| 46 | + |
| 47 | + ackInbox.expectValue(Done.getInstance()); |
| 48 | + unpersistent.getEventProbe().expectPersisted(AccountEntity.AccountCreated.INSTANCE); |
| 49 | + |
| 50 | + // internal state is only exposed by the behavior via responses to messages or if it happens |
| 51 | + // to snapshot. This particular behavior never snapshots, so we query within the actor's |
| 52 | + // protocol |
| 53 | + assertFalse(unpersistent.getSnapshotProbe().hasEffects()); |
| 54 | + |
| 55 | + ReplyInbox<AccountEntity.CurrentBalance> currentBalanceInbox = |
| 56 | + testkit.runAsk(AccountEntity.GetBalance::new); |
| 57 | + |
| 58 | + assertEquals(BigDecimal.ZERO, currentBalanceInbox.receiveReply().balance); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void handleDepositAndWithdraw() { |
| 63 | + UnpersistentBehavior<AccountEntity.Command, AccountEntity.Event, AccountEntity.Account> |
| 64 | + unpersistent = openedAccount(); |
| 65 | + |
| 66 | + BehaviorTestKit<AccountEntity.Command> testkit = unpersistent.getBehaviorTestKit(); |
| 67 | + BigDecimal currentBalance; |
| 68 | + |
| 69 | + testkit |
| 70 | + .runAskWithStatus( |
| 71 | + Done.class, replyTo -> new AccountEntity.Deposit(BigDecimal.valueOf(100), replyTo)) |
| 72 | + .expectValue(Done.getInstance()); |
| 73 | + |
| 74 | + assertEquals( |
| 75 | + BigDecimal.valueOf(100), |
| 76 | + unpersistent |
| 77 | + .getEventProbe() |
| 78 | + .expectPersistedClass(AccountEntity.Deposited.class) |
| 79 | + .persistedObject() |
| 80 | + .amount); |
| 81 | + |
| 82 | + currentBalance = |
| 83 | + testkit |
| 84 | + .runAsk(AccountEntity.CurrentBalance.class, AccountEntity.GetBalance::new) |
| 85 | + .receiveReply() |
| 86 | + .balance; |
| 87 | + |
| 88 | + assertEquals(BigDecimal.valueOf(100), currentBalance); |
| 89 | + |
| 90 | + testkit |
| 91 | + .runAskWithStatus( |
| 92 | + Done.class, replyTo -> new AccountEntity.Withdraw(BigDecimal.valueOf(10), replyTo)) |
| 93 | + .expectValue(Done.getInstance()); |
| 94 | + |
| 95 | + // can save the persistence effect for in-depth inspection |
| 96 | + PersistenceEffect<AccountEntity.Withdrawn> withdrawEffect = |
| 97 | + unpersistent.getEventProbe().expectPersistedClass(AccountEntity.Withdrawn.class); |
| 98 | + assertEquals(BigDecimal.valueOf(10), withdrawEffect.persistedObject().amount); |
| 99 | + assertEquals(3L, withdrawEffect.sequenceNr()); |
| 100 | + assertTrue(withdrawEffect.tags().isEmpty()); |
| 101 | + |
| 102 | + currentBalance = |
| 103 | + testkit |
| 104 | + .runAsk(AccountEntity.CurrentBalance.class, AccountEntity.GetBalance::new) |
| 105 | + .receiveReply() |
| 106 | + .balance; |
| 107 | + |
| 108 | + assertEquals(BigDecimal.valueOf(90), currentBalance); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void rejectWithdrawOverdraft() { |
| 113 | + UnpersistentBehavior<AccountEntity.Command, AccountEntity.Event, AccountEntity.Account> |
| 114 | + unpersistent = accountWithBalance(BigDecimal.valueOf(100)); |
| 115 | + |
| 116 | + BehaviorTestKit<AccountEntity.Command> testkit = unpersistent.getBehaviorTestKit(); |
| 117 | + |
| 118 | + testkit |
| 119 | + .runAskWithStatus( |
| 120 | + Done.class, replyTo -> new AccountEntity.Withdraw(BigDecimal.valueOf(110), replyTo)) |
| 121 | + .expectErrorMessage("not enough funds to withdraw 110"); |
| 122 | + |
| 123 | + assertFalse(unpersistent.getEventProbe().hasEffects()); |
| 124 | + } |
| 125 | + |
| 126 | + // #test |
| 127 | + private UnpersistentBehavior<AccountEntity.Command, AccountEntity.Event, AccountEntity.Account> |
| 128 | + emptyAccount() { |
| 129 | + return |
| 130 | + // #unpersistent-behavior |
| 131 | + UnpersistentBehavior.fromEventSourced( |
| 132 | + AccountEntity.create("1", PersistenceId.of("Account", "1")), |
| 133 | + null, // use the initial state |
| 134 | + 0 // initial sequence number |
| 135 | + ); |
| 136 | + // #unpersistent-behavior |
| 137 | + } |
| 138 | + |
| 139 | + private UnpersistentBehavior<AccountEntity.Command, AccountEntity.Event, AccountEntity.Account> |
| 140 | + openedAccount() { |
| 141 | + return |
| 142 | + // #unpersistent-behavior-provided-state |
| 143 | + UnpersistentBehavior.fromEventSourced( |
| 144 | + AccountEntity.create("1", PersistenceId.of("Account", "1")), |
| 145 | + new AccountEntity.EmptyAccount() |
| 146 | + .openedAccount(), // duplicate the event handler for AccountCreated on an EmptyAccount |
| 147 | + 1 // assume that CreateAccount was the first command |
| 148 | + ); |
| 149 | + // #unpersistent-behavior-provided-state |
| 150 | + } |
| 151 | + |
| 152 | + private UnpersistentBehavior<AccountEntity.Command, AccountEntity.Event, AccountEntity.Account> |
| 153 | + accountWithBalance(BigDecimal balance) { |
| 154 | + return UnpersistentBehavior.fromEventSourced( |
| 155 | + AccountEntity.create("1", PersistenceId.of("Account", "1")), |
| 156 | + new AccountEntity.OpenedAccount(balance), |
| 157 | + 2); |
| 158 | + } |
| 159 | + // #test |
| 160 | +} |
| 161 | +// #test |
0 commit comments