diff options
| author | Kostya <mail@sartin.in> | 2026-10-09 15:32:09 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-09 15:32:33 +0300 |
| commit | 9c1a10712a724b448de9e066ff76f97059c86b40 (patch) | |
| tree | bae467b5c29c32181e98cf13e7d5de0f155ba0e1 /service/src/test/java/market/guess | |
| parent | 9a62e94261e6531c6b60d296322e0cef2a5b21e1 (diff) | |
| download | guess-market-9c1a10712a724b448de9e066ff76f97059c86b40.tar.gz guess-market-9c1a10712a724b448de9e066ff76f97059c86b40.tar.xz guess-market-9c1a10712a724b448de9e066ff76f97059c86b40.zip | |
catalog: make Loader a stream-based strategy
Loader was tied to the disk: load(Path) did its own file checks and
opening, so the v3 loader, which reads HTTP uploads, could not
implement it, and the server had to hand its catalog a stub loader
that refused everything.
Reduce Loader to a single strategy method, load(InputStream, source,
owner). The v1, v2 and v3 loaders are now interchangeable
implementations, picked by wiring: v1 for the console, v2 for the
desktop, v3 for the server. The owner is the uploading user; v1 and v2
ignore it because their files name their own Market Makers.
Opening a file by path is now LocalCatalogContext.loadEvents' job,
with the same INVALID_PATH, FILE_NOT_FOUND and NOT_XML errors as
before. The server catalog uses the v3 loader directly. A path load
there passes no uploader, which v3 refuses, so nothing reaches the
server's market except an upload.
Drop Loader.seed, which every loader implemented by throwing and
nothing called.
Diffstat (limited to 'service/src/test/java/market/guess')
| -rw-r--r-- | service/src/test/java/market/guess/service/Ex2LoaderTest.java | 25 | ||||
| -rw-r--r-- | service/src/test/java/market/guess/service/Ex3LoaderTest.java | 14 |
2 files changed, 28 insertions, 11 deletions
diff --git a/service/src/test/java/market/guess/service/Ex2LoaderTest.java b/service/src/test/java/market/guess/service/Ex2LoaderTest.java index d2da8e0..978e4bf 100644 --- a/service/src/test/java/market/guess/service/Ex2LoaderTest.java +++ b/service/src/test/java/market/guess/service/Ex2LoaderTest.java | |||
| @@ -7,6 +7,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; | |||
| 7 | 7 | ||
| 8 | import java.nio.file.Files; | 8 | import java.nio.file.Files; |
| 9 | import java.nio.file.Path; | 9 | import java.nio.file.Path; |
| 10 | import market.guess.service.catalog.LocalCatalogContext; | ||
| 11 | import market.guess.service.catalog.infrastructure.MarketContext; | ||
| 10 | import market.guess.service.catalog.infrastructure.mapper.v2.EventMapperV2; | 12 | import market.guess.service.catalog.infrastructure.mapper.v2.EventMapperV2; |
| 11 | import market.guess.service.catalog.infrastructure.provider.v2.XMLLoaderV2; | 13 | import market.guess.service.catalog.infrastructure.provider.v2.XMLLoaderV2; |
| 12 | import market.guess.service.catalog.infrastructure.provider.v2.XMLValidatorV2; | 14 | import market.guess.service.catalog.infrastructure.provider.v2.XMLValidatorV2; |
| @@ -19,19 +21,20 @@ import org.junit.jupiter.api.io.TempDir; | |||
| 19 | class Ex2LoaderTest { | 21 | class Ex2LoaderTest { |
| 20 | private InMemoryEventRepository eventRepo; | 22 | private InMemoryEventRepository eventRepo; |
| 21 | private InMemoryUserRepository userRepo; | 23 | private InMemoryUserRepository userRepo; |
| 22 | private XMLLoaderV2 loader; | 24 | private LocalCatalogContext catalog; |
| 23 | 25 | ||
| 24 | @BeforeEach | 26 | @BeforeEach |
| 25 | void setUp() { | 27 | void setUp() { |
| 26 | eventRepo = new InMemoryEventRepository(); | 28 | eventRepo = new InMemoryEventRepository(); |
| 27 | userRepo = new InMemoryUserRepository(); | 29 | userRepo = new InMemoryUserRepository(); |
| 28 | loader = new XMLLoaderV2(eventRepo, userRepo, new EventMapperV2(), new XMLValidatorV2()); | 30 | var loader = new XMLLoaderV2(eventRepo, userRepo, new EventMapperV2(), new XMLValidatorV2()); |
| 31 | catalog = new LocalCatalogContext(loader, new MarketContext(eventRepo, userRepo), eventRepo); | ||
| 29 | } | 32 | } |
| 30 | 33 | ||
| 31 | @Test | 34 | @Test |
| 32 | void testLoadSmallXml() { | 35 | void testLoadSmallXml() { |
| 33 | var path = Path.of("test-data/ex-2/small.xml"); | 36 | var path = Path.of("test-data/ex-2/small.xml"); |
| 34 | var res = loader.load(path); | 37 | var res = catalog.loadEvents(path); |
| 35 | assertTrue(res.isSuccess(), "Loading small.xml should succeed"); | 38 | assertTrue(res.isSuccess(), "Loading small.xml should succeed"); |
| 36 | assertEquals(2, res.getData().eventsLoaded()); | 39 | assertEquals(2, res.getData().eventsLoaded()); |
| 37 | assertEquals(2, eventRepo.getAll().size()); | 40 | assertEquals(2, eventRepo.getAll().size()); |
| @@ -53,7 +56,7 @@ class Ex2LoaderTest { | |||
| 53 | @Test | 56 | @Test |
| 54 | void testLoadMultipleXml() { | 57 | void testLoadMultipleXml() { |
| 55 | var path = Path.of("test-data/ex-2/multiple.xml"); | 58 | var path = Path.of("test-data/ex-2/multiple.xml"); |
| 56 | var res = loader.load(path); | 59 | var res = catalog.loadEvents(path); |
| 57 | assertTrue(res.isSuccess(), "Loading multiple.xml should succeed"); | 60 | assertTrue(res.isSuccess(), "Loading multiple.xml should succeed"); |
| 58 | assertEquals(4, res.getData().eventsLoaded()); | 61 | assertEquals(4, res.getData().eventsLoaded()); |
| 59 | assertEquals(4, eventRepo.getAll().size()); | 62 | assertEquals(4, eventRepo.getAll().size()); |
| @@ -63,7 +66,7 @@ class Ex2LoaderTest { | |||
| 63 | @Test | 66 | @Test |
| 64 | void testError2XmlFailsOnZeroInitialCash() { | 67 | void testError2XmlFailsOnZeroInitialCash() { |
| 65 | var path = Path.of("test-data/ex-2/error-2.xml"); | 68 | var path = Path.of("test-data/ex-2/error-2.xml"); |
| 66 | var res = loader.load(path); | 69 | var res = catalog.loadEvents(path); |
| 67 | assertFalse(res.isSuccess(), "error-2.xml must fail validation"); | 70 | assertFalse(res.isSuccess(), "error-2.xml must fail validation"); |
| 68 | assertTrue( | 71 | assertTrue( |
| 69 | res.getMessage().contains("initial cash") || res.getDetails().contains("initial cash"), | 72 | res.getMessage().contains("initial cash") || res.getDetails().contains("initial cash"), |
| @@ -73,7 +76,7 @@ class Ex2LoaderTest { | |||
| 73 | @Test | 76 | @Test |
| 74 | void testError3XmlFailsOnUnknownEventId() { | 77 | void testError3XmlFailsOnUnknownEventId() { |
| 75 | var path = Path.of("test-data/ex-2/error-3.xml"); | 78 | var path = Path.of("test-data/ex-2/error-3.xml"); |
| 76 | var res = loader.load(path); | 79 | var res = catalog.loadEvents(path); |
| 77 | assertFalse(res.isSuccess(), "error-3.xml must fail validation"); | 80 | assertFalse(res.isSuccess(), "error-3.xml must fail validation"); |
| 78 | assertTrue( | 81 | assertTrue( |
| 79 | res.getMessage().contains("12") || res.getDetails().contains("12"), | 82 | res.getMessage().contains("12") || res.getDetails().contains("12"), |
| @@ -83,14 +86,14 @@ class Ex2LoaderTest { | |||
| 83 | @Test | 86 | @Test |
| 84 | void testEventWithTwoMarketMakersFails(@TempDir Path dir) throws Exception { | 87 | void testEventWithTwoMarketMakersFails(@TempDir Path dir) throws Exception { |
| 85 | // Avrum now also claims event 1, which Tikva already makes; event 2 is left without one. | 88 | // Avrum now also claims event 1, which Tikva already makes; event 2 is left without one. |
| 86 | var res = loader.load(smallXmlWith(dir, "<event id=\"2\"/>", "<event id=\"1\"/>")); | 89 | var res = catalog.loadEvents(smallXmlWith(dir, "<event id=\"2\"/>", "<event id=\"1\"/>")); |
| 87 | assertFalse(res.isSuccess()); | 90 | assertFalse(res.isSuccess()); |
| 88 | assertTrue(res.getDetails().contains("more than one market maker"), res.getDetails()); | 91 | assertTrue(res.getDetails().contains("more than one market maker"), res.getDetails()); |
| 89 | } | 92 | } |
| 90 | 93 | ||
| 91 | @Test | 94 | @Test |
| 92 | void testEventWithoutMarketMakerFails(@TempDir Path dir) throws Exception { | 95 | void testEventWithoutMarketMakerFails(@TempDir Path dir) throws Exception { |
| 93 | var res = loader.load(smallXmlWith(dir, "<event id=\"2\"/>", "")); | 96 | var res = catalog.loadEvents(smallXmlWith(dir, "<event id=\"2\"/>", "")); |
| 94 | assertFalse(res.isSuccess()); | 97 | assertFalse(res.isSuccess()); |
| 95 | assertTrue(res.getDetails().contains("Event ID 2 has no market maker"), res.getDetails()); | 98 | assertTrue(res.getDetails().contains("Event ID 2 has no market maker"), res.getDetails()); |
| 96 | } | 99 | } |
| @@ -117,8 +120,8 @@ class Ex2LoaderTest { | |||
| 117 | {"ex2-event-two-mms.xml", "DUPLICATE_MM"}, | 120 | {"ex2-event-two-mms.xml", "DUPLICATE_MM"}, |
| 118 | }; | 121 | }; |
| 119 | for (var c : cases) { | 122 | for (var c : cases) { |
| 120 | assertTrue(loader.load(Path.of("test-data/ex-2/small.xml")).isSuccess()); | 123 | assertTrue(catalog.loadEvents(Path.of("test-data/ex-2/small.xml")).isSuccess()); |
| 121 | var res = loader.load(Path.of("test-data/ex-2/invalid", c[0])); | 124 | var res = catalog.loadEvents(Path.of("test-data/ex-2/invalid", c[0])); |
| 122 | assertFalse(res.isSuccess(), c[0] + " must be rejected"); | 125 | assertFalse(res.isSuccess(), c[0] + " must be rejected"); |
| 123 | assertEquals(c[1], res.getMessage(), c[0] + " rejected for the wrong reason: " + res.getDetails()); | 126 | assertEquals(c[1], res.getMessage(), c[0] + " rejected for the wrong reason: " + res.getDetails()); |
| 124 | assertEquals(2, eventRepo.getAll().size(), c[0] + " must not replace the loaded file"); | 127 | assertEquals(2, eventRepo.getAll().size(), c[0] + " must not replace the loaded file"); |
| @@ -129,7 +132,7 @@ class Ex2LoaderTest { | |||
| 129 | @Test | 132 | @Test |
| 130 | void testEx1MultipleXmlFailsBecauseV1NotSupported() { | 133 | void testEx1MultipleXmlFailsBecauseV1NotSupported() { |
| 131 | var path = Path.of("test-data/ex-1/multiple.xml"); | 134 | var path = Path.of("test-data/ex-1/multiple.xml"); |
| 132 | var res = loader.load(path); | 135 | var res = catalog.loadEvents(path); |
| 133 | assertFalse(res.isSuccess(), "ex-1 multiple.xml must fail because V1 schema is not supported"); | 136 | assertFalse(res.isSuccess(), "ex-1 multiple.xml must fail because V1 schema is not supported"); |
| 134 | } | 137 | } |
| 135 | } | 138 | } |
diff --git a/service/src/test/java/market/guess/service/Ex3LoaderTest.java b/service/src/test/java/market/guess/service/Ex3LoaderTest.java index 745b1fd..611e396 100644 --- a/service/src/test/java/market/guess/service/Ex3LoaderTest.java +++ b/service/src/test/java/market/guess/service/Ex3LoaderTest.java | |||
| @@ -12,20 +12,25 @@ import java.nio.charset.StandardCharsets; | |||
| 12 | import java.nio.file.Files; | 12 | import java.nio.file.Files; |
| 13 | import java.nio.file.Path; | 13 | import java.nio.file.Path; |
| 14 | import market.guess.model.event.EventStatus; | 14 | import market.guess.model.event.EventStatus; |
| 15 | import market.guess.service.catalog.LocalCatalogContext; | ||
| 16 | import market.guess.service.catalog.infrastructure.MarketContext; | ||
| 15 | import market.guess.service.catalog.infrastructure.mapper.v3.EventMapperV3; | 17 | import market.guess.service.catalog.infrastructure.mapper.v3.EventMapperV3; |
| 16 | import market.guess.service.catalog.infrastructure.provider.v3.XMLLoaderV3; | 18 | import market.guess.service.catalog.infrastructure.provider.v3.XMLLoaderV3; |
| 17 | import market.guess.service.catalog.infrastructure.provider.v3.XMLValidatorV3; | 19 | import market.guess.service.catalog.infrastructure.provider.v3.XMLValidatorV3; |
| 18 | import market.guess.service.catalog.infrastructure.repository.InMemoryEventRepository; | 20 | import market.guess.service.catalog.infrastructure.repository.InMemoryEventRepository; |
| 21 | import market.guess.service.catalog.infrastructure.repository.InMemoryUserRepository; | ||
| 19 | import org.junit.jupiter.api.BeforeEach; | 22 | import org.junit.jupiter.api.BeforeEach; |
| 20 | import org.junit.jupiter.api.Test; | 23 | import org.junit.jupiter.api.Test; |
| 21 | 24 | ||
| 22 | class Ex3LoaderTest { | 25 | class Ex3LoaderTest { |
| 23 | private InMemoryEventRepository eventRepo; | 26 | private InMemoryEventRepository eventRepo; |
| 27 | private InMemoryUserRepository userRepo; | ||
| 24 | private XMLLoaderV3 loader; | 28 | private XMLLoaderV3 loader; |
| 25 | 29 | ||
| 26 | @BeforeEach | 30 | @BeforeEach |
| 27 | void setUp() { | 31 | void setUp() { |
| 28 | eventRepo = new InMemoryEventRepository(); | 32 | eventRepo = new InMemoryEventRepository(); |
| 33 | userRepo = new InMemoryUserRepository(); | ||
| 29 | loader = new XMLLoaderV3(eventRepo, new EventMapperV3(), new XMLValidatorV3()); | 34 | loader = new XMLLoaderV3(eventRepo, new EventMapperV3(), new XMLValidatorV3()); |
| 30 | } | 35 | } |
| 31 | 36 | ||
| @@ -71,6 +76,15 @@ class Ex3LoaderTest { | |||
| 71 | } | 76 | } |
| 72 | 77 | ||
| 73 | @Test | 78 | @Test |
| 79 | void pathLoadsThroughTheCatalogHaveNoUploaderAndAreRefused() { | ||
| 80 | var catalog = | ||
| 81 | new LocalCatalogContext(loader, new MarketContext(eventRepo, userRepo), eventRepo); | ||
| 82 | var res = catalog.loadEvents(Path.of("test-data/ex-3/small.xml")); | ||
| 83 | assertEquals("INVALID_UPLOADER", res.getMessage()); | ||
| 84 | assertTrue(eventRepo.getAll().isEmpty()); | ||
| 85 | } | ||
| 86 | |||
| 87 | @Test | ||
| 74 | void collisionRejectsWholeFileAndKeepsMarket() { | 88 | void collisionRejectsWholeFileAndKeepsMarket() { |
| 75 | loader.load(stream(market(lmsr("Rain"))), "a.xml", "alice"); | 89 | loader.load(stream(market(lmsr("Rain"))), "a.xml", "alice"); |
| 76 | 90 | ||