use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class CompositeIndexingIT method shouldSeeNodeAddedToByLabelIndexInTransaction.
@Test
public void shouldSeeNodeAddedToByLabelIndexInTransaction() throws Exception {
try (Transaction ignore = graphDatabaseAPI.beginTx()) {
DataWriteOperations writeOperations = statement().dataWriteOperations();
long nodeID = writeOperations.nodeCreate();
for (int propID : index.schema().getPropertyIds()) {
writeOperations.nodeSetProperty(nodeID, DefinedProperty.intProperty(propID, propID));
}
writeOperations.nodeAddLabel(nodeID, LABEL_ID);
PrimitiveLongIterator resultIterator = seek();
assertThat(resultIterator.next(), equalTo(nodeID));
assertFalse(resultIterator.hasNext());
}
}
use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class CompositeIndexingIT method shouldNotSeeNodeThatHasAPropertyRemovedInTransaction.
@Test
public void shouldNotSeeNodeThatHasAPropertyRemovedInTransaction() throws Exception {
long nodeID = createNode();
try (Transaction ignore = graphDatabaseAPI.beginTx()) {
statement().dataWriteOperations().nodeRemoveProperty(nodeID, index.schema().getPropertyIds()[0]);
assertFalse(seek().hasNext());
}
}
use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class UniqueFactoryTest method shouldCreateNodeAndIndexItIfMissing.
@Test
public void shouldCreateNodeAndIndexItIfMissing() {
// given
GraphDatabaseService graphdb = mock(GraphDatabaseService.class);
@SuppressWarnings("unchecked") Index<Node> index = mock(Index.class);
Transaction tx = mock(Transaction.class);
when(graphdb.beginTx()).thenReturn(tx);
when(index.getGraphDatabase()).thenReturn(graphdb);
@SuppressWarnings("unchecked") IndexHits<Node> indexHits = mock(IndexHits.class);
when(index.get("key1", "value1")).thenReturn(indexHits);
Node indexedNode = mock(Node.class);
when(graphdb.createNode()).thenReturn(indexedNode);
final AtomicBoolean initializeCalled = new AtomicBoolean(false);
UniqueFactory.UniqueNodeFactory unique = new UniqueFactory.UniqueNodeFactory(index) {
@Override
protected void initialize(Node created, Map<String, Object> properties) {
initializeCalled.set(true);
assertEquals(Collections.singletonMap("key1", "value1"), properties);
}
};
// when
Node node = unique.getOrCreate("key1", "value1");
// then
assertSame(node, indexedNode);
verify(index).get("key1", "value1");
verify(index).putIfAbsent(indexedNode, "key1", "value1");
verify(graphdb, times(1)).createNode();
verify(tx).success();
assertTrue("Node not initialized", initializeCalled.get());
}
use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class UniqueFactoryTest method shouldCreateNodeWithOutcomeAndIndexItIfMissing.
@Test
public void shouldCreateNodeWithOutcomeAndIndexItIfMissing() {
// given
GraphDatabaseService graphdb = mock(GraphDatabaseService.class);
@SuppressWarnings("unchecked") Index<Node> index = mock(Index.class);
Transaction tx = mock(Transaction.class);
when(graphdb.beginTx()).thenReturn(tx);
when(index.getGraphDatabase()).thenReturn(graphdb);
@SuppressWarnings("unchecked") IndexHits<Node> indexHits = mock(IndexHits.class);
when(index.get("key1", "value1")).thenReturn(indexHits);
Node indexedNode = mock(Node.class);
when(graphdb.createNode()).thenReturn(indexedNode);
final AtomicBoolean initializeCalled = new AtomicBoolean(false);
UniqueFactory.UniqueNodeFactory unique = new UniqueFactory.UniqueNodeFactory(index) {
@Override
protected void initialize(Node created, Map<String, Object> properties) {
initializeCalled.set(true);
assertEquals(Collections.singletonMap("key1", "value1"), properties);
}
};
// when
UniqueEntity<Node> node = unique.getOrCreateWithOutcome("key1", "value1");
// then
assertSame(node.entity(), indexedNode);
assertTrue(node.wasCreated());
verify(index).get("key1", "value1");
verify(index).putIfAbsent(indexedNode, "key1", "value1");
verify(graphdb, times(1)).createNode();
verify(tx).success();
assertTrue("Node not initialized", initializeCalled.get());
}
use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class TestTransactionEvents method makeSureHandlerIsntCalledWhenTxRolledBack.
@Test
public void makeSureHandlerIsntCalledWhenTxRolledBack() {
DummyTransactionEventHandler<Integer> handler = new DummyTransactionEventHandler<>(10);
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
db.registerTransactionEventHandler(handler);
try {
try (Transaction ignore = db.beginTx()) {
db.createNode().delete();
}
assertNull(handler.beforeCommit);
assertNull(handler.afterCommit);
assertNull(handler.afterRollback);
} finally {
db.unregisterTransactionEventHandler(handler);
}
}
Aggregations