From 95eb4ff500defc4e34d043bbc096593ffcadecea Mon Sep 17 00:00:00 2001 From: Kostya Date: Thu, 8 Oct 2026 16:45:02 +0300 Subject: catalog: add cumulative v3 XML loader and event mapper Exercise 3 uploads arrive over HTTP and must never be written to the server's disk, so load Schema v3 straight from an InputStream. Each valid file is appended to the market rather than replacing it, and the uploading user becomes the Market Maker of every event in it. With no id in v3, the mapper keys events by their trimmed name, so the repository's case-insensitive lookup finds them by name. Display ids continue from the current market size. The whole file is validated and mapped before anything is added, so a rejected upload leaves the market as it was. Loading is synchronized so two concurrent uploads cannot both pass the name-collision check. DTDs and external entities are disabled on the parser, since the input is untrusted and an XXE payload could otherwise read server files. Check the loader against the course's Exercise 3 sample files, kept with their schema under test-data/ex-3/. --- .../java/market/guess/service/Ex3LoaderTest.java | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 service/src/test/java/market/guess/service/Ex3LoaderTest.java (limited to 'service/src/test/java/market/guess') diff --git a/service/src/test/java/market/guess/service/Ex3LoaderTest.java b/service/src/test/java/market/guess/service/Ex3LoaderTest.java new file mode 100644 index 0000000..745b1fd --- /dev/null +++ b/service/src/test/java/market/guess/service/Ex3LoaderTest.java @@ -0,0 +1,102 @@ +package market.guess.service; + +import static market.guess.service.Ex3ValidatorTest.lmsr; +import static market.guess.service.Ex3ValidatorTest.market; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import market.guess.model.event.EventStatus; +import market.guess.service.catalog.infrastructure.mapper.v3.EventMapperV3; +import market.guess.service.catalog.infrastructure.provider.v3.XMLLoaderV3; +import market.guess.service.catalog.infrastructure.provider.v3.XMLValidatorV3; +import market.guess.service.catalog.infrastructure.repository.InMemoryEventRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class Ex3LoaderTest { + private InMemoryEventRepository eventRepo; + private XMLLoaderV3 loader; + + @BeforeEach + void setUp() { + eventRepo = new InMemoryEventRepository(); + loader = new XMLLoaderV3(eventRepo, new EventMapperV3(), new XMLValidatorV3()); + } + + private static InputStream stream(String xml) { + return new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); + } + + @Test + void uploadsAccumulateAndUploaderBecomesMarketMaker() { + var first = loader.load(stream(market(lmsr("Rain"), lmsr("Snow"))), "a.xml", "alice"); + assertTrue(first.isSuccess(), first.getDetails()); + assertEquals(2, first.getData().eventsLoaded()); + + var second = loader.load(stream(market(lmsr("Hail"))), "b.xml", "bob"); + assertTrue(second.isSuccess(), second.getDetails()); + assertEquals(3, eventRepo.getAll().size()); + + var rain = eventRepo.get("rain").orElseThrow(); + assertEquals("Rain", rain.getName()); + assertEquals("alice", rain.getMarketMaker()); + assertEquals(EventStatus.DRAFT, rain.getState()); + assertEquals("bob", eventRepo.get("Hail").orElseThrow().getMarketMaker()); + } + + @Test + void loadsCourseSampleFilesCumulatively() throws Exception { + try (var small = Files.newInputStream(Path.of("test-data/ex-3/small.xml")); + var multiple = Files.newInputStream(Path.of("test-data/ex-3/multiple.xml"))) { + var first = loader.load(small, "small.xml", "alice"); + assertTrue(first.isSuccess(), first.getDetails()); + var second = loader.load(multiple, "multiple.xml", "bob"); + assertTrue(second.isSuccess(), second.getDetails()); + assertEquals(3, second.getData().eventsLoaded()); + } + assertEquals(4, eventRepo.getAll().size()); + assertEquals("alice", eventRepo.get("Mujtaba is Dead").orElseThrow().getMarketMaker()); + assertEquals("bob", eventRepo.get("World Cap Winner").orElseThrow().getMarketMaker()); + + try (var again = Files.newInputStream(Path.of("test-data/ex-3/small.xml"))) { + assertEquals("NAME_TAKEN", loader.load(again, "small.xml", "carol").getMessage()); + } + assertEquals(4, eventRepo.getAll().size()); + } + + @Test + void collisionRejectsWholeFileAndKeepsMarket() { + loader.load(stream(market(lmsr("Rain"))), "a.xml", "alice"); + + var res = loader.load(stream(market(lmsr("Fog"), lmsr("rain"))), "b.xml", "bob"); + assertFalse(res.isSuccess()); + assertEquals("NAME_TAKEN", res.getMessage()); + assertEquals(1, eventRepo.getAll().size()); + assertTrue(eventRepo.get("Fog").isEmpty()); + } + + @Test + void rejectsMalformedXmlAndMissingUploader() { + assertEquals( + "XML_PARSE_ERROR", loader.load(stream(""), "x.xml", "alice").getMessage()); + assertEquals( + "INVALID_UPLOADER", loader.load(stream(market(lmsr("Rain"))), "x.xml", " ").getMessage()); + assertTrue(eventRepo.getAll().isEmpty()); + } + + @Test + void refusesDtdsSoExternalEntitiesCannotBeRead() { + var xxe = + "]>" + + market(lmsr("&x;")); + var res = loader.load(stream(xxe), "x.xml", "alice"); + assertEquals("XML_PARSE_ERROR", res.getMessage()); + assertTrue(eventRepo.getAll().isEmpty()); + } +} -- cgit v1.2.3