Refine search
@Test @Documented( "When sending queries that\n" + "return nested results like list and maps,\n" + "these will get serialized into nested JSON representations\n" + "according to their types." ) @Graph( value = {"I know you"}, autoIndexNodes = true ) public void nested_results() throws Exception { data.get(); String script = "MATCH (n) WHERE n.name in ['I', 'you'] RETURN collect(n.name)"; String response = cypherRestCall( script, Status.OK ); Map<String,Object> resultMap = jsonToMap( response ); assertEquals( 2, resultMap.size() ); assertThat( response, anyOf( containsString( "\"I\",\"you\"" ), containsString( "\"you\",\"I\"" ), containsString( "\"I\", \"you\"" ), containsString( "\"you\", \"I\"" ) ) ); }
@Test @Graph( "jim knows joe" ) public void shouldReturn400WhenSendinIncompatibleJsonProperties() { Map<String, Object> map = new HashMap<>(); map.put( "jim", new HashMap<String, Object>() ); gen.get().payload( JsonHelper.createJsonFrom( map ) ).expectedStatus( 400 ).put( getPropertiesUri( data.get().get( "jim" ) ) ); }
@Test @Title( "Errors" ) @Documented( "Errors on the server will be reported as a JSON-formatted message, exception name and stacktrace." ) @Graph( "I know you" ) public void error_gets_returned_as_json() throws Exception { String response = cypherRestCall( "MATCH (x {name: 'I'}) RETURN x.dummy/0", Status.BAD_REQUEST ); Map<String,Object> output = jsonToMap( response ); assertTrue( output.toString(), output.containsKey( "message" ) ); assertTrue( output.containsKey( "exception" ) ); assertTrue( output.containsKey( "stackTrace" ) ); }
@Test @Graph( value = {"I know you"}, autoIndexNodes = false ) public void array_property() { setProperty( "I", "array1", new int[]{1, 2, 3} ); setProperty( "I", "array2", new String[]{"a", "b", "c"} ); String script = "MATCH (n) WHERE id(n) = %I% RETURN n.array1, n.array2"; String response = cypherRestCall( script, Status.OK ); assertThat( response, anyOf( containsString( "[ 1, 2, 3 ]" ), containsString( "[1,2,3]" ) ) ); assertThat( response, anyOf( containsString( "[ \"a\", \"b\", \"c\" ]" ), containsString( "[\"a\",\"b\",\"c\"]" ) ) ); }
@Test @Documented( "Create multiple nodes with properties using Cypher. See the request for the parameter sent " + "with the query." ) @Title( "Create multiple nodes with properties" ) @Graph public void send_query_to_create_multiple_nodes_from_a_map() { data.get(); String script = "UNWIND {props} AS properties CREATE (n:Person) SET n = properties RETURN n"; String params = "\"props\" : [ { \"name\" : \"Andres\", \"position\" : \"Developer\" }, " + "{ \"name\" : \"Michael\", \"position\" : \"Developer\" } ]"; String response = cypherRestCall( script, Status.OK, params ); assertTrue( response.contains( "name" ) ); assertTrue( response.contains( "Michael" ) ); assertTrue( response.contains( "Andres" ) ); }
@Test @Graph( { "a TO b", "b TO c", "c TO a" } ) public void canCreateGraphFromMultipleStrings() { Map<String,Node> graph = data.get(); Set<Node> unique = new HashSet<>(); Node n = graph.get( "a" ); while ( unique.add( n ) ) { try ( Transaction ignored = graphdb.beginTx() ) { n = n.getSingleRelationship( RelationshipType.withName( "TO" ), Direction.OUTGOING ).getEndNode(); } } assertEquals( graph.size(), unique.size() ); }
@Test @Title( "Return paths" ) @Documented( "Paths can be returned just like other return types." ) @Graph( "I know you" ) public void return_paths() throws Exception { String script = "MATCH path = (x {name: 'I'})--(friend) RETURN path, friend.name"; String response = cypherRestCall( script, Status.OK ); assertEquals( 2, (jsonToMap( response )).size() ); assertThat( response, containsString( "data" ) ); assertThat( response, containsString( "you" ) ); }
@Test @Title( "Create a node with multiple properties" ) @Documented( "Create a node with a label and multiple properties using Cypher. See the request for the parameter " + "sent with the query." ) @Graph public void send_query_to_create_a_node_from_a_map() { data.get(); String script = "CREATE (n:Person { props } ) RETURN n"; String params = "\"props\" : { \"position\" : \"Developer\", \"name\" : \"Michael\", \"awesome\" : true, \"children\" : 3 }"; String response = cypherRestCall( script, Status.OK, params ); assertTrue( response.contains( "name" ) ); assertTrue( response.contains( "Michael" ) ); }
@Test @Graph( { "a:Person EATS b:Banana" } ) public void ensurePeopleCanEatBananas() { Map<String,Node> graph = data.get(); Node a = graph.get( "a" ); Node b = graph.get( "b" ); try ( Transaction ignored = graphdb.beginTx() ) { assertTrue( a.hasLabel( label( "Person" ) ) ); assertTrue( b.hasLabel( label( "Banana" ) ) ); } }
@Test @Title( "Retrieve query metadata" ) @Documented( "By passing in an additional GET parameter when you execute Cypher queries, metadata about the " + "query will be returned, such as how many labels were added or removed by the query." ) @Graph( nodes = {@NODE( name = "I", setNameProperty = true, labels = {@LABEL( "Director" )} )} ) public void testQueryStatistics() throws JsonParseException { // Given String script = createScript( "MATCH (n {name: 'I'}) SET n:Actor REMOVE n:Director RETURN labels(n)" ); // When Map<String,Object> output = jsonToMap( doCypherRestCall( cypherUri() + "?includeStats=true", script, Status.OK ) ); // Then @SuppressWarnings( "unchecked" ) Map<String,Object> stats = (Map<String,Object>) output.get( "stats" ); assertThat( stats, isA( Map.class ) ); assertThat( stats.get( "contains_updates" ), is( true ) ); assertThat( stats.get( "labels_added" ), is( 1 ) ); assertThat( stats.get( "labels_removed" ), is( 1 ) ); assertThat( stats.get( "nodes_created" ), is( 0 ) ); assertThat( stats.get( "nodes_deleted" ), is( 0 ) ); assertThat( stats.get( "properties_set" ), is( 0 ) ); assertThat( stats.get( "relationships_created" ), is( 0 ) ); assertThat( stats.get( "relationship_deleted" ), is( 0 ) ); }
/** * Ensure that order of data and column is ok. */ @Test @Graph( nodes = {@NODE( name = "I", setNameProperty = true ), @NODE( name = "you", setNameProperty = true ), @NODE( name = "him", setNameProperty = true, properties = { @PROP( key = "age", value = "25", type = GraphDescription.PropType.INTEGER )} )}, relationships = { @REL( start = "I", end = "him", type = "know", properties = {} ), @REL( start = "I", end = "you", type = "know", properties = {} )} ) public void testDataColumnOrder() { String script = createScript( "MATCH (x)-[r]->(n) WHERE id(x) = %I% RETURN type(r), n.name, n.age" ); String response = cypherRestCall( script, Status.OK ); assertThat( response.indexOf( "columns" ) < response.indexOf( "data" ), is( true ) ); }
@Documented( "Adding a label to a node." ) @Test @GraphDescription.Graph( nodes = { @NODE( name = "Clint Eastwood", setNameProperty = true ) } ) public void adding_a_label_to_a_node() { Map<String,Node> nodes = data.get(); String nodeUri = getNodeUri( nodes.get( "Clint Eastwood" ) ); gen.get() .expectedStatus( 204 ) .payload( createJsonFrom( "Person" ) ) .post( nodeUri + "/labels" ); }
/** * The automatic relationship index can not be removed. */ @Test @Graph( nodes = {@NODE( name = "I", setNameProperty = true )}, autoIndexNodes = true ) public void Relationship_AutoIndex_is_not_removable() { data.get(); gen.get() .expectedStatus( 405 ) .delete( relationshipAutoIndexUri() ) .entity(); }
@Test @Title( "Send a query" ) @Documented( "A simple query returning all nodes connected to some node, returning the node and the name " + "property, if it exists, otherwise `NULL`:" ) @Graph( nodes = {@NODE( name = "I", setNameProperty = true ), @NODE( name = "you", setNameProperty = true ), @NODE( name = "him", setNameProperty = true, properties = { @PROP( key = "age", value = "25", type = GraphDescription.PropType.INTEGER )} )}, relationships = { @REL( start = "I", end = "him", type = "know", properties = {} ), @REL( start = "I", end = "you", type = "know", properties = {} )} ) public void testPropertyColumn() { String script = createScript( "MATCH (x {name: 'I'})-[r]->(n) RETURN type(r), n.name, n.age" ); String response = cypherRestCall( script, Status.OK ); assertThat( response, containsString( "you" ) ); assertThat( response, containsString( "him" ) ); assertThat( response, containsString( "25" ) ); assertThat( response, not( containsString( "\"x\"" ) ) ); }
@Test @Graph( nodes = {@NODE( name = "Romeo", setNameProperty = true ), @NODE( name = "Juliet", setNameProperty = true )}, relationships = { @REL( start = "Romeo", end = "Juliet", type = "LOVES", properties = { @PROP( key = "cost", value = "high", type = GraphDescription.PropType.STRING )} )} ) public void get_single_property_on_a_relationship() { Relationship loves = getFirstRelationshipFromRomeoNode(); String response = gen().expectedStatus( ClientResponse.Status.OK ).get( getRelPropURI( loves, "cost" ) ).entity(); assertTrue( response.contains( "high" ) ); }
@Test @Documented( "Create a node with a label and a property using Cypher. See the request for the parameter " + "sent with the query." ) @Title( "Create a node" ) @Graph public void send_query_to_create_a_node() { data.get(); String script = "CREATE (n:Person { name : {name} }) RETURN n"; String response = cypherRestCall( script, Status.OK, Pair.of( "name", "Andres" ) ); assertTrue( response.contains( "name" ) ); assertTrue( response.contains( "Andres" ) ); }
@Test @Graph( "Joe knows Sara" ) public void providing_bad_JSON() { String jsonString = "{\"to\" : \"" + getNodeUri( data.get().get( "Joe" ) ) + "\", \"type\" : \"LOVES\", \"data\" : {\"foo\" : **BAD JSON HERE*** \"bar\"}}"; gen.get().expectedStatus( Status.BAD_REQUEST.getStatusCode() ).payload( jsonString ).post( getNodeUri( getNode( "Joe" ) ) + "/relationships" ).entity(); }
@Test @Title( "Profile a query" ) @Documented( "By passing in an extra parameter, you can ask the cypher executor to return a profile of the " + "query as it is executed. This can help in locating bottlenecks." ) @Graph( nodes = {@NODE( name = "I", setNameProperty = true ), @NODE( name = "you", setNameProperty = true ), @NODE( name = "him", setNameProperty = true, properties = { @PROP( key = "age", value = "25", type = GraphDescription.PropType.INTEGER )} )}, relationships = { @REL( start = "I", end = "him", type = "know", properties = {} ), @REL( start = "I", end = "you", type = "know", properties = {} )} ) public void testProfiling() throws Exception { String script = createScript( "MATCH (x)-[r]->(n) WHERE id(x) = %I% RETURN type(r), n.name, n.age" ); // WHEN String response = doCypherRestCall( cypherUri() + "?profile=true", script, Status.OK ); // THEN Map<String,Object> des = jsonToMap( response ); assertThat( des.get( "plan" ), instanceOf( Map.class ) ); @SuppressWarnings( "unchecked" ) Map<String,Object> plan = (Map<String,Object>) des.get( "plan" ); assertThat( plan.get( "name" ), instanceOf( String.class ) ); assertThat( plan.get( "children" ), instanceOf( Collection.class ) ); assertThat( plan.get( "rows" ), instanceOf( Number.class ) ); assertThat( plan.get( "dbHits" ), instanceOf( Number.class ) ); }
@Test @Graph( nodes = {@NODE( name = "Romeo", setNameProperty = true ), @NODE( name = "Juliet", setNameProperty = true )}, relationships = { @REL( start = "Romeo", end = "Juliet", type = "LOVES", properties = { @PROP( key = "cost", value = "high", type = GraphDescription.PropType.STRING ), @PROP( key = "since", value = "1day", type = GraphDescription.PropType.STRING )} )} ) public void get_all_properties_on_a_relationship() { Relationship loves = getFirstRelationshipFromRomeoNode(); String response = gen().expectedStatus( ClientResponse.Status.OK ).get( getRelPropsURI( loves ) ).entity(); assertTrue( response.contains( "high" ) ); }