|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2016 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.v1.tck; |
| 20 | + |
| 21 | +import cucumber.api.DataTable; |
| 22 | +import cucumber.api.java.en.Given; |
| 23 | +import cucumber.api.java.en.Then; |
| 24 | +import cucumber.api.java.en.When; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import org.neo4j.driver.v1.ResultCursor; |
| 33 | +import org.neo4j.driver.v1.Value; |
| 34 | +import org.neo4j.driver.v1.tck.tck.util.runners.CypherStatementRunner; |
| 35 | +import org.neo4j.driver.v1.tck.tck.util.runners.MappedParametersRunner; |
| 36 | +import org.neo4j.driver.v1.tck.tck.util.runners.StringRunner; |
| 37 | + |
| 38 | +import static org.junit.Assert.assertEquals; |
| 39 | +import static org.junit.Assert.assertTrue; |
| 40 | +import static org.neo4j.driver.v1.tck.DriverComplianceIT.session; |
| 41 | +import static org.neo4j.driver.v1.tck.Environment.mappedTypes; |
| 42 | +import static org.neo4j.driver.v1.tck.Environment.runners; |
| 43 | +import static org.neo4j.driver.v1.tck.tck.util.ResultParser.getParametersFromListOfKeysAndValues; |
| 44 | +import static org.neo4j.driver.v1.tck.tck.util.ResultParser.parseExpected; |
| 45 | +import static org.neo4j.driver.v1.tck.tck.util.ResultParser.parseGiven; |
| 46 | +import static org.neo4j.driver.v1.tck.tck.util.Types.getType; |
| 47 | + |
| 48 | + |
| 49 | +public class CypherComplianceSteps |
| 50 | +{ |
| 51 | + @Given( "^init: (.*);$" ) |
| 52 | + public void init_( String statement ) throws Throwable |
| 53 | + { |
| 54 | + session.run( statement ); |
| 55 | + } |
| 56 | + |
| 57 | + @When( "^running: (.*);$" ) |
| 58 | + public void running_( String statement ) throws Throwable |
| 59 | + { |
| 60 | + runners.add( new StringRunner( statement ).runCypherStatement() ); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @When( "^running parametrized: (.*);$" ) |
| 65 | + public void running_param_bar_match_a_r_b_where_r_foo_param_return_b( String statement, DataTable stringParam ) |
| 66 | + throws Throwable |
| 67 | + { |
| 68 | + List<String> keys = stringParam.topCells(); |
| 69 | + List<String> values = stringParam.diffableRows().get( 1 ).convertedRow; |
| 70 | + Map<String, Value> params = getParametersFromListOfKeysAndValues( keys, values ); |
| 71 | + runners.add( new MappedParametersRunner( statement, params ).runCypherStatement() ); |
| 72 | + } |
| 73 | + |
| 74 | + @Then( "^result should be ([^\"]*)\\(s\\)$" ) |
| 75 | + public void result_should_be_a_type_containing(String type, DataTable table) throws Throwable |
| 76 | + { |
| 77 | + for( CypherStatementRunner runner : runners) |
| 78 | + { |
| 79 | + ResultCursor rc = runner.result(); |
| 80 | + List<String> keys = table.topCells(); |
| 81 | + Collection<Map> given = new ArrayList<>( ); |
| 82 | + Collection<Map> expected = new ArrayList<>( ); |
| 83 | + int i = 0; |
| 84 | + while ( rc.next() ) |
| 85 | + { |
| 86 | + assertTrue( keys.size() == rc.record().keys().size() ); |
| 87 | + assertTrue( keys.containsAll( rc.record().keys() ) ); |
| 88 | + given.add( parseGiven( rc.record().asMap() ) ); |
| 89 | + expected.add( parseExpected( table.diffableRows().get( i + 1 ).convertedRow, keys, getType( type ) ) ); |
| 90 | + i++; |
| 91 | + } |
| 92 | + assertTrue( expected.size() > 0 ); |
| 93 | + assertTrue( expected.iterator().next().size() > 0 ); |
| 94 | + assertTrue( equalRecords( expected, given) ); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Then( "^result should be mixed:" ) |
| 99 | + public void result_should_be_mixed( DataTable table ) throws Throwable |
| 100 | + { |
| 101 | + for( CypherStatementRunner runner : runners) |
| 102 | + { |
| 103 | + ResultCursor rc = runner.result(); |
| 104 | + List<String> keys = table.topCells(); |
| 105 | + Collection<Map> given = new ArrayList<>( ); |
| 106 | + Collection<Map> expected = new ArrayList<>( ); |
| 107 | + int i = 0; |
| 108 | + while ( rc.next() ) |
| 109 | + { |
| 110 | + assertTrue( keys.size() == rc.record().keys().size() ); |
| 111 | + assertTrue( keys.containsAll( rc.record().keys() ) ); |
| 112 | + Map<String,Value> tmpGiven = new HashMap<>( ); |
| 113 | + for ( String key : keys ) |
| 114 | + { |
| 115 | + tmpGiven.put( key, parseGiven( rc.record().asMap().get( key ) ) ); |
| 116 | + } |
| 117 | + given.add( tmpGiven ); |
| 118 | + expected.add( parseExpected( table.diffableRows().get( i + 1 ).convertedRow, keys, mappedTypes ) ); |
| 119 | + i++; |
| 120 | + } |
| 121 | + assertTrue( expected.size() > 0 ); |
| 122 | + assertTrue( expected.iterator().next().size() > 0 ); |
| 123 | + assertTrue( equalRecords( expected, given ) ); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Then( "^result should be empty$" ) |
| 128 | + public void result_should_be_empty(DataTable table) throws Throwable |
| 129 | + { |
| 130 | + for (CypherStatementRunner runner : runners) |
| 131 | + { |
| 132 | + assertEquals( runner.result().list().size(), 0 ); |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + private boolean equalRecords( Collection<Map> one, Collection<Map> other ) |
| 138 | + { |
| 139 | + if (one.size() != other.size() ) |
| 140 | + { |
| 141 | + return false; |
| 142 | + } |
| 143 | + for (Map c1 : one) |
| 144 | + { |
| 145 | + int otherSize = other.size(); |
| 146 | + for (Map c2 : other) |
| 147 | + { |
| 148 | + if (c1.equals(c2)) |
| 149 | + { |
| 150 | + other.remove( c2 ); |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + if (otherSize == other.size()) |
| 155 | + { |
| 156 | + return false; |
| 157 | + } |
| 158 | + } |
| 159 | + return other.size() == 0; |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + @Then( "^result should map to types:$" ) |
| 164 | + public void result_should_map_to_types(DataTable table) throws Throwable |
| 165 | + { |
| 166 | + List<String> keys = table.topCells(); |
| 167 | + List<String> values = table.diffableRows().get( 1 ).convertedRow; |
| 168 | + mappedTypes = new HashMap<>( ); |
| 169 | + for (int i = 0; i < keys.size(); i++) |
| 170 | + { |
| 171 | + mappedTypes.put( keys.get( i ), getType( values.get( i ) ) ); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments