Search in sources :

Example 36 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class Neo4jTransactionalContextTest method neverStopsExecutingQueryDuringCommitAndRestartTx.

@SuppressWarnings("ConstantConditions")
@Test
public void neverStopsExecutingQueryDuringCommitAndRestartTx() {
    // Given
    KernelTransaction initialKTX = mock(KernelTransaction.class);
    InternalTransaction initialTransaction = mock(InternalTransaction.class, new ReturnsDeepStubs());
    KernelTransaction.Type transactionType = KernelTransaction.Type.implicit;
    SecurityContext securityContext = SecurityContext.AUTH_DISABLED;
    when(initialTransaction.transactionType()).thenReturn(transactionType);
    when(initialTransaction.securityContext()).thenReturn(securityContext);
    QueryRegistryOperations initialQueryRegistry = mock(QueryRegistryOperations.class);
    ExecutingQuery executingQuery = mock(ExecutingQuery.class);
    PropertyContainerLocker locker = null;
    ThreadToStatementContextBridge txBridge = mock(ThreadToStatementContextBridge.class);
    KernelTransaction secondKTX = mock(KernelTransaction.class);
    InternalTransaction secondTransaction = mock(InternalTransaction.class);
    Statement secondStatement = mock(Statement.class);
    QueryRegistryOperations secondQueryRegistry = mock(QueryRegistryOperations.class);
    when(executingQuery.queryText()).thenReturn("X");
    when(executingQuery.queryParameters()).thenReturn(Collections.emptyMap());
    when(initialStatement.queryRegistration()).thenReturn(initialQueryRegistry);
    when(queryService.beginTransaction(transactionType, securityContext)).thenReturn(secondTransaction);
    when(txBridge.getKernelTransactionBoundToThisThread(true)).thenReturn(initialKTX, secondKTX);
    when(txBridge.get()).thenReturn(secondStatement);
    when(secondStatement.queryRegistration()).thenReturn(secondQueryRegistry);
    Neo4jTransactionalContext context = new Neo4jTransactionalContext(queryService, null, guard, txBridge, locker, initialTransaction, initialStatement, executingQuery);
    // When
    context.commitAndRestartTx();
    // Then
    Object[] mocks = { txBridge, initialTransaction, initialQueryRegistry, initialKTX, secondQueryRegistry, secondKTX };
    InOrder order = Mockito.inOrder(mocks);
    // (0) Constructor
    order.verify(initialTransaction).transactionType();
    order.verify(initialTransaction).securityContext();
    // (1) Unbind old
    order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
    order.verify(txBridge).unbindTransactionFromCurrentThread();
    // (2) Register and unbind new
    order.verify(txBridge).get();
    order.verify(secondQueryRegistry).registerExecutingQuery(executingQuery);
    order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
    order.verify(txBridge).unbindTransactionFromCurrentThread();
    // (3) Rebind, unregister, and close old
    order.verify(txBridge).bindTransactionToCurrentThread(initialKTX);
    order.verify(initialQueryRegistry).unregisterExecutingQuery(executingQuery);
    order.verify(initialTransaction).success();
    order.verify(initialTransaction).close();
    order.verify(txBridge).unbindTransactionFromCurrentThread();
    // (4) Rebind new
    order.verify(txBridge).bindTransactionToCurrentThread(secondKTX);
    verifyNoMoreInteractions(mocks);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InOrder(org.mockito.InOrder) Statement(org.neo4j.kernel.api.Statement) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryRegistryOperations(org.neo4j.kernel.api.QueryRegistryOperations) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) PropertyContainerLocker(org.neo4j.kernel.impl.coreapi.PropertyContainerLocker) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) ReturnsDeepStubs(org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs) Test(org.junit.Test)

Example 37 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class Neo4jTransactionalContextTest method rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit.

@SuppressWarnings("ConstantConditions")
@Test
public void rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit() {
    // Given
    InternalTransaction initialTransaction = mock(InternalTransaction.class, new ReturnsDeepStubs());
    KernelTransaction.Type transactionType = KernelTransaction.Type.implicit;
    SecurityContext securityContext = SecurityContext.AUTH_DISABLED;
    when(initialTransaction.transactionType()).thenReturn(transactionType);
    when(initialTransaction.securityContext()).thenReturn(securityContext);
    GraphDatabaseQueryService queryService = mock(GraphDatabaseQueryService.class);
    KernelTransaction initialKTX = mock(KernelTransaction.class);
    Statement initialStatement = mock(Statement.class);
    QueryRegistryOperations initialQueryRegistry = mock(QueryRegistryOperations.class);
    ExecutingQuery executingQuery = mock(ExecutingQuery.class);
    PropertyContainerLocker locker = new PropertyContainerLocker();
    ThreadToStatementContextBridge txBridge = mock(ThreadToStatementContextBridge.class);
    KernelTransaction secondKTX = mock(KernelTransaction.class);
    InternalTransaction secondTransaction = mock(InternalTransaction.class);
    Statement secondStatement = mock(Statement.class);
    QueryRegistryOperations secondQueryRegistry = mock(QueryRegistryOperations.class);
    when(executingQuery.queryText()).thenReturn("X");
    when(executingQuery.queryParameters()).thenReturn(Collections.emptyMap());
    Mockito.doThrow(RuntimeException.class).when(initialTransaction).close();
    when(initialStatement.queryRegistration()).thenReturn(initialQueryRegistry);
    when(queryService.beginTransaction(transactionType, securityContext)).thenReturn(secondTransaction);
    when(txBridge.getKernelTransactionBoundToThisThread(true)).thenReturn(initialKTX, secondKTX);
    when(txBridge.get()).thenReturn(secondStatement);
    when(secondStatement.queryRegistration()).thenReturn(secondQueryRegistry);
    Neo4jTransactionalContext context = new Neo4jTransactionalContext(queryService, null, guard, txBridge, locker, initialTransaction, initialStatement, executingQuery);
    // When
    try {
        context.commitAndRestartTx();
        throw new AssertionError("Expected RuntimeException to be thrown");
    } catch (RuntimeException e) {
        // Then
        Object[] mocks = { txBridge, initialTransaction, initialQueryRegistry, initialKTX, secondQueryRegistry, secondKTX, secondTransaction };
        InOrder order = Mockito.inOrder(mocks);
        // (0) Constructor
        order.verify(initialTransaction).transactionType();
        order.verify(initialTransaction).securityContext();
        // (1) Unbind old
        order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
        order.verify(txBridge).unbindTransactionFromCurrentThread();
        // (2) Register and unbind new
        order.verify(txBridge).get();
        order.verify(secondQueryRegistry).registerExecutingQuery(executingQuery);
        order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
        order.verify(txBridge).unbindTransactionFromCurrentThread();
        // (3) Rebind, unregister, and close old
        order.verify(txBridge).bindTransactionToCurrentThread(initialKTX);
        order.verify(initialQueryRegistry).unregisterExecutingQuery(executingQuery);
        order.verify(initialTransaction).success();
        order.verify(initialTransaction).close();
        order.verify(txBridge).bindTransactionToCurrentThread(secondKTX);
        order.verify(secondTransaction).failure();
        order.verify(secondTransaction).close();
        order.verify(txBridge).unbindTransactionFromCurrentThread();
        verifyNoMoreInteractions(mocks);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InOrder(org.mockito.InOrder) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) Statement(org.neo4j.kernel.api.Statement) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryRegistryOperations(org.neo4j.kernel.api.QueryRegistryOperations) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) PropertyContainerLocker(org.neo4j.kernel.impl.coreapi.PropertyContainerLocker) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) ReturnsDeepStubs(org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs) Test(org.junit.Test)

Example 38 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class BuiltInProceduresIT method listRelationshipTypes.

@Test
public void listRelationshipTypes() throws Throwable {
    // Given
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    int relType = statement.tokenWriteOperations().relationshipTypeGetOrCreateForName("MyRelType");
    long startNodeId = statement.dataWriteOperations().nodeCreate();
    long endNodeId = statement.dataWriteOperations().nodeCreate();
    statement.dataWriteOperations().relationshipCreate(relType, startNodeId, endNodeId);
    commit();
    // When
    RawIterator<Object[], ProcedureException> stream = procedureCallOpsInNewTx().procedureCallRead(procedureName("db", "relationshipTypes"), new Object[0]);
    // Then
    assertThat(asList(stream), contains(equalTo(new Object[] { "MyRelType" })));
}
Also used : Statement(org.neo4j.kernel.api.Statement) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Example 39 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class BuiltInProceduresIT method listAllLabels.

@Test
public void listAllLabels() throws Throwable {
    // Given
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    long nodeId = statement.dataWriteOperations().nodeCreate();
    int labelId = statement.tokenWriteOperations().labelGetOrCreateForName("MyLabel");
    statement.dataWriteOperations().nodeAddLabel(nodeId, labelId);
    commit();
    // When
    RawIterator<Object[], ProcedureException> stream = procedureCallOpsInNewTx().procedureCallRead(procedureName("db", "labels"), new Object[0]);
    // Then
    assertThat(asList(stream), contains(equalTo(new Object[] { "MyLabel" })));
}
Also used : Statement(org.neo4j.kernel.api.Statement) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Example 40 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class KernelIT method removingExistingLabelFromNodeShouldRespondTrue.

@Test
public void removingExistingLabelFromNodeShouldRespondTrue() throws Exception {
    // GIVEN
    Transaction tx = db.beginTx();
    Node node = db.createNode();
    Statement statement = statementContextSupplier.get();
    int labelId = statement.tokenWriteOperations().labelGetOrCreateForName("mylabel");
    statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId);
    statement.close();
    tx.success();
    tx.close();
    // WHEN
    tx = db.beginTx();
    statement = statementContextSupplier.get();
    boolean removed = statement.dataWriteOperations().nodeRemoveLabel(node.getId(), labelId);
    // THEN
    assertTrue("Should have been removed now", removed);
    tx.close();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Statement(org.neo4j.kernel.api.Statement) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Aggregations

Statement (org.neo4j.kernel.api.Statement)158 Test (org.junit.Test)76 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)56 Transaction (org.neo4j.graphdb.Transaction)44 ReadOperations (org.neo4j.kernel.api.ReadOperations)40 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)30 EntityNotFoundException (org.neo4j.kernel.api.exceptions.EntityNotFoundException)23 NotFoundException (org.neo4j.graphdb.NotFoundException)21 Node (org.neo4j.graphdb.Node)20 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)19 PropertyNotFoundException (org.neo4j.kernel.api.exceptions.PropertyNotFoundException)13 KeyReadOperations (org.neo4j.kernel.impl.api.operations.KeyReadOperations)11 ArrayList (java.util.ArrayList)10 DependencyResolver (org.neo4j.graphdb.DependencyResolver)10 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)10 Label (org.neo4j.graphdb.Label)9 KernelAPI (org.neo4j.kernel.api.KernelAPI)9 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)9 InvalidTransactionTypeKernelException (org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException)8 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)8