private static JsonNode get( Iterable<JsonNode> jsonNodes, String id ) { for ( JsonNode jsonNode : jsonNodes ) { if ( id.equals( jsonNode.get( "id" ).getTextValue() ) ) { return jsonNode; } } return null; } }
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() ); }
private Set<String> identifiersOf( JsonNode root ) { Set<String> parentIds = new HashSet<>(); for ( JsonNode id : root.get( "identifiers" ) ) { parentIds.add( id.asText() ); } return parentIds; }
private JsonNode findNamedBean( JsonNode beans, String beanName ) { for ( JsonNode bean : beans ) { JsonNode name = bean.get( "name" ); if ( name != null && name.asText().endsWith( ",name=" + beanName ) ) { return bean; } } throw new NoSuchElementException(); }
@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 ) ); }
private JsonNode assertIsPlanRoot( String result ) throws JsonParseException { JsonNode json = jsonNode( result ); JsonNode results = json.get( "results" ).get( 0 ); JsonNode plan = results.get( "plan" ); assertTrue( "Expected plan to be an object", plan != null && plan.isObject() ); JsonNode root = plan.get("root"); assertTrue("Expected plan to be an object", root != null && root.isObject()); return root; }
@Test public void shouldIncludeCause() throws Exception { // Given ExceptionRepresentation rep = new ExceptionRepresentation( new RuntimeException( "Hoho", new RuntimeException( "Haha", new RuntimeException( "HAHA!" ) ) ) ); // When JsonNode out = serialize( rep ); // Then assertThat( out.get("cause").get("message").asText(), is( "Haha" ) ); assertThat( out.get( "cause" ).get("cause").get("message").asText(), is( "HAHA!") ); }
private void assertQueryGetsValue( ServerControls server, String query, long value ) throws Throwable { HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(), quotedJson( "{ 'statements': [ { 'statement': '" + query + "' } ] }" ) ); assertEquals( "[]", response.get( "errors" ).toString() ); JsonNode result = response.get( "results" ).get( 0 ); assertEquals( "value", result.get( "columns" ).get( 0 ).asText() ); assertEquals( value, result.get( "data" ).get( 0 ).get( "row" ).get( 0 ).asLong() ); }
private void assertQueryGetsValue( ServerControls server, String query, long value ) throws Throwable { HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(), quotedJson( "{ 'statements': [ { 'statement': '" + query + "' } ] }" ) ); assertEquals( "[]", response.get( "errors" ).toString() ); JsonNode result = response.get( "results" ).get( 0 ); assertEquals( "value", result.get( "columns" ).get( 0 ).asText() ); assertEquals( value, result.get( "data" ).get( 0 ).get( "row" ).get( 0 ).asLong() ); }
private static double[] coordinatesAsArray( JsonNode element ) { return Iterables.stream( element.get( "coordinates" ) ) .mapToDouble( JsonNode::asDouble ) .toArray(); }
@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 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() ) ); } }
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" ) ); }
@Test public void shouldSayMalformedHeaderIfMalformedAuthorization() throws Exception { // Given startServerWithConfiguredUser(); // When HTTP.Response response = HTTP.withHeaders( HttpHeaders.AUTHORIZATION, "This makes no sense" ).GET( dataURL() ); // Then assertThat( response.status(), equalTo( 400 ) ); assertThat( response.get( "errors" ).get( 0 ).get( "code" ).asText(), equalTo( "Neo.ClientError.Request.InvalidFormat" ) ); assertThat( response.get( "errors" ).get( 0 ).get( "message" ).asText(), equalTo( "Invalid authentication header." ) ); }
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 void assertTemporalEquals( JsonNode data, String value, String type ) { JsonNode row = getSingle( data, "row" ); assertThat( row.asText(), equalTo( value ) ); JsonNode meta = getSingle( data, "meta" ); assertEquals( type, meta.get( "type" ).asText() ); }