diff --git a/src/main/java/ru/ewc/checklogic/FullServerContext.java b/src/main/java/ru/ewc/checklogic/FullServerContext.java index 22bb4b1..bb8345e 100644 --- a/src/main/java/ru/ewc/checklogic/FullServerContext.java +++ b/src/main/java/ru/ewc/checklogic/FullServerContext.java @@ -25,6 +25,7 @@ package ru.ewc.checklogic; import java.net.URI; +import java.nio.file.Paths; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -74,7 +75,6 @@ public final class FullServerContext { */ private final StateFactory states; - // @todo #34 Reduce the number of ServerContext methods FullServerContext(final StateFactory initial, final URI tables, final URI commands) { this.states = initial; this.root = this.states.getRoot(); @@ -169,4 +169,13 @@ public void initialize() { this.state = this.states.initialState(); this.context = new ComputationContext(this.state, this.tables, this.commands); } + + public boolean hasTestsFolder() { + return Paths.get(this.root, "states").toFile().exists(); + } + + public void createTestFolder() { + Paths.get(this.root, "states").toFile().mkdirs(); + this.context = new ComputationContext(this.state, this.tables, this.commands); + } } diff --git a/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java b/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java index 4e8f54b..bcb860c 100644 --- a/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java +++ b/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java @@ -57,6 +57,7 @@ public void register(final WebFramework web) { web.registerPath(GET, "test", this::httpGetRouter); web.registerPath(GET, "state", this::httpGetRouter); web.registerPath(POST, "state", this::httpPostRouter); + web.registerPath(POST, "test", this::httpPostRouter); } private Response httpPostRouter(final Request request) { @@ -69,6 +70,9 @@ private Response httpPostRouter(final Request request) { } else { result = this.pages.uninitializedPage(); } + } else if (!this.context.hasTestsFolder() && "test".equals(address)) { + this.context.createTestFolder(); + result = Response.htmlOk("OK", Map.of("HX-Redirect", "/test")); } else { result = new Response(NOT_FOUND, "", PLAIN_TEXT); } @@ -84,7 +88,11 @@ private Response httpGetRouter(final Request request) { if (address.isEmpty()) { result = this.pages.indexPage(); } else if ("test".equals(address)) { - result = this.pages.testPage(); + if (!this.context.hasTestsFolder()) { + result = this.pages.noTestsFolder(); + } else { + result = this.pages.testPage(); + } } else if ("state".equals(address)) { result = this.pages.statePage(this.context); } else { diff --git a/src/main/java/ru/ewc/checklogic/server/WebPages.java b/src/main/java/ru/ewc/checklogic/server/WebPages.java index 5d508bc..41f8816 100644 --- a/src/main/java/ru/ewc/checklogic/server/WebPages.java +++ b/src/main/java/ru/ewc/checklogic/server/WebPages.java @@ -65,6 +65,10 @@ public Response uninitializedPage() { return Response.htmlOk(this.templateNamed("templates/uninitialized.html", Map.of())); } + public Response noTestsFolder() { + return Response.htmlOk(this.templateNamed("templates/noTestsFolder.html", Map.of())); + } + public Response testPage() { final String results = FileUtils.readFileNames(this.root) .map(this::performTest) @@ -117,4 +121,5 @@ private TestResult performTest(final TestData test) { } return result; } + } diff --git a/src/main/resources/templates/noTestsFolder.html b/src/main/resources/templates/noTestsFolder.html new file mode 100644 index 0000000..c192ddf --- /dev/null +++ b/src/main/resources/templates/noTestsFolder.html @@ -0,0 +1,44 @@ + + + +
+