Skip to content

Commit fb5ea75

Browse files
committed
refactor(test): made state creation process more extensible
Refs: #64
1 parent 148a319 commit fb5ea75

File tree

11 files changed

+82
-109
lines changed

11 files changed

+82
-109
lines changed

src/main/java/ru/ewc/checklogic/FileStateFactory.java

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424
package ru.ewc.checklogic;
2525

2626
import java.io.File;
27+
import java.io.IOException;
2728
import java.io.InputStream;
2829
import java.io.OutputStream;
2930
import java.nio.charset.StandardCharsets;
3031
import java.nio.file.Files;
31-
import java.nio.file.NoSuchFileException;
32-
import java.nio.file.Path;
32+
import java.util.ArrayList;
3333
import java.util.HashMap;
3434
import java.util.List;
3535
import java.util.Map;
36-
import java.util.stream.Collectors;
3736
import lombok.SneakyThrows;
3837
import org.yaml.snakeyaml.Yaml;
3938
import ru.ewc.decisions.api.InMemoryLocator;
@@ -45,70 +44,58 @@
4544
*
4645
* @since 0.3.2
4746
*/
48-
public final class FileStateFactory extends StateFactory {
47+
public final class FileStateFactory implements StateFactory {
48+
/**
49+
* An instance of server configuration.
50+
*/
51+
private final ServerConfiguration config;
4952

5053
/**
51-
* Optional source of locators to be added to the initial (clean) state.
54+
* The collection of all the state and functions locators.
5255
*/
53-
private InputStream src;
56+
private final List<Locator> locators;
5457

55-
public FileStateFactory(final String root) {
56-
super(root);
58+
public FileStateFactory(final ServerConfiguration config) {
59+
this.config = config;
60+
this.locators = new ArrayList<>(1);
5761
}
5862

5963
@Override
60-
@SneakyThrows
6164
public State initialState() {
62-
State state;
63-
try (InputStream file = Files.newInputStream(Path.of(this.getRoot(), "application.yaml"))) {
64-
state = FileStateFactory.stateFromAppConfig(file);
65-
state.locators().putAll(this.locatorsFromFile());
66-
} catch (final NoSuchFileException exception) {
67-
state = State.EMPTY;
68-
}
69-
return state;
65+
this.locators.clear();
66+
this.loadLocatorsFromApplicationConfig();
67+
this.loadInMemoryRequestLocator();
68+
return new State(this.locators);
7069
}
7170

72-
@Override
73-
public StateFactory with(final InputStream file) {
74-
this.src = file;
75-
return this;
71+
private void loadInMemoryRequestLocator() {
72+
this.locators.add(
73+
new InMemoryLocator(this.config.requestLocatorName(), new HashMap<>())
74+
);
7675
}
7776

78-
@Override
7977
@SneakyThrows
80-
public void initialize() {
81-
final File config = Path.of(this.getRoot(), "application.yaml").toFile();
82-
if (!config.exists() && config.createNewFile()) {
83-
try (OutputStream out = Files.newOutputStream(config.toPath())) {
78+
private InputStream applicationConfigFile() {
79+
final File fileconf = this.config.applicationConfig().toFile();
80+
if (!fileconf.exists() && fileconf.createNewFile()) {
81+
try (OutputStream out = Files.newOutputStream(fileconf.toPath())) {
8482
out.write("locators:\n - request\n".getBytes(StandardCharsets.UTF_8));
8583
}
8684
}
85+
return Files.newInputStream(this.config.applicationConfig());
8786
}
8887

8988
@SuppressWarnings("unchecked")
90-
private Map<String, Locator> locatorsFromFile() {
91-
final Map<String, Locator> locators = new HashMap<>();
92-
if (this.src != null) {
93-
final Map<String, Map<String, Object>> raw =
94-
(Map<String, Map<String, Object>>) new Yaml().loadAll(this.src).iterator().next();
95-
if (raw == null) {
96-
throw new IllegalStateException(
97-
"There is no Arrange section in the test file, you should add one"
98-
);
99-
}
100-
raw.forEach((name, data) -> locators.put(name, new InMemoryLocator(name, data)));
89+
private void loadLocatorsFromApplicationConfig() {
90+
try (InputStream file = this.applicationConfigFile()) {
91+
final Map<String, Object> yaml = new Yaml().load(file);
92+
this.locators.addAll(
93+
((List<String>) yaml.get("locators")).stream()
94+
.map(name -> new InMemoryLocator(name, new HashMap<>()))
95+
.toList()
96+
);
97+
} catch (final IOException exception) {
98+
this.locators.addAll(List.of());
10199
}
102-
return locators;
103-
}
104-
105-
@SneakyThrows
106-
@SuppressWarnings("unchecked")
107-
private static State stateFromAppConfig(final InputStream file) {
108-
final Map<String, Object> config = new Yaml().load(file);
109-
return new State(((List<String>) config.get("locators")).stream()
110-
.map(name -> new InMemoryLocator(name, new HashMap<>()))
111-
.collect(Collectors.toList())
112-
);
113100
}
114101
}

src/main/java/ru/ewc/checklogic/MockStateFactory.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package ru.ewc.checklogic;
2525

26-
import java.io.InputStream;
2726
import java.util.List;
2827
import java.util.Map;
2928
import ru.ewc.decisions.api.InMemoryLocator;
@@ -34,11 +33,7 @@
3433
*
3534
* @since 0.3.2
3635
*/
37-
public final class MockStateFactory extends StateFactory {
38-
public MockStateFactory(final String root) {
39-
super(root);
40-
}
41-
36+
public final class MockStateFactory implements StateFactory {
4237
@Override
4338
public State initialState() {
4439
return new State(
@@ -49,13 +44,4 @@ public State initialState() {
4944
);
5045
}
5146

52-
@Override
53-
public StateFactory with(final InputStream file) {
54-
return this;
55-
}
56-
57-
@Override
58-
public void initialize() {
59-
// do nothing
60-
}
6147
}

src/main/java/ru/ewc/checklogic/ServerConfiguration.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package ru.ewc.checklogic;
2525

26+
import java.nio.file.Path;
2627
import java.util.HashMap;
2728
import java.util.Map;
2829

@@ -32,15 +33,25 @@
3233
* @since 0.3.2
3334
*/
3435
public final class ServerConfiguration {
36+
/**
37+
* The path to a root folder of the external business logic resources.
38+
*/
39+
private final String root;
40+
3541
/**
3642
* The parameters of the context.
3743
*/
38-
private final Map<String, String> parameters = new HashMap<>(
39-
Map.of(
40-
"request", "request",
41-
"command", "available"
42-
)
43-
);
44+
private final Map<String, String> parameters;
45+
46+
public ServerConfiguration(final String root) {
47+
this.root = root;
48+
this.parameters = new HashMap<>(
49+
Map.of(
50+
"request", "request",
51+
"command", "available"
52+
)
53+
);
54+
}
4455

4556
/**
4657
* Returns the value of the specified parameter.
@@ -69,4 +80,12 @@ public String commandAvailabilityField() {
6980
public String requestLocatorName() {
7081
return this.getParameterValue("request");
7182
}
83+
84+
public Path applicationConfig() {
85+
return Path.of(this.root, "application.yaml");
86+
}
87+
88+
public String getRoot() {
89+
return this.root;
90+
}
7291
}

src/main/java/ru/ewc/checklogic/ServerContextFactory.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,20 @@ private ServerContextFactory(
5959
}
6060

6161
public static ServerContextFactory testable() {
62+
final String path = "root folder";
6263
return new ServerContextFactory(
63-
"root folder",
64-
new MockStateFactory("root folder"),
65-
new ServerConfiguration()
64+
path,
65+
new MockStateFactory(),
66+
new ServerConfiguration(path)
6667
);
6768
}
6869

6970
public static ServerContextFactory create(final String root) {
71+
final ServerConfiguration config = new ServerConfiguration(root);
7072
return new ServerContextFactory(
7173
root,
72-
new FileStateFactory(root),
73-
new ServerConfiguration()
74+
new FileStateFactory(config),
75+
config
7476
);
7577
}
7678

src/main/java/ru/ewc/checklogic/ServerInstance.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public final class ServerInstance {
7878
final ServerConfiguration server) {
7979
this.states = initial;
8080
this.server = server;
81-
this.root = this.states.getRoot();
81+
this.root = this.server.getRoot();
8282
this.state = this.states.initialState();
8383
this.tables = tables;
8484
this.context = new ComputationContext(this.state, this.getAllTables());
@@ -153,7 +153,6 @@ public String getRoot() {
153153
}
154154

155155
public void initialize() {
156-
this.states.initialize();
157156
this.state = this.states.initialState();
158157
this.context = new ComputationContext(this.state, this.getAllTables());
159158
}

src/main/java/ru/ewc/checklogic/StateFactory.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package ru.ewc.checklogic;
2525

26-
import java.io.InputStream;
2726
import ru.ewc.state.State;
2827

2928
/**
@@ -33,28 +32,6 @@
3332
*
3433
* @since 0.3.2
3534
*/
36-
public abstract class StateFactory {
37-
/**
38-
* The root path for the external business logic resources.
39-
*/
40-
private final String root;
41-
42-
public StateFactory(final String root) {
43-
this.root = root;
44-
}
45-
46-
/**
47-
* Returns the path to the root folder of the external business logic resources.
48-
*
49-
* @return Path to the root folder as a string.
50-
*/
51-
public String getRoot() {
52-
return this.root;
53-
}
54-
55-
public abstract State initialState();
56-
57-
public abstract StateFactory with(InputStream file);
58-
59-
public abstract void initialize();
35+
interface StateFactory {
36+
State initialState();
6037
}

src/main/java/ru/ewc/checklogic/server/WebPages.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public WebPages(
6868
}
6969

7070
public static WebPages testable() {
71-
return new WebPages(new MockTemplateRender(), "root folder", new ServerConfiguration());
71+
return new WebPages(
72+
new MockTemplateRender(),
73+
"root folder",
74+
new ServerConfiguration("root folder")
75+
);
7276
}
7377

7478
public Response uninitializedPage() {

src/test/java/ru/ewc/checklogic/ServerConfigurationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
final class ServerConfigurationTest {
3636
@Test
3737
void emptyServerContextHasDefaultRequestLocatorName() {
38-
final ServerConfiguration context = new ServerConfiguration();
38+
final ServerConfiguration context = new ServerConfiguration("any");
3939
MatcherAssert.assertThat(
4040
"New server context should have 'request' as default incoming request locator's name",
4141
context.getParameterValue("request"),
@@ -45,7 +45,7 @@ void emptyServerContextHasDefaultRequestLocatorName() {
4545

4646
@Test
4747
void emptyServerContextHasDefaultCommandAvailabilityOutcome() {
48-
final ServerConfiguration context = new ServerConfiguration();
48+
final ServerConfiguration context = new ServerConfiguration("any");
4949
MatcherAssert.assertThat(
5050
"New server context should have 'available' as default command availability outcome",
5151
context.getParameterValue("command"),
@@ -55,7 +55,7 @@ void emptyServerContextHasDefaultCommandAvailabilityOutcome() {
5555

5656
@Test
5757
void shouldUpdateRequestLocatorName() {
58-
final ServerConfiguration context = new ServerConfiguration();
58+
final ServerConfiguration context = new ServerConfiguration("any");
5959
context.setParameterValue("request", "incoming");
6060
MatcherAssert.assertThat(
6161
"Context server should update its incoming request locator's name",
@@ -66,7 +66,7 @@ void shouldUpdateRequestLocatorName() {
6666

6767
@Test
6868
void shouldUpdateCommandAvailabilityOutcome() {
69-
final ServerConfiguration context = new ServerConfiguration();
69+
final ServerConfiguration context = new ServerConfiguration("any");
7070
context.setParameterValue("command", "enabled");
7171
MatcherAssert.assertThat(
7272
"Context server should update its command availability outcome",

src/test/java/ru/ewc/checklogic/server/ContextPageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class ContextPageTest {
4141
void shouldCreateMockServer() {
4242
final ContextPage target = new ContextPage(
4343
ServerInstance.testable(),
44-
new ServerConfiguration()
44+
new ServerConfiguration("any")
4545
);
4646
final Response response = target.contextPage(ServerTestObjects.emptyRequest());
4747
MatcherAssert.assertThat(

src/test/java/ru/ewc/checklogic/server/config/ConfigPageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
final class ConfigPageTest {
3838
@Test
3939
void shouldNotUpdateParametersNotPresentInTheRequest() {
40-
final ConfigPage target = new ConfigPage(new ServerConfiguration());
40+
final ConfigPage target = new ConfigPage(new ServerConfiguration("any"));
4141
target.updateParametersFrom(ServerTestObjects.emptyRequest());
4242
MatcherAssert.assertThat(
4343
"Parameters should not be updated if the Request is empty",
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
locators:
2-
- request
32
- table
43
- cells

0 commit comments

Comments
 (0)