aboutsummaryrefslogtreecommitdiffstats
path: root/service/src/test/java
diff options
context:
space:
mode:
authorKostya <mail@sartin.in>2026-10-08 11:24:18 +0300
committerKostya <mail@sartin.in>2026-10-08 11:24:45 +0300
commit930eb953e8cb598a99002cb41f3b8f57d05f1fa7 (patch)
treeeab822cb5ce7eb4bc56908d944519c34981473a2 /service/src/test/java
parent1a8b75f96f0c6855581be3a38069f09eca37d1f5 (diff)
downloadguess-market-930eb953e8cb598a99002cb41f3b8f57d05f1fa7.tar.gz
guess-market-930eb953e8cb598a99002cb41f3b8f57d05f1fa7.tar.xz
guess-market-930eb953e8cb598a99002cb41f3b8f57d05f1fa7.zip
catalog: add v3 XML validator for cumulative uploads
Schema v3 drops event ids and users, and uploads now add to a live market instead of replacing it. Validate a v3 document against the Exercise 1 rules (commission 0-90%, positive b and d, non-negative initial inventory, a known trading method), require exactly two options per event, and treat the event name as its identity: reject names that repeat within the file or collide with an event already in the repository, ignoring case and surrounding whitespace. The validator works on the unmarshalled object graph only and never touches the repository beyond reading it, so a rejected upload leaves the market untouched.
Diffstat (limited to 'service/src/test/java')
-rw-r--r--service/src/test/java/market/guess/service/Ex3ValidatorTest.java171
1 files changed, 171 insertions, 0 deletions
diff --git a/service/src/test/java/market/guess/service/Ex3ValidatorTest.java b/service/src/test/java/market/guess/service/Ex3ValidatorTest.java
new file mode 100644
index 0000000..72417f5
--- /dev/null
+++ b/service/src/test/java/market/guess/service/Ex3ValidatorTest.java
@@ -0,0 +1,171 @@
1package market.guess.service;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertTrue;
5
6import jakarta.xml.bind.JAXBContext;
7import java.io.StringReader;
8import java.util.List;
9import market.guess.model.event.CommissionTiming;
10import market.guess.model.event.EventStatus;
11import market.guess.service.catalog.infrastructure.provider.v3.XMLValidatorV3;
12import market.guess.service.catalog.infrastructure.repository.InMemoryEventRepository;
13import market.guess.service.catalog.model.v3.GuessMarket;
14import market.guess.service.domain.Event;
15import market.guess.service.mechanism.LmsrTradingMechanism;
16import org.junit.jupiter.api.BeforeEach;
17import org.junit.jupiter.api.Test;
18
19class Ex3ValidatorTest {
20 private InMemoryEventRepository eventRepo;
21 private XMLValidatorV3 validator;
22
23 @BeforeEach
24 void setUp() {
25 eventRepo = new InMemoryEventRepository();
26 validator = new XMLValidatorV3();
27 }
28
29 static String event(String name, String commission, String options, String method) {
30 return "<GM-event name=\""
31 + name
32 + "\"><description>desc</description>"
33 + commission
34 + "<GM-options>"
35 + options
36 + "</GM-options><GM-method>"
37 + method
38 + "</GM-method></GM-event>";
39 }
40
41 static String lmsr(String name) {
42 return event(
43 name,
44 "<commission type=\"on-close\">5</commission>",
45 "<GM-option>Yes</GM-option><GM-option>No</GM-option>",
46 "<GM-LMSR><b>100</b></GM-LMSR>");
47 }
48
49 static String market(String... events) {
50 return "<Guess-Market><GM-events>" + String.join("", events) + "</GM-events></Guess-Market>";
51 }
52
53 private String validate(String xml) throws Exception {
54 var seed =
55 (GuessMarket)
56 JAXBContext.newInstance(GuessMarket.class)
57 .createUnmarshaller()
58 .unmarshal(new StringReader(xml));
59 var res = validator.validate(seed, eventRepo);
60 return res.isSuccess() ? "OK" : res.getMessage();
61 }
62
63 @Test
64 void acceptsLmsrAndOrderBookEvents() throws Exception {
65 var ob =
66 event(
67 "Cup",
68 "<commission type=\"on-purchase\">90</commission>",
69 "<GM-option>Argentina</GM-option><GM-option>Spain</GM-option>",
70 "<GM-order-book allow-mint=\"true\" initial=\"0\" d=\"1\"/>");
71 assertEquals("OK", validate(market(lmsr("Rain"), ob)));
72 }
73
74 @Test
75 void rejectsDuplicateNameWithinFile() throws Exception {
76 assertEquals("DUPLICATE_NAME", validate(market(lmsr("Rain"), lmsr(" rain "))));
77 }
78
79 @Test
80 void rejectsNameAlreadyInMarket() throws Exception {
81 eventRepo.add(
82 new Event(
83 "Rain",
84 1,
85 "Rain",
86 "desc",
87 0,
88 CommissionTiming.ON_CLOSE,
89 new LmsrTradingMechanism(100, 2),
90 EventStatus.DRAFT,
91 List.of(),
92 "alice"));
93 assertEquals("NAME_TAKEN", validate(market(lmsr("RAIN"))));
94 }
95
96 @Test
97 void rejectsNonBinaryEvents() throws Exception {
98 var three =
99 event(
100 "Three",
101 "<commission type=\"on-close\">5</commission>",
102 "<GM-option>A</GM-option><GM-option>B</GM-option><GM-option>C</GM-option>",
103 "<GM-LMSR><b>100</b></GM-LMSR>");
104 var one =
105 event(
106 "One",
107 "<commission type=\"on-close\">5</commission>",
108 "<GM-option>A</GM-option>",
109 "<GM-LMSR><b>100</b></GM-LMSR>");
110 assertEquals("INVALID_OPTIONS_COUNT", validate(market(three)));
111 assertEquals("INVALID_OPTIONS_COUNT", validate(market(one)));
112 }
113
114 @Test
115 void rejectsEx1RuleViolations() throws Exception {
116 var yesNo = "<GM-option>Yes</GM-option><GM-option>No</GM-option>";
117 var lmsrMethod = "<GM-LMSR><b>100</b></GM-LMSR>";
118 var onClose5 = "<commission type=\"on-close\">5</commission>";
119
120 assertEquals(
121 "COMMISSION_OUT_OF_RANGE",
122 validate(
123 market(
124 event("E", "<commission type=\"on-close\">91</commission>", yesNo, lmsrMethod))));
125 assertEquals(
126 "COMMISSION_OUT_OF_RANGE",
127 validate(
128 market(
129 event("E", "<commission type=\"on-close\">-1</commission>", yesNo, lmsrMethod))));
130 assertEquals(
131 "INVALID_COMMISSION_TYPE",
132 validate(
133 market(event("E", "<commission type=\"later\">5</commission>", yesNo, lmsrMethod))));
134 assertEquals(
135 "INVALID_LIQUIDITY",
136 validate(market(event("E", onClose5, yesNo, "<GM-LMSR><b>0</b></GM-LMSR>"))));
137 assertEquals(
138 "INVALID_DENOMINATOR",
139 validate(
140 market(
141 event(
142 "E",
143 onClose5,
144 yesNo,
145 "<GM-order-book allow-mint=\"true\" initial=\"0\" d=\"0\"/>"))));
146 assertEquals(
147 "INVALID_INITIAL",
148 validate(
149 market(
150 event(
151 "E",
152 onClose5,
153 yesNo,
154 "<GM-order-book allow-mint=\"true\" initial=\"-1\" d=\"1\"/>"))));
155 assertEquals(
156 "DUPLICATE_OPTION",
157 validate(
158 market(
159 event(
160 "E", onClose5, "<GM-option>Yes</GM-option><GM-option>yes</GM-option>", lmsrMethod))));
161 assertEquals("INVALID_METHOD", validate(market(event("E", onClose5, yesNo, ""))));
162 assertEquals("MISSING_NAME", validate(market(lmsr(" "))));
163 assertEquals("EMPTY_EVENTS", validate(market()));
164 }
165
166 @Test
167 void validationNeverMutatesTheMarket() throws Exception {
168 validate(market(lmsr("Rain")));
169 assertTrue(eventRepo.getAll().isEmpty());
170 }
171}