aboutsummaryrefslogtreecommitdiffstats
path: root/service/src/test/java/market
diff options
context:
space:
mode:
authorKostya <mail@sartin.in>2026-10-09 10:18:41 +0300
committerKostya <mail@sartin.in>2026-10-09 10:19:12 +0300
commit246c1ab5ffce1a1bb3c0534868b29f6e4c862a62 (patch)
tree2418952c9844205d7f003b130f39fc1738a952f1 /service/src/test/java/market
parent95eb4ff500defc4e34d043bbc096593ffcadecea (diff)
downloadguess-market-246c1ab5ffce1a1bb3c0534868b29f6e4c862a62.tar.gz
guess-market-246c1ab5ffce1a1bb3c0534868b29f6e4c862a62.tar.xz
guess-market-246c1ab5ffce1a1bb3c0534868b29f6e4c862a62.zip
account: let users deposit cash into their account
Exercise 3 replaces the fixed initial cash of Exercise 2 with accounts that users top up themselves. Add AccountContext.deposit, which credits a DEPOSIT entry through LedgerContext so it carries a timestamp and a running balance like every other ledger movement, and return the updated account. The amount crosses the api boundary as a string, like all money values. Reject anything that is not a positive number with at most two decimal places instead of rounding it, so a user is never credited a different amount than they asked for. A deposit can bring a negative account back above zero, which lifts the block.
Diffstat (limited to 'service/src/test/java/market')
-rw-r--r--service/src/test/java/market/guess/service/DepositTest.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/service/src/test/java/market/guess/service/DepositTest.java b/service/src/test/java/market/guess/service/DepositTest.java
new file mode 100644
index 0000000..048e07d
--- /dev/null
+++ b/service/src/test/java/market/guess/service/DepositTest.java
@@ -0,0 +1,60 @@
1package market.guess.service;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertFalse;
5import static org.junit.jupiter.api.Assertions.assertTrue;
6
7import java.time.Clock;
8import market.guess.model.ledger.LedgerType;
9import market.guess.service.catalog.infrastructure.repository.InMemoryUserRepository;
10import market.guess.service.domain.User;
11import market.guess.service.ledger.LedgerContext;
12import market.guess.service.ledger.LocalAccountContext;
13import org.junit.jupiter.api.BeforeEach;
14import org.junit.jupiter.api.Test;
15
16class DepositTest {
17 private InMemoryUserRepository userRepo;
18 private LocalAccountContext accounts;
19
20 @BeforeEach
21 void setUp() {
22 userRepo = new InMemoryUserRepository();
23 userRepo.add(new User("alice", 100));
24 accounts = new LocalAccountContext(userRepo, new LedgerContext(userRepo, Clock.systemUTC()));
25 }
26
27 @Test
28 void depositCreditsBalanceAndRecordsEntry() {
29 var res = accounts.deposit("Alice", "250.5");
30 assertTrue(res.isSuccess(), res.getDetails());
31 assertEquals("350.50", res.getData().balance());
32
33 var entry = res.getData().entries().getLast();
34 assertEquals(LedgerType.DEPOSIT, entry.type());
35 assertEquals("250.50", entry.amount());
36 assertEquals("350.50", entry.balanceAfter());
37 }
38
39 @Test
40 void depositUnblocksNegativeAccount() {
41 var ledger = new LedgerContext(userRepo, Clock.systemUTC());
42 ledger.debit("alice", LedgerType.COMMISSION, new java.math.BigDecimal("150"), "fee");
43 assertTrue(userRepo.get("alice").orElseThrow().isBlocked());
44
45 accounts.deposit("alice", "60");
46 assertFalse(userRepo.get("alice").orElseThrow().isBlocked());
47 }
48
49 @Test
50 void rejectsInvalidDeposits() {
51 for (var bad : new String[] {"0", "-5", "abc", "", "1.005", null}) {
52 var res = accounts.deposit("alice", bad);
53 assertFalse(res.isSuccess(), "should reject: " + bad);
54 assertEquals("INVALID_AMOUNT", res.getMessage());
55 }
56 assertEquals("ACCOUNT_NOT_FOUND", accounts.deposit("nobody", "10").getMessage());
57 assertEquals("100.00", accounts.getAccount("alice").getData().balance());
58 assertTrue(accounts.getAccount("alice").getData().entries().isEmpty());
59 }
60}