diff --git a/src/main/java/ru/ewc/checklogic/LogicChecker.java b/src/main/java/ru/ewc/checklogic/LogicChecker.java index dd45ec1..252334f 100644 --- a/src/main/java/ru/ewc/checklogic/LogicChecker.java +++ b/src/main/java/ru/ewc/checklogic/LogicChecker.java @@ -30,6 +30,7 @@ import ru.ewc.checklogic.server.CommandPage; import ru.ewc.checklogic.server.ContextPage; import ru.ewc.checklogic.server.Endpoints; +import ru.ewc.checklogic.server.ResourceTemplateRender; import ru.ewc.checklogic.server.StatePage; import ru.ewc.checklogic.server.config.ConfigPage; @@ -55,11 +56,12 @@ public static void main(final String[] args) { final ServerInstance context = factory.initialState(); final FullSystem minum = FullSystem.initialize(); final WebFramework web = minum.getWebFramework(); + final ResourceTemplateRender render = new ResourceTemplateRender(); registerEndpoints(web, new ConfigPage(factory.configuration())); registerEndpoints(web, new CommandPage(context)); registerEndpoints(web, new ContextPage(context, factory.configuration())); - registerEndpoints(web, new AllEndpoints(context, factory.configuration())); - registerEndpoints(web, new StatePage(context)); + registerEndpoints(web, new AllEndpoints(context, factory.configuration(), render)); + registerEndpoints(web, new StatePage(context, factory.configuration(), render)); minum.block(); } diff --git a/src/main/java/ru/ewc/checklogic/ServerInstance.java b/src/main/java/ru/ewc/checklogic/ServerInstance.java index d34c0f2..ef8438b 100644 --- a/src/main/java/ru/ewc/checklogic/ServerInstance.java +++ b/src/main/java/ru/ewc/checklogic/ServerInstance.java @@ -25,10 +25,10 @@ package ru.ewc.checklogic; import java.net.URI; -import java.nio.file.Paths; import java.util.List; import java.util.Map; import lombok.Getter; +import ru.ewc.checklogic.testing.CheckSuite; import ru.ewc.decisions.api.ComputationContext; import ru.ewc.decisions.api.DecisionTables; import ru.ewc.decisions.api.DecitaException; @@ -151,15 +151,6 @@ public void initialize() { this.context = new ComputationContext(this.state, this.getAllTables()); } - public boolean hasTestsFolder() { - return Paths.get(this.root, "tests").toFile().exists(); - } - - public void createTestFolder() { - Paths.get(this.root, "tests").toFile().mkdirs(); - this.context = new ComputationContext(this.state, this.getAllTables()); - } - public boolean isNotSpecified(final String arg) { final String[] args = arg.split("::"); final boolean function = this.server.functionsLocatorName().equals(args[0]); @@ -167,6 +158,10 @@ public boolean isNotSpecified(final String arg) { return function && !this.states.functionSpecified(args[1]) || request; } + public void createState(final String include, final CheckSuite suite) { + suite.findAndPerform(include, this.context); + } + private DecisionTables getAllTables() { return DecisionTables.using(new CombinedCsvFileReader(this.tables, ".csv", ";")); } diff --git a/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java b/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java index a91eeab..9721f2e 100644 --- a/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java +++ b/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java @@ -46,9 +46,13 @@ public final class AllEndpoints implements Endpoints { */ private final WebPages pages; - public AllEndpoints(final ServerInstance context, final ServerConfiguration config) { + public AllEndpoints( + final ServerInstance context, + final ServerConfiguration config, + final ResourceTemplateRender render + ) { this.context = context; - this.pages = new WebPages(new ResourceTemplateRender(), context.getRoot(), config); + this.pages = new WebPages(render, context.getRoot(), config); } @Override @@ -56,28 +60,6 @@ public void register(final WebFramework web) { web.registerPartialPath(GET, "static", AllEndpoints::staticResource); web.registerPath(GET, "", this::httpGetRouter); 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) { - final Response result; - final String address = request.requestLine().getPathDetails().getIsolatedPath(); - if (this.context.isEmpty()) { - if ("state".equals(address)) { - this.context.initialize(); - result = Response.htmlOk("OK", Map.of("HX-Redirect", "/")); - } 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); - } - return result; } private Response httpGetRouter(final Request request) { @@ -96,13 +78,7 @@ private Response getAddressFor(final Request request) { if (address.isEmpty()) { result = this.renderHtmlFor("templates/index.html"); } else if ("test".equals(address)) { - if (this.context.hasTestsFolder()) { - result = this.pages.testPage(); - } else { - result = this.renderHtmlFor("templates/noTestsFolder.html"); - } - } else if ("state".equals(address)) { - result = this.pages.statePage(this.context); + result = this.pages.testPage(); } else { result = new Response(NOT_FOUND, "", PLAIN_TEXT); } diff --git a/src/main/java/ru/ewc/checklogic/server/StatePage.java b/src/main/java/ru/ewc/checklogic/server/StatePage.java index f16ca38..3cbb978 100644 --- a/src/main/java/ru/ewc/checklogic/server/StatePage.java +++ b/src/main/java/ru/ewc/checklogic/server/StatePage.java @@ -27,8 +27,14 @@ import com.renomad.minum.web.RequestLine; import com.renomad.minum.web.Response; import com.renomad.minum.web.WebFramework; +import java.net.URI; +import java.nio.file.Path; import java.util.Map; +import java.util.stream.Collectors; +import ru.ewc.checklogic.ServerConfiguration; import ru.ewc.checklogic.ServerInstance; +import ru.ewc.checklogic.testing.CheckSuite; +import ru.ewc.decisions.input.CombinedCsvFileReader; /** * I am a class providing access to the state page. @@ -41,13 +47,52 @@ public final class StatePage implements Endpoints { */ private final ServerInstance context; - public StatePage(final ServerInstance context) { + /** + * The server configuration. + */ + private final ServerConfiguration config; + + /** + * The template renderer, creating pages to be served. + */ + private final ResourceTemplateRender processors; + + public StatePage( + final ServerInstance context, + final ServerConfiguration config, + final ResourceTemplateRender processors + ) { this.context = context; + this.config = config; + this.processors = processors; } @Override public void register(final WebFramework web) { - web.registerPath(RequestLine.Method.DELETE, "state", this::resetState); + web.registerPath(GET, "state", this::statePage); + web.registerPath(POST, "state", this::createState); + web.registerPath(DELETE, "state", this::resetState); + } + + private Response statePage(final Request request) { + assert request.requestLine().getMethod().equals(RequestLine.Method.GET); + final StoredState stored = new StoredState(this.context.storedState()); + return Response.htmlOk( + this.processors.renderInLayout( + "templates/state.html", + Map.of( + "state", stored.asHtmlList(), + "includes", this.listOfIncludes() + ) + ) + ); + } + + private Response createState(final Request request) { + assert request.requestLine().getMethod().equals(RequestLine.Method.POST); + final String include = request.body().asString("include"); + this.context.createState(include, this.testSuite()); + return Response.htmlOk("OK", Map.of("HX-Redirect", "/state")); } private Response resetState(final Request request) { @@ -55,4 +100,23 @@ private Response resetState(final Request request) { this.context.initialize(); return Response.htmlOk("OK", Map.of("HX-Redirect", "/state")); } + + private CheckSuite testSuite() { + return CheckSuite.using( + new CombinedCsvFileReader(this.testsFolder(), ".csv", ";"), + this.context.getRoot(), + this.config.requestLocatorName() + ); + } + + private URI testsFolder() { + return Path.of(this.context.getRoot(), "tests").toUri(); + } + + private String listOfIncludes() { + return this.testSuite().checkNames().stream() + .sorted() + .map(name -> "%s".formatted(name, name)) + .collect(Collectors.joining()); + } } diff --git a/src/main/java/ru/ewc/checklogic/server/WebPages.java b/src/main/java/ru/ewc/checklogic/server/WebPages.java index 87491c9..5420fd1 100644 --- a/src/main/java/ru/ewc/checklogic/server/WebPages.java +++ b/src/main/java/ru/ewc/checklogic/server/WebPages.java @@ -30,7 +30,6 @@ import java.util.Map; import java.util.stream.Collectors; import ru.ewc.checklogic.ServerConfiguration; -import ru.ewc.checklogic.ServerInstance; import ru.ewc.checklogic.testing.CheckSuite; import ru.ewc.checklogic.testing.TestResult; import ru.ewc.decisions.input.CombinedCsvFileReader; @@ -108,16 +107,6 @@ public Response testPage() { ); } - public Response statePage(final ServerInstance context) { - final StoredState stored = new StoredState(context.storedState()); - return Response.htmlOk( - this.renderInLayout( - "templates/state.html", - Map.of("state", stored.asHtmlList()) - ) - ); - } - public String renderInLayout(final String template, final Map values) { return this.processors.renderInLayout(template, values); } diff --git a/src/main/java/ru/ewc/checklogic/testing/CheckFile.java b/src/main/java/ru/ewc/checklogic/testing/CheckFile.java index 631bbb7..94f3b48 100644 --- a/src/main/java/ru/ewc/checklogic/testing/CheckFile.java +++ b/src/main/java/ru/ewc/checklogic/testing/CheckFile.java @@ -76,7 +76,8 @@ public List performChecks(final String root, final CheckSuite files) .toList(); } - public void performInSameContext(final ComputationContext ctx) { + public void performInSameContext(final ComputationContext ctx, final CheckSuite files) { + this.suite = files; this.getTestResult(this.tests.getFirst(), ctx); } diff --git a/src/main/java/ru/ewc/checklogic/testing/CheckSuite.java b/src/main/java/ru/ewc/checklogic/testing/CheckSuite.java index 0b97a15..5cb9e7e 100644 --- a/src/main/java/ru/ewc/checklogic/testing/CheckSuite.java +++ b/src/main/java/ru/ewc/checklogic/testing/CheckSuite.java @@ -75,6 +75,10 @@ public List perform() { public void findAndPerform(final String file, final ComputationContext ctx) { this.tests.stream().filter(test -> file.equals(test.getFile())) .findFirst() - .ifPresent(test -> test.performInSameContext(ctx)); + .ifPresent(test -> test.performInSameContext(ctx, this)); + } + + public List checkNames() { + return this.tests.stream().map(CheckFile::getFile).toList(); } } diff --git a/src/main/resources/templates/state.html b/src/main/resources/templates/state.html index 19ac022..1277ecc 100644 --- a/src/main/resources/templates/state.html +++ b/src/main/resources/templates/state.html @@ -25,9 +25,21 @@ State entities - + Reset + + + + + {{ includes }} + + Create state + + + {{ state }} diff --git a/src/main/resources/templates/uninitialized.html b/src/main/resources/templates/uninitialized.html index 7276839..b164513 100644 --- a/src/main/resources/templates/uninitialized.html +++ b/src/main/resources/templates/uninitialized.html @@ -22,4 +22,4 @@ ~ SOFTWARE. --> Server is not initialized -Create barebones context \ No newline at end of file +You'll need to create the basic folder structure for the business logic source files.
You'll need to create the basic folder structure for the business logic source files.