Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike/new decita syntax #59

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<dependency>
<groupId>io.github.nergal-perm</groupId>
<artifactId>java-decita</artifactId>
<version>0.7.1</version>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>com.renomad</groupId>
Expand All @@ -125,11 +125,6 @@
<artifactId>jcabi-matchers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/ru/ewc/checklogic/FileStateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.SneakyThrows;
import org.yaml.snakeyaml.Yaml;
import ru.ewc.decisions.api.Locator;
Expand Down Expand Up @@ -64,7 +63,7 @@ public State initialState() {
state = FileStateFactory.stateFromAppConfig(file);
state.locators().putAll(this.locatorsFromFile());
} catch (final NoSuchFileException exception) {
state = new NullState(Map.of());
state = State.EMPTY;
}
return state;
}
Expand Down Expand Up @@ -97,7 +96,7 @@ private Map<String, Locator> locatorsFromFile() {
"There is no Arrange section in the test file, you should add one"
);
}
raw.forEach((name, data) -> locators.put(name, new InMemoryStorage(data)));
raw.forEach((name, data) -> locators.put(name, new InMemoryStorage(name, data)));
}
return locators;
}
Expand All @@ -106,14 +105,9 @@ private Map<String, Locator> locatorsFromFile() {
@SuppressWarnings("unchecked")
private static State stateFromAppConfig(final InputStream file) {
final Map<String, Object> config = new Yaml().load(file);
final Stream<String> names = ((List<String>) config.get("locators")).stream();
return new State(
names.collect(
Collectors.toMap(
name -> name,
name -> new InMemoryStorage(new HashMap<>())
)
)
return new State(((List<String>) config.get("locators")).stream()
.map(name -> new InMemoryStorage(name, new HashMap<>()))
.collect(Collectors.toList())
);
}
}
101 changes: 0 additions & 101 deletions src/main/java/ru/ewc/checklogic/FileUtils.java

This file was deleted.

21 changes: 18 additions & 3 deletions src/main/java/ru/ewc/checklogic/InMemoryStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public final class InMemoryStorage implements Locator {
/**
* The name of this instance.
*/
private final String name;

/**
* Simple key-value storage.
*/
Expand All @@ -45,14 +50,19 @@ public final class InMemoryStorage implements Locator {
/**
* Ctor.
*
* @param name This instance name, used to find the storage among other Locators.
* @param storage The pre-filled key-value storage to start with.
*/
public InMemoryStorage(final Map<String, Object> storage) {
public InMemoryStorage(final String name, final Map<String, Object> storage) {
this.name = name;
this.storage = storage;
}

public static InMemoryStorage from(final List<String> values) {
final InMemoryStorage storage = new InMemoryStorage(HashMap.newHashMap(values.size()));
public static InMemoryStorage from(final String name, final List<String> values) {
final InMemoryStorage storage = new InMemoryStorage(
name,
HashMap.newHashMap(values.size())
);
values.forEach(
value -> {
final String[] split = value.split(":");
Expand All @@ -75,4 +85,9 @@ public void setFragmentValue(final String fragment, final String value) {
public Map<String, Object> state() {
return this.storage;
}

@Override
public String locatorName() {
return this.name;
}
}
2 changes: 1 addition & 1 deletion src/main/java/ru/ewc/checklogic/LogicChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void main(final String[] args) {
}
final String root = args[0];
final ServerContextFactory factory = ServerContextFactory.create(root);
final FullServerContext context = factory.initialState();
final ServerInstance context = factory.initialState();
final FullSystem minum = FullSystem.initialize();
final WebFramework web = minum.getWebFramework();
registerEndpoints(web, new ConfigPage(factory.configuration()));
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/ru/ewc/checklogic/MockStateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package ru.ewc.checklogic;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import ru.ewc.state.State;

Expand All @@ -40,9 +41,9 @@ public MockStateFactory(final String root) {
@Override
public State initialState() {
return new State(
Map.of(
"locator", new InMemoryStorage(Map.of("fragment", "value")),
"request", new InMemoryStorage(Map.of())
List.of(
new InMemoryStorage("locator", Map.of("fragment", "value")),
new InMemoryStorage("request", Map.of())
)
);
}
Expand Down
39 changes: 0 additions & 39 deletions src/main/java/ru/ewc/checklogic/NullState.java

This file was deleted.

35 changes: 10 additions & 25 deletions src/main/java/ru/ewc/checklogic/ServerContextFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
*/
package ru.ewc.checklogic;

import java.io.InputStream;
import java.net.URI;
import java.nio.file.Path;
import ru.ewc.decisions.api.ComputationContext;

/**
* I am a factory for creating server contexts.
Expand Down Expand Up @@ -78,35 +79,19 @@ public static ServerContextFactory create(final String root) {
*
* @return A new server context initialized with the basic set of empty Locators.
*/
public FullServerContext initialState() {
final FullServerContext result = new FullServerContext(
this.factory,
Path.of(this.root, "tables").toUri(),
Path.of(this.root, "commands").toUri(),
this.config
);
result.cache("command", "available");
result.cache("request", "request");
return result;
public ServerInstance initialState() {
return new ServerInstance(this.factory, this.tablesFolder(), this.config);
}

/**
* Creates a new server context from a state file. Used in state-based tests, where each test
* gets its own initial state, described in test "Arrange" section.
*
* @param file The stream of the test file's contents.
* @return A new server context initialized with state described in test file.
*/
public FullServerContext fromStateFile(final InputStream file) {
return new FullServerContext(
this.factory.with(file),
Path.of(this.root, "tables").toUri(),
Path.of(this.root, "commands").toUri(),
this.config
);
public ComputationContext context() {
return new ComputationContext(this.factory.initialState(), this.tablesFolder());
}

public ServerConfiguration configuration() {
return this.config;
}

private URI tablesFolder() {
return Path.of(this.root, "tables").toUri();
}
}
Loading
Loading