|
| 1 | +package org.neo4j.driver.v1.tck; |
| 2 | + |
| 3 | +import cucumber.api.DataTable; |
| 4 | +import cucumber.api.java.en.And; |
| 5 | +import cucumber.api.java.en.Then; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.neo4j.driver.v1.Record; |
| 12 | +import org.neo4j.driver.v1.Value; |
| 13 | +import org.neo4j.driver.v1.exceptions.NoSuchRecordException; |
| 14 | +import org.neo4j.driver.v1.tck.tck.util.ResultParser; |
| 15 | +import org.neo4j.driver.v1.tck.tck.util.runners.CypherStatementRunner; |
| 16 | + |
| 17 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 18 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 19 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | +import static org.neo4j.driver.v1.Values.ofValue; |
| 21 | +import static org.neo4j.driver.v1.tck.Environment.runners; |
| 22 | +import static org.neo4j.driver.v1.tck.tck.util.ResultParser.parseGiven; |
| 23 | +import static org.neo4j.driver.v1.tck.tck.util.ResultParser.parseStringValue; |
| 24 | + |
| 25 | + |
| 26 | +public class DriverStatementResultAPISteps |
| 27 | +{ |
| 28 | + private void compareSingleRecord( Record record, List<String> keys, List<String> values ) |
| 29 | + { |
| 30 | + |
| 31 | + assertThat( record.size(), equalTo( keys.size() ) ); |
| 32 | + assertThat( ResultParser.parseExpected( values, keys ), |
| 33 | + equalTo( parseGiven( record.asMap( ofValue() ) ) ) ); |
| 34 | + } |
| 35 | + |
| 36 | + @Then( "^using `Single` on `Statement Result` gives a `Record` containing:$" ) |
| 37 | + public void usingSingleOnStatementReslutGivesARecordContaining( DataTable table ) throws Throwable |
| 38 | + { |
| 39 | + List<String> keys = table.diffableRows().get( 0 ).convertedRow; |
| 40 | + List<String> values = table.diffableRows().get( 1 ).convertedRow; |
| 41 | + |
| 42 | + for ( CypherStatementRunner runner : runners ) |
| 43 | + { |
| 44 | + compareSingleRecord( runner.result().single(), keys, values ); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Then( "^using `Single` on `Statement Result` throws exception:$" ) |
| 49 | + public void usingSingleOnStatmentReslutThrowsException( DataTable table ) throws Throwable |
| 50 | + { |
| 51 | + for ( CypherStatementRunner runner : runners ) |
| 52 | + { |
| 53 | + Record single = null; |
| 54 | + boolean success = true; |
| 55 | + try |
| 56 | + { |
| 57 | + single = runner.result().single(); |
| 58 | + } |
| 59 | + catch ( NoSuchRecordException e ) |
| 60 | + { |
| 61 | + assertThat( e.getMessage().startsWith( table.diffableRows().get( 1 ).convertedRow.get( 0 ) ), |
| 62 | + equalTo( true ) ); |
| 63 | + success = false; |
| 64 | + } |
| 65 | + if ( success ) |
| 66 | + { |
| 67 | + throw new Exception( "Excpected exception to be thrown but was not! Got: " + single ); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Then( "^using `Peek` on `Statement Result` gives null$" ) |
| 73 | + public void usingPeekOnStatmentReslutGivesNull() throws Throwable |
| 74 | + { |
| 75 | + for ( CypherStatementRunner runner : runners ) |
| 76 | + { |
| 77 | + Record single = runner.result().peek(); |
| 78 | + assertThat( single, equalTo( null ) ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Then( "^using `Peek` on `Statement Result` gives a `Record` containing:$" ) |
| 83 | + public void usingPeekOnStatmentReslutGivesARecord( DataTable table ) throws Throwable |
| 84 | + { |
| 85 | + List<String> keys = table.diffableRows().get( 0 ).convertedRow; |
| 86 | + List<String> values = table.diffableRows().get( 1 ).convertedRow; |
| 87 | + for ( CypherStatementRunner runner : runners ) |
| 88 | + { |
| 89 | + compareSingleRecord( runner.result().peek(), keys, values ); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @And( "^using `Next` on `Statement Result` gives a `Record` containing:$" ) |
| 94 | + public void usingNextOnStatementResultGivesARecordContaining( DataTable table ) throws Throwable |
| 95 | + { |
| 96 | + List<String> keys = table.diffableRows().get( 0 ).convertedRow; |
| 97 | + List<String> values = table.diffableRows().get( 1 ).convertedRow; |
| 98 | + |
| 99 | + for ( CypherStatementRunner runner : runners ) |
| 100 | + { |
| 101 | + compareSingleRecord( runner.result().next(), keys, values ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @And( "^using `Next` on `Statement Result` gives null$" ) |
| 106 | + public void usingNextOnStatementResultGivesNull() throws Throwable |
| 107 | + { |
| 108 | + for ( CypherStatementRunner runner : runners ) |
| 109 | + { |
| 110 | + Record single = runner.result().next(); |
| 111 | + assertThat( single, equalTo( null ) ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Then( "^using `Keys` on `Statement Result` gives:$" ) |
| 116 | + public void usingKeysOnStatementResultGives( List<String> data ) throws Throwable |
| 117 | + { |
| 118 | + HashSet<String> expected = new HashSet<>( data ); |
| 119 | + expected.remove( data.get( 0 ) ); |
| 120 | + for ( CypherStatementRunner runner : runners ) |
| 121 | + { |
| 122 | + HashSet<String> keys = new HashSet<>( runner.result().keys() ); |
| 123 | + assertThat( keys, equalTo( expected ) ); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @And( "^using `Next` on `Statement Result` gives a `Record`$" ) |
| 128 | + public void usingNextOnStatementResultGivesARecord() throws Throwable |
| 129 | + { |
| 130 | + for ( CypherStatementRunner runner : runners ) |
| 131 | + { |
| 132 | + Record single = runner.result().next(); |
| 133 | + assertThat( single, instanceOf( Record.class ) ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Then( "^it is not possible to go back$" ) |
| 138 | + public void itIsNotPossibleToGoBack() throws Throwable |
| 139 | + { |
| 140 | + //Move along |
| 141 | + } |
| 142 | + |
| 143 | + @And( "^using `List` on `Statement Result` gives a list of size (\\d+), the previous records are lost$" ) |
| 144 | + public void usingListOnStatementResultGivesAListOfSizeThePreviousRecordsAreLost( int size ) throws Throwable |
| 145 | + { |
| 146 | + for ( CypherStatementRunner runner : runners ) |
| 147 | + { |
| 148 | + assertThat( runner.result().list().size(), equalTo( size ) ); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Then( "^iterating through the `Statement Result` should follow the native code pattern$" ) |
| 153 | + public void iteratingThroughTheStatementResultShouldFollowTheNativeCodePattern() throws Throwable |
| 154 | + { |
| 155 | + for ( CypherStatementRunner runner : runners ) |
| 156 | + { |
| 157 | + while ( runner.result().hasNext() ) |
| 158 | + { |
| 159 | + runner.result().next(); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Then( "^using `List` on `Statement Result` gives:$" ) |
| 165 | + public void usingListOnStatementResultGives( DataTable table ) throws Throwable |
| 166 | + { |
| 167 | + List<String> keys = table.topCells(); |
| 168 | + List<List<String>> expected = new ArrayList<>(); |
| 169 | + for ( int i = 1; i < table.diffableRows().size(); i++ ) |
| 170 | + { |
| 171 | + expected.add( table.diffableRows().get( i ).convertedRow ); |
| 172 | + } |
| 173 | + for ( CypherStatementRunner runner : runners ) |
| 174 | + { |
| 175 | + while ( runner.result().hasNext() ) |
| 176 | + { |
| 177 | + List<Record> list = runner.result().list(); |
| 178 | + while ( !list.isEmpty() ) |
| 179 | + { |
| 180 | + int size = list.size(); |
| 181 | + for ( List<String> expectedRecord : expected ) |
| 182 | + { |
| 183 | + try |
| 184 | + { |
| 185 | + compareSingleRecord( list.get( 0 ), keys, expectedRecord ); |
| 186 | + list.remove( 0 ); |
| 187 | + break; |
| 188 | + } |
| 189 | + catch ( AssertionError ignore ) {} |
| 190 | + } |
| 191 | + if ( size == list.size() ) |
| 192 | + { |
| 193 | + throw new Exception( "Actual does not match expected." ); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + @Then( "^using `Keys` on the single record gives:$" ) |
| 201 | + public void usingKeysOnTheSingleRecordGives( List<String> data ) throws Throwable |
| 202 | + { |
| 203 | + HashSet<String> expected = new HashSet<>( data ); |
| 204 | + expected.remove( data.get( 0 ) ); |
| 205 | + for ( CypherStatementRunner runner : runners ) |
| 206 | + { |
| 207 | + HashSet<String> keys = new HashSet<>( runner.result().single().keys() ); |
| 208 | + assertThat( keys, equalTo( expected ) ); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + @Then( "^using `Values` on the single record gives:$" ) |
| 213 | + public void usingValuesOnTheSingleRecordGives( List<String> data ) throws Throwable |
| 214 | + { |
| 215 | + for ( CypherStatementRunner runner : runners ) |
| 216 | + { |
| 217 | + List<Value> givens = runner.result().single().values(); |
| 218 | + for ( int i = 1; i < data.size(); i++ ) |
| 219 | + { |
| 220 | + Value given = parseGiven( givens.get( i - 1 ) ); |
| 221 | + Value expected = parseStringValue( data.get( i ) ); |
| 222 | + assertThat( given, equalTo( expected ) ); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + @Then( "^using `Get` with index (\\d+) on the single record gives:$" ) |
| 228 | + public void usingGetWithIndexOnTheSingleRecordGives( int index, List<String> data ) throws Throwable |
| 229 | + { |
| 230 | + Value expected = parseStringValue( data.get( 1 ) ); |
| 231 | + for ( CypherStatementRunner runner : runners ) |
| 232 | + { |
| 233 | + Value given = parseGiven( runner.result().single().get( index ) ); |
| 234 | + assertThat( given, equalTo( expected ) ); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + @Then( "^using `Get` with key `(.*)` on the single record gives:$" ) |
| 239 | + public void usingGetWithKeyNOnTheSingleRecordGives( String key, List<String> data ) throws Throwable |
| 240 | + { |
| 241 | + Value expected = parseStringValue( data.get( 1 ) ); |
| 242 | + for ( CypherStatementRunner runner : runners ) |
| 243 | + { |
| 244 | + Value given = parseGiven( runner.result().single().get( key ) ); |
| 245 | + assertThat( given, equalTo( expected ) ); |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments