private static JsonNode getSingle( JsonNode node, String key ) { JsonNode data = node.get( key ); assertEquals( 1, data.size() ); return data.get( 0 ); } }
private static JsonNode getSingleData( HTTP.Response response ) throws JsonParseException { JsonNode data = response.get( "results" ).get( 0 ).get( "data" ); assertEquals( 1, data.size() ); return data.get( 0 ); }
@Test public void shouldWriteNestedMaps() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator json = new JsonFactory( new Neo4jJsonCodec() ).createJsonGenerator( out ); JsonNode row = serialize( out, json, new RowWriter( ) ); MatcherAssert.assertThat( row.size(), equalTo( 1 ) ); JsonNode firstCell = row.get( 0 ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); }
@Test public void shouldWriteNestedMaps() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator json = new JsonFactory( new Neo4jJsonCodec() ).createJsonGenerator( out ); JsonNode rest = serialize( out, json, new RestRepresentationWriter( URI.create( "localhost" ) ) ); MatcherAssert.assertThat( rest.size(), equalTo( 1 ) ); JsonNode firstCell = rest.get( 0 ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); }
@Test public void shouldSerializeMapsCorrectlyInRowsFormat() throws Exception { Response response = http.POST( "db/data/transaction/commit", quotedJson( "{ 'statements': [ { 'statement': 'RETURN {one:{two:[true, {three: 42}]}}' } ] }" ) ); // then assertThat( response.status(), equalTo( 200 ) ); JsonNode data = response.get( "results" ).get( 0 ); JsonNode row = data.get( "data" ).get( 0 ).get( "row" ); assertThat( row.size(), equalTo( 1 ) ); JsonNode firstCell = row.get( 0 ); assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); assertThat( response.get( "errors" ).size(), equalTo( 0 ) ); }
private static void assertTypeEqual( String expectedType, HTTP.Response response ) throws JsonParseException { JsonNode data = response.get( "results" ).get( 0 ).get( "data" ); JsonNode meta = data.get( 0 ).get( "meta" ); assertEquals( 1, meta.size() ); assertEquals( expectedType, meta.get( 0 ).get( "type" ).asText() ); }
@Test public void should_serialize_collect_correctly() throws Exception { // given http.POST( "db/data/transaction/commit", singleStatement( "CREATE (n:Foo)" ) ); // when Response response = http.POST( "db/data/transaction/commit", quotedJson( "{ 'statements': [ { 'statement': 'MATCH (n:Foo) RETURN COLLECT(n)' } ] }" ) ); // then assertThat( response.status(), equalTo( 200 ) ); JsonNode data = response.get( "results" ).get( 0 ); assertThat( data.get( "columns" ).get( 0 ).asText(), equalTo( "COLLECT(n)" ) ); assertThat( data.get( "data" ).get( 0 ).get( "row" ).size(), equalTo( 1 ) ); assertThat( data.get( "data" ).get( 0 ).get( "row" ).get( 0 ).get( 0 ).size(), equalTo( 0 ) ); assertThat( response.get( "errors" ).size(), equalTo( 0 ) ); }
@Test public void shouldHandleMapParametersCorrectly() throws Exception { Response response = http.POST( "db/data/transaction/commit", quotedJson("{ 'statements': [ { 'statement': " + "'WITH {map} AS map RETURN map[0]', 'parameters':{'map':[{'index':0,'name':'a'},{'index':1,'name':'b'}]} } ] }")); // then assertThat( response.status(), equalTo( 200 ) ); JsonNode data = response.get( "results" ).get( 0 ); JsonNode row = data.get( "data" ).get( 0 ).get( "row" ); assertThat( row.size(), equalTo( 1 ) ); assertThat( row.get(0).get("index").asInt(), equalTo( 0 ) ); assertThat( row.get(0).get("name").asText(), equalTo( "a" ) ); assertThat( response.get( "errors" ).size(), equalTo( 0 ) ); }
@Test public void shouldExtractNodesFromRow() throws Exception { // given Map<String, Object> row = new HashMap<>(); row.put( "n1", n1 ); row.put( "n2", n2 ); row.put( "n3", n3 ); row.put( "other.thing", "hello" ); row.put( "some.junk", 0x0099cc ); // when JsonNode result = write( row ); // then assertNodes( result ); assertEquals( "there should be no relationships", 0, result.get( "graph" ).get( "relationships" ).size() ); }
@Test public void shouldFixturesWork() throws Exception { // Given the rule in the beginning of this class // When I run this test // Then HTTP.Response response = HTTP.POST( neo4j.httpURI().toString() + "db/data/transaction/commit", quotedJson( "{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}" ) ); assertThat( response.get( "results" ).get( 0 ).get( "data" ).size(), equalTo( 2 ) ); }
private static void assertListEquals( String what, List<String> expected, JsonNode jsonNode ) { assertTrue( what + " - should be a list", jsonNode.isArray() ); List<String> actual = new ArrayList<>( jsonNode.size() ); for ( JsonNode node : jsonNode ) { actual.add( node.getTextValue() ); } assertEquals( what, expected, actual ); }
@Test public void shouldWorkWhenHittingTheASTCacheInCypher() throws JsonParseException { // give a cached plan Response response = http.POST( "db/data/transaction/commit", singleStatement( "MATCH (group:Group {name: \\\"AAA\\\"}) RETURN *" ) ); assertThat( response.status(), equalTo( 200 ) ); assertThat( response.get( "errors" ).size(), equalTo( 0 ) ); // when we hit the ast cache response = http.POST( "db/data/transaction/commit", singleStatement( "MATCH (group:Group {name: \\\"BBB\\\"}) RETURN *" ) ); // then no errors (in particular no NPE) assertThat( response.status(), equalTo( 200 ) ); assertThat( response.get( "errors" ).size(), equalTo( 0 ) ); }
private void assertNodes( JsonNode result ) { JsonNode nodes = result.get( "graph" ).get( "nodes" ); assertEquals( "there should be 3 nodes", 3, nodes.size() ); assertNode( "17", nodes, asList( "Foo" ), property( "name", "n1" ) ); assertNode( "666", nodes, Arrays.asList(), property( "name", "n2" ) ); assertNode( "42", nodes, asList( "Foo", "Bar" ), property( "name", "n3" ) ); }
private void assertRelationships( JsonNode result ) { JsonNode relationships = result.get( "graph" ).get( "relationships" ); assertEquals( "there should be 2 relationships", 2, relationships.size() ); assertRelationship( "7", relationships, "17", "ONE", "666", property( "name", "r1" ) ); assertRelationship( "8", relationships, "17", "TWO", "42", property( "name", "r2" ) ); }
private static void assertNode( String id, JsonNode nodes, List<String> labels, Property... properties ) { JsonNode node = get( nodes, id ); assertListEquals( "Node[" + id + "].labels", labels, node.get( "labels" ) ); JsonNode props = node.get( "properties" ); assertEquals( "length( Node[" + id + "].properties )", properties.length, props.size() ); for ( Property property : properties ) { assertJsonEquals( "Node[" + id + "].properties[" + property.key() + "]", property.value(), props.get( property.key() ) ); } }
@Test public void shouldHandleStringFixtures() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); // When try ( ServerControls server = getServerBuilder( targetFolder ) .withFixture( "CREATE (a:User)" ) .newServer() ) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson( "{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}" ) ); assertThat( response.get( "results" ).get(0).get("data").size(), equalTo(1)); } }
@Test public void shouldIgnoreEmptyFixtureFiles() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); FileUtils.writeToFile( new File( targetFolder, "fixture1.cyp" ), "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false ); FileUtils.writeToFile( new File( targetFolder, "fixture2.cyp" ), "", false ); // When try ( ServerControls server = getServerBuilder( targetFolder ) .withFixture( targetFolder ).newServer() ) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson( "{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}" ) ); assertThat( response.get( "results" ).get(0).get("data").size(), equalTo(1)); } }
private static void assertRelationship( String id, JsonNode relationships, String startNodeId, String type, String endNodeId, Property... properties ) { JsonNode relationship = get( relationships, id ); assertEquals( "Relationship[" + id + "].labels", type, relationship.get( "type" ).getTextValue() ); assertEquals( "Relationship[" + id + "].startNode", startNodeId, relationship.get( "startNode" ).getTextValue() ); assertEquals( "Relationship[" + id + "].endNode", endNodeId, relationship.get( "endNode" ).getTextValue() ); JsonNode props = relationship.get( "properties" ); assertEquals( "length( Relationship[" + id + "].properties )", properties.length, props.size() ); for ( Property property : properties ) { assertJsonEquals( "Relationship[" + id + "].properties[" + property.key() + "]", property.value(), props.get( property.key() ) ); } }
public static void assertNoErrors( HTTP.Response response ) throws JsonParseException { assertEquals( "[]", response.get( "errors" ).toString() ); assertEquals( 0, response.get( "errors" ).size() ); } }