@Test
  public void testFindByIdFound() {
    // Arrange
    //
    String id = "ABCDEF";
    SubscriptionExpiry expiry = SubscriptionExpiry.NEVER;
    Subscription expected = Subscription.builder().id(id).expiry(expiry).build();

    when(mock_node.getProperty(eq(Subscription.PROP_ID))).thenReturn(expected.id());
    when(mock_node.getProperty(eq(Subscription.PROP_EXPIRY)))
        .thenReturn(expected.expiry().toString());

    when(mock_db.beginTx()).thenReturn(mock_tx);
    when(mock_db.findNode(eq(label), eq(Subscription.PROP_ID), eq(expected.id())))
        .thenReturn(mock_node);

    // Act
    //
    Subscription subs = s_dao.findById(id);

    // Assert
    //
    assertNotNull(subs);
    assertTrue(expected.equals(subs));
    verify(mock_db, atLeastOnce()).beginTx();
    verify(mock_db, times(1)).findNode(any(), any(), any());
    verify(mock_tx, atLeastOnce()).success();
    verify(mock_tx, times(1)).close();
  }
Example #2
0
  @Test
  public void insert() throws InterruptedException {
    // START SNIPPET: insert
    BatchInserter inserter = null;
    try {
      inserter =
          BatchInserters.inserter(
              new File("target/batchinserter-example").getAbsolutePath(), fileSystem);

      Label personLabel = DynamicLabel.label("Person");
      inserter.createDeferredSchemaIndex(personLabel).on("name").create();

      Map<String, Object> properties = new HashMap<>();

      properties.put("name", "Mattias");
      long mattiasNode = inserter.createNode(properties, personLabel);

      properties.put("name", "Chris");
      long chrisNode = inserter.createNode(properties, personLabel);

      RelationshipType knows = DynamicRelationshipType.withName("KNOWS");
      inserter.createRelationship(mattiasNode, chrisNode, knows, null);
    } finally {
      if (inserter != null) {
        inserter.shutdown();
      }
    }
    // END SNIPPET: insert

    // try it out from a normal db
    GraphDatabaseService db =
        new TestGraphDatabaseFactory()
            .setFileSystem(fileSystem)
            .newImpermanentDatabase(new File("target/batchinserter-example").getAbsolutePath());
    try (Transaction tx = db.beginTx()) {
      Label personLabelForTesting = DynamicLabel.label("Person");
      Node mNode = db.findNode(personLabelForTesting, "name", "Mattias");
      Node cNode =
          mNode
              .getSingleRelationship(DynamicRelationshipType.withName("KNOWS"), Direction.OUTGOING)
              .getEndNode();
      assertThat((String) cNode.getProperty("name"), is("Chris"));
      assertThat(db.schema().getIndexes(personLabelForTesting).iterator().hasNext(), is(true));
    } finally {
      db.shutdown();
    }
  }
  @Test
  public void testFindByIdNotFound() {
    // Arrange
    //
    String id = "Foobar";
    when(mock_db.beginTx()).thenReturn(mock_tx);
    when(mock_db.findNode(any(), any(), any())).thenReturn(null);

    // Act
    //
    Subscription subs = s_dao.findById(id);

    // Assert
    //
    assertThat(subs, is(nullValue()));
    verify(mock_db, atLeastOnce()).beginTx();
    verify(mock_db, times(1)).findNode(any(), any(), any());
    verify(mock_tx, atLeastOnce()).success();
    verify(mock_tx, times(1)).close();
  }
  @Test
  public void nodeCanBecomeSchemaIndexableInBeforeCommitByAddingLabel() throws Exception {
    // Given we have a schema index...
    GraphDatabaseService db = dbRule.getGraphDatabaseService();
    final Label label = DynamicLabel.label("Label");
    IndexDefinition index;
    try (Transaction tx = db.beginTx()) {
      index = db.schema().indexFor(label).on("indexed").create();
      tx.success();
    }

    // ... and a transaction event handler that likes to add the indexed property on nodes
    db.registerTransactionEventHandler(
        new TransactionEventHandler.Adapter<Object>() {
          @Override
          public Object beforeCommit(TransactionData data) throws Exception {
            Iterator<Node> nodes = data.createdNodes().iterator();
            if (nodes.hasNext()) {
              Node node = nodes.next();
              node.addLabel(label);
            }
            return null;
          }
        });

    // When we create a node with the right property, but not the right label...
    try (Transaction tx = db.beginTx()) {
      db.schema().awaitIndexesOnline(10, TimeUnit.SECONDS);
      Node node = db.createNode();
      node.setProperty("indexed", "value");
      node.setProperty("random", 42);
      tx.success();
    }

    // Then we should be able to look it up through the index.
    try (Transaction ignore = db.beginTx()) {
      Node node = db.findNode(label, "indexed", "value");
      assertThat(node.getProperty("random"), is((Object) 42));
    }
  }