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 | |
| 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')
7 files changed, 59 insertions, 66 deletions
diff --git a/service/src/main/java/market/guess/service/catalog/LocalCatalogContext.java b/service/src/main/java/market/guess/service/catalog/LocalCatalogContext.java index d767fd1..12fff3c 100644 --- a/service/src/main/java/market/guess/service/catalog/LocalCatalogContext.java +++ b/service/src/main/java/market/guess/service/catalog/LocalCatalogContext.java | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | package market.guess.service.catalog; | 1 | package market.guess.service.catalog; |
| 2 | 2 | ||
| 3 | import java.io.IOException; | ||
| 3 | import java.math.BigDecimal; | 4 | import java.math.BigDecimal; |
| 5 | import java.nio.file.Files; | ||
| 4 | import java.nio.file.Path; | 6 | import java.nio.file.Path; |
| 5 | import java.time.Instant; | 7 | import java.time.Instant; |
| 6 | import java.util.ArrayList; | 8 | import java.util.ArrayList; |
| @@ -41,7 +43,22 @@ public final class LocalCatalogContext implements CatalogContext { | |||
| 41 | 43 | ||
| 42 | @Override | 44 | @Override |
| 43 | public Result<LoadResult> loadEvents(Path path) { | 45 | public Result<LoadResult> loadEvents(Path path) { |
| 44 | return provider.load(path); | 46 | if (path == null) { |
| 47 | return Result.error("INVALID_PATH", "File path cannot be null."); | ||
| 48 | } | ||
| 49 | if (!Files.isRegularFile(path)) { | ||
| 50 | return Result.error("FILE_NOT_FOUND", "No such file: " + path); | ||
| 51 | } | ||
| 52 | if (path.getFileName() == null | ||
| 53 | || !path.getFileName().toString().toLowerCase().endsWith(".xml")) { | ||
| 54 | return Result.error("NOT_XML", "The file must have a .xml extension."); | ||
| 55 | } | ||
| 56 | |||
| 57 | try (var stream = Files.newInputStream(path)) { | ||
| 58 | return provider.load(stream, path.toString(), null); | ||
| 59 | } catch (IOException e) { | ||
| 60 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); | ||
| 61 | } | ||
| 45 | } | 62 | } |
| 46 | 63 | ||
| 47 | @Override | 64 | @Override |
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/Loader.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/Loader.java index 62406bb..78e9ef1 100644 --- a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/Loader.java +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/Loader.java | |||
| @@ -1,12 +1,9 @@ | |||
| 1 | package market.guess.service.catalog.infrastructure.provider; | 1 | package market.guess.service.catalog.infrastructure.provider; |
| 2 | 2 | ||
| 3 | import java.nio.file.Path; | 3 | import java.io.InputStream; |
| 4 | import market.guess.api.CatalogContext; | ||
| 5 | import market.guess.api.LoadResult; | 4 | import market.guess.api.LoadResult; |
| 6 | import market.guess.api.Result; | 5 | import market.guess.api.Result; |
| 7 | 6 | ||
| 8 | public interface Loader { | 7 | public interface Loader { |
| 9 | Result<LoadResult> seed(CatalogContext context); | 8 | Result<LoadResult> load(InputStream in, String source, String owner); |
| 10 | |||
| 11 | Result<LoadResult> load(Path path); | ||
| 12 | } | 9 | } |
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v1/XMLLoaderV1.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v1/XMLLoaderV1.java index 44e1c7f..4e93528 100644 --- a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v1/XMLLoaderV1.java +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v1/XMLLoaderV1.java | |||
| @@ -2,11 +2,9 @@ package market.guess.service.catalog.infrastructure.provider.v1; | |||
| 2 | 2 | ||
| 3 | import jakarta.xml.bind.JAXBContext; | 3 | import jakarta.xml.bind.JAXBContext; |
| 4 | import jakarta.xml.bind.JAXBException; | 4 | import jakarta.xml.bind.JAXBException; |
| 5 | import java.io.InputStream; | ||
| 5 | import java.math.BigDecimal; | 6 | import java.math.BigDecimal; |
| 6 | import java.nio.file.Files; | ||
| 7 | import java.nio.file.Path; | ||
| 8 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
| 9 | import market.guess.api.CatalogContext; | ||
| 10 | import market.guess.api.LoadResult; | 8 | import market.guess.api.LoadResult; |
| 11 | import market.guess.api.Result; | 9 | import market.guess.api.Result; |
| 12 | import market.guess.service.catalog.infrastructure.mapper.v1.EventMapperV1; | 10 | import market.guess.service.catalog.infrastructure.mapper.v1.EventMapperV1; |
| @@ -43,21 +41,8 @@ public final class XMLLoaderV1 implements Loader { | |||
| 43 | } | 41 | } |
| 44 | 42 | ||
| 45 | @Override | 43 | @Override |
| 46 | public Result<LoadResult> load(Path path) { | 44 | public Result<LoadResult> load(InputStream stream, String source, String owner) { |
| 47 | if (path == null) { | 45 | try { |
| 48 | return Result.error("INVALID_PATH", "File path cannot be null."); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (!Files.isRegularFile(path)) { | ||
| 52 | return Result.error("FILE_NOT_FOUND", "No such file: " + path); | ||
| 53 | } | ||
| 54 | |||
| 55 | if (path.getFileName() == null | ||
| 56 | || !path.getFileName().toString().toLowerCase().endsWith(".xml")) { | ||
| 57 | return Result.error("NOT_XML", "The file must have a .xml extension."); | ||
| 58 | } | ||
| 59 | |||
| 60 | try (var stream = Files.newInputStream(path)) { | ||
| 61 | var seed = (GuessMarket) context.createUnmarshaller().unmarshal(stream); | 46 | var seed = (GuessMarket) context.createUnmarshaller().unmarshal(stream); |
| 62 | 47 | ||
| 63 | var validation = validator.validate(seed); | 48 | var validation = validator.validate(seed); |
| @@ -80,7 +65,7 @@ public final class XMLLoaderV1 implements Loader { | |||
| 80 | 65 | ||
| 81 | userRepository.add(new User("Tester", new BigDecimal("500.00"))); | 66 | userRepository.add(new User("Tester", new BigDecimal("500.00"))); |
| 82 | 67 | ||
| 83 | return Result.ok(new LoadResult(path.toString(), eventRepository.getAll().size())); | 68 | return Result.ok(new LoadResult(source, eventRepository.getAll().size())); |
| 84 | } catch (JAXBException e) { | 69 | } catch (JAXBException e) { |
| 85 | var cause = | 70 | var cause = |
| 86 | e.getLinkedException() != null ? e.getLinkedException().getMessage() : e.getMessage(); | 71 | e.getLinkedException() != null ? e.getLinkedException().getMessage() : e.getMessage(); |
| @@ -91,9 +76,4 @@ public final class XMLLoaderV1 implements Loader { | |||
| 91 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); | 76 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); |
| 92 | } | 77 | } |
| 93 | } | 78 | } |
| 94 | |||
| 95 | @Override | ||
| 96 | public Result<LoadResult> seed(CatalogContext context) { | ||
| 97 | throw new UnsupportedOperationException("Unimplemented method 'seed'"); | ||
| 98 | } | ||
| 99 | } | 79 | } |
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v2/XMLLoaderV2.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v2/XMLLoaderV2.java index 7ca141f..1000e6e 100644 --- a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v2/XMLLoaderV2.java +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v2/XMLLoaderV2.java | |||
| @@ -2,13 +2,11 @@ package market.guess.service.catalog.infrastructure.provider.v2; | |||
| 2 | 2 | ||
| 3 | import jakarta.xml.bind.JAXBContext; | 3 | import jakarta.xml.bind.JAXBContext; |
| 4 | import jakarta.xml.bind.JAXBException; | 4 | import jakarta.xml.bind.JAXBException; |
| 5 | import java.io.InputStream; | ||
| 5 | import java.math.BigDecimal; | 6 | import java.math.BigDecimal; |
| 6 | import java.nio.file.Files; | ||
| 7 | import java.nio.file.Path; | ||
| 8 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
| 9 | import java.util.HashMap; | 8 | import java.util.HashMap; |
| 10 | import java.util.Map; | 9 | import java.util.Map; |
| 11 | import market.guess.api.CatalogContext; | ||
| 12 | import market.guess.api.LoadResult; | 10 | import market.guess.api.LoadResult; |
| 13 | import market.guess.api.Result; | 11 | import market.guess.api.Result; |
| 14 | import market.guess.service.catalog.infrastructure.mapper.v2.EventMapperV2; | 12 | import market.guess.service.catalog.infrastructure.mapper.v2.EventMapperV2; |
| @@ -45,21 +43,8 @@ public final class XMLLoaderV2 implements Loader { | |||
| 45 | } | 43 | } |
| 46 | 44 | ||
| 47 | @Override | 45 | @Override |
| 48 | public Result<LoadResult> load(Path path) { | 46 | public Result<LoadResult> load(InputStream stream, String source, String owner) { |
| 49 | if (path == null) { | 47 | try { |
| 50 | return Result.error("INVALID_PATH", "File path cannot be null."); | ||
| 51 | } | ||
| 52 | |||
| 53 | if (!Files.isRegularFile(path)) { | ||
| 54 | return Result.error("FILE_NOT_FOUND", "No such file: " + path); | ||
| 55 | } | ||
| 56 | |||
| 57 | if (path.getFileName() == null | ||
| 58 | || !path.getFileName().toString().toLowerCase().endsWith(".xml")) { | ||
| 59 | return Result.error("NOT_XML", "The file must have a .xml extension."); | ||
| 60 | } | ||
| 61 | |||
| 62 | try (var stream = Files.newInputStream(path)) { | ||
| 63 | var seed = (GuessMarket) contextV2.createUnmarshaller().unmarshal(stream); | 48 | var seed = (GuessMarket) contextV2.createUnmarshaller().unmarshal(stream); |
| 64 | 49 | ||
| 65 | var validation = validatorV2.validate(seed); | 50 | var validation = validatorV2.validate(seed); |
| @@ -100,7 +85,7 @@ public final class XMLLoaderV2 implements Loader { | |||
| 100 | eventRepository.add(e); | 85 | eventRepository.add(e); |
| 101 | } | 86 | } |
| 102 | 87 | ||
| 103 | return Result.ok(new LoadResult(path.toString(), eventRepository.getAll().size())); | 88 | return Result.ok(new LoadResult(source, eventRepository.getAll().size())); |
| 104 | } catch (JAXBException e) { | 89 | } catch (JAXBException e) { |
| 105 | var cause = | 90 | var cause = |
| 106 | e.getLinkedException() != null ? e.getLinkedException().getMessage() : e.getMessage(); | 91 | e.getLinkedException() != null ? e.getLinkedException().getMessage() : e.getMessage(); |
| @@ -111,9 +96,4 @@ public final class XMLLoaderV2 implements Loader { | |||
| 111 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); | 96 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); |
| 112 | } | 97 | } |
| 113 | } | 98 | } |
| 114 | |||
| 115 | @Override | ||
| 116 | public Result<LoadResult> seed(CatalogContext context) { | ||
| 117 | throw new UnsupportedOperationException("Unimplemented method 'seed'"); | ||
| 118 | } | ||
| 119 | } | 99 | } |
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java index 59aa3ad..bf17f23 100644 --- a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java | |||
| @@ -9,11 +9,12 @@ import javax.xml.stream.XMLStreamException; | |||
| 9 | import market.guess.api.LoadResult; | 9 | import market.guess.api.LoadResult; |
| 10 | import market.guess.api.Result; | 10 | import market.guess.api.Result; |
| 11 | import market.guess.service.catalog.infrastructure.mapper.v3.EventMapperV3; | 11 | import market.guess.service.catalog.infrastructure.mapper.v3.EventMapperV3; |
| 12 | import market.guess.service.catalog.infrastructure.provider.Loader; | ||
| 12 | import market.guess.service.catalog.infrastructure.repository.EventRepository; | 13 | import market.guess.service.catalog.infrastructure.repository.EventRepository; |
| 13 | import market.guess.service.catalog.model.v3.GMEvent; | 14 | import market.guess.service.catalog.model.v3.GMEvent; |
| 14 | import market.guess.service.catalog.model.v3.GuessMarket; | 15 | import market.guess.service.catalog.model.v3.GuessMarket; |
| 15 | 16 | ||
| 16 | public final class XMLLoaderV3 { | 17 | public final class XMLLoaderV3 implements Loader { |
| 17 | private final JAXBContext contextV3; | 18 | private final JAXBContext contextV3; |
| 18 | private final XMLInputFactory xmlInput; | 19 | private final XMLInputFactory xmlInput; |
| 19 | private final EventRepository eventRepository; | 20 | private final EventRepository eventRepository; |
| @@ -35,6 +36,7 @@ public final class XMLLoaderV3 { | |||
| 35 | xmlInput.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); | 36 | xmlInput.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 39 | @Override | ||
| 38 | public synchronized Result<LoadResult> load(InputStream in, String source, String uploader) { | 40 | public synchronized Result<LoadResult> load(InputStream in, String source, String uploader) { |
| 39 | if (in == null) { | 41 | if (in == null) { |
| 40 | return Result.error("INVALID_INPUT", "No file content was provided."); | 42 | return Result.error("INVALID_INPUT", "No file content was provided."); |
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 | ||