@SuppressWarnings( "unchecked" ) @Test @Graph( "Joe knows John" ) public void shouldPerformMultipleOperations() throws Exception
for ( NODE node : graph.nodes() ) for ( REL rel : graph.relationships() ) parse( graph.value(), nodes, relationships ); return new GraphDescription( nodes.values().toArray( NO_NODES ), relationships.toArray( NO_RELS ), graph.autoIndexNodes(), graph.autoIndexRelationships() );
/** * Of course, Neo4j primitives liek +Nodes+, +Relationships+ and * +GraphDatabaseService+ are returned as Neo4j REST entities */ @Test @Graph( value = { "George knows Sara", "George knows Ian" }, autoIndexNodes = true ) public void returning_Neo4j_primitives() throws UnsupportedEncodingException { String script = "g.getRawGraph()"; String response = doRestCall( script, OK ); assertTrue( response.contains( "neo4j_version" ) ); }
/** * */ @Test @Graph( value = { "George knows Sara", "George knows Ian" }, autoIndexNodes = true ) public void returning_paths() throws UnsupportedEncodingException { String script = "g.v(%George%).out().paths()"; String response = doRestCall( script, OK ); assertTrue( response.contains( "Ian" ) ); assertTrue( response.contains( "Sara" ) ); }
@Graph( "Joe knows John" ) public void execute_multiple_operations_in_batch_streaming() throws Exception
@Test @Graph( nodes = { @NODE( name = "a", setNameProperty = true ), @NODE( name = "b", setNameProperty = true ), @NODE( name = "c", setNameProperty = true ), @NODE( name = "d", setNameProperty = true ), @NODE( name = "e", setNameProperty = true ), @NODE( name = "f", setNameProperty = true ) }, relationships = {
"points for the database, and some version and extension information." ) @Test @Graph( "I know you" ) public void assert200OkFromGet() throws Exception
@Test @Graph( "Peter likes Jazz" ) public void shouldHandleEscapedStrings() throws ClientHandlerException, UniformInterfaceException, JSONException, JsonParseException
@Test @Graph( nodes = { @NODE( name = "a", setNameProperty = true ), @NODE( name = "b", setNameProperty = true ), @NODE( name = "c", setNameProperty = true ), @NODE( name = "d", setNameProperty = true ), @NODE( name = "e", setNameProperty = true ), @NODE( name = "f", setNameProperty = true ) }, relationships = {
@Graph( nodes = {@NODE( name = "I" ), @NODE( name = "you" )}, relationships = {@REL( start = "I", end = "you", type = "know", properties = {@PROP( key = "since", value = "today" )} )}, autoIndexRelationships = true ) public void autoindexed_relationships_cannot_be_removed_manually()
@Test @Graph( "Peter likes Jazz" ) public void shouldHandleEscapedStrings() throws ClientHandlerException, UniformInterfaceException, JSONException, JsonParseException
@Test @Graph( nodes = { @NODE( name = "a", setNameProperty = true ), @NODE( name = "b", setNameProperty = true ), @NODE( name = "c", setNameProperty = true ), @NODE( name = "d", setNameProperty = true ), @NODE( name = "e", setNameProperty = true ),
@Graph( value = { "I know you" }, nodes = { @NODE( name = "I", properties = { @PROP( key = "name", value = "me" ) } ) } ) private void verifyIKnowYou( String type, String myName ) { try ( Transaction ignored = graphdb.beginTx() ) { Map<String, Node> graph = data.get(); assertEquals( "Wrong graph size.", 2, graph.size() ); Node iNode = graph.get( "I" ); assertNotNull( "The node 'I' was not defined", iNode ); Node you = graph.get( "you" ); assertNotNull( "The node 'you' was not defined", you ); assertEquals( "'I' has wrong 'name'.", myName, iNode.getProperty( "name" ) ); assertEquals( "'you' has wrong 'name'.", "you", you.getProperty( "name" ) ); Iterator<Relationship> rels = iNode.getRelationships().iterator(); assertTrue( "'I' has too few relationships", rels.hasNext() ); Relationship rel = rels.next(); assertEquals( "'I' is not related to 'you'", you, rel.getOtherNode( iNode ) ); assertEquals( "Wrong relationship type.", type, rel.getType().name() ); assertFalse( "'I' has too many relationships", rels.hasNext() ); rels = you.getRelationships().iterator(); assertTrue( "'you' has too few relationships", rels.hasNext() ); rel = rels.next(); assertEquals( "'you' is not related to 'i'", iNode, rel.getOtherNode( you ) ); assertEquals( "Wrong relationship type.", type, rel.getType().name() ); assertFalse( "'you' has too many relationships", rels.hasNext() ); assertEquals( "wrong direction", iNode, rel.getStartNode() ); } }
/** * It is not allowed to remove entries manually from automatic indexes. */ @Test @Graph( nodes = {@NODE( name = "I", setNameProperty = true )}, autoIndexNodes = true ) public void autoindexed_items_cannot_be_removed_manually() { long id = data.get() .get( "I" ) .getId(); String indexName; try ( Transaction tx = graphdb().beginTx() ) { indexName = graphdb().index().getNodeAutoIndexer().getAutoIndex().getName(); tx.success(); } gen.get() .expectedStatus( 405 ) .delete( getDataUri() + "index/node/" + indexName + "/name/I/" + id ) .entity(); gen.get() .expectedStatus( 405 ) .delete( getDataUri() + "index/node/" + indexName + "/name/" + id ) .entity(); gen.get() .expectedStatus( 405 ) .delete( getDataUri() + "index/node/" + indexName + "/" + id ) .entity(); }
@Test @Documented( "Get nodes by label and property.\n" + "\n" + "You can retrieve all nodes with a given label and property by passing one property as a query parameter.\n" + "Notice that the property value is JSON-encoded and then URL-encoded.\n" + "\n" + "If there is an index available on the label/property combination you send, that index will be used. If no\n" + "index is available, all nodes with the given label will be filtered through to find matching nodes.\n" + "\n" + "Currently, it is not possible to search using multiple properties." ) @GraphDescription.Graph( nodes = {@NODE( name = "Donald Sutherland", labels = {@LABEL( "Person" )} ), @NODE( name = "Clint Eastwood", labels = {@LABEL( "Person" )}, properties = { @PROP( key = "name", value = "Clint Eastwood" )} ), @NODE( name = "Steven Spielberg", labels = {@LABEL( "Person" )}, properties = { @PROP( key = "name", value = "Steven Spielberg" )} )} ) public void get_nodes_with_label_and_property() throws JsonParseException, UnsupportedEncodingException { data.get(); String labelName = "Person"; String result = gen.get() .expectedStatus( 200 ) .get( getNodesWithLabelAndPropertyUri( labelName, "name", "Clint Eastwood" ) ) .entity(); List<?> parsed = (List<?>) readJson( result ); assertEquals( asSet( "Clint Eastwood" ), Iterables.asSet( map( getProperty( "name", String.class ), parsed ) ) ); }
@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 ) ); }
@Test @Graph( value = { "a to c", "a to d", "c to b", "d to e", "b to f", "c to f", "f to g", "d to g", "e to g", "c to g" } ) @Title( "Find all shortest paths" ) @Documented( "The +shortestPath+ algorithm can find multiple paths between the same nodes, like in this example." ) public void shouldBeAbleToFindAllShortestPaths() throws JsonParseException { // Get all shortest paths long a = nodeId( data.get(), "a" ); long g = nodeId( data.get(), "g" ); String response = gen() .expectedStatus( Status.OK.getStatusCode() ) .payload( getAllShortestPathPayLoad( g ) ) .post( getServerUri() + "db/data/node/" + a + "/paths" ) .entity(); Collection<?> result = (Collection<?>) JsonHelper.readJson( response ); assertEquals( 2, result.size() ); for ( Object representation : result ) { Map<?, ?> path = (Map<?, ?>) representation; assertThatPathStartsWith( path, a ); assertThatPathEndsWith( path, g ); assertThatPathHasLength( path, 2 ); } }
@Test @Documented( "List all labels.\n" + " \n" + "By default, the server will return labels in use only. If you also want to return labels not in use,\n" + "append the \"in_use=0\" query parameter." ) @GraphDescription.Graph( nodes = { @NODE( name = "Clint Eastwood", setNameProperty = true, labels = { @LABEL( "Person" ), @LABEL( "Actor" ), @LABEL( "Director" ) } ), @NODE( name = "Donald Sutherland", setNameProperty = true, labels = { @LABEL( "Person" ), @LABEL( "Actor" ) } ), @NODE( name = "Steven Spielberg", setNameProperty = true, labels = { @LABEL( "Person" ), @LABEL( "Director" ) } ) } ) public void list_all_labels() throws JsonParseException { data.get(); String uri = getLabelsUri(); String body = gen.get() .expectedStatus( 200 ) .get( uri ) .entity(); Set<?> parsed = Iterables.asSet((List<?>) readJson( body )); assertTrue( parsed.contains( "Person" ) ); assertTrue( parsed.contains( "Actor" ) ); assertTrue( parsed.contains( "Director" ) ); }
@SuppressWarnings( "unchecked" ) @Documented( "Get all uniqueness constraints for a label." ) @Test @GraphDescription.Graph( nodes = {} ) public void getLabelUniquenessPropertyConstraints() throws JsonParseException { data.get(); String labelName = labels.newInstance(); String propertyKey1 = properties.newInstance(); String propertyKey2 = properties.newInstance(); createLabelUniquenessPropertyConstraint( labelName, propertyKey1 ); createLabelUniquenessPropertyConstraint( labelName, propertyKey2 ); String result = gen.get().expectedStatus( 200 ).get( getSchemaConstraintLabelUniquenessUri( labelName ) ).entity(); List<Map<String, Object>> serializedList = jsonToList( result ); Map<String, Object> constraint1 = new HashMap<>( ); constraint1.put( "type", ConstraintType.UNIQUENESS.name() ); constraint1.put( "label", labelName ); constraint1.put( "property_keys", singletonList( propertyKey1 ) ); Map<String, Object> constraint2 = new HashMap<>( ); constraint2.put( "type", ConstraintType.UNIQUENESS.name() ); constraint2.put( "label", labelName ); constraint2.put( "property_keys", singletonList( propertyKey2 ) ); assertThat( serializedList, hasItems( constraint1, constraint2 ) ); }
@Test @Graph( nodes = { @NODE( name = "I", properties = { @PROP( key = "name", value = "me" ), @PROP( key = "bool", value = "true", type = GraphDescription.PropType.BOOLEAN ) } ), @NODE( name = "you", setNameProperty = true ) }, relationships = { @REL( start = "I", end = "you", type = "knows", properties = { @PROP( key = "name", value = "relProp" ), @PROP( key = "valid", value = "true", type = GraphDescription.PropType.BOOLEAN ) } ) }, autoIndexRelationships = true ) public void canCreateMoreInvolvedGraphWithPropertiesAndAutoIndex() { data.get(); verifyIKnowYou( "knows", "me" ); try ( Transaction ignored = graphdb.beginTx() ) { assertEquals( true, data.get().get( "I" ).getProperty( "bool" ) ); assertFalse( "node autoindex enabled.", graphdb().index().getNodeAutoIndexer().isEnabled() ); try ( IndexHits<Relationship> relationships = graphdb().index() .getRelationshipAutoIndexer().getAutoIndex().get( "name", "relProp" ) ) { assertTrue( "can't look up rel.", relationships.hasNext() ); } assertTrue( "relationship autoindex enabled.", graphdb().index().getRelationshipAutoIndexer().isEnabled() ); } }