use of org.neo4j.server.rest.transactional.error.Neo4jError 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.server.rest.transactional.error.Neo4jError 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.server.rest.transactional.error.Neo4jError in project neo4j by neo4j.
the class ExecutionResultSerializerTest method shouldSerializeResponseWithErrorsOnly.
@Test
public void shouldSerializeResponseWithErrorsOnly() throws Exception {
// given
ByteArrayOutputStream output = new ByteArrayOutputStream();
ExecutionResultSerializer serializer = getSerializerWith(output);
// when
serializer.errors(asList(new Neo4jError(Status.Request.InvalidFormat, new Exception("cause1"))));
serializer.finish();
// then
String result = output.toString(UTF_8.name());
assertEquals("{\"results\":[],\"errors\":[{\"code\":\"Neo.ClientError.Request.InvalidFormat\",\"message\":\"cause1\"}]}", result);
}
use of org.neo4j.server.rest.transactional.error.Neo4jError in project neo4j by neo4j.
the class ExecutionResultSerializerTest method shouldProduceWellFormedJsonEvenIfResultIteratorThrowsExceptionOnHasNext.
@Test
public void shouldProduceWellFormedJsonEvenIfResultIteratorThrowsExceptionOnHasNext() 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).thenThrow(new RuntimeException("Stuff went wrong!"));
when(executionResult.next()).thenReturn(data);
// 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.server.rest.transactional.error.Neo4jError in project neo4j by neo4j.
the class StatementDeserializerTest method shouldRejectMapWithADifferentFieldBeforeStatement.
@Test
public void shouldRejectMapWithADifferentFieldBeforeStatement() throws Exception {
// NOTE: We don't really want this behaviour, but it's a symptom of keeping
// streaming behaviour while moving the statement list into a map.
String json = "{ \"timeout\" : 200, \"statements\" : [ { \"statement\" : \"ignored\", \"parameters\" : {}} ] }";
assertYieldsErrors(json, new Neo4jError(Status.Request.InvalidFormat, new DeserializationException("Unable to deserialize request. Expected first field to be 'statements', but was 'timeout'.")));
}
Aggregations