diff --git a/pom.xml b/pom.xml index 220180b..bedde4e 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,13 @@ 1.14.2 test + + + ch.qos.logback + logback-classic + 1.2.3 + test + @@ -83,6 +90,10 @@ org.apache.maven.plugins maven-project-info-reports-plugin + + maven-surefire-plugin + 2.22.2 + diff --git a/src/main/java/org/assertj/neo4j/api/BaseDriverAssert.java b/src/main/java/org/assertj/neo4j/api/BaseDriverAssert.java deleted file mode 100644 index ef83ef1..0000000 --- a/src/main/java/org/assertj/neo4j/api/BaseDriverAssert.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.assertj.neo4j.api; - -import org.assertj.core.api.AbstractAssert; -import org.neo4j.driver.Driver; -import org.neo4j.driver.Result; -import org.neo4j.driver.Session; -import org.neo4j.driver.TransactionConfig; - -import java.util.function.Consumer; - -/** - * @author patouche - 5/26/20. - */ -public class BaseDriverAssert - extends AbstractAssert - implements DriverAssert { - - public BaseDriverAssert(final Driver driver) { - super(driver, Driver.class); - } - - @Override - public Driver getDriver() { - return this.actual; - } - - private DriverResultAssert executing(final String cypherQuery) { - try (Session session = this.getDriver().session()) { - final Result result = session.run(cypherQuery, TransactionConfig.builder() - .build()); - return new DriverResultAssert(getDriver(), result); - } - } - - - public BaseDriverAssert executing(final String cypherQuery, final Consumer fn) { - final DriverResultAssert executing = this.executing(cypherQuery); - fn.accept(executing); - return myself; - } - -} diff --git a/src/main/java/org/assertj/neo4j/api/DriverAssert.java b/src/main/java/org/assertj/neo4j/api/DriverAssert.java index 4e5869f..143a7e5 100644 --- a/src/main/java/org/assertj/neo4j/api/DriverAssert.java +++ b/src/main/java/org/assertj/neo4j/api/DriverAssert.java @@ -1,20 +1,49 @@ package org.assertj.neo4j.api; +import org.assertj.core.api.AbstractAssert; import org.neo4j.driver.Driver; import org.neo4j.driver.Result; import org.neo4j.driver.Session; import org.neo4j.driver.TransactionConfig; +import java.util.concurrent.Callable; import java.util.function.Consumer; +import java.util.stream.Stream; /** * @author patouche - 5/26/20. */ -interface DriverAssert { - - Driver getDriver(); - - - - +public class DriverAssert extends AbstractAssert { + + public DriverAssert(final Driver driver) { + super(driver, DriverAssert.class); + } + + private DriverResultAssert executing(final String query) { + try (Session session = this.actual.session()) { + final Result result = session.run(query, TransactionConfig.builder() + .build()); + return new DriverResultAssert(actual, result); + } + } + + public DriverAssert executing(final String query, final Consumer fn) { + final DriverResultAssert executing = this.executing(query).as("Query results of '%s'", query); + fn.accept(executing); + return myself; + } + + public DriverAssert withDataset(final String... queries) { + Stream.of(queries).forEach(q -> { + try (final Session session = this.actual.session()) { + session.run(q, TransactionConfig.builder().build()); + } + }); + return myself; + } + + public DriverAssert when(Runnable runnable) { + runnable.run(); + return myself; + } } diff --git a/src/main/java/org/assertj/neo4j/api/DriverAssertions.java b/src/main/java/org/assertj/neo4j/api/DriverAssertions.java index 14dc778..239d594 100644 --- a/src/main/java/org/assertj/neo4j/api/DriverAssertions.java +++ b/src/main/java/org/assertj/neo4j/api/DriverAssertions.java @@ -7,9 +7,8 @@ */ public class DriverAssertions { - public static BaseDriverAssert assertThat(Driver driver) { - return new BaseDriverAssert(driver); + public static DriverAssert assertThat(Driver driver) { + return new DriverAssert(driver); } - } diff --git a/src/main/java/org/assertj/neo4j/api/DriverResultAssert.java b/src/main/java/org/assertj/neo4j/api/DriverResultAssert.java index d0cd12e..39d983f 100644 --- a/src/main/java/org/assertj/neo4j/api/DriverResultAssert.java +++ b/src/main/java/org/assertj/neo4j/api/DriverResultAssert.java @@ -7,17 +7,16 @@ import org.neo4j.driver.Result; import java.util.Map; +import java.util.Objects; import java.util.stream.Stream; /** * @author patouche - 5/26/20. */ -public class DriverResultAssert - extends AbstractIterableAssert>, - Map, - MapAssert> - implements DriverAssert { +public class DriverResultAssert extends AbstractIterableAssert>, + Map, + MapAssert> { private final Driver driver; @@ -32,18 +31,11 @@ private DriverResultAssert(final Driver driver, final Iterable toAssert(final Map value, final String description) { return new MapAssert<>(value).as(description); } - - @Override protected DriverResultAssert newAbstractIterableAssert(final Iterable> iterable) { return new DriverResultAssert(this.driver, iterable); @@ -56,4 +48,9 @@ public MapAssert single() { public DriverResultAssert filteredOnKeys(final String... keys) { return filteredOn(m -> Stream.of(keys).allMatch(m::containsKey)); } + + public DriverResultAssert filteredOnKeyValue(final String key, final Object value) { + return filteredOn(m -> Objects.equals(m.get(key), value)); + } + } diff --git a/src/test/java/org/assertj/neo4j/api/IntegrationTest.java b/src/test/java/org/assertj/neo4j/api/IntegrationTest.java index 8f3ed23..18ca235 100644 --- a/src/test/java/org/assertj/neo4j/api/IntegrationTest.java +++ b/src/test/java/org/assertj/neo4j/api/IntegrationTest.java @@ -2,10 +2,9 @@ import org.junit.ClassRule; import org.junit.Test; -import org.neo4j.driver.AuthTokens; import org.neo4j.driver.Driver; import org.neo4j.driver.GraphDatabase; -import org.neo4j.driver.Record; +import org.neo4j.driver.Session; import org.testcontainers.containers.Neo4jContainer; /** @@ -18,26 +17,47 @@ public class IntegrationTest { public static Neo4jContainer NEO4J_CONTAINER = new Neo4jContainer<>("neo4j:3.4.18") .withAdminPassword(null); + private static Driver dataset(Neo4jContainer container) { + final Driver driver = GraphDatabase.driver(container.getBoltUrl()); + return driver; + } + @Test public void version_3_4() throws Exception { // GIVEN + final Driver driver = GraphDatabase.driver(NEO4J_CONTAINER.getBoltUrl()); + // WHEN // THEN - final Driver driver = GraphDatabase.driver(NEO4J_CONTAINER.getBoltUrl()); DriverAssertions.assertThat(driver) - .executing("MATCH (n) RETURN", result -> { - result - .filteredOnKeys("key-1", "key-2") - .single(); + .withDataset( + "CREATE (:Language {name: \"Java\" })", + "CREATE (:Language {name: \"Golang\" })", + "CREATE (:Language {name: \"Kotlin\" })", + "CREATE (:Language {name: \"Python\" })", + "CREATE (:Language {name: \"JavaScript\" })", + "CREATE (:Language {name: \"Rust\" })", + "CREATE (:Language {name: \"Elixir\" })", + "CREATE (:Language {name: \"C\" })", + "CREATE (:Language {name: \"C++\" })", + "CREATE (:Language {name: \"Lisp\" })" + ) + .when(() -> { + // Should invoke test method + try (Session session = driver.session()) { + session.run("CREATE (:Language {name: \"Scala\" })"); + } }) - .executing("MATCH (n) RETURN", result -> { - result - .filteredOnKeys("key-2", "key-3") - .single(); - }); - + .executing("MATCH (n1) RETURN n1", result -> result + .hasSize(11)) + .executing("MATCH (n2) RETURN n2", result -> result + .filteredOnKeys("name") + .hasSize(11)) + .executing("MATCH (n:Language) RETURN n", result -> result + .filteredOnKeyValue("name", "Scala") + .single()); } }