-
Notifications
You must be signed in to change notification settings - Fork 199
/
GraphAssertions.java
74 lines (66 loc) · 3.14 KB
/
GraphAssertions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Hibernate OGM, Domain model persistence for NoSQL datastores
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.ogm.datastore.neo4j.test.dsl;
import static org.fest.assertions.Assertions.assertThat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.fest.assertions.Fail;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.ResourceIterator;
/**
* Assertion methods to check the mapping of nodes and relationships in Neo4j.
*
* @author Davide D'Alto
*/
public class GraphAssertions {
public static NodeForGraphAssertions node(String alias, String... labels) {
return new NodeForGraphAssertions( alias, labels);
}
public static void assertThatExists(ExecutionEngine engine, NodeForGraphAssertions node) throws Exception {
String nodeAsCypher = node.toCypher();
String query = "MATCH " + nodeAsCypher + " RETURN " + node.getAlias();
ResourceIterator<Object> columnAs = engine.execute( query, node.getParams() ).columnAs( node.getAlias() );
assertThat( columnAs.hasNext() ).as( "Node [" + node.getAlias() + "] not found, Looked for " + nodeAsCypher + " with parameters: " + node.getParams() ).isTrue();
PropertyContainer propertyContainer = (PropertyContainer) columnAs.next();
Iterable<String> propertyKeys = propertyContainer.getPropertyKeys();
List<String> unexpectedProperties = new ArrayList<String>();
Set<String> expectedProperties = node.getProperties().keySet();
for ( Iterator<String> iterator = propertyKeys.iterator(); iterator.hasNext(); ) {
String actual = iterator.next();
if ( !expectedProperties.contains( actual ) ) {
unexpectedProperties.add( actual );
}
}
List<String> missingProperties = new ArrayList<String>();
if ( expectedProperties != null ) {
for ( String expected : expectedProperties ) {
if ( !propertyContainer.hasProperty( expected ) ) {
missingProperties.add( expected );
}
}
}
assertThat( unexpectedProperties ).as( "Unexpected properties for node [" + node.getAlias() + "]" ).isEmpty();
assertThat( missingProperties ).as( "Missing properties for node [" + node.getAlias() + "]" ).isEmpty();
if ( columnAs.hasNext() ) {
Fail.fail( "Unexpected result returned: " + columnAs.next() );
}
}
public static void assertThatExists(ExecutionEngine engine, RelationshipsChainForGraphAssertions relationship) throws Exception {
String relationshipAsCypher = relationship.toCypher();
NodeForGraphAssertions node = relationship.getStart();
String query = "MATCH " + relationshipAsCypher + " RETURN " + node.getAlias();
ResourceIterator<Object> columnAs = engine.execute( query, relationship.getParams() ).columnAs( node.getAlias() );
assertThat( columnAs.hasNext() ).as( "Relationships not found, Looked for " + relationshipAsCypher + " with parameters: " + relationship.getParams() ).isTrue();
columnAs.next();
if ( columnAs.hasNext() ) {
Fail.fail( "Unexpected result returned: " + columnAs.next() );
}
}
}