Search in sources :

Example 1 with PackedOutputArray

use of org.neo4j.bolt.v1.packstream.PackedOutputArray in project neo4j by neo4j.

the class Neo4jPackTest method shouldTreatCharArrayAsListOfStrings.

@Test
public void shouldTreatCharArrayAsListOfStrings() throws IOException {
    // Given
    PackedOutputArray output = new PackedOutputArray();
    Neo4jPack.Packer packer = new Neo4jPack.Packer(output);
    packer.pack(new char[] { 'W', 'H', 'Y' });
    Object unpacked = unpacked(output.bytes());
    // Then
    assertThat(unpacked, instanceOf(List.class));
    assertThat(unpacked, equalTo(asList("W", "H", "Y")));
}
Also used : ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) PackedOutputArray(org.neo4j.bolt.v1.packstream.PackedOutputArray) Test(org.junit.Test)

Example 2 with PackedOutputArray

use of org.neo4j.bolt.v1.packstream.PackedOutputArray in project neo4j by neo4j.

the class Neo4jPackTest method packed.

private byte[] packed(Object object) throws IOException {
    PackedOutputArray output = new PackedOutputArray();
    Neo4jPack.Packer packer = new Neo4jPack.Packer(output);
    packer.pack(object);
    return output.bytes();
}
Also used : PackedOutputArray(org.neo4j.bolt.v1.packstream.PackedOutputArray)

Example 3 with PackedOutputArray

use of org.neo4j.bolt.v1.packstream.PackedOutputArray in project neo4j by neo4j.

the class Neo4jPackTest method shouldFailNicelyWhenPackingAMapWithUnpackableValues.

@SuppressWarnings("unchecked")
@Test
public void shouldFailNicelyWhenPackingAMapWithUnpackableValues() throws IOException {
    // Given
    PackedOutputArray output = new PackedOutputArray();
    Neo4jPack.Packer packer = new Neo4jPack.Packer(output);
    packer.packRawMap(map("unpackable", new Unpackable()));
    Object unpacked = unpacked(output.bytes());
    // Then
    assertThat(unpacked, instanceOf(Map.class));
    Map<String, Object> unpackedMap = (Map<String, Object>) unpacked;
    assertThat(unpackedMap, equalTo(map("unpackable", null)));
    assertTrue(packer.hasErrors());
}
Also used : PackedOutputArray(org.neo4j.bolt.v1.packstream.PackedOutputArray) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with PackedOutputArray

use of org.neo4j.bolt.v1.packstream.PackedOutputArray in project neo4j by neo4j.

the class Neo4jPackTest method shouldBeAbleToPackAndUnpackMapStream.

@SuppressWarnings("unchecked")
@Test
public void shouldBeAbleToPackAndUnpackMapStream() throws IOException {
    // Given
    PackedOutputArray output = new PackedOutputArray();
    Neo4jPack.Packer packer = new Neo4jPack.Packer(output);
    packer.packMapStreamHeader();
    for (Map.Entry<String, Object> entry : ALICE.getAllProperties().entrySet()) {
        packer.pack(entry.getKey());
        packer.pack(entry.getValue());
    }
    packer.packEndOfStream();
    Object unpacked = unpacked(output.bytes());
    // Then
    assertThat(unpacked, instanceOf(Map.class));
    Map<String, Object> unpackedMap = (Map<String, Object>) unpacked;
    assertThat(unpackedMap, equalTo(ALICE.getAllProperties()));
}
Also used : PackedOutputArray(org.neo4j.bolt.v1.packstream.PackedOutputArray) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with PackedOutputArray

use of org.neo4j.bolt.v1.packstream.PackedOutputArray in project neo4j by neo4j.

the class Neo4jPackTest method shouldFailWhenTryingToPackAndUnpackMapStreamContainingNullKeys.

@SuppressWarnings("unchecked")
@Test
public void shouldFailWhenTryingToPackAndUnpackMapStreamContainingNullKeys() throws IOException {
    // Given
    PackedOutputArray output = new PackedOutputArray();
    Neo4jPack.Packer packer = new Neo4jPack.Packer(output);
    packer.packMapStreamHeader();
    HashMap<String, Object> map = new HashMap<>();
    map.put(null, 42L);
    map.put("foo", 1337L);
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        packer.pack(entry.getKey());
        packer.pack(entry.getValue());
    }
    packer.packEndOfStream();
    // When
    PackedInputArray input = new PackedInputArray(output.bytes());
    Neo4jPack.Unpacker unpacker = new Neo4jPack.Unpacker(input);
    unpacker.unpack();
    // Then
    assertThat(unpacker.consumeError().get(), equalTo(Neo4jError.from(Status.Request.Invalid, "Value `null` is not supported as key in maps, must be a non-nullable string.")));
}
Also used : HashMap(java.util.HashMap) PackedInputArray(org.neo4j.bolt.v1.packstream.PackedInputArray) PackedOutputArray(org.neo4j.bolt.v1.packstream.PackedOutputArray) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

PackedOutputArray (org.neo4j.bolt.v1.packstream.PackedOutputArray)9 Test (org.junit.Test)8 HashMap (java.util.HashMap)4 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 List (java.util.List)2 PackedInputArray (org.neo4j.bolt.v1.packstream.PackedInputArray)2 Label (org.neo4j.graphdb.Label)1