use of org.neo4j.graphdb.Result in project neo4j by neo4j.
the class ExecutionResultSerializerTest method shouldSerializeResponseWithCommitUriAndResults.
@Test
public void shouldSerializeResponseWithCommitUriAndResults() throws Exception {
// given
ByteArrayOutputStream output = new ByteArrayOutputStream();
ExecutionResultSerializer serializer = getSerializerWith(output);
Result executionResult = mockExecutionResult(map("column1", "value1", "column2", "value2"));
// when
serializer.transactionCommitUri(URI.create("commit/uri/1"));
serializer.statementResult(executionResult, false);
serializer.finish();
// then
String result = output.toString(UTF_8.name());
assertEquals("{\"commit\":\"commit/uri/1\",\"results\":[{\"columns\":[\"column1\",\"column2\"]," + "\"data\":[{\"row\":[\"value1\",\"value2\"],\"meta\":[null,null]}]}],\"errors\":[]}", result);
}
use of org.neo4j.graphdb.Result in project neo4j by neo4j.
the class ExecutionResultSerializerTest method shouldProduceWellFormedJsonEvenIfResultIteratorThrowsExceptionOnNext.
@Test
public void shouldProduceWellFormedJsonEvenIfResultIteratorThrowsExceptionOnNext() throws Exception {
// given
ByteArrayOutputStream output = new ByteArrayOutputStream();
ExecutionResultSerializer serializer = getSerializerWith(output);
Map<String, Object> data = map("column1", "value1", "column2", "value2");
Result executionResult = mock(Result.class);
mockAccept(executionResult);
when(executionResult.columns()).thenReturn(new ArrayList<>(data.keySet()));
when(executionResult.hasNext()).thenReturn(true, true, false);
when(executionResult.next()).thenReturn(data).thenThrow(new RuntimeException("Stuff went wrong!"));
// when
try {
serializer.statementResult(executionResult, false);
fail("should have thrown exception");
} catch (RuntimeException e) {
serializer.errors(asList(new Neo4jError(Status.Statement.ExecutionFailed, e)));
}
serializer.finish();
// then
String result = output.toString(UTF_8.name());
assertEquals("{\"results\":[{\"columns\":[\"column1\",\"column2\"],\"data\":[{\"row\":[\"value1\",\"value2\"],\"meta\":[null,null]}]}]," + "\"errors\":[{\"code\":\"Neo.DatabaseError.Statement.ExecutionFailed\",\"message\":\"Stuff went wrong!\",\"stackTrace\":***}]}", replaceStackTrace(result, "***"));
}
use of org.neo4j.graphdb.Result in project neo4j by neo4j.
the class ExecutionResultSerializerTest method shouldSerializeResponseWithCommitUriAndResultsAndErrors.
@Test
public void shouldSerializeResponseWithCommitUriAndResultsAndErrors() throws Exception {
// given
ByteArrayOutputStream output = new ByteArrayOutputStream();
ExecutionResultSerializer serializer = getSerializerWith(output);
Result executionResult = mockExecutionResult(map("column1", "value1", "column2", "value2"));
// when
serializer.transactionCommitUri(URI.create("commit/uri/1"));
serializer.statementResult(executionResult, false);
serializer.errors(asList(new Neo4jError(Status.Request.InvalidFormat, new Exception("cause1"))));
serializer.finish();
// then
String result = output.toString(UTF_8.name());
assertEquals("{\"commit\":\"commit/uri/1\",\"results\":[{\"columns\":[\"column1\",\"column2\"]," + "\"data\":[{\"row\":[\"value1\",\"value2\"],\"meta\":[null,null]}]}]," + "\"errors\":[{\"code\":\"Neo.ClientError.Request.InvalidFormat\",\"message\":\"cause1\"}]}", result);
}
use of org.neo4j.graphdb.Result in project neo4j by neo4j.
the class ExecutionResultSerializerTest method shouldNotReturnPositionWhenEmptyPosition.
@Test
public void shouldNotReturnPositionWhenEmptyPosition() throws IOException {
// given
ByteArrayOutputStream output = new ByteArrayOutputStream();
ExecutionResultSerializer serializer = getSerializerWith(output);
Notification notification = NotificationCode.CARTESIAN_PRODUCT.notification(InputPosition.empty);
List<Notification> notifications = Arrays.asList(notification);
Result executionResult = mockExecutionResult(null, notifications, map("column1", "value1", "column2", "value2"));
// when
serializer.transactionCommitUri(URI.create("commit/uri/1"));
serializer.statementResult(executionResult, false);
serializer.notifications(notifications);
serializer.finish();
// then
String result = output.toString(UTF_8.name());
assertEquals("{\"commit\":\"commit/uri/1\",\"results\":[{\"columns\":[\"column1\",\"column2\"]," + "\"data\":[{\"row\":[\"value1\",\"value2\"],\"meta\":[null,null]}]}],\"notifications\":[{\"code\":\"Neo" + ".ClientNotification.Statement.CartesianProductWarning\",\"severity\":\"WARNING\",\"title\":\"This " + "query builds a cartesian product between disconnected patterns.\",\"description\":\"If a " + "part of a query contains multiple disconnected patterns, this will build a cartesian product" + " between all those parts. This may produce a large amount of data and slow down query " + "processing. While occasionally intended, it may often be possible to reformulate the query " + "that avoids the use of this cross product, perhaps by adding a relationship between the " + "different parts or by using OPTIONAL MATCH\"}],\"errors\":[]}", result);
}
use of org.neo4j.graphdb.Result in project neo4j by neo4j.
the class TransactionHandleTest method shouldSuspendTransactionAndReleaseForOtherRequestsAfterExecutingStatements.
@Test
public void shouldSuspendTransactionAndReleaseForOtherRequestsAfterExecutingStatements() throws Exception {
// given
TransitionalPeriodTransactionMessContainer kernel = mockKernel();
TransitionalTxManagementKernelTransaction transactionContext = kernel.newTransaction(explicit, AUTH_DISABLED, -1);
TransactionRegistry registry = mock(TransactionRegistry.class);
QueryExecutionEngine executionEngine = mock(QueryExecutionEngine.class);
TransactionalContext transactionalContext = prepareKernelWithQuerySession(kernel);
Result executionResult = mock(Result.class);
when(executionEngine.executeQuery("query", map(), transactionalContext)).thenReturn(executionResult);
when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
TransactionHandle handle = getTransactionHandle(kernel, executionEngine, registry);
ExecutionResultSerializer output = mock(ExecutionResultSerializer.class);
// when
handle.execute(statements(new Statement("query", map(), false, (ResultDataContent[]) null)), output, mock(HttpServletRequest.class));
// then
InOrder transactionOrder = inOrder(transactionContext, registry);
transactionOrder.verify(transactionContext).suspendSinceTransactionsAreStillThreadBound();
transactionOrder.verify(registry).release(1337L, handle);
InOrder outputOrder = inOrder(output);
outputOrder.verify(output).transactionCommitUri(uriScheme.txCommitUri(1337));
outputOrder.verify(output).statementResult(executionResult, false, (ResultDataContent[]) null);
outputOrder.verify(output).notifications(anyCollectionOf(Notification.class));
outputOrder.verify(output).transactionStatus(anyLong());
outputOrder.verify(output).errors(argThat(hasNoErrors()));
outputOrder.verify(output).finish();
verifyNoMoreInteractions(output);
}
Aggregations