forked from joel-costigliola/assertj-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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<BaseDriverAssert, Driver> | ||
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<DriverResultAssert> fn) { | ||
final DriverResultAssert executing = this.executing(cypherQuery); | ||
fn.accept(executing); | ||
return myself; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.assertj.neo4j.api; | ||
|
||
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. | ||
*/ | ||
interface DriverAssert { | ||
|
||
Driver getDriver(); | ||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.assertj.neo4j.api; | ||
|
||
import org.neo4j.driver.Driver; | ||
|
||
/** | ||
* @author patouche - 5/26/20. | ||
*/ | ||
public class DriverAssertions { | ||
|
||
public static BaseDriverAssert assertThat(Driver driver) { | ||
return new BaseDriverAssert(driver); | ||
} | ||
|
||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/org/assertj/neo4j/api/DriverResultAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.assertj.neo4j.api; | ||
|
||
import org.assertj.core.api.AbstractIterableAssert; | ||
import org.assertj.core.api.MapAssert; | ||
import org.neo4j.driver.Driver; | ||
import org.neo4j.driver.Record; | ||
import org.neo4j.driver.Result; | ||
|
||
import java.util.Map; | ||
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 { | ||
|
||
private final Driver driver; | ||
|
||
public DriverResultAssert(final Driver driver, final Result result) { | ||
// List<Map<String, Object>> | ||
this(driver, result.list(Record::asMap)); | ||
} | ||
|
||
private DriverResultAssert(final Driver driver, final Iterable<? extends Map<String, Object>> result) { | ||
// List<Map<String, Object>> | ||
super(result, DriverResultAssert.class); | ||
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); | ||
} | ||
|
||
public MapAssert<String, Object> single() { | ||
return hasSize(1).first(); | ||
} | ||
|
||
public DriverResultAssert filteredOnKeys(final String... keys) { | ||
return filteredOn(m -> Stream.of(keys).allMatch(m::containsKey)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.assertj.neo4j.api; | ||
|
||
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.testcontainers.containers.Neo4jContainer; | ||
|
||
/** | ||
* @author patouche - 5/26/20. | ||
*/ | ||
|
||
public class IntegrationTest { | ||
|
||
@ClassRule | ||
public static Neo4jContainer NEO4J_CONTAINER = new Neo4jContainer<>("neo4j:3.4.18") | ||
.withAdminPassword(null); | ||
|
||
@Test | ||
public void version_3_4() throws Exception { | ||
|
||
// GIVEN | ||
// 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(); | ||
}) | ||
.executing("MATCH (n) RETURN", result -> { | ||
result | ||
.filteredOnKeys("key-2", "key-3") | ||
.single(); | ||
}); | ||
|
||
|
||
} | ||
} |