Search in sources :

Example 1 with SpecialInt

use of org.janusgraph.graphdb.serializer.SpecialInt in project janusgraph by JanusGraph.

the class JanusGraphTest method testDataTypes.

/**
 * Test the different data types that JanusGraph supports natively and ensure that invalid data types aren't allowed
 */
@Test
public void testDataTypes() {
    clopen(option(CUSTOM_ATTRIBUTE_CLASS, "attribute10"), SpecialInt.class.getCanonicalName(), option(CUSTOM_SERIALIZER_CLASS, "attribute10"), SpecialIntSerializer.class.getCanonicalName());
    PropertyKey num = makeKey("num", SpecialInt.class);
    PropertyKey barr = makeKey("barr", byte[].class);
    PropertyKey booleanValue = makeKey("boolval", Boolean.class);
    PropertyKey birthday = makeKey("birthday", Instant.class);
    PropertyKey location = makeKey("location", Geoshape.class);
    PropertyKey boundary = makeKey("boundary", Geoshape.class);
    PropertyKey precise = makeKey("precise", Double.class);
    PropertyKey any = mgmt.makePropertyKey("any").cardinality(Cardinality.LIST).dataType(Object.class).make();
    try {
        // Not a valid data type - primitive
        makeKey("pint", int.class);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        // Not a valid data type - interface
        makeKey("number", Number.class);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    finishSchema();
    clopen();
    booleanValue = tx.getPropertyKey("boolval");
    num = tx.getPropertyKey("num");
    barr = tx.getPropertyKey("barr");
    birthday = tx.getPropertyKey("birthday");
    location = tx.getPropertyKey("location");
    boundary = tx.getPropertyKey("boundary");
    precise = tx.getPropertyKey("precise");
    any = tx.getPropertyKey("any");
    assertEquals(Boolean.class, booleanValue.dataType());
    assertEquals(byte[].class, barr.dataType());
    assertEquals(Object.class, any.dataType());
    final Instant c = Instant.ofEpochSecond(1429225756);
    final Geoshape point = Geoshape.point(10.0, 10.0);
    final Geoshape shape = Geoshape.box(10.0, 10.0, 20.0, 20.0);
    JanusGraphVertex v = tx.addVertex();
    v.property(n(booleanValue), true);
    v.property(VertexProperty.Cardinality.single, n(birthday), c);
    v.property(VertexProperty.Cardinality.single, n(num), new SpecialInt(10));
    v.property(VertexProperty.Cardinality.single, n(barr), new byte[] { 1, 2, 3, 4 });
    v.property(VertexProperty.Cardinality.single, n(location), point);
    v.property(VertexProperty.Cardinality.single, n(boundary), shape);
    v.property(VertexProperty.Cardinality.single, n(precise), 10.12345);
    v.property(n(any), "Hello");
    v.property(n(any), 10L);
    int[] testArray = { 5, 6, 7 };
    v.property(n(any), testArray);
    // ######## VERIFICATION ##########
    assertTrue(v.<Boolean>value("boolval"));
    assertEquals(10, v.<SpecialInt>value("num").getValue());
    assertEquals(c, v.value("birthday"));
    assertEquals(4, v.<byte[]>value("barr").length);
    assertEquals(point, v.<Geoshape>value("location"));
    assertEquals(shape, v.<Geoshape>value("boundary"));
    assertEquals(10.12345, v.<Double>value("precise"), 0.000001);
    assertCount(3, v.properties("any"));
    for (Object prop : v.query().labels("any").properties()) {
        Object value = ((JanusGraphVertexProperty<?>) prop).value();
        if (value instanceof String)
            assertEquals("Hello", value);
        else if (value instanceof Long)
            assertEquals(10L, value);
        else if (value.getClass().isArray()) {
            assertTrue(Arrays.equals(testArray, (int[]) value));
        } else
            fail();
    }
    clopen();
    v = getV(tx, v);
    // ######## VERIFICATION (copied from above) ##########
    assertTrue(v.<Boolean>value("boolval"));
    assertEquals(10, v.<SpecialInt>value("num").getValue());
    assertEquals(c, v.value("birthday"));
    assertEquals(4, v.<byte[]>value("barr").length);
    assertEquals(point, v.<Geoshape>value("location"));
    assertEquals(shape, v.<Geoshape>value("boundary"));
    assertEquals(10.12345, v.<Double>value("precise"), 0.000001);
    assertCount(3, v.properties("any"));
    for (Object prop : v.query().labels("any").properties()) {
        Object value = ((JanusGraphVertexProperty<?>) prop).value();
        if (value instanceof String)
            assertEquals("Hello", value);
        else if (value instanceof Long)
            assertEquals(10L, value);
        else if (value.getClass().isArray()) {
            assertTrue(Arrays.equals(testArray, (int[]) value));
        } else
            fail();
    }
}
Also used : SpecialIntSerializer(org.janusgraph.graphdb.serializer.SpecialIntSerializer) SpecialInt(org.janusgraph.graphdb.serializer.SpecialInt) Instant(java.time.Instant) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Geoshape(org.janusgraph.core.attribute.Geoshape) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Aggregations

Instant (java.time.Instant)1 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)1 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)1 PropertyKey (org.janusgraph.core.PropertyKey)1 Geoshape (org.janusgraph.core.attribute.Geoshape)1 SpecialInt (org.janusgraph.graphdb.serializer.SpecialInt)1 SpecialIntSerializer (org.janusgraph.graphdb.serializer.SpecialIntSerializer)1 Test (org.junit.Test)1