DeleteOperation extends AbstractNeo4JOperation { @Override public void execute(final Connection connection, final Graph<Node, Edge> graph) throws SQLException { final List<Path> nodes = graph.vertexSet().stream().map(n -> n.toPath().withAllAttributes().build()).collect(toList()); final List<ReferenceExpression> nodeIds = graph.vertexSet().stream().map(n -> identifier(n.getId())).collect(toList()); final List<Path> edges = graph.edgeSet().stream().map(e -> e.toPath().withAllAttributes().withNodeIdAttributes().build()) .collect(toList()); final List<ReferenceExpression> edgeIds = graph.edgeSet().stream().map(e -> identifier(e.getId())).collect(toList()); final UpdateNext deleteEdgesQuery = match(edges.toArray(new Path[edges.size()])).delete(edgeIds); final UpdateNext deleteNodesQuery = match(nodes.toArray(new Path[nodes.size()])).delete(nodeIds); executeQuery(connection, deleteEdgesQuery.toString()); executeQuery(connection, deleteNodesQuery.toString()); }  @Override void execute(final Connection connection, final Graph<Node, Edge> graph);  }
@Test public void testExecute() throws Exception { final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"), ImmutableMap.<String, Object>builder().put("id", 1l).build()); final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"), ImmutableMap.<String, Object>builder().put("id", 2l).build()); final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"), ImmutableMap.<String, Object>builder().put("id", 3l).build()); final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class)); graph.addVertex(n1); graph.addVertex(n2); graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1); operation.execute(connection, graph); final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class); verify(operation, times(2)).executeQuery(eq(connection), queryCaptor.capture()); final List<String> queries = queryCaptor.getAllValues(); final String query1 = queries.get(0); final String query2 = queries.get(1); assertThat(query1, containsString("MATCH (n1:A {id:1})-[e1:E {id:3}]->(`n2:A {id:2}`) DELETE e1")); assertThat(query2, containsString("MATCH (n1:A {id:1}),(n2:A {id:2}) DELETE n1,n2")); }