Skip to content

Commit

Permalink
test(server): base for testing the web server config page
Browse files Browse the repository at this point in the history
Refs: #44
  • Loading branch information
nergal-perm committed Aug 3, 2024
1 parent cf63771 commit 1bf25de
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ru/ewc/checklogic/server/AllEndpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class AllEndpoints implements Endpoints {

public AllEndpoints(final FullServerContext context) {
this.context = context;
this.pages = new WebPages(new ResourceTemplateProcessors(), context.getRoot());
this.pages = WebPages.create(new ResourceTemplateRender(), context.getRoot());
}

@Override
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/ru/ewc/checklogic/server/MockTemplateRender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/
package ru.ewc.checklogic.server;

import java.util.Map;

public class MockTemplateRender implements TemplateRender {
@Override
public String renderTemplateWith(String template, Map<String, String> values) {
return "Hello, world";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@
*
* @since 0.3.2
*/
public final class ResourceTemplateProcessors implements TemplateProcessors {
public final class ResourceTemplateRender implements TemplateRender {
/**
* The map of template processors for the templates. Used to lazy load the processors, because
* they are expensive to create.
*/
private final Map<String, TemplateProcessor> processors;

public ResourceTemplateProcessors() {
public ResourceTemplateRender() {
this.processors = new HashMap<>();
}

@Override
public TemplateProcessor forTemplate(final String template) {
public String renderTemplateWith(String template, Map<String, String> values) {
this.processors.putIfAbsent(
template,
TemplateProcessor.buildProcessor(WebResource.readFileFromResources(template))
);
return this.processors.get(template);
return this.processors.get(template).renderTemplate(values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@
*/
package ru.ewc.checklogic.server;

import com.renomad.minum.templating.TemplateProcessor;
import java.util.Map;

/**
* I am a collection of template processors that render the pages to be served.
*
* @since 0.3.2
*/
public interface TemplateProcessors {
/**
* I return a template processor for the given template.
*
* @param template The name of the template to be rendered by a processor.
* @return The processor for the template.
*/
TemplateProcessor forTemplate(String template);
public interface TemplateRender {

String renderTemplateWith(String template, Map<String, String> values);
}
29 changes: 20 additions & 9 deletions src/main/java/ru/ewc/checklogic/server/WebPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,36 @@ public final class WebPages {
/**
* The template processors to be used for rendering the pages.
*/
private final TemplateProcessors processors;
private final TemplateRender processors;

/**
* The root path for the external business logic resources.
*/
private final String root;

public WebPages(final TemplateProcessors processors, final String root) {
private WebPages(final TemplateRender processors, final String root) {
this.processors = processors;
this.root = root;
}

public static WebPages create(final TemplateRender processors, final String root) {
return new WebPages(processors, root);
}

public static WebPages testable() {
return new WebPages(new MockTemplateRender(), "root folder");
}

public Response indexPage() {
return Response.htmlOk(this.templateNamed("templates/index.html", Map.of()));
return Response.htmlOk(this.renderedTemplate("templates/index.html", Map.of()));
}

public Response uninitializedPage() {
return Response.htmlOk(this.templateNamed("templates/uninitialized.html", Map.of()));
return Response.htmlOk(this.renderedTemplate("templates/uninitialized.html", Map.of()));
}

public Response noTestsFolder() {
return Response.htmlOk(this.templateNamed("templates/noTestsFolder.html", Map.of()));
return Response.htmlOk(this.renderedTemplate("templates/noTestsFolder.html", Map.of()));
}

public Response testPage() {
Expand All @@ -82,7 +90,7 @@ public Response testPage() {
.collect(Collectors.joining());
final double elapsed = (System.currentTimeMillis() - start) / 1000.0;
return Response.htmlOk(
this.templateNamed(
this.renderedTemplate(
"templates/test.html",
Map.of(
"tests", "%s".formatted(rows),
Expand All @@ -100,7 +108,7 @@ public Response testPage() {
public Response statePage(final FullServerContext context) {
final StoredState stored = new StoredState(context.storedState());
return Response.htmlOk(
this.templateNamed(
this.renderedTemplate(
"templates/state.html",
Map.of(
"state", stored.asHtmlList(),
Expand All @@ -111,8 +119,8 @@ public Response statePage(final FullServerContext context) {
);
}

private String templateNamed(final String template, final Map<String, String> values) {
return this.processors.forTemplate(template).renderTemplate(values);
private String renderedTemplate(final String template, final Map<String, String> values) {
return this.processors.renderTemplateWith(template, values);
}

// @todo #47 Move performTest method to dedicated test runner
Expand Down Expand Up @@ -145,4 +153,7 @@ private TestResult performTest(final TestData test) {
return result;
}

public String configPage() {
return this.renderedTemplate("templates/config.html", Map.of());
}
}
40 changes: 40 additions & 0 deletions src/test/java/ru/ewc/checklogic/server/WebPagesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/
package ru.ewc.checklogic.server;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

class WebPagesTest {
@Test
void shouldRenderConfigPage() {
final WebPages pages = WebPages.testable();
MatcherAssert.assertThat(
"Should render the exact config page",
pages.configPage(),
Matchers.is("Hello, world")
);
}
}

0 comments on commit 1bf25de

Please sign in to comment.