Search in sources :

Example 71 with AnyValue

use of org.neo4j.values.AnyValue in project neo4j by neo4j.

the class Neo4jPackV1Test method shouldBeAbleToPackAndUnpackList.

@Test
void shouldBeAbleToPackAndUnpackList() throws IOException {
    // Given
    PackedOutputArray output = new PackedOutputArray();
    Neo4jPack.Packer packer = neo4jPack.newPacker(output);
    packer.packListHeader(ALICE.labels().length());
    List<String> expected = new ArrayList<>();
    TextArray labels = ALICE.labels();
    for (int i = 0; i < labels.length(); i++) {
        String labelName = labels.stringValue(i);
        packer.pack(labelName);
        expected.add(labelName);
    }
    AnyValue unpacked = unpacked(output.bytes());
    // Then
    assertThat(unpacked).isInstanceOf(ListValue.class);
    ListValue unpackedList = (ListValue) unpacked;
    assertThat(unpackedList).isEqualTo(ValueUtils.asListValue(expected));
}
Also used : ListValue(org.neo4j.values.virtual.ListValue) ArrayList(java.util.ArrayList) AnyValue(org.neo4j.values.AnyValue) TextArray(org.neo4j.values.storable.TextArray) Test(org.junit.jupiter.api.Test)

Example 72 with AnyValue

use of org.neo4j.values.AnyValue in project neo4j by neo4j.

the class Neo4jPackV1Test method shouldBeAbleToPackAndUnpackMap.

@Test
void shouldBeAbleToPackAndUnpackMap() throws IOException {
    // Given
    PackedOutputArray output = new PackedOutputArray();
    Neo4jPack.Packer packer = neo4jPack.newPacker(output);
    packer.packMapHeader(ALICE.properties().size());
    ALICE.properties().foreach((s, value) -> {
        try {
            packer.pack(s);
            packer.pack(value);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
    AnyValue unpacked = unpacked(output.bytes());
    // Then
    assertThat(unpacked).isInstanceOf(MapValue.class);
    MapValue unpackedMap = (MapValue) unpacked;
    assertThat(unpackedMap).isEqualTo(ALICE.properties());
}
Also used : AnyValue(org.neo4j.values.AnyValue) UncheckedIOException(java.io.UncheckedIOException) MapValue(org.neo4j.values.virtual.MapValue) IOException(java.io.IOException) BoltIOException(org.neo4j.bolt.messaging.BoltIOException) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.jupiter.api.Test)

Example 73 with AnyValue

use of org.neo4j.values.AnyValue in project neo4j by neo4j.

the class Neo4jPackV1Test method shouldTreatSingleCharAsSingleCharacterString.

@Test
void shouldTreatSingleCharAsSingleCharacterString() throws IOException {
    // Given
    PackedOutputArray output = new PackedOutputArray();
    Neo4jPack.Packer packer = neo4jPack.newPacker(output);
    packer.pack(charValue('C'));
    AnyValue unpacked = unpacked(output.bytes());
    // Then
    assertThat(unpacked).isInstanceOf(TextValue.class);
    assertThat(unpacked).isEqualTo(stringValue("C"));
}
Also used : AnyValue(org.neo4j.values.AnyValue) Test(org.junit.jupiter.api.Test)

Example 74 with AnyValue

use of org.neo4j.values.AnyValue in project neo4j by neo4j.

the class Neo4jPackV2Test method unpack.

@SuppressWarnings("unchecked")
private static <T extends AnyValue> T unpack(PackedOutputArray output) {
    try {
        Neo4jPackV2 neo4jPack = new Neo4jPackV2();
        PackedInputArray input = new PackedInputArray(output.bytes());
        Neo4jPack.Unpacker unpacker = neo4jPack.newUnpacker(input);
        AnyValue unpack = unpacker.unpack();
        return (T) unpack;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : AnyValue(org.neo4j.values.AnyValue) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 75 with AnyValue

use of org.neo4j.values.AnyValue 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)

Aggregations

AnyValue (org.neo4j.values.AnyValue)95 Test (org.junit.jupiter.api.Test)58 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)19 ListValue (org.neo4j.values.virtual.ListValue)14 CallableUserFunction (org.neo4j.kernel.api.procedure.CallableUserFunction)11 RelationshipValue (org.neo4j.values.virtual.RelationshipValue)11 CallableProcedure (org.neo4j.kernel.api.procedure.CallableProcedure)10 List (java.util.List)9 TextValue (org.neo4j.values.storable.TextValue)9 RawIterator (org.neo4j.collection.RawIterator)8 MapValue (org.neo4j.values.virtual.MapValue)8 Context (org.neo4j.kernel.api.procedure.Context)7 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)7 ArrayList (java.util.ArrayList)6 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 Values.stringValue (org.neo4j.values.storable.Values.stringValue)6 LocalDate (java.time.LocalDate)5 LocalTime (java.time.LocalTime)5 ZonedDateTime (java.time.ZonedDateTime)5 Arrays (java.util.Arrays)5