Search in sources :

Example 36 with DataList

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

the class TestRecordAndUnionTemplate method testInvalidDataOnUnionWithAliases.

@Test
public void testInvalidDataOnUnionWithAliases() throws Exception {
    DataMap data = new DataMap();
    Foo foo = new Foo(data);
    // Test with invalid data types for union field
    List<?> invalidDataTypes = asList(false, "abc", new DataList());
    for (Object invalidDataType : invalidDataTypes) {
        data.put("unionWithAliases", invalidDataType);
        try {
            foo.getUnionWithAliases();
            fail("Should have thrown an exception");
        } catch (TemplateOutputCastException e) {
        // Do nothing
        }
    }
    // Test with invalid data maps for the union field
    List<Object> invalidData = new ArrayList<>();
    invalidData.add(Data.NULL);
    invalidData.add(new DataMap());
    invalidData.add(new DataMap(TestUtil.asMap("int", 1, "invalid", 2)));
    invalidData.add(new DataMap(TestUtil.asMap("invalid", 2)));
    invalidData.add(new DataMap(TestUtil.asMap("string", "something")));
    invalidData.add(new DataMap(TestUtil.asMap("com.linkedin.pegasus.generator.test.Alphabet", "A")));
    for (Object invalid : invalidData) {
        data.put("unionWithAliases", invalid);
        try {
            foo.getUnionWithAliases().memberType();
            fail("Should have thrown an exception");
        } catch (TemplateOutputCastException e) {
        // Do nothing
        }
    }
}
Also used : DataList(com.linkedin.data.DataList) ArrayList(java.util.ArrayList) TestUtil.asReadOnlyDataMap(com.linkedin.data.TestUtil.asReadOnlyDataMap) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 37 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 = 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<>();
    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.memberKeyName(), "int");
    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
    value = 32;
    Bar bar = new Bar();
    bar.setInt(value);
    union2.setBar(bar);
    assertFalse(union2.isNull());
    assertTrue(union2.isBar());
    assertEquals(union2.memberKeyName(), "Bar");
    assertEquals(union2.getBar().getInt(), value);
    assertSame(union2.memberType(), Foo.Union.MEMBER_Bar);
    assertNull(union2.getInt());
    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(union2.memberKeyName(), "Bar");
        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.memberKeyName(), "Bar");
        assertEquals(union2.getBar().getInt(), origValue);
        assertTrue(unionCopy.isEnumType());
        assertEquals(unionCopy.memberKeyName(), "EnumType");
        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);
    // memberKeyName cannot be used with null union
    try {
        exc = null;
        union.memberKeyName();
    } 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) TestUtil.asReadOnlyDataMap(com.linkedin.data.TestUtil.asReadOnlyDataMap) DataMap(com.linkedin.data.DataMap) DataList(com.linkedin.data.DataList) Test(org.testng.annotations.Test)

Example 38 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 = 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 = asList(88, 99L, 77.0f);
    List<?> castTo = 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) TestUtil.asReadOnlyDataMap(com.linkedin.data.TestUtil.asReadOnlyDataMap) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 39 with DataList

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

the class TestMapTemplate method testEnumMap.

@Test
public void testEnumMap() {
    // must be unique
    Map<String, Fruits> input = asMap("apple", Fruits.APPLE, "orange", Fruits.ORANGE, "banana", Fruits.BANANA);
    Map<String, Fruits> adds = asMap("grapes", Fruits.GRAPES, "pineapple", Fruits.PINEAPPLE);
    Map<String, Object> badInput = asMap("boolean", true, "integer", 1, "long", 2L, "float", 3.0f, "double", 4.0, "string", "hello", "bytes", ByteString.empty(), "object", new Object(), "null", null, "array", new StringArray(), "record", new FooRecord());
    Map<String, Object> badOutput = asMap("boolean", true, "integer", 1, "long", 2L, "float", 3.0f, "double", 4.0, "string", "hello", "bytes", ByteString.empty(), "map", new DataMap(), "list", new DataList());
    testMap(EnumMapTemplate.class, EnumMapTemplate.SCHEMA, input, adds);
    testMapBadInput(EnumMapTemplate.class, EnumMapTemplate.SCHEMA, input, badInput, badOutput);
}
Also used : DataList(com.linkedin.data.DataList) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 40 with DataList

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

the class TestRecordAndUnionTemplate method testBytesField.

@Test
public void testBytesField() {
    List<ByteString> good = Arrays.asList(ByteString.copyAvroString("11", false), ByteString.copyAvroString("22", false), ByteString.copyAvroString("33", false));
    Foo foo = new Foo();
    for (ByteString byteString : good) {
        foo.setBytes(byteString);
        assertEquals(foo.getBytes(), byteString);
        assertTrue(foo.hasBytes());
        assertEquals(foo.toString(), "{bytes=" + byteString + '}');
        Exception exc = null;
        try {
            // clone
            Foo fooClone = foo.clone();
            assertEquals(foo, fooClone);
            fooClone.removeBytes();
            assertTrue(foo.hasBytes());
            assertEquals(foo.getBytes(), byteString);
            assertFalse(fooClone.hasBytes());
            // copy
            Foo fooCopy = foo.copy();
            assertEquals(foo, fooCopy);
            assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
            fooCopy.removeBytes();
            assertTrue(foo.hasBytes());
            assertEquals(foo.getBytes(), byteString);
            assertFalse(fooCopy.hasBytes());
        } catch (CloneNotSupportedException e) {
            exc = e;
        }
        assertTrue(exc == null);
        foo.removeBytes();
        assertFalse(foo.hasBytes());
        assertEquals(foo.toString(), "{}");
    }
    List<?> badInput = asList(false, 33, "\u0100", new DataList());
    DataMap map = new DataMap();
    foo = new Foo(map);
    for (Object bad : badInput) {
        map.put("bytes", bad);
        Exception exc = null;
        try {
            foo.getBytes();
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof TemplateOutputCastException);
    }
    List<?> castFrom = asList("88");
    List<?> castTo = asList(ByteString.copyAvroString("88", false));
    for (int i = 0; i < castFrom.size(); ++i) {
        map.put("bytes", castFrom.get(i));
        ByteString result = foo.getBytes();
        assertEquals(result, castTo.get(i));
    }
    // legacy test
    ByteString[] t = { ByteString.copyAvroString("apple", false), ByteString.copyAvroString("orange", false), ByteString.copyAvroString("banana", false) };
    foo = new Foo();
    foo.set1Bytes(t[0]);
    assertEquals(foo.getBytes(), t[0]);
    foo.set2Bytes(t[1]);
    assertEquals(foo.getBytes(), t[1]);
    foo.set2Bytes(t[2], SetMode.DISALLOW_NULL);
    assertEquals(foo.getBytes(), t[2]);
    foo.set2Bytes(null, SetMode.REMOVE_IF_NULL);
    assertFalse(foo.hasBytes());
}
Also used : DataList(com.linkedin.data.DataList) ByteString(com.linkedin.data.ByteString) TestUtil.asReadOnlyDataMap(com.linkedin.data.TestUtil.asReadOnlyDataMap) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

DataList (com.linkedin.data.DataList)202 DataMap (com.linkedin.data.DataMap)151 Test (org.testng.annotations.Test)109 ByteString (com.linkedin.data.ByteString)72 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)34 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)33 ArrayList (java.util.ArrayList)27 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)22 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)15 DataSchema (com.linkedin.data.schema.DataSchema)14 RecordTemplate (com.linkedin.data.template.RecordTemplate)14 HashMap (java.util.HashMap)13 MapDataSchema (com.linkedin.data.schema.MapDataSchema)12 TestUtil.asReadOnlyDataMap (com.linkedin.data.TestUtil.asReadOnlyDataMap)11 Map (java.util.Map)11 List (java.util.List)10 DataProvider (org.testng.annotations.DataProvider)10 DataComplex (com.linkedin.data.DataComplex)9 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)5 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)5