Search in sources :

Example 66 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestDataObjectToObjectCache method testValueOverwrite.

@Test
public void testValueOverwrite() {
    DataObjectToObjectCache<DataTemplate<?>> testCache = new DataObjectToObjectCache<DataTemplate<?>>();
    DataMap mapKey = new DataMap();
    DataList listKey = new DataList();
    Object objKey = new Object();
    testCache.put(mapKey, new Bar());
    testCache.put(listKey, new Bar());
    testCache.put(objKey, new Bar());
    Bar mapBar = new Bar();
    Bar listBar = new Bar();
    Bar objBar = new Bar();
    testCache.put(mapKey, mapBar);
    testCache.put(listKey, listBar);
    testCache.put(objKey, objBar);
    Assert.assertSame(testCache.get(mapKey), mapBar);
    Assert.assertSame(testCache.get(listKey), listBar);
    Assert.assertSame(testCache.get(objKey), objBar);
}
Also used : DataList(com.linkedin.data.DataList) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 67 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestJacksonDataTemplateCodec method testArray.

@Test
public void testArray() throws IOException {
    Object[][] inputs = { { // empty record array
    asList(), "[]", "[]" }, { // non-empty record array
    asList(new DataMap(asMap("int", 1, "long", 12L, "float", 3.0f, "double", 2.0, "long1", 12L, "double1", 3.0))), "[{\"long1\":12,\"int\":1,\"long\":12,\"double\":2.0,\"double1\":3.0,\"float\":3.0}]", "[{\"int\":1,\"long\":12,\"float\":3.0,\"double\":2.0,\"double1\":3.0,\"long1\":12}]" } };
    for (Object[] row : inputs) {
        @SuppressWarnings("unchecked") DataList list = new DataList((List<Object>) row[0]);
        FooArray fooArray = new FooArray(list);
        String noOrder = templateToString(fooArray, false);
        String order = templateToString(fooArray, true);
        //We have to compare reconstructed DataLists because the key/value pairs inside of the map which is inside of
        //the list are in a non-deterministic order
        DataList expectedNoOrderList = stringToDataList(noOrder);
        DataList actualNoOrderList = stringToDataList((String) row[1]);
        assertEquals(expectedNoOrderList, actualNoOrderList);
        assertEquals(order, row[2]);
    }
}
Also used : DataList(com.linkedin.data.DataList) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 68 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestRecordAndUnionTemplate method testBooleanField.

@Test
public void testBooleanField() {
    List<Boolean> good = Arrays.asList(Boolean.FALSE, Boolean.TRUE);
    Foo foo = new Foo();
    for (Boolean aBoolean : good) {
        // Object
        foo.setBoolean(aBoolean, SetMode.DISALLOW_NULL);
        assertEquals(foo.isBoolean(), aBoolean);
        assertTrue(foo.hasBoolean());
        assertEquals(foo.toString(), "{boolean=" + aBoolean + '}');
        Exception exc = null;
        try {
            // clone
            Foo fooClone = foo.clone();
            assertEquals(foo, fooClone);
            fooClone.removeBoolean();
            assertTrue(foo.hasBoolean());
            assertEquals(foo.isBoolean(), aBoolean);
            assertFalse(fooClone.hasBoolean());
            // copy
            Foo fooCopy = foo.copy();
            assertEquals(foo, fooCopy);
            assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
            fooCopy.removeBoolean();
            assertTrue(foo.hasBoolean());
            assertEquals(foo.isBoolean(), aBoolean);
            assertFalse(fooCopy.hasBoolean());
        } catch (CloneNotSupportedException e) {
            exc = e;
        }
        assertTrue(exc == null);
        foo.removeBoolean();
        assertFalse(foo.hasBoolean());
        assertEquals(foo.toString(), "{}");
        // primitive
        foo.setBoolean(aBoolean.booleanValue());
        assertEquals(foo.isBoolean(), aBoolean);
        assertTrue(foo.hasBoolean());
        assertEquals(foo.toString(), "{boolean=" + aBoolean + '}');
        exc = null;
        try {
            // clone
            Foo fooClone = foo.clone();
            assertEquals(foo, fooClone);
            fooClone.removeBoolean();
            assertTrue(foo.hasBoolean());
            assertEquals(foo.isBoolean(), aBoolean);
            assertFalse(fooClone.hasBoolean());
            // copy
            Foo fooCopy = foo.copy();
            assertEquals(foo, fooCopy);
            assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
            fooCopy.removeBoolean();
            assertTrue(foo.hasBoolean());
            assertEquals(foo.isBoolean(), aBoolean);
            assertFalse(fooCopy.hasBoolean());
        } catch (CloneNotSupportedException e) {
            exc = e;
        }
        assertTrue(exc == null);
        foo.removeBoolean();
        assertFalse(foo.hasBoolean());
        assertEquals(foo.toString(), "{}");
    }
    List<?> badInput = TestUtil.asList(3, "abc", new DataList());
    DataMap map = new DataMap();
    foo = new Foo(map);
    for (Object bad : badInput) {
        map.put("boolean", bad);
        Exception exc = null;
        try {
            foo.isBoolean();
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof TemplateOutputCastException);
    }
    // legacy test
    Boolean[] t = { true, false, true };
    foo = new Foo();
    foo.set1Boolean(t[0].booleanValue());
    assertEquals(foo.isBoolean(), t[0]);
    foo.set2Boolean(t[1].booleanValue());
    assertEquals(foo.isBoolean(), t[1]);
    foo.set2Boolean(t[2], SetMode.DISALLOW_NULL);
    assertEquals(foo.isBoolean(), t[2]);
    foo.set2Boolean(null, SetMode.REMOVE_IF_NULL);
    assertFalse(foo.hasBoolean());
}
Also used : DataList(com.linkedin.data.DataList) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 69 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestRecordAndUnionTemplate method testDoubleField.

@Test
public void testDoubleField() {
    List<Double> good = Arrays.asList(11.0, 22.0, 33.0);
    Foo foo = new Foo();
    for (Double aDouble : good) {
        foo.setDouble(aDouble.doubleValue());
        assertEquals(foo.getDouble(), aDouble);
        assertTrue(foo.hasDouble());
        assertEquals(foo.toString(), "{double=" + aDouble + '}');
        Exception exc = null;
        try {
            // clone
            Foo fooClone = foo.clone();
            assertEquals(foo, fooClone);
            fooClone.removeDouble();
            assertTrue(foo.hasDouble());
            assertEquals(foo.getDouble(), aDouble);
            assertFalse(fooClone.hasDouble());
            // copy
            Foo fooCopy = foo.copy();
            assertEquals(foo, fooCopy);
            assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
            fooCopy.removeDouble();
            assertTrue(foo.hasDouble());
            assertEquals(foo.getDouble(), aDouble);
            assertFalse(fooCopy.hasDouble());
        } catch (CloneNotSupportedException e) {
            exc = e;
        }
        assertTrue(exc == null);
        foo.removeDouble();
        assertFalse(foo.hasDouble());
        assertEquals(foo.toString(), "{}");
    }
    List<?> badInput = TestUtil.asList(false, "abc", new DataList());
    DataMap map = new DataMap();
    foo = new Foo(map);
    for (Object bad : badInput) {
        map.put("double", bad);
        Exception exc = null;
        try {
            foo.getDouble();
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof TemplateOutputCastException);
    }
    List<?> castFrom = TestUtil.asList(88, 99L, 77.0f);
    List<?> castTo = TestUtil.asList(88.0, 99.0, 77.0);
    for (int i = 0; i < castFrom.size(); ++i) {
        map.put("double", castFrom.get(i));
        Double result = foo.getDouble();
        assertEquals(result, castTo.get(i));
    }
    // legacy test
    Double[] t = { 77.0, 88.0, 99.0 };
    foo = new Foo();
    foo.set1Double(t[0].doubleValue());
    assertEquals(foo.getDouble(), t[0]);
    foo.set2Double(t[1].doubleValue());
    assertEquals(foo.getDouble(), t[1]);
    foo.set2Double(t[2], SetMode.DISALLOW_NULL);
    assertEquals(foo.getDouble(), t[2]);
    foo.set2Double(null, SetMode.REMOVE_IF_NULL);
    assertFalse(foo.hasDouble());
}
Also used : DataList(com.linkedin.data.DataList) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 70 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestRecordAndUnionTemplate method testUnionField.

@Test
public void testUnionField() throws CloneNotSupportedException {
    List<?> badOutput = TestUtil.asList(false, "abc", new DataList());
    DataMap map = new DataMap();
    Foo foo = new Foo(map);
    for (Object bad : badOutput) {
        map.put("union", bad);
        Exception exc = null;
        try {
            foo.getUnion();
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof TemplateOutputCastException);
    }
    // test memberType
    List<Object> badOutputMap = new ArrayList<Object>();
    badOutputMap.add(Data.NULL);
    badOutputMap.add(new DataMap());
    badOutputMap.add(new DataMap(TestUtil.asMap("int", 1, "invalid", 2)));
    badOutputMap.add(new DataMap(TestUtil.asMap("invalid", 2)));
    for (Object bad : badOutputMap) {
        map.put("union", bad);
        Exception exc = null;
        try {
            foo.getUnion().memberType();
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof TemplateOutputCastException);
    }
    // test union accessing underlying map
    DataMap unionMap = new DataMap();
    Integer value = 4;
    unionMap.put("int", value);
    map.put("union", unionMap);
    Foo.Union union2 = foo.getUnion();
    assertFalse(union2.isNull());
    assertTrue(union2.isInt());
    assertEquals(union2.getInt(), value);
    assertSame(union2.memberType(), Foo.Union.MEMBER_int);
    assertNull(union2.getBar());
    int lastHashCode = union2.hashCode();
    Foo.Union lastUnion = union2.clone();
    // test union set and get wrapped
    unionMap.clear();
    value = 32;
    Bar bar = new Bar();
    bar.setInt(value.intValue());
    union2.setBar(bar);
    assertFalse(union2.isNull());
    assertTrue(union2.isBar());
    assertEquals(union2.getBar().getInt(), value);
    assertSame(union2.memberType(), Foo.Union.MEMBER_Bar);
    assertNull(union2.getInt());
    assertTrue(union2.equals(union2));
    assertNotNull(union2);
    assertFalse(union2.equals(new Object()));
    int hashCode = union2.hashCode();
    assertFalse(hashCode == lastHashCode);
    lastHashCode = hashCode;
    assertFalse(union2.equals(lastUnion));
    lastUnion = union2.clone();
    // test union clone with wrapped member
    Exception exc;
    try {
        exc = null;
        Foo.Union unionClone = union2.clone();
        assertFalse(unionClone.isNull());
        assertFalse(unionClone.isInt());
        assertTrue(unionClone.isBar());
        assertEquals(unionClone.getBar().getInt(), value);
        assertSame(unionClone.getBar(), union2.getBar());
        assertEquals(unionClone.getBar(), union2.getBar());
        assertEquals(unionClone, union2);
    } catch (CloneNotSupportedException e) {
        exc = e;
    }
    assertTrue(exc == null);
    // test union copy with wrapped member
    try {
        Integer origValue = union2.getBar().getInt();
        Foo.Union unionCopy = union2.copy();
        assertEquals(union2, unionCopy);
        Integer newValue = origValue + 1;
        unionCopy.getBar().setInt(newValue);
        assertTrue(union2.isBar());
        assertTrue(unionCopy.isBar());
        assertEquals(unionCopy.getBar().getInt(), newValue);
        assertEquals(union2.getBar().getInt(), origValue);
        assertFalse(union2.equals(unionCopy));
        assertFalse(union2.getBar().equals(unionCopy.getBar()));
        unionCopy = union2.copy();
        unionCopy.setEnumType(EnumType.APPLE);
        assertTrue(union2.isBar());
        assertEquals(union2.getBar().getInt(), origValue);
        assertTrue(unionCopy.isEnumType());
        assertSame(unionCopy.getEnumType(), EnumType.APPLE);
    } catch (CloneNotSupportedException e) {
        exc = e;
    }
    assertTrue(exc == null);
    // test set bad wrapped
    try {
        exc = null;
        union2.setBar(new Bar() {
        });
    } catch (Exception e) {
        exc = e;
    }
    assertTrue(exc != null);
    assertTrue(exc instanceof ClassCastException);
    // test union set and get direct
    value = 127;
    union2.setInt(127);
    assertFalse(union2.isNull());
    assertTrue(union2.isInt());
    assertEquals(union2.getInt(), value);
    assertSame(union2.memberType(), Foo.Union.MEMBER_int);
    assertEquals(union2.toString(), "{int=127}");
    assertNull(union2.getBar());
    assertTrue(union2.equals(union2));
    assertNotNull(union2);
    assertFalse(union2.equals(new Object()));
    hashCode = union2.hashCode();
    assertFalse(hashCode == lastHashCode);
    assertFalse(union2.equals(lastUnion));
    union2.clone();
    // test union clone with direct member
    try {
        exc = null;
        Foo.Union unionClone = union2.clone();
        assertFalse(unionClone.isNull());
        assertTrue(unionClone.isInt());
        assertFalse(unionClone.isBar());
        assertEquals(unionClone.getInt(), value);
        assertEquals(unionClone.getInt(), union2.getInt());
        assertSame(unionClone.getInt(), union2.getInt());
        assertEquals(unionClone, union2);
        Integer newValue = 256;
        unionClone.setInt(newValue.intValue());
        assertEquals(unionClone.getInt(), newValue);
        assertEquals(union2.getInt(), value);
    } catch (CloneNotSupportedException e) {
        exc = e;
    }
    assertTrue(exc == null);
    // test union copy with direct member
    try {
        exc = null;
        Foo.Union unionCopy = union2.copy();
        assertEquals(union2, unionCopy);
        assertTrue(union2.isInt());
        Integer origValue = union2.getInt();
        Integer newValue = origValue + 1;
        unionCopy.setInt(newValue);
        assertTrue(unionCopy.isInt());
        assertEquals(unionCopy.getInt(), newValue);
        assertEquals(union2.getInt(), origValue);
        assertFalse(union2.equals(unionCopy));
        // change type
        unionCopy = union2.copy();
        unionCopy.setEnumType(EnumType.APPLE);
        assertTrue(unionCopy.isEnumType());
        assertSame(unionCopy.getEnumType(), EnumType.APPLE);
        assertTrue(union2.isInt());
        assertEquals(union2.getInt(), origValue);
    } catch (CloneNotSupportedException e) {
        exc = e;
    }
    assertTrue(exc == null);
    // test null union
    foo.setUnion(new Foo.Union());
    Foo.Union union = foo.getUnion();
    assertTrue(union.isNull());
    assertFalse(union.isInt());
    assertFalse(union.isBar());
    assertEquals(union.toString(), Data.NULL.toString());
    assertSame(union.data(), Data.NULL);
    try {
        // clone
        exc = null;
        Foo.Union unionClone = union.clone();
        assertTrue(unionClone.isNull());
        assertFalse(unionClone.isInt());
        assertFalse(unionClone.isBar());
        assertEquals(unionClone.toString(), Data.NULL.toString());
        assertSame(unionClone.data(), Data.NULL);
        // copy
        Foo.Union unionCopy = union.copy();
        assertTrue(unionCopy.isNull());
        assertFalse(unionCopy.isInt());
        assertFalse(unionCopy.isBar());
        assertEquals(unionCopy.toString(), Data.NULL.toString());
        assertSame(unionCopy.data(), Data.NULL);
    } catch (CloneNotSupportedException e) {
        exc = e;
    }
    assertTrue(exc == null);
    // test null unions throw exception on get and set methods.
    try {
        exc = null;
        union.getInt();
    } catch (Exception e) {
        exc = e;
    }
    assertTrue(exc != null);
    assertTrue(exc instanceof NullUnionUnsupportedOperationException);
    try {
        exc = null;
        union.setInt(4);
    } catch (Exception e) {
        exc = e;
    }
    assertTrue(exc != null);
    assertTrue(exc instanceof NullUnionUnsupportedOperationException);
    try {
        exc = null;
        union.getBar();
    } catch (Exception e) {
        exc = e;
    }
    assertTrue(exc != null);
    assertTrue(exc instanceof NullUnionUnsupportedOperationException);
    try {
        exc = null;
        union.setBar(new Bar());
    } catch (Exception e) {
        exc = e;
    }
    assertTrue(exc != null);
    assertTrue(exc instanceof NullUnionUnsupportedOperationException);
    // test legacy
    union = new Foo.Union(new DataMap());
    Integer i = 43;
    // legacy set
    union.set2Int(i.intValue());
    assertTrue(union.isInt());
    assertEquals(union.getInt(), i);
    assertFalse(union.isEnumType());
    assertNull(union.getEnumType());
    // legacy set
    union.set2EnumType(EnumType.ORANGE);
    assertTrue(union.isEnumType());
    assertSame(union.getEnumType(), EnumType.ORANGE);
    assertFalse(union.isInt());
    assertNull(union.getInt());
    // legacy set
    union.set2EnumType(EnumType.APPLE);
    assertTrue(union.isEnumType());
    assertSame(union.getEnumType(), EnumType.APPLE);
    assertFalse(union.isInt());
    assertNull(union.getInt());
    i = 66;
    // legacy set
    union.set2Int(i.intValue());
    assertTrue(union.isInt());
    assertEquals(union.getInt(), i);
    assertFalse(union.isEnumType());
    assertNull(union.getEnumType());
}
Also used : ArrayList(java.util.ArrayList) DataMap(com.linkedin.data.DataMap) DataList(com.linkedin.data.DataList) Test(org.testng.annotations.Test)

Aggregations

DataList (com.linkedin.data.DataList)160 DataMap (com.linkedin.data.DataMap)126 Test (org.testng.annotations.Test)102 ByteString (com.linkedin.data.ByteString)64 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)34 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)33 ArrayList (java.util.ArrayList)22 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)17 DataSchema (com.linkedin.data.schema.DataSchema)10 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)10 HashMap (java.util.HashMap)10 MapDataSchema (com.linkedin.data.schema.MapDataSchema)9 RecordTemplate (com.linkedin.data.template.RecordTemplate)9 Map (java.util.Map)8 DataProvider (org.testng.annotations.DataProvider)8 List (java.util.List)7 PathSpec (com.linkedin.data.schema.PathSpec)6 PatchTree (com.linkedin.data.transform.patch.request.PatchTree)5 DataComplex (com.linkedin.data.DataComplex)4 CustomPoint (com.linkedin.data.template.TestCustom.CustomPoint)4