diff --git a/graphwalker-core/src/test/java/org/graphwalker/core/condition/ComplexConditionsTest.java b/graphwalker-core/src/test/java/org/graphwalker/core/condition/ComplexConditionsTest.java index 0013b21e..ebe29577 100644 --- a/graphwalker-core/src/test/java/org/graphwalker/core/condition/ComplexConditionsTest.java +++ b/graphwalker-core/src/test/java/org/graphwalker/core/condition/ComplexConditionsTest.java @@ -27,6 +27,7 @@ */ import org.graphwalker.core.generator.RandomPath; +import org.graphwalker.core.generator.SingletonRandomGenerator; import org.graphwalker.core.machine.Context; import org.graphwalker.core.machine.Machine; import org.graphwalker.core.machine.SimpleMachine; @@ -36,6 +37,9 @@ import java.util.concurrent.TimeUnit; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + /** * @author Nils Olsson */ @@ -64,6 +68,7 @@ public class ComplexConditionsTest { @Test public void combinedCondition_2_vertices() throws Exception { + SingletonRandomGenerator.setSeed(1111); Context context = new TestExecutionContext(); CombinedCondition condition = new CombinedCondition(); condition.addStopCondition(new ReachedVertex("v_Browse")); @@ -75,10 +80,12 @@ public void combinedCondition_2_vertices() throws Exception { while (machine.hasNextStep()) { machine.getNextStep(); } + assertThat(machine.getCurrentContext().getCurrentElement().getName(), is("v_LoginPrompted")); } @Test public void combinedCondition_2_edges() throws Exception { + SingletonRandomGenerator.setSeed(1111); Context context = new TestExecutionContext(); CombinedCondition condition = new CombinedCondition(); condition.addStopCondition(new ReachedEdge("e_ToggleRememberMe")); @@ -90,10 +97,12 @@ public void combinedCondition_2_edges() throws Exception { while (machine.hasNextStep()) { machine.getNextStep(); } + assertThat(machine.getCurrentContext().getCurrentElement().getName(), is("e_InvalidCredentials")); } @Test public void combinedCondition_vertex_edge() throws Exception { + SingletonRandomGenerator.setSeed(1111); Context context = new TestExecutionContext(); CombinedCondition condition = new CombinedCondition(); condition.addStopCondition(new ReachedVertex("v_Browse")); @@ -105,10 +114,12 @@ public void combinedCondition_vertex_edge() throws Exception { while (machine.hasNextStep()) { machine.getNextStep(); } + assertThat(machine.getCurrentContext().getCurrentElement().getName(), is("e_InvalidCredentials")); } @Test public void alternativeCondition_2_vertices() throws Exception { + SingletonRandomGenerator.setSeed(1111); Context context = new TestExecutionContext(); AlternativeCondition condition = new AlternativeCondition(); condition.addStopCondition(new ReachedVertex("v_Browse")); @@ -120,10 +131,12 @@ public void alternativeCondition_2_vertices() throws Exception { while (machine.hasNextStep()) { machine.getNextStep(); } + assertThat(machine.getCurrentContext().getCurrentElement().getName(), is("v_LoginPrompted")); } @Test public void alternativeCondition_2_edges() throws Exception { + SingletonRandomGenerator.setSeed(1111); Context context = new TestExecutionContext(); AlternativeCondition condition = new AlternativeCondition(); condition.addStopCondition(new ReachedEdge("e_ToggleRememberMe")); @@ -135,10 +148,12 @@ public void alternativeCondition_2_edges() throws Exception { while (machine.hasNextStep()) { machine.getNextStep(); } + assertThat(machine.getCurrentContext().getCurrentElement().getName(), is("e_InvalidCredentials")); } @Test public void alternativeCondition_vertex_edge() throws Exception { + SingletonRandomGenerator.setSeed(1111); Context context = new TestExecutionContext(); AlternativeCondition condition = new AlternativeCondition(); condition.addStopCondition(new ReachedVertex("v_Browse")); @@ -150,11 +165,37 @@ public void alternativeCondition_vertex_edge() throws Exception { while (machine.hasNextStep()) { machine.getNextStep(); } + assertThat(machine.getCurrentContext().getCurrentElement().getName(), is("e_InvalidCredentials")); + } + + // random((reached_vertex(v_Browse) AND edge_coverage(100)) OR time(1000)) + @Test + public void and_plus_or_a() throws Exception { + SingletonRandomGenerator.setSeed(1111); + Context context = new TestExecutionContext(); + + CombinedCondition c1 = new CombinedCondition(); + c1.addStopCondition(new ReachedVertex("v_Browse")); + c1.addStopCondition(new EdgeCoverage(100)); + + AlternativeCondition c2 = new AlternativeCondition(); + c2.addStopCondition(new TimeDuration(1000, TimeUnit.SECONDS)); + c2.addStopCondition(c1); + + context.setModel(model.build()).setPathGenerator(new RandomPath(c2)); + context.setNextElement(context.getModel().findElements("e_Init").get(0)); + + Machine machine = new SimpleMachine(context); + while (machine.hasNextStep()) { + machine.getNextStep(); + } + assertThat(machine.getCurrentContext().getCurrentElement().getName(), is("v_Browse")); } - // random((reached_vertex(v_Browse) AND edge_coverage(100)) OR time(10)) + // random((reached_vertex(v_Browse) AND edge_coverage(100)) OR reached_vertex(v_LoginPrompted)) @Test - public void and_plus_or() throws Exception { + public void and_plus_or_b() throws Exception { + SingletonRandomGenerator.setSeed(1111); Context context = new TestExecutionContext(); CombinedCondition c1 = new CombinedCondition(); @@ -162,7 +203,7 @@ public void and_plus_or() throws Exception { c1.addStopCondition(new EdgeCoverage(100)); AlternativeCondition c2 = new AlternativeCondition(); - c2.addStopCondition(new TimeDuration(10, TimeUnit.SECONDS)); + c2.addStopCondition(new ReachedVertex("v_LoginPrompted")); c2.addStopCondition(c1); context.setModel(model.build()).setPathGenerator(new RandomPath(c2)); @@ -172,5 +213,35 @@ public void and_plus_or() throws Exception { while (machine.hasNextStep()) { machine.getNextStep(); } + assertThat(machine.getCurrentContext().getCurrentElement().getName(), is("v_LoginPrompted")); + } + + // random((reached_vertex(v_Browse) AND edge_coverage(100)) OR (reached_vertex(v_ClientNotRunning) AND edge_coverage(100))) + @Test + public void and_plus_or_c() throws Exception { + SingletonRandomGenerator.setSeed(1111); + Context context = new TestExecutionContext(); + + CombinedCondition c1 = new CombinedCondition(); + c1.addStopCondition(new ReachedVertex("v_Browse")); + c1.addStopCondition(new EdgeCoverage(100)); + + CombinedCondition c2 = new CombinedCondition(); + c2.addStopCondition(new ReachedVertex("v_ClientNotRunning")); + c2.addStopCondition(new EdgeCoverage(100)); + + AlternativeCondition c3 = new AlternativeCondition(); + c3.addStopCondition(c1); + c3.addStopCondition(c2); + + + context.setModel(model.build()).setPathGenerator(new RandomPath(c3)); + context.setNextElement(context.getModel().findElements("e_Init").get(0)); + + Machine machine = new SimpleMachine(context); + while (machine.hasNextStep()) { + machine.getNextStep(); + } + assertThat(machine.getCurrentContext().getCurrentElement().getName(), is("v_Browse")); } } diff --git a/graphwalker-dsl/src/test/java/org/graphwalker/dsl/GeneratorFactoryTest.java b/graphwalker-dsl/src/test/java/org/graphwalker/dsl/GeneratorFactoryTest.java index 9bef3e1e..d0a208cd 100644 --- a/graphwalker-dsl/src/test/java/org/graphwalker/dsl/GeneratorFactoryTest.java +++ b/graphwalker-dsl/src/test/java/org/graphwalker/dsl/GeneratorFactoryTest.java @@ -331,6 +331,28 @@ public void issue_341_b() { assertThat(c2.getStopConditions().get(1), instanceOf(ReachedVertex.class)); } + @Test + public void issue_341_c() { + PathGenerator generator = GeneratorFactory.parse("random((reached_vertex(v_Browse) AND edge_coverage(100)) OR (reached_vertex(v_ClientNotRunning) AND edge_coverage(100)))"); + assertThat(generator, instanceOf(RandomPath.class)); + assertThat(generator.getStopCondition(), instanceOf(AlternativeCondition.class)); + + AlternativeCondition c1 = (AlternativeCondition) generator.getStopCondition(); + assertThat(c1.getStopConditions().size(), is(2)); + assertThat(c1.getStopConditions().get(0), instanceOf(CombinedCondition.class)); + assertThat(c1.getStopConditions().get(1), instanceOf(CombinedCondition.class)); + + CombinedCondition c2 = (CombinedCondition)c1.getStopConditions().get(0); + assertThat(c2.getStopConditions().size(), is(2)); + assertThat(c2.getStopConditions().get(0), instanceOf(ReachedVertex.class)); + assertThat(c2.getStopConditions().get(1), instanceOf(EdgeCoverage.class)); + + CombinedCondition c3 = (CombinedCondition)c1.getStopConditions().get(1); + assertThat(c3.getStopConditions().size(), is(2)); + assertThat(c3.getStopConditions().get(0), instanceOf(ReachedVertex.class)); + assertThat(c3.getStopConditions().get(1), instanceOf(EdgeCoverage.class)); + } + @Test public void predefinedPath_predefinedPathStopCondition() { PathGenerator generator = GeneratorFactory.parse("predefined_path(predefined_path)");