@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 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));
  }
Exemplo n.º 3
0
 private Node createLabeledNode(Label... labels) {
   try (Transaction tx = dbRule.getGraphDatabaseService().beginTx()) {
     Node node = dbRule.getGraphDatabaseService().createNode(labels);
     tx.success();
     return node;
   }
 }
  @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 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();
   }
 }
Exemplo n.º 7
0
 private void addLabels(Node node, Label... labels) {
   try (Transaction tx = dbRule.getGraphDatabaseService().beginTx()) {
     for (Label label : labels) {
       node.addLabel(label);
     }
     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_7_create_movies() {
   try (Transaction tx = graphDb.beginTx()) {
     assertNotNull(graphDb.getNodeById(3));
     assertNotNull(graphDb.getNodeById(4));
     assertNotNull(graphDb.getNodeById(5));
     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 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_3_create_multiple_users() {
   usersAndMovies.createMultipleUsersInSingleTransaction();
   try (Transaction tx = graphDb.beginTx()) {
     assertNotNull(graphDb.getNodeById(1));
     assertNotNull(graphDb.getNodeById(2));
     tx.success();
   }
 }
 @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 = 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);
 }
  @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 listing3_9_add_properties_to_relationships() {
   usersAndMovies.addPropertiesToRelationships();
   try (Transaction tx = graphDb.beginTx()) {
     Relationship hasSeen =
         usersAndMovies.user1.getSingleRelationship(
             MyRelationshipTypes.HAS_SEEN, Direction.OUTGOING);
     assertEquals(5, hasSeen.getProperty("stars"));
     tx.success();
   }
 }
 @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(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 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
 public void testQueryIndex() {
   final MatrixDataGraph matrixDataGraph = new MatrixDataGraph(getGraphDatabase());
   matrixDataGraph.createNodespace();
   final Transaction tx = restAPI.beginTx();
   final Index<Node> index = restAPI.index().forNodes("heroes");
   final IndexHits<Node> heroes = index.query("name:Neo");
   tx.success();
   tx.finish();
   assertEquals("1 hero", 1, heroes.size());
   assertEquals("Neo indexed", matrixDataGraph.getNeoNode(), heroes.iterator().next());
 }
 @Test
 public void testCreateNode() {
   final Transaction tx = restAPI.beginTx();
   Node n1 = restAPI.createNode(map("name", "node1"));
   Node n2 = restAPI.createNode(map("name", "node2"));
   final int count = countExistingNodes();
   assertEquals("only reference node", 1, count);
   tx.success();
   tx.finish();
   assertEquals("node1", n1.getProperty("name"));
   assertEquals("node1", loadRealNode(n1).getProperty("name"));
   assertEquals("node2", n2.getProperty("name"));
 }
 @Test
 public void listing3_11_node_labels_and_property() {
   try {
     usersAndMovies.addLabelToMovies();
   } catch (Exception e) {
     // ignore if index already exist
   }
   try (Transaction tx = graphDb.beginTx()) {
     ResourceIterable<Node> movies =
         graphDb.findNodesByLabelAndProperty(DynamicLabel.label("MOVIES"), "name", "Fargo");
     assertEquals(1, IteratorUtil.count(movies));
     tx.success();
   }
 }
  @Test
  public void testCreateNodeAndAddToIndex() throws Exception {
    RestIndex<Node> index =
        restAPI.createIndex(Node.class, "index", LuceneIndexImplementation.FULLTEXT_CONFIG);
    final Transaction tx = restAPI.beginTx();

    Node n1 = restAPI.createNode(map());
    index.add(n1, "key", "value");

    tx.success();
    tx.finish();
    Node node = index.get("key", "value").getSingle();
    assertEquals("created node found in index", n1, node);
  }
 @Test
 public void testDeleteRelationship() {
   Transaction tx = restAPI.beginTx();
   Node n1 = restAPI.createNode(map("name", "newnode1"));
   Node n2 = restAPI.createNode(map("name", "newnode2"));
   Relationship rel = restAPI.createRelationship(n1, n2, Type.TEST, map("name", "rel"));
   rel.delete();
   tx.success();
   tx.finish();
   Relationship foundRelationship =
       TestHelper.firstRelationshipBetween(
           n1.getRelationships(Type.TEST, Direction.OUTGOING), n1, n2);
   Assert.assertNull("found relationship", foundRelationship);
 }
  @Test
  public void testSetNodeProperties() {
    final Transaction tx = restAPI.beginTx();

    Node n1 = restAPI.createNode(map("name", "node1"));
    n1.setProperty("test", "true");
    n1.setProperty("test2", "stilltrue");
    tx.success();
    tx.finish();
    assertEquals("node1", n1.getProperty("name"));
    assertEquals("true", n1.getProperty("test"));
    assertEquals("stilltrue", n1.getProperty("test2"));
    assertEquals("true", loadRealNode(n1).getProperty("test"));
    assertEquals("stilltrue", loadRealNode(n1).getProperty("test2"));
  }
  @Test
  public void testCreateRelationshipToNodeOutsideofBatch() throws Exception {
    final Node node1 = restAPI.createNode(map());
    final Transaction tx = restAPI.beginTx();

    Node node2 = restAPI.createNode(map());
    final Relationship relationship =
        node1.createRelationshipTo(node2, DynamicRelationshipType.withName("foo"));

    tx.success();
    tx.finish();
    assertEquals("foo", relationship.getType().name());
    assertEquals(
        "foo", getGraphDatabase().getRelationshipById(relationship.getId()).getType().name());
  }
  @Test(expected = RestResultException.class)
  public void testFailingCreateNodeAndAddToIndex() throws Exception {
    RestIndex<Node> index =
        restAPI.createIndex(
            Node.class,
            "index",
            MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "type", "fulltext_ _"));
    final Transaction tx = restAPI.beginTx();

    Node n1 = restAPI.createNode(map());
    index.add(n1, "key", "value");

    tx.success();
    tx.finish();
    Node node = index.get("key", "value").getSingle();
    assertEquals("created node found in index", n1, node);
  }
 @Test
 public void shouldExecuteCallbackInTransaction() throws Exception {
   Node refNode =
       neo4jTemplate.exec(
           new GraphCallback<Node>() {
             @Override
             public Node doWithGraph(GraphDatabase graph) throws Exception {
               Node referenceNode = graph.getReferenceNode();
               referenceNode.setProperty("test", "testDoInTransaction");
               return referenceNode;
             }
           });
   Transaction tx = graphDatabase.beginTx();
   try {
     assertEquals("same reference node", referenceNode, refNode);
     assertTestPropertySet(referenceNode, "testDoInTransaction");
   } finally {
     tx.success();
     tx.finish();
   }
 }
Exemplo n.º 30
0
  @Test
  public void shouldHandleLargeAmountsOfNodesAddedAndRemovedInSameTx() throws Exception {
    // Given
    GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI();
    int labelsToAdd = 80;
    int labelsToRemove = 40;

    // When
    Node node;
    try (Transaction tx = db.beginTx()) {
      node = db.createNode();

      // I create a lot of labels, enough to push the store to use two dynamic records
      for (int l = 0; l < labelsToAdd; l++) {
        node.addLabel(label("Label-" + l));
      }

      // and I delete some of them, enough to bring the number of dynamic records needed down to 1
      for (int l = 0; l < labelsToRemove; l++) {
        node.removeLabel(label("Label-" + l));
      }

      tx.success();
    }

    // Then
    try (Transaction ignore = db.beginTx()) {
      // All the labels remaining should be in the label scan store
      for (int l = labelsToAdd - 1; l >= labelsToRemove; l--) {
        Label label = label("Label-" + l);
        assertThat(
            "Should have founnd node when looking for label " + label,
            single(GlobalGraphOperations.at(db).getAllNodesWithLabel(label)),
            equalTo(node));
      }
    }
  }