@Test public void clearGraphWithoutRuntimeShouldDeleteBasedOnRelInclusionPolicy() { try (Transaction tx = database.beginTx()) { String cypher = "CREATE " + "(purple:Purple {name:'Purple'})<-[:REL]-(red1:Red {name:'Red'})-[:REL]->(black1:Black {name:'Black'})-[:REL]->(green:Green {name:'Green'})," + "(red2:Red {name:'Red'})-[:REL]->(black2:Black {name:'Black'}), (blue1:Blue)-[:REL2]->(blue2:Blue)"; populateDatabase(cypher); tx.success(); } InclusionPolicies inclusionPolicies = InclusionPolicies.all().with(new BlueNodeInclusionPolicy()).with(new Rel2InclusionPolicy()); try (Transaction tx = database.beginTx()) { clearGraph(database, inclusionPolicies); tx.success(); } try (Transaction tx = database.beginTx()) { assertEquals(6, count(at(database).getAllNodes())); assertEquals(4, count(at(database).getAllRelationships())); tx.success(); } }
@Test public void shouldRetrieveMultipleNodesWithSameValueFromIndex() throws Exception { // this test was included here for now as a precondition for the following test // given GraphDatabaseService graph = dbRule.getGraphDatabaseService(); createIndex(graph, LABEL1, "name"); Node node1, node2; try (Transaction tx = graph.beginTx()) { node1 = graph.createNode(LABEL1); node1.setProperty("name", "Stefan"); node2 = graph.createNode(LABEL1); node2.setProperty("name", "Stefan"); tx.success(); } try (Transaction tx = graph.beginTx()) { ResourceIterator<Node> result = graph.findNodes(LABEL1, "name", "Stefan"); assertEquals(asSet(node1, node2), asSet(result)); tx.success(); } }
@Test public void shouldCorrectlyUpdateIndexesWhenChangingLabelsAndPropertyAtTheSameTime() throws Exception { // Given GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService(); Node myNode = createNode(beansAPI, map("name", "Hawking"), LABEL1, LABEL2); createIndex(beansAPI, LABEL1, "name"); createIndex(beansAPI, LABEL2, "name"); createIndex(beansAPI, LABEL3, "name"); // When try (Transaction tx = beansAPI.beginTx()) { myNode.removeLabel(LABEL1); myNode.addLabel(LABEL3); myNode.setProperty("name", "Einstein"); tx.success(); } // Then assertThat(myNode, inTx(beansAPI, hasProperty("name").withValue("Einstein"))); assertThat(labels(myNode), containsOnly(LABEL2, LABEL3)); assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Hawking", beansAPI), isEmpty()); assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Einstein", beansAPI), isEmpty()); assertThat(findNodesByLabelAndProperty(LABEL2, "name", "Hawking", beansAPI), isEmpty()); assertThat( findNodesByLabelAndProperty(LABEL2, "name", "Einstein", beansAPI), containsOnly(myNode)); assertThat(findNodesByLabelAndProperty(LABEL3, "name", "Hawking", beansAPI), isEmpty()); assertThat( findNodesByLabelAndProperty(LABEL3, "name", "Einstein", beansAPI), containsOnly(myNode)); }
public void rn() throws IOException { List<RoadLink> links = loadToDatabase("/Users/duduba/gj.mif", "/Users/duduba/gj.mid", false); GraphDatabaseService graphDb = neo.getDb(); Index<Node> nodeIndex = neo.getNodeIndex(); for (RoadLink link : links) { Transaction tx = graphDb.beginTx(); try { Node fromNode = nodeIndex.get("id", link.fromNode).getSingle(); if (fromNode == null) { fromNode = graphDb.createNode(); fromNode.setProperty("id", link.fromNode); nodeIndex.add(fromNode, "id", link.fromNode); } Node toNode = nodeIndex.get("id", link.toNode).getSingle(); if (toNode == null) { toNode = graphDb.createNode(); toNode.setProperty("id", link.toNode); nodeIndex.add(toNode, "id", link.toNode); } Relationship r = fromNode.createRelationshipTo(toNode, Neo.RelTypes.TO); r.setProperty("no", link.no); r.setProperty("name", link.name); tx.success(); } finally { tx.finish(); } } logger.debug("haha, it's ok!"); }
@Test public void shouldRollbackViaStatus() throws Exception { new TransactionTemplate(neo4jTransactionManager) .execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(final TransactionStatus status) { neo4jTemplate.exec( new GraphCallback.WithoutResult() { @Override public void doWithGraphWithoutResult(GraphDatabase graph) throws Exception { graph .getReferenceNode() .setProperty("test", "shouldRollbackTransactionOnException"); status.setRollbackOnly(); } }); } }); Transaction tx = graphDatabase.beginTx(); try { Assert.assertThat( (String) graphDatabase.getReferenceNode().getProperty("test", "not set"), not("shouldRollbackTransactionOnException")); } finally { tx.success(); tx.finish(); } }
@Test public void testAddToIndex() { final MatrixDataGraph matrixDataGraph = new MatrixDataGraph(getGraphDatabase()); matrixDataGraph.createNodespace(); final RestNode neoNode = restAPI.getNodeById(matrixDataGraph.getNeoNode().getId()); final Transaction tx = restAPI.beginTx(); restAPI.index().forNodes("heroes").add(neoNode, "indexname", "Neo2"); Node n1 = restAPI.createNode(map("name", "Apoc")); final Index<Node> index = restAPI.index().forNodes("heroes"); index.add(n1, "indexname", "Apoc"); final Node indexResult = getGraphDatabase().index().forNodes("heroes").get("indexname", "Neo2").getSingle(); assertNull(indexResult); final IndexHits<Node> heroes = index.query("indexname:Apoc"); tx.success(); tx.finish(); assertEquals("1 hero", 1, heroes.size()); IndexManager realIndex = getGraphDatabase().index(); Index<Node> goodGuys = realIndex.forNodes("heroes"); IndexHits<Node> hits = goodGuys.get("indexname", "Apoc"); Node apoc = hits.getSingle(); assertEquals("Apoc indexed", apoc, heroes.iterator().next()); }
@Test public void testCreateRelationship() { Node n1 = restAPI.createNode(map("name", "newnode1")); final Transaction tx = restAPI.beginTx(); Node n2 = restAPI.createNode(map("name", "newnode2")); RestRelationship rel = restAPI.createRelationship(n1, n2, Type.TEST, map("name", "rel")); Iterable<Relationship> allRelationships = n1.getRelationships(); tx.success(); tx.finish(); Relationship foundRelationship = TestHelper.firstRelationshipBetween( n1.getRelationships(Type.TEST, Direction.OUTGOING), n1, n2); Assert.assertNotNull("found relationship", foundRelationship); assertEquals("same relationship", rel, foundRelationship); assertEquals("rel", rel.getProperty("name")); assertThat( n1.getRelationships(Type.TEST, Direction.OUTGOING), new IsRelationshipToNodeMatcher(n1, n2)); assertThat(n1.getRelationships(Direction.OUTGOING), new IsRelationshipToNodeMatcher(n1, n2)); assertThat(n1.getRelationships(Direction.BOTH), new IsRelationshipToNodeMatcher(n1, n2)); assertThat(n1.getRelationships(Type.TEST), new IsRelationshipToNodeMatcher(n1, n2)); assertThat(allRelationships, new IsRelationshipToNodeMatcher(n1, n2)); }
private Node createLabeledNode(Label... labels) { try (Transaction tx = dbRule.getGraphDatabaseService().beginTx()) { Node node = dbRule.getGraphDatabaseService().createNode(labels); tx.success(); return node; } }
private static List<Double> getWeightVectorForClass( Map<String, List<LinkedHashMap<String, Object>>> documents, String key, List<Integer> featureIndexList, GraphDatabaseService db) { List<Double> weightVector; Transaction tx = db.beginTx(); // Get class id Long classId = db.findNodesByLabelAndProperty(DynamicLabel.label("Class"), "name", key) .iterator() .next() .getId(); // Get weight vector for class List<Long> longs = documents .get(key) .stream() .map(a -> ((Integer) a.get("feature")).longValue()) .collect(Collectors.toList()); weightVector = featureIndexList .stream() .map(i -> longs.contains(i.longValue()) ? tfidf(db, i.longValue(), classId) : 0.0) .collect(Collectors.toList()); tx.success(); tx.close(); return weightVector; }
@Test public void listing3_4_create_realationships_between_users() { usersAndMovies.createSimpleRelationshipsBetweenUsers(); try (Transaction tx = graphDb.beginTx()) { assertTrue(usersAndMovies.user1.hasRelationship(MyRelationshipTypes.IS_FRIEND_OF)); tx.success(); } }
@Test public void listing3_2_create_single_user() { usersAndMovies.createSingleUser(); try (Transaction tx = graphDb.beginTx()) { assertNotNull(graphDb.getNodeById(0)); tx.success(); } }
@Test public void shouldUseDynamicPropertiesToIndexANodeWhenAddedAlongsideExistingPropertiesInASeparateTransaction() throws Exception { // Given GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); // When long id; { try (Transaction tx = beansAPI.beginTx()) { Node myNode = beansAPI.createNode(); id = myNode.getId(); myNode.setProperty("key0", true); myNode.setProperty("key1", true); tx.success(); } } { IndexDefinition indexDefinition; try (Transaction tx = beansAPI.beginTx()) { indexDefinition = beansAPI.schema().indexFor(LABEL1).on("key2").create(); tx.success(); } waitForIndex(beansAPI, indexDefinition); } Node myNode; { try (Transaction tx = beansAPI.beginTx()) { myNode = beansAPI.getNodeById(id); myNode.addLabel(LABEL1); myNode.setProperty("key2", LONG_STRING); myNode.setProperty("key3", LONG_STRING); tx.success(); } } // Then assertThat(myNode, inTx(beansAPI, hasProperty("key2").withValue(LONG_STRING))); assertThat(myNode, inTx(beansAPI, hasProperty("key3").withValue(LONG_STRING))); assertThat( findNodesByLabelAndProperty(LABEL1, "key2", LONG_STRING, beansAPI), containsOnly(myNode)); }
private void addLabels(Node node, Label... labels) { try (Transaction tx = dbRule.getGraphDatabaseService().beginTx()) { for (Label label : labels) { node.addLabel(label); } tx.success(); } }
@Test public void deletedNewPropsShouldNotInfluenceEquality() { // bug test try (Transaction tx = database.beginTx()) { database.createNode().setProperty("accident", "dummy"); database.createNode(); tx.success(); } try (Transaction tx = database.beginTx()) { database.getNodeById(0).delete(); tx.success(); } String cypher = "CREATE (n)"; assertSameGraph(database, cypher); }
@Test public void deletedNewLabelShouldNotInfluenceEquality() { // bug test try (Transaction tx = database.beginTx()) { database.createNode(DynamicLabel.label("Accident")); database.createNode(DynamicLabel.label("RealLabel")); tx.success(); } try (Transaction tx = database.beginTx()) { database.getNodeById(0).delete(); tx.success(); } String cypher = "CREATE (n:RealLabel)"; assertSameGraph(database, cypher); }
@Test public void listing3_5_add_properties_to_user_nodes() { usersAndMovies.addPropertiesToUserNodes(); try (Transaction tx = graphDb.beginTx()) { assertTrue(usersAndMovies.user1.hasProperty("name")); assertEquals("John Johnson", usersAndMovies.user1.getProperty("name")); tx.success(); } }
@Test public void listing3_6_add_more_properties_to_user_nodes() { usersAndMovies.addMorePropertiesToUsers(); try (Transaction tx = graphDb.beginTx()) { assertTrue(usersAndMovies.user1.hasProperty("year_of_birth")); assertEquals(1982, usersAndMovies.user1.getProperty("year_of_birth")); tx.success(); } }
@Test public void deletedRelationshipWithNewTypeShouldNotInfluenceEquality() { // bug test try (Transaction tx = database.beginTx()) { Node node1 = database.createNode(); Node node2 = database.createNode(); node1.createRelationshipTo(node2, DynamicRelationshipType.withName("ACCIDENT")); tx.success(); } try (Transaction tx = database.beginTx()) { GlobalGraphOperations.at(database).getAllRelationships().iterator().next().delete(); tx.success(); } String cypher = "CREATE (n), (m)"; assertSameGraph(database, cypher); }
@Test public void listing3_7_create_movies() { try (Transaction tx = graphDb.beginTx()) { assertNotNull(graphDb.getNodeById(3)); assertNotNull(graphDb.getNodeById(4)); assertNotNull(graphDb.getNodeById(5)); tx.success(); } }
@Before public void setUp() throws Exception { Transaction tx = neo4jTemplate.getGraphDatabase().beginTx(); try { Neo4jHelper.cleanDb(neo4jTemplate); } finally { tx.success(); tx.finish(); } tx = neo4jTemplate.getGraphDatabase().beginTx(); try { referenceNode = graphDatabase.getReferenceNode(); createData(); } finally { tx.success(); tx.finish(); } }
@Test public void listing3_8_add_type_properties_to_all_nodes() { usersAndMovies.addTypePropertiesToNodes(); try (Transaction tx = graphDb.beginTx()) { assertEquals("User", usersAndMovies.user1.getProperty("type")); assertEquals("Movie", usersAndMovies.movie1.getProperty("type")); tx.success(); } }
@Test public void listing3_3_create_multiple_users() { usersAndMovies.createMultipleUsersInSingleTransaction(); try (Transaction tx = graphDb.beginTx()) { assertNotNull(graphDb.getNodeById(1)); assertNotNull(graphDb.getNodeById(2)); tx.success(); } }
@Test(expected = org.neo4j.graphdb.NotFoundException.class) public void testDeleteNode() { final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map("name", "node1")); n1.delete(); Node n2 = restAPI.createNode(map("name", "node2")); tx.success(); tx.finish(); loadRealNode(n1); }
private Node createNode( GraphDatabaseService beansAPI, Map<String, Object> properties, Label... labels) { try (Transaction tx = beansAPI.beginTx()) { Node node = beansAPI.createNode(labels); for (Map.Entry<String, Object> property : properties.entrySet()) node.setProperty(property.getKey(), property.getValue()); tx.success(); return node; } }
@Test public void listing3_10_node_labels() { usersAndMovies.addLabelToMovies(); try (Transaction tx = graphDb.beginTx()) { ResourceIterable<Node> movies = GlobalGraphOperations.at(graphDb).getAllNodesWithLabel(DynamicLabel.label("MOVIES")); assertEquals(3, IteratorUtil.count(movies)); tx.success(); } }
@Test(expected = UnsupportedOperationException.class) public void testRemoveEntryFromIndexWithGivenNodeAndKeyAndValue() { final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map("name", "node1")); final Index<Node> index = restAPI.index().forNodes("testIndex"); index.add(n1, "indexname", "Node1"); index.remove(n1, "indexname", "Node1"); tx.success(); tx.finish(); assertNull(index.get("indexname", "Node1").getSingle()); }
@Test public void testRemoveEntryFromIndexWithGivenNode() { Node n1 = restAPI.createNode(map("name", "node1")); final Index<Node> index = restAPI.index().forNodes("testIndex"); index.add(n1, "indexname", "Node1"); final Transaction tx = restAPI.beginTx(); index.remove(n1); tx.success(); tx.finish(); assertNull(index.get("indexname", "Node1").getSingle()); }
@Test public void testDeleteIndex() { final MatrixDataGraph matrixDataGraph = new MatrixDataGraph(getGraphDatabase()); matrixDataGraph.createNodespace(); final Transaction tx = restAPI.beginTx(); final Index<Node> heroes = restAPI.index().forNodes("heroes"); heroes.delete(); tx.success(); tx.finish(); Assert.assertFalse(getGraphDatabase().index().existsForNodes("heroes")); }
@Test(expected = RestResultException.class) public void testFailingDoubleDelete() throws Exception { final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map()); n1.delete(); n1.delete(); tx.success(); tx.finish(); }
@Test public void equalArraysWithDifferentTypeShouldBeEqual() { try (Transaction tx = database.beginTx()) { database.createNode().setProperty("number", new int[] {123, 124}); tx.success(); } String cypher = "CREATE (n {number:[123,124]})"; assertSameGraph(database, cypher); }