Search in sources :

Example 16 with InputEventStream

use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.

the class InvocationTest method shouldCommitSinglePeriodicCommitStatement.

@Test
void shouldCommitSinglePeriodicCommitStatement() {
    // given
    String queryText = "USING PERIODIC COMMIT CREATE()";
    var transaction = mock(InternalTransaction.class);
    when(databaseFacade.beginTransaction(eq(IMPLICIT), any(LoginContext.class), any(ClientConnectionInfo.class), anyLong(), any(TimeUnit.class))).thenReturn(transaction);
    when(transaction.execute(eq(queryText), any())).thenReturn(executionResult);
    when(executionEngine.isPeriodicCommit(queryText)).thenReturn(true);
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement(queryText, map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    mockDefaultResult();
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    // when
    invocation.execute(outputEventStream);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeStatementStart(statement, List.of("c1", "c2", "c3"));
    verifyDefaultResultRows(outputOrder);
    outputOrder.verify(outputEventStream).writeStatementEnd(queryExecutionType, queryStatistics, executionPlanDescription, notifications);
    outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.COMMITTED, uriScheme.txCommitUri(1337L), -1);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : ClientConnectionInfo(org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo) LoginContext(org.neo4j.internal.kernel.api.security.LoginContext) InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) TimeUnit(java.util.concurrent.TimeUnit) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 17 with InputEventStream

use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.

the class InvocationTest method shouldHandleErrorWhenStartingTransaction.

@Test
void shouldHandleErrorWhenStartingTransaction() {
    // given
    when(databaseFacade.beginTransaction(any(), any(), any())).thenThrow(new IllegalStateException("Something went wrong"));
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    mockDefaultResult();
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    // when
    invocation.execute(outputEventStream);
    // then
    verify(log).error(eq("Failed to start transaction"), any(IllegalStateException.class));
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeFailure(Status.Transaction.TransactionStartFailed, "Something went wrong");
    outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.NO_TRANSACTION, uriScheme.txCommitUri(1337L), -1);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 18 with InputEventStream

use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.

the class InvocationTest method shouldCommitTransactionAndTellRegistryToForgetItsHandle.

@Test
void shouldCommitTransactionAndTellRegistryToForgetItsHandle() {
    // given
    when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    mockDefaultResult();
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    // when
    invocation.execute(outputEventStream);
    // then
    InOrder transactionOrder = inOrder(internalTransaction, registry);
    transactionOrder.verify(internalTransaction).commit();
    transactionOrder.verify(registry).forget(1337L);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeStatementStart(statement, List.of("c1", "c2", "c3"));
    verifyDefaultResultRows(outputOrder);
    outputOrder.verify(outputEventStream).writeStatementEnd(queryExecutionType, queryStatistics, executionPlanDescription, notifications);
    outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.COMMITTED, uriScheme.txCommitUri(1337L), -1);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 19 with InputEventStream

use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.

the class InvocationTest method shouldRollbackTransactionIfExecutionErrorOccurs.

@Test
void shouldRollbackTransactionIfExecutionErrorOccurs() {
    // given
    when(internalTransaction.execute("query", emptyMap())).thenThrow(new IllegalStateException("Something went wrong"));
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    // when
    invocation.execute(outputEventStream);
    // then
    verify(internalTransaction).execute("query", emptyMap());
    verify(internalTransaction).rollback();
    verify(registry).forget(1337L);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeFailure(Status.Statement.ExecutionFailed, "Something went wrong");
    outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.ROLLED_BACK, uriScheme.txCommitUri(1337L), -1);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 20 with InputEventStream

use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.

the class InvocationTest method shouldKeepTransactionOpenIfConnectionErrorWhenWritingOutputInImplicitTransaction.

@Test
void shouldKeepTransactionOpenIfConnectionErrorWhenWritingOutputInImplicitTransaction() {
    // given
    when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry, false);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    mockDefaultResult();
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, false);
    doThrow(new ConnectionException("Connection error", new IOException("Broken pipe"))).when(outputEventStream).writeStatementEnd(any(), any(), any(), any());
    // when
    var e = assertThrows(ConnectionException.class, () -> invocation.execute(outputEventStream));
    assertEquals("Connection error", e.getMessage());
    // then
    verify(internalTransaction, never()).rollback();
    verify(registry, never()).forget(1337L);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeStatementStart(statement, List.of("c1", "c2", "c3"));
    verifyDefaultResultRows(outputOrder);
    outputOrder.verify(outputEventStream).writeStatementEnd(queryExecutionType, queryStatistics, executionPlanDescription, notifications);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) IOException(java.io.IOException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Aggregations

InputEventStream (org.neo4j.server.http.cypher.format.api.InputEventStream)25 MemoryPool (org.neo4j.memory.MemoryPool)24 Test (org.junit.jupiter.api.Test)21 Statement (org.neo4j.server.http.cypher.format.api.Statement)18 InOrder (org.mockito.InOrder)17 IOException (java.io.IOException)5 LoginContext (org.neo4j.internal.kernel.api.security.LoginContext)5 ConnectionException (org.neo4j.server.http.cypher.format.api.ConnectionException)4 URI (java.net.URI)3 Collections.emptyMap (java.util.Collections.emptyMap)3 Map (java.util.Map)3 Objects.requireNonNullElse (java.util.Objects.requireNonNullElse)3 Optional (java.util.Optional)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 DELETE (javax.ws.rs.DELETE)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 PathParam (javax.ws.rs.PathParam)3 Context (javax.ws.rs.core.Context)3 HttpHeaders (javax.ws.rs.core.HttpHeaders)3