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));
}
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());
}
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"));
}
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);
}
}
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);
}
}
Aggregations