|
| 1 | +package org.neo4j.driver.v1.tck; |
| 2 | + |
| 3 | +import cucumber.api.java.en.And; |
| 4 | +import cucumber.api.java.en.Then; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Iterator; |
| 10 | + |
| 11 | +import org.neo4j.driver.internal.InternalPath; |
| 12 | +import org.neo4j.driver.internal.InternalRelationship; |
| 13 | +import org.neo4j.driver.internal.value.PathValue; |
| 14 | +import org.neo4j.driver.v1.Entity; |
| 15 | +import org.neo4j.driver.v1.Identity; |
| 16 | +import org.neo4j.driver.v1.Node; |
| 17 | +import org.neo4j.driver.v1.Pair; |
| 18 | +import org.neo4j.driver.v1.Path; |
| 19 | +import org.neo4j.driver.v1.Record; |
| 20 | +import org.neo4j.driver.v1.Relationship; |
| 21 | +import org.neo4j.driver.v1.Value; |
| 22 | + |
| 23 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 24 | +import static org.hamcrest.CoreMatchers.not; |
| 25 | +import static org.junit.Assert.assertThat; |
| 26 | +import static org.junit.Assert.assertTrue; |
| 27 | +import static org.neo4j.driver.v1.tck.DriverComplianceIT.session; |
| 28 | + |
| 29 | +public class DriverEqualitySteps |
| 30 | +{ |
| 31 | + HashMap<String,Value> savedValues = new HashMap<>(); |
| 32 | + |
| 33 | + @And( "^`(.*)` is single value result of: (.*)$" ) |
| 34 | + public void valueIsSingleValueResultOfMATCHNLabelRETURNN( String key, String statement ) throws Throwable |
| 35 | + { |
| 36 | + Record r = session.run( statement ).single(); |
| 37 | + assertThat( r.size(), equalTo( 1 ) ); |
| 38 | + savedValues.put( key, r.get( 0 ) ); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + @Then( "^saved values should all equal$" ) |
| 43 | + public void savedValuesShouldAllEqual() throws Throwable |
| 44 | + { |
| 45 | + assertTrue( savedValues.values().size() > 1 ); |
| 46 | + Collection values = savedValues.values(); |
| 47 | + Iterator<Value> itr = values.iterator(); |
| 48 | + Value v = itr.next(); |
| 49 | + while ( itr.hasNext() ) |
| 50 | + { |
| 51 | + assertThat( v, equalTo( itr.next() ) ); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Then( "^none of the saved values should be equal$" ) |
| 56 | + public void noneOfTheSavedValuesShouldBeEqual() throws Throwable |
| 57 | + { |
| 58 | + assertTrue( savedValues.values().size() > 1 ); |
| 59 | + Collection values = savedValues.values(); |
| 60 | + Iterator<Value> itr = values.iterator(); |
| 61 | + Value v = itr.next(); |
| 62 | + while ( itr.hasNext() ) |
| 63 | + { |
| 64 | + assertThat( v, not(equalTo( itr.next() ) ) ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @And( "^`(.*)` is a copy of `(.*)` path with flipped relationship direction$" ) |
| 69 | + public void valueIsACopyOfValuePathWithFlippedRelationshipDirection( String key1, String key2 ) throws |
| 70 | + Throwable |
| 71 | + { |
| 72 | + Path path = savedValues.get( key2 ).asPath(); |
| 73 | + Iterable<Relationship> relationships = path.relationships(); |
| 74 | + Iterator<Node> nodes= path.nodes().iterator(); |
| 75 | + |
| 76 | + ArrayList<Entity> entities = new ArrayList<>( ); |
| 77 | + entities.add( nodes.next() ); |
| 78 | + |
| 79 | + for ( Relationship rel : relationships ) |
| 80 | + { |
| 81 | + Identity id = rel.identity(); |
| 82 | + Identity end = rel.start(); |
| 83 | + Identity start = rel.end(); |
| 84 | + HashMap<String,Value> properties = new HashMap<>(); |
| 85 | + for ( Pair<String,Value> pair : rel.properties() ) |
| 86 | + { |
| 87 | + properties.put( pair.key(), pair.value() ); |
| 88 | + } |
| 89 | + entities.add( |
| 90 | + new InternalRelationship( |
| 91 | + id.asLong(), |
| 92 | + start.asLong(), |
| 93 | + end.asLong(), |
| 94 | + rel.type(), |
| 95 | + properties ) ); |
| 96 | + entities.add( nodes.next() ); |
| 97 | + } |
| 98 | + Path newPath = new InternalPath( entities ); |
| 99 | + savedValues.put( key1, new PathValue( newPath) ); |
| 100 | + } |
| 101 | +} |
0 commit comments