From 930eb953e8cb598a99002cb41f3b8f57d05f1fa7 Mon Sep 17 00:00:00 2001 From: Kostya Date: Thu, 8 Oct 2026 11:24:18 +0300 Subject: 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. --- .../market/guess/service/Ex3ValidatorTest.java | 171 +++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 service/src/test/java/market/guess/service/Ex3ValidatorTest.java (limited to 'service/src/test/java/market') 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 @@ +package market.guess.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import jakarta.xml.bind.JAXBContext; +import java.io.StringReader; +import java.util.List; +import market.guess.model.event.CommissionTiming; +import market.guess.model.event.EventStatus; +import market.guess.service.catalog.infrastructure.provider.v3.XMLValidatorV3; +import market.guess.service.catalog.infrastructure.repository.InMemoryEventRepository; +import market.guess.service.catalog.model.v3.GuessMarket; +import market.guess.service.domain.Event; +import market.guess.service.mechanism.LmsrTradingMechanism; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class Ex3ValidatorTest { + private InMemoryEventRepository eventRepo; + private XMLValidatorV3 validator; + + @BeforeEach + void setUp() { + eventRepo = new InMemoryEventRepository(); + validator = new XMLValidatorV3(); + } + + static String event(String name, String commission, String options, String method) { + return "desc" + + commission + + "" + + options + + "" + + method + + ""; + } + + static String lmsr(String name) { + return event( + name, + "5", + "YesNo", + "100"); + } + + static String market(String... events) { + return "" + String.join("", events) + ""; + } + + private String validate(String xml) throws Exception { + var seed = + (GuessMarket) + JAXBContext.newInstance(GuessMarket.class) + .createUnmarshaller() + .unmarshal(new StringReader(xml)); + var res = validator.validate(seed, eventRepo); + return res.isSuccess() ? "OK" : res.getMessage(); + } + + @Test + void acceptsLmsrAndOrderBookEvents() throws Exception { + var ob = + event( + "Cup", + "90", + "ArgentinaSpain", + ""); + assertEquals("OK", validate(market(lmsr("Rain"), ob))); + } + + @Test + void rejectsDuplicateNameWithinFile() throws Exception { + assertEquals("DUPLICATE_NAME", validate(market(lmsr("Rain"), lmsr(" rain ")))); + } + + @Test + void rejectsNameAlreadyInMarket() throws Exception { + eventRepo.add( + new Event( + "Rain", + 1, + "Rain", + "desc", + 0, + CommissionTiming.ON_CLOSE, + new LmsrTradingMechanism(100, 2), + EventStatus.DRAFT, + List.of(), + "alice")); + assertEquals("NAME_TAKEN", validate(market(lmsr("RAIN")))); + } + + @Test + void rejectsNonBinaryEvents() throws Exception { + var three = + event( + "Three", + "5", + "ABC", + "100"); + var one = + event( + "One", + "5", + "A", + "100"); + assertEquals("INVALID_OPTIONS_COUNT", validate(market(three))); + assertEquals("INVALID_OPTIONS_COUNT", validate(market(one))); + } + + @Test + void rejectsEx1RuleViolations() throws Exception { + var yesNo = "YesNo"; + var lmsrMethod = "100"; + var onClose5 = "5"; + + assertEquals( + "COMMISSION_OUT_OF_RANGE", + validate( + market( + event("E", "91", yesNo, lmsrMethod)))); + assertEquals( + "COMMISSION_OUT_OF_RANGE", + validate( + market( + event("E", "-1", yesNo, lmsrMethod)))); + assertEquals( + "INVALID_COMMISSION_TYPE", + validate( + market(event("E", "5", yesNo, lmsrMethod)))); + assertEquals( + "INVALID_LIQUIDITY", + validate(market(event("E", onClose5, yesNo, "0")))); + assertEquals( + "INVALID_DENOMINATOR", + validate( + market( + event( + "E", + onClose5, + yesNo, + "")))); + assertEquals( + "INVALID_INITIAL", + validate( + market( + event( + "E", + onClose5, + yesNo, + "")))); + assertEquals( + "DUPLICATE_OPTION", + validate( + market( + event( + "E", onClose5, "Yesyes", lmsrMethod)))); + assertEquals("INVALID_METHOD", validate(market(event("E", onClose5, yesNo, "")))); + assertEquals("MISSING_NAME", validate(market(lmsr(" ")))); + assertEquals("EMPTY_EVENTS", validate(market())); + } + + @Test + void validationNeverMutatesTheMarket() throws Exception { + validate(market(lmsr("Rain"))); + assertTrue(eventRepo.getAll().isEmpty()); + } +} -- cgit v1.2.3