Skip to content

Commit

Permalink
feat(server): initialize an empty tests folder
Browse files Browse the repository at this point in the history
Closes: #34
  • Loading branch information
nergal-perm committed Jul 31, 2024
1 parent 3551e55 commit 807d016
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/main/java/ru/ewc/checklogic/FullServerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
10 changes: 9 additions & 1 deletion src/main/java/ru/ewc/checklogic/server/AllEndpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ru/ewc/checklogic/server/WebPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -117,4 +121,5 @@ private TestResult performTest(final TestData test) {
}
return result;
}

}
44 changes: 44 additions & 0 deletions src/main/resources/templates/noTestsFolder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!--
~ MIT License
~
~ Copyright (c) 2024 Decision-Driven Development
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>LC Configuration</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/htmx.org@1.9.12"
integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2"
crossorigin="anonymous"></script>
<script src="https://unpkg.com/hyperscript.org@0.9.12"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
<link rel="stylesheet" href="static/main.css">
</head>
<body class="container">
<h1>There is no folder with test files</h1>
<button class="btn btn-primary" hx-trigger="click" hx-post="/test" hx-swap="delete">Create folder for tests</button>
</body>
</html>

1 comment on commit 807d016

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 807d016 Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 34-c8a8d84b disappeared from src/main/java/ru/ewc/checklogic/FullServerContext.java), that's why I closed #51. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.