aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/test/java')
-rw-r--r--server/src/test/java/market/guess/server/AccountTest.java112
1 files changed, 112 insertions, 0 deletions
diff --git a/server/src/test/java/market/guess/server/AccountTest.java b/server/src/test/java/market/guess/server/AccountTest.java
new file mode 100644
index 0000000..0e094b8
--- /dev/null
+++ b/server/src/test/java/market/guess/server/AccountTest.java
@@ -0,0 +1,112 @@
1package market.guess.server;
2
3import static market.guess.server.EventsTest.loggedIn;
4import static market.guess.server.EventsTest.path;
5import static market.guess.server.EventsTest.upload;
6import static org.junit.jupiter.api.Assertions.assertEquals;
7import static org.junit.jupiter.api.Assertions.assertFalse;
8import static org.junit.jupiter.api.Assertions.assertTrue;
9
10import com.google.gson.JsonObject;
11import com.google.gson.JsonParser;
12import java.math.BigDecimal;
13import java.net.http.HttpClient;
14import java.nio.file.Path;
15import org.junit.jupiter.api.AfterAll;
16import org.junit.jupiter.api.BeforeAll;
17import org.junit.jupiter.api.Test;
18import org.junit.jupiter.api.io.TempDir;
19
20class AccountTest {
21 @TempDir static Path base;
22 static TestServer server;
23
24 @BeforeAll
25 static void start() throws Exception {
26 server = new TestServer(base);
27 }
28
29 @AfterAll
30 static void stop() throws Exception {
31 server.close();
32 }
33
34 private static JsonObject account(HttpClient client) throws Exception {
35 var res = server.get(client, "/user/account");
36 assertEquals(200, res.statusCode(), res.body());
37 return JsonParser.parseString(res.body()).getAsJsonObject();
38 }
39
40 private static BigDecimal balance(HttpClient client) throws Exception {
41 return new BigDecimal(account(client).get("balance").getAsString());
42 }
43
44 private static JsonObject userRow(HttpClient client, String name) throws Exception {
45 for (var row : JsonParser.parseString(server.get(client, "/users").body()).getAsJsonArray()) {
46 if (row.getAsJsonObject().get("username").getAsString().equals(name)) {
47 return row.getAsJsonObject();
48 }
49 }
50 throw new AssertionError(name + " not listed");
51 }
52
53 @Test
54 void twoUsersTradeAnEventEndToEnd() throws Exception {
55 var maya = loggedIn(server, "Maya");
56 var tom = loggedIn(server, "Tom");
57 var event = "Mujtaba is Dead";
58 var yes = event + ":0";
59
60 var deposit = server.post(maya, "/user/deposit", "amount", "1000");
61 assertEquals(200, deposit.statusCode(), deposit.body());
62 assertEquals(new BigDecimal("1000.00"), balance(maya));
63 assertTrue(account(maya).toString().contains("DEPOSIT"));
64
65 assertEquals(200, upload(server, maya, "small.xml").statusCode());
66 assertTrue(userRow(tom, "Maya").get("marketMaker").getAsBoolean());
67 assertFalse(userRow(tom, "Tom").get("marketMaker").getAsBoolean());
68
69 assertEquals(200, server.post(maya, path(event) + "/open").statusCode());
70 var afterSubsidy = balance(maya);
71 assertTrue(afterSubsidy.compareTo(new BigDecimal("1000")) < 0, "LMSR subsidy was not paid");
72
73 server.post(tom, "/user/deposit", "amount", "100");
74 var order =
75 server.post(
76 tom, "/orders", "event", event, "option", yes, "side", "BUY", "price", "1", "quantity", "10");
77 assertEquals(200, order.statusCode(), order.body());
78 var afterBuy = balance(tom);
79 assertTrue(afterBuy.compareTo(new BigDecimal("100")) < 0);
80 assertEquals(afterSubsidy, balance(maya));
81
82 assertEquals(afterBuy.toPlainString(), userRow(maya, "Tom").get("balance").getAsString());
83
84 var resolve = server.post(maya, path(event) + "/resolve", "option", yes);
85 assertEquals(200, resolve.statusCode(), resolve.body());
86 assertEquals(afterBuy.add(new BigDecimal("10")), balance(tom));
87 }
88
89 @Test
90 void depositValidationAndLogin() throws Exception {
91 var ana = loggedIn(server, "Ana");
92 var bad = server.post(ana, "/user/deposit", "amount", "-5");
93 assertEquals(400, bad.statusCode());
94 assertTrue(bad.body().contains("INVALID_AMOUNT"), bad.body());
95 assertEquals(400, server.post(ana, "/user/deposit").statusCode());
96 assertEquals(BigDecimal.ZERO.setScale(2), balance(ana));
97
98 var anon = TestServer.client();
99 assertEquals(401, server.post(anon, "/user/deposit", "amount", "5").statusCode());
100 assertEquals(401, server.get(anon, "/user/account").statusCode());
101 assertEquals(401, server.get(anon, "/users").statusCode());
102 }
103
104 @Test
105 void directoryShowsWhoIsOnline() throws Exception {
106 var watcher = loggedIn(server, "Wes");
107 var leaver = loggedIn(server, "Lee");
108 assertTrue(userRow(watcher, "Lee").get("online").getAsBoolean());
109 server.post(leaver, "/logout");
110 assertFalse(userRow(watcher, "Lee").get("online").getAsBoolean());
111 }
112}