diff options
| author | Kostya <mail@sartin.in> | 2026-10-08 11:24:18 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-08 11:24:45 +0300 |
| commit | 930eb953e8cb598a99002cb41f3b8f57d05f1fa7 (patch) | |
| tree | eab822cb5ce7eb4bc56908d944519c34981473a2 /service/src | |
| parent | 1a8b75f96f0c6855581be3a38069f09eca37d1f5 (diff) | |
| download | guess-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')
| -rw-r--r-- | service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLValidatorV3.java | 135 | ||||
| -rw-r--r-- | service/src/test/java/market/guess/service/Ex3ValidatorTest.java | 171 |
2 files changed, 306 insertions, 0 deletions
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLValidatorV3.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLValidatorV3.java new file mode 100644 index 0000000..3a76448 --- /dev/null +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLValidatorV3.java | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | package market.guess.service.catalog.infrastructure.provider.v3; | ||
| 2 | |||
| 3 | import java.util.HashSet; | ||
| 4 | import market.guess.api.Result; | ||
| 5 | import market.guess.service.catalog.infrastructure.repository.EventRepository; | ||
| 6 | import market.guess.service.catalog.model.v3.GMEvent; | ||
| 7 | import market.guess.service.catalog.model.v3.GMLMSR; | ||
| 8 | import market.guess.service.catalog.model.v3.GMOrderBook; | ||
| 9 | import market.guess.service.catalog.model.v3.GuessMarket; | ||
| 10 | |||
| 11 | public final class XMLValidatorV3 { | ||
| 12 | private static final int MIN_COMMISSION = 0; | ||
| 13 | private static final int MAX_COMMISSION = 90; | ||
| 14 | |||
| 15 | public Result<Void> validate(GuessMarket seed, EventRepository existing) { | ||
| 16 | if (seed == null || seed.getGMEvents() == null) { | ||
| 17 | return Result.error("INVALID_XML", "The XML file contains no GM-events element."); | ||
| 18 | } | ||
| 19 | |||
| 20 | var events = seed.getGMEvents().getGMEvent(); | ||
| 21 | if (events.isEmpty()) { | ||
| 22 | return Result.error("EMPTY_EVENTS", "The XML file does not contain any events."); | ||
| 23 | } | ||
| 24 | |||
| 25 | var seenNames = new HashSet<String>(); | ||
| 26 | |||
| 27 | for (GMEvent e : events) { | ||
| 28 | if (e.getName() == null || e.getName().isBlank()) { | ||
| 29 | return Result.error("MISSING_NAME", "An event has an empty name."); | ||
| 30 | } | ||
| 31 | var name = e.getName().trim(); | ||
| 32 | if (!seenNames.add(name.toLowerCase())) { | ||
| 33 | return Result.error( | ||
| 34 | "DUPLICATE_NAME", | ||
| 35 | "Event '" + name + "' appears more than once. Every event name must be unique."); | ||
| 36 | } | ||
| 37 | if (existing.getAll().stream().anyMatch(x -> x.getName().equalsIgnoreCase(name))) { | ||
| 38 | return Result.error( | ||
| 39 | "NAME_TAKEN", "An event named '" + name + "' already exists in the market."); | ||
| 40 | } | ||
| 41 | |||
| 42 | if (e.getDescription() == null || e.getDescription().isBlank()) { | ||
| 43 | return Result.error("MISSING_DESCRIPTION", "Event '" + name + "' has an empty description."); | ||
| 44 | } | ||
| 45 | |||
| 46 | if (e.getCommission() == null) { | ||
| 47 | return Result.error("MISSING_COMMISSION", "Event '" + name + "' is missing commission."); | ||
| 48 | } | ||
| 49 | var pct = e.getCommission().getValue(); | ||
| 50 | if (pct < MIN_COMMISSION || pct > MAX_COMMISSION) { | ||
| 51 | return Result.error( | ||
| 52 | "COMMISSION_OUT_OF_RANGE", | ||
| 53 | "Event '" | ||
| 54 | + name | ||
| 55 | + "' commission is " | ||
| 56 | + pct | ||
| 57 | + "%. It must be between " | ||
| 58 | + MIN_COMMISSION | ||
| 59 | + "% and " | ||
| 60 | + MAX_COMMISSION | ||
| 61 | + "%."); | ||
| 62 | } | ||
| 63 | |||
| 64 | var type = e.getCommission().getType(); | ||
| 65 | if (type == null | ||
| 66 | || (!type.equalsIgnoreCase("on-close") && !type.equalsIgnoreCase("on-purchase"))) { | ||
| 67 | return Result.error( | ||
| 68 | "INVALID_COMMISSION_TYPE", | ||
| 69 | "Event '" | ||
| 70 | + name | ||
| 71 | + "' has invalid commission type '" | ||
| 72 | + type | ||
| 73 | + "'. Must be 'on-close' or 'on-purchase'."); | ||
| 74 | } | ||
| 75 | |||
| 76 | if (e.getGMOptions() == null) { | ||
| 77 | return Result.error("MISSING_OPTIONS", "Event '" + name + "' is missing GM-options."); | ||
| 78 | } | ||
| 79 | var options = e.getGMOptions().getGMOption(); | ||
| 80 | if (options.size() != 2) { | ||
| 81 | return Result.error( | ||
| 82 | "INVALID_OPTIONS_COUNT", | ||
| 83 | "Event '" + name + "' must have exactly 2 options, but found " + options.size() + "."); | ||
| 84 | } | ||
| 85 | var opt0 = options.get(0) == null ? "" : options.get(0).trim(); | ||
| 86 | var opt1 = options.get(1) == null ? "" : options.get(1).trim(); | ||
| 87 | if (opt0.isEmpty() || opt1.isEmpty()) { | ||
| 88 | return Result.error("EMPTY_OPTION", "Event '" + name + "' has an empty option name."); | ||
| 89 | } | ||
| 90 | if (opt0.equalsIgnoreCase(opt1)) { | ||
| 91 | return Result.error( | ||
| 92 | "DUPLICATE_OPTION", "Event '" + name + "' has duplicate option names: '" + opt0 + "'."); | ||
| 93 | } | ||
| 94 | |||
| 95 | if (e.getGMMethod() == null) { | ||
| 96 | return Result.error("MISSING_METHOD", "Event '" + name + "' is missing GM-method."); | ||
| 97 | } | ||
| 98 | if (e.getGMMethod().getGMLMSR() instanceof GMLMSR lmsr) { | ||
| 99 | if (lmsr.getB() <= 0) { | ||
| 100 | return Result.error( | ||
| 101 | "INVALID_LIQUIDITY", | ||
| 102 | "Event '" | ||
| 103 | + name | ||
| 104 | + "' has invalid liquidity b=" | ||
| 105 | + lmsr.getB() | ||
| 106 | + ". b must be a positive integer (> 0)."); | ||
| 107 | } | ||
| 108 | } else if (e.getGMMethod().getGMOrderBook() instanceof GMOrderBook ob) { | ||
| 109 | if (ob.getD() <= 0) { | ||
| 110 | return Result.error( | ||
| 111 | "INVALID_DENOMINATOR", | ||
| 112 | "Event '" | ||
| 113 | + name | ||
| 114 | + "' has invalid denominator d=" | ||
| 115 | + ob.getD() | ||
| 116 | + ". d must be a positive integer (> 0)."); | ||
| 117 | } | ||
| 118 | if (ob.getInitial() < 0) { | ||
| 119 | return Result.error( | ||
| 120 | "INVALID_INITIAL", | ||
| 121 | "Event '" | ||
| 122 | + name | ||
| 123 | + "' has invalid initial inventory: " | ||
| 124 | + ob.getInitial() | ||
| 125 | + ". Must be >= 0."); | ||
| 126 | } | ||
| 127 | } else { | ||
| 128 | return Result.error( | ||
| 129 | "INVALID_METHOD", "Event '" + name + "' has unknown or missing trading method."); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | return Result.ok(); | ||
| 134 | } | ||
| 135 | } | ||
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 @@ | |||
| 1 | package market.guess.service; | ||
| 2 | |||
| 3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 4 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 5 | |||
| 6 | import jakarta.xml.bind.JAXBContext; | ||
| 7 | import java.io.StringReader; | ||
| 8 | import java.util.List; | ||
| 9 | import market.guess.model.event.CommissionTiming; | ||
| 10 | import market.guess.model.event.EventStatus; | ||
| 11 | import market.guess.service.catalog.infrastructure.provider.v3.XMLValidatorV3; | ||
| 12 | import market.guess.service.catalog.infrastructure.repository.InMemoryEventRepository; | ||
| 13 | import market.guess.service.catalog.model.v3.GuessMarket; | ||
| 14 | import market.guess.service.domain.Event; | ||
| 15 | import market.guess.service.mechanism.LmsrTradingMechanism; | ||
| 16 | import org.junit.jupiter.api.BeforeEach; | ||
| 17 | import org.junit.jupiter.api.Test; | ||
| 18 | |||
| 19 | class 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 | } | ||