Search in sources :

Example 6 with RecordMessage

use of org.neo4j.bolt.v3.messaging.response.RecordMessage in project neo4j by neo4j.

the class BoltResponseMessageReader method read.

public void read(BoltResponseMessageWriter messageWriter) throws IOException {
    try {
        unpacker.unpackStructHeader();
        final int signature = unpacker.unpackStructSignature();
        BoltResponseMessage message = BoltResponseMessage.withSignature(signature);
        try {
            switch(message) {
                case SUCCESS:
                    MapValue successMetadata = unpacker.unpackMap();
                    messageWriter.write(new SuccessMessage(successMetadata));
                    break;
                case RECORD:
                    long length = unpacker.unpackListHeader();
                    final AnyValue[] fields = new AnyValue[(int) length];
                    for (int i = 0; i < length; i++) {
                        fields[i] = unpacker.unpack();
                    }
                    messageWriter.write(new RecordMessage(fields));
                    break;
                case IGNORED:
                    messageWriter.write(IgnoredMessage.IGNORED_MESSAGE);
                    break;
                case FAILURE:
                    MapValue failureMetadata = unpacker.unpackMap();
                    String code = failureMetadata.containsKey("code") ? ((StringValue) failureMetadata.get("code")).stringValue() : Status.General.UnknownError.name();
                    AnyValue msgValue = failureMetadata.get("message");
                    String msg = msgValue != NO_VALUE ? ((StringValue) msgValue).stringValue() : "<No message supplied>";
                    messageWriter.write(new FailureMessage(Neo4jError.codeFromString(code), msg));
                    break;
                default:
                    throw new BoltIOException(Status.Request.InvalidFormat, String.format("Message 0x%s is not supported.", Integer.toHexString(signature)));
            }
        } catch (IllegalArgumentException e) {
            throw new BoltIOException(Status.Request.InvalidFormat, String.format("Message 0x%s is not a valid message signature.", Integer.toHexString(signature)));
        }
    } catch (PackStream.PackStreamException e) {
        throw new BoltIOException(Status.Request.InvalidFormat, String.format("Unable to read message type. Error was: %s.", e.getMessage()), e);
    }
}
Also used : MapValue(org.neo4j.values.virtual.MapValue) PackStream(org.neo4j.bolt.packstream.PackStream) SuccessMessage(org.neo4j.bolt.v3.messaging.response.SuccessMessage) AnyValue(org.neo4j.values.AnyValue) FailureMessage(org.neo4j.bolt.v3.messaging.response.FailureMessage) RecordMessage(org.neo4j.bolt.v3.messaging.response.RecordMessage)

Example 7 with RecordMessage

use of org.neo4j.bolt.v3.messaging.response.RecordMessage in project neo4j by neo4j.

the class BoltResponseMessageTest method shouldHandleCommonMessages.

@Test
void shouldHandleCommonMessages() throws Throwable {
    assertSerializes(new RecordMessage(new AnyValue[] { longValue(1L), stringValue("b"), longValue(2L) }));
    assertSerializes(new SuccessMessage(VirtualValues.EMPTY_MAP));
    assertSerializes(new FailureMessage(Status.General.UnknownError, "Err"));
    assertSerializes(IGNORED_MESSAGE);
}
Also used : SuccessMessage(org.neo4j.bolt.v3.messaging.response.SuccessMessage) AnyValue(org.neo4j.values.AnyValue) FailureMessage(org.neo4j.bolt.v3.messaging.response.FailureMessage) RecordMessage(org.neo4j.bolt.v3.messaging.response.RecordMessage) Test(org.junit.jupiter.api.Test)

Example 8 with RecordMessage

use of org.neo4j.bolt.v3.messaging.response.RecordMessage in project neo4j by neo4j.

the class BoltResponseMessageWriterV3Test method shouldNotifyOutputAboutFailedRecordMessage.

@Test
void shouldNotifyOutputAboutFailedRecordMessage() throws Exception {
    PackOutput output = mock(PackOutput.class);
    Neo4jPack.Packer packer = mock(Neo4jPack.Packer.class);
    IOException error = new IOException("Unable to pack 42");
    doThrow(error).when(packer).pack(longValue(42));
    var writer = newWriter(output, packer);
    var e = assertThrows(IOException.class, () -> writer.write(new RecordMessage(new AnyValue[] { stringValue("42"), longValue(42) })));
    assertEquals(error, e);
    InOrder inOrder = inOrder(output, packer);
    inOrder.verify(output).beginMessage();
    inOrder.verify(packer).pack(stringValue("42"));
    inOrder.verify(packer).pack(longValue(42));
    inOrder.verify(output).messageFailed();
}
Also used : InOrder(org.mockito.InOrder) IOException(java.io.IOException) PackOutput(org.neo4j.bolt.packstream.PackOutput) Neo4jPack(org.neo4j.bolt.packstream.Neo4jPack) RecordMessage(org.neo4j.bolt.v3.messaging.response.RecordMessage) Test(org.junit.jupiter.api.Test)

Aggregations

RecordMessage (org.neo4j.bolt.v3.messaging.response.RecordMessage)8 Test (org.junit.jupiter.api.Test)6 AnyValue (org.neo4j.values.AnyValue)5 Neo4jPack (org.neo4j.bolt.packstream.Neo4jPack)4 InOrder (org.mockito.InOrder)3 PackOutput (org.neo4j.bolt.packstream.PackOutput)3 IOException (java.io.IOException)2 FailureMessage (org.neo4j.bolt.v3.messaging.response.FailureMessage)2 SuccessMessage (org.neo4j.bolt.v3.messaging.response.SuccessMessage)2 BoltResponseMessageRecorder (org.neo4j.bolt.messaging.BoltResponseMessageRecorder)1 ResponseMessage (org.neo4j.bolt.messaging.ResponseMessage)1 PackStream (org.neo4j.bolt.packstream.PackStream)1 BoltConnection (org.neo4j.bolt.runtime.BoltConnection)1 BoltResult (org.neo4j.bolt.runtime.BoltResult)1 Value (org.neo4j.values.storable.Value)1 MapValue (org.neo4j.values.virtual.MapValue)1