Search in sources :

Example 1 with Neo4jError

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, "***"));
}
Also used : Neo4jError(org.neo4j.server.rest.transactional.error.Neo4jError) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Result(org.neo4j.graphdb.Result) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

Example 2 with Neo4jError

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);
}
Also used : Neo4jError(org.neo4j.server.rest.transactional.error.Neo4jError) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonParseException(org.neo4j.server.rest.domain.JsonParseException) IOException(java.io.IOException) Result(org.neo4j.graphdb.Result) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

Example 3 with Neo4jError

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);
}
Also used : Neo4jError(org.neo4j.server.rest.transactional.error.Neo4jError) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonParseException(org.neo4j.server.rest.domain.JsonParseException) IOException(java.io.IOException) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

Example 4 with Neo4jError

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, "***"));
}
Also used : Neo4jError(org.neo4j.server.rest.transactional.error.Neo4jError) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Result(org.neo4j.graphdb.Result) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

Example 5 with Neo4jError

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'.")));
}
Also used : Neo4jError(org.neo4j.server.rest.transactional.error.Neo4jError) Test(org.junit.Test)

Aggregations

Neo4jError (org.neo4j.server.rest.transactional.error.Neo4jError)14 IOException (java.io.IOException)8 Test (org.junit.Test)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Neo4jJsonCodecTest (org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)6 Result (org.neo4j.graphdb.Result)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)4 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)4 JsonToken (org.codehaus.jackson.JsonToken)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 JsonParseException (org.codehaus.jackson.JsonParseException)1 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)1 CypherException (org.neo4j.cypher.CypherException)1 InvalidSemanticsException (org.neo4j.cypher.InvalidSemanticsException)1 AuthorizationViolationException (org.neo4j.graphdb.security.AuthorizationViolationException)1