Skip to content

Commit

Permalink
feat(state): ability to load state from any test file
Browse files Browse the repository at this point in the history
Closes: #69
  • Loading branch information
nergal-perm committed Sep 17, 2024
1 parent 43ab144 commit 9a600ee
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 60 deletions.
6 changes: 4 additions & 2 deletions src/main/java/ru/ewc/checklogic/LogicChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}

Expand Down
15 changes: 5 additions & 10 deletions src/main/java/ru/ewc/checklogic/ServerInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,22 +151,17 @@ 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]);
final boolean request = this.server.requestLocatorName().equals(args[0]);
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", ";"));
}
Expand Down
38 changes: 7 additions & 31 deletions src/main/java/ru/ewc/checklogic/server/AllEndpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,20 @@ 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
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) {
Expand All @@ -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);
}
Expand Down
68 changes: 66 additions & 2 deletions src/main/java/ru/ewc/checklogic/server/StatePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,18 +47,76 @@ 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) {
assert request.requestLine().getMethod().equals(RequestLine.Method.DELETE);
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 -> "<option value=\"%s\">%s</option>".formatted(name, name))
.collect(Collectors.joining());
}
}
11 changes: 0 additions & 11 deletions src/main/java/ru/ewc/checklogic/server/WebPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> values) {
return this.processors.renderInLayout(template, values);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ru/ewc/checklogic/testing/CheckFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public List<TestResult> 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);
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/ru/ewc/checklogic/testing/CheckSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public List<TestResult> 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<String> checkNames() {
return this.tests.stream().map(CheckFile::getFile).toList();
}
}
14 changes: 13 additions & 1 deletion src/main/resources/templates/state.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,21 @@
<div class="col">
<h1>State entities</h1>
<div class="row">
<div class="col col-4">
<div class="col col-2">
<button class="btn btn-primary" hx-delete="/state" hx-trigger="click">Reset</button>
</div>
<div class="col col-10">
<form class="mb-3" hx-post="/state">
<div class="input-group">
<select class="form-select" id="include" name="include"
placeholder="Select a state to include"
aria-label="Example select with button addon">
{{ includes }}
</select>
<button class="btn btn-primary" type="submit">Create state</button>
</div>
</form>
</div>
</div>
<div class="row">
{{ state }}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/uninitialized.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
~ SOFTWARE.
-->
<h1>Server is not initialized</h1>
<button class="btn btn-primary" hx-trigger="click" hx-post="/state" hx-swap="delete">Create barebones context</button>
<p>You'll need to create the basic folder structure for the business logic source files.</p>

0 comments on commit 9a600ee

Please sign in to comment.