Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Jun 20, 2024
1 parent b53ba40 commit 72b1822
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
2 changes: 1 addition & 1 deletion java/src/main/java/io/cucumber/query/LineageReducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.cucumber.messages.types.Scenario;
import io.cucumber.messages.types.TableRow;

interface LineageReducer<T> {
public interface LineageReducer<T> {
default void add(GherkinDocument document) {

}
Expand Down
24 changes: 24 additions & 0 deletions java/src/main/java/io/cucumber/query/LocationOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.cucumber.query;

import io.cucumber.messages.types.Location;
import io.cucumber.messages.types.Scenario;
import io.cucumber.messages.types.TableRow;

class LocationOf implements LineageReducer<Location> {
private Location location;

@Override
public void add(Scenario scenario) {
location = scenario.getLocation();
}

@Override
public void add(TableRow example, int index) {
location = example.getLocation();
}

@Override
public Location finish() {
return location;
}
}
58 changes: 53 additions & 5 deletions java/src/main/java/io/cucumber/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.cucumber.messages.types.Examples;
import io.cucumber.messages.types.Feature;
import io.cucumber.messages.types.GherkinDocument;
import io.cucumber.messages.types.Hook;
import io.cucumber.messages.types.Location;
import io.cucumber.messages.types.Pickle;
import io.cucumber.messages.types.PickleStep;
import io.cucumber.messages.types.Rule;
Expand All @@ -20,6 +22,7 @@
import io.cucumber.messages.types.TestStepFinished;
import io.cucumber.messages.types.TestStepResult;
import io.cucumber.messages.types.TestStepResultStatus;
import io.cucumber.messages.types.TestStepStarted;
import io.cucumber.messages.types.Timestamp;
import io.cucumber.query.LineageReducerStrategy.Descending;

Expand Down Expand Up @@ -64,6 +67,7 @@
public final class Query {
private final Comparator<TestStepResult> testStepResultComparator = nullsFirst(comparing(o -> o.getStatus().ordinal()));
private final Deque<TestCaseStarted> testCaseStarted = new ConcurrentLinkedDeque<>();
private final Map<String, TestCaseStarted> testCaseStartedById = new ConcurrentHashMap<>();
private final Map<String, TestCaseFinished> testCaseFinishedByTestCaseStartedId = new ConcurrentHashMap<>();
private final Map<String, List<TestStepFinished>> testStepsFinishedByTestCaseStartedId = new ConcurrentHashMap<>();
private final Map<String, Pickle> pickleById = new ConcurrentHashMap<>();
Expand All @@ -72,6 +76,7 @@ public final class Query {
private final Map<String, TestStep> testStepById = new ConcurrentHashMap<>();
private final Map<String, PickleStep> pickleStepById = new ConcurrentHashMap<>();
private final Map<Object, Lineage> lineageById = new ConcurrentHashMap<>();
private final Map<String, Hook> hookById = new ConcurrentHashMap<>();
private TestRunStarted testRunStarted;
private TestRunFinished testRunFinished;

Expand Down Expand Up @@ -137,6 +142,11 @@ public Optional<Feature> findFeatureBy(TestCaseStarted testCaseStarted) {
return findLineageBy(testCaseStarted).flatMap(Lineage::feature);
}

public Optional<Hook> findHookBy(TestStep testStep) {
requireNonNull(testStep);
return testStep.getHookId().map(hookById::get);
}

public Optional<TestStepResult> findMostSevereTestStepResulBy(TestCaseStarted testCaseStarted) {
requireNonNull(testCaseStarted);
return findTestStepsFinishedBy(testCaseStarted)
Expand Down Expand Up @@ -193,12 +203,16 @@ public String findNameOf(TableRow element, NamingStrategy namingStrategy) {
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

public String findNameOf(Pickle element, NamingStrategy namingStrategy) {
public Optional<String> findNameOf(Pickle element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
return findLineageBy(element)
.map(lineage -> namingStrategy.reduce(lineage, element))
.orElseThrow(createElementWasNotPartOfThisQueryObject());
.map(lineage -> namingStrategy.reduce(lineage, element));
}

public Optional<Location> findLocationOf(Pickle element) {
requireNonNull(element);
return reduceLinageOf(element, LocationOf::new);
}

private static Supplier<IllegalArgumentException> createElementWasNotPartOfThisQueryObject() {
Expand Down Expand Up @@ -254,7 +268,7 @@ <T> Optional<T> reduceLinageOf(TableRow element, Supplier<LineageReducer<T>> red
.map(strategy::reduce);
}

<T> Optional<T> reduceLinageOf(Pickle element, Supplier<LineageReducer<T>> reducerSupplier) {
public <T> Optional<T> reduceLinageOf(Pickle element, Supplier<LineageReducer<T>> reducerSupplier) {
requireNonNull(element);
requireNonNull(reducerSupplier);
Descending<T> strategy = new Descending<>(reducerSupplier);
Expand All @@ -269,8 +283,15 @@ public Optional<Pickle> findPickleBy(TestCaseStarted testCaseStarted) {
.map(pickleById::get);
}

public Optional<Pickle> findPickleBy(TestStepStarted testStepStarted) {
requireNonNull(testStepStarted);
return findTestCaseBy(testStepStarted)
.map(TestCase::getPickleId)
.map(pickleById::get);
}

public Optional<PickleStep> findPickleStepBy(TestStep testStep) {
requireNonNull(testCaseStarted);
requireNonNull(testStep);
return testStep.getPickleStepId()
.map(pickleStepById::get);
}
Expand All @@ -281,11 +302,27 @@ public Optional<Step> findStepBy(PickleStep pickleStep) {
return ofNullable(stepById.get(stepId));
}

public Optional<Step> findStepBy(TestStep testStep) {
requireNonNull(testStep);
return findPickleStepBy(testStep)
.flatMap(this::findStepBy);
}

public Optional<TestCase> findTestCaseBy(TestCaseStarted testCaseStarted) {
requireNonNull(testCaseStarted);
return ofNullable(testCaseById.get(testCaseStarted.getTestCaseId()));
}

public Optional<TestCase> findTestCaseBy(TestStepStarted testCaseStarted) {
return findTestCaseStartedBy(testCaseStarted)
.flatMap(this::findTestCaseBy);
}

public Optional<TestCaseStarted> findTestCaseStartedBy(TestStepStarted testStepStarted) {
requireNonNull(testStepStarted);
return ofNullable(testCaseStartedById.get(testStepStarted.getTestCaseStartedId()));
}

public Optional<Duration> findTestCaseDurationBy(TestCaseStarted testCaseStarted) {
requireNonNull(testCaseStarted);
Timestamp started = testCaseStarted.getTimestamp();
Expand Down Expand Up @@ -321,6 +358,11 @@ public Optional<TestRunStarted> findTestRunStarted() {
return ofNullable(testRunStarted);
}

public Optional<TestStep> findTestStepBy(TestStepStarted testStepStarted) {
requireNonNull(testStepStarted);
return ofNullable(testStepById.get(testStepStarted.getTestStepId()));
}

public Optional<TestStep> findTestStepBy(TestStepFinished testStepFinished) {
requireNonNull(testStepFinished);
return ofNullable(testStepById.get(testStepFinished.getTestStepId()));
Expand Down Expand Up @@ -351,6 +393,7 @@ public void update(Envelope envelope) {
envelope.getGherkinDocument().ifPresent(this::updateGherkinDocument);
envelope.getPickle().ifPresent(this::updatePickle);
envelope.getTestCase().ifPresent(this::updateTestCase);
envelope.getHook().ifPresent(this::updateHook);
}

private Optional<Lineage> findLineageBy(GherkinDocument element) {
Expand Down Expand Up @@ -397,6 +440,7 @@ private Optional<Lineage> findLineageBy(TestCaseStarted testCaseStarted) {

private void updateTestCaseStarted(TestCaseStarted testCaseStarted) {
this.testCaseStarted.add(testCaseStarted);
this.testCaseStartedById.put(testCaseStarted.getId(), testCaseStarted);
}

private void updateTestCase(TestCase event) {
Expand Down Expand Up @@ -450,6 +494,10 @@ private void updateSteps(List<Step> steps) {
steps.forEach(step -> stepById.put(step.getId(), step));
}

private void updateHook(Hook event) {
this.hookById.put(event.getId(), event);
}

private <K, E> BiFunction<K, List<E>, List<E>> updateList(E element) {
return (key, existing) -> {
if (existing != null) {
Expand Down

0 comments on commit 72b1822

Please sign in to comment.