Skip to content

Commit

Permalink
[joel-costigliola#75] First clean after api design
Browse files Browse the repository at this point in the history
  • Loading branch information
Patouche committed May 27, 2020
1 parent 8e95b89 commit 8ece79e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 78 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
<version>1.14.2</version>
<scope>test</scope>
</dependency>
<!-- Or slf4j-nop ? -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand All @@ -83,6 +90,10 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</reporting>

Expand Down
42 changes: 0 additions & 42 deletions src/main/java/org/assertj/neo4j/api/BaseDriverAssert.java

This file was deleted.

43 changes: 36 additions & 7 deletions src/main/java/org/assertj/neo4j/api/DriverAssert.java
Original file line number Diff line number Diff line change
@@ -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<DriverAssert, Driver> {

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<DriverResultAssert> 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;
}
}
5 changes: 2 additions & 3 deletions src/main/java/org/assertj/neo4j/api/DriverAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}
23 changes: 10 additions & 13 deletions src/main/java/org/assertj/neo4j/api/DriverResultAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<DriverResultAssert,
Iterable<? extends Map<String, Object>>,
Map<String, Object>,
MapAssert<String, Object>>
implements DriverAssert {
public class DriverResultAssert extends AbstractIterableAssert<DriverResultAssert,
Iterable<? extends Map<String, Object>>,
Map<String, Object>,
MapAssert<String, Object>> {

private final Driver driver;

Expand All @@ -32,18 +31,11 @@ private DriverResultAssert(final Driver driver, final Iterable<? extends Map<Str
this.driver = driver;
}

@Override
public Driver getDriver() {
return this.driver;
}

@Override
protected MapAssert<String, Object> toAssert(final Map<String, Object> value, final String description) {
return new MapAssert<>(value).as(description);
}



@Override
protected DriverResultAssert newAbstractIterableAssert(final Iterable<? extends Map<String, Object>> iterable) {
return new DriverResultAssert(this.driver, iterable);
Expand All @@ -56,4 +48,9 @@ public MapAssert<String, Object> 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));
}

}
46 changes: 33 additions & 13 deletions src/test/java/org/assertj/neo4j/api/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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());

}
}

0 comments on commit 8ece79e

Please sign in to comment.