Search in sources :

Example 16 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class TestDataIterator method testGetChild.

@Test(dataProvider = "orders")
public void testGetChild(IterationOrder order) throws IOException {
    String input = "{ \"a\" : { \"a1\" : \"A1\" }, \"b\" : [ 1.0, 2.0 ] }";
    Object o = jsonToObject(input);
    Builder.create(o, null, order).iterate(new Builder.Callback() {

        public void callback(DataElement e) {
            if (e.getName().equals("a")) {
                assertEquals(e.getChild("a1"), "A1");
                assertTrue(e.getChild("x") == null);
            } else if (e.getName().equals("b")) {
                assertEquals(e.getChild(0), 1.0);
                assertEquals(e.getChild(1), 2.0);
                assertTrue(e.getChild(-1) == null);
                assertTrue(e.getChild(2) == null);
            }
        }
    });
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 17 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class TestDataIterator method traverse.

public List<String> traverse(DataElement element, IterationOrder order, boolean usePath) {
    List<String> traversalList = new ArrayList<String>();
    DataIterator it = Builder.create(element, order).dataIterator();
    DataElement current;
    while ((current = it.next()) != null) {
        StringBuilder s = new StringBuilder();
        if (usePath) {
            s.append("path=").append(current.pathAsString());
        } else {
            s.append("name=").append(current.getName());
        }
        s.append(", class=").append(current.getValue().getClass().getName());
        Object value = current.getValue();
        if ((value instanceof DataComplex) == false) {
            s.append(", value=");
            if (value instanceof ByteString) {
                s.append(((ByteString) value).asAvroString());
            } else {
                s.append(value.toString());
            }
        }
        traversalList.add(s.toString());
    }
    return traversalList;
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) DataComplex(com.linkedin.data.DataComplex) ByteString(com.linkedin.data.ByteString) ArrayList(java.util.ArrayList) ByteString(com.linkedin.data.ByteString)

Example 18 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class TestDataIterator method testNoSchemaWithParentDataElement.

@Test
public void testNoSchemaWithParentDataElement() {
    DataMap root = new DataMap();
    root.put("bytes", ByteString.copyAvroString("abc", false));
    root.put("int", 1);
    root.put("string", "foo");
    root.put("boolean", false);
    root.put("double", 4.0);
    root.put("long", 2L);
    root.put("float", 3.0f);
    DataMap grandParent = new DataMap();
    DataMap parent = new DataMap();
    grandParent.put("child", parent);
    parent.put("child", root);
    DataElement grandParentElement = new SimpleDataElement(grandParent, null);
    DataElement parentElement = new SimpleDataElement(parent, "child", null, grandParentElement);
    DataElement element = new SimpleDataElement(root, "child", null, parentElement);
    /*
    //Possible preExpected output:
    String preExpected =
      "  path=/child/child, class=com.linkedin.data.DataMap\n" +
      "   path=/child/child/bytes, class=com.linkedin.data.ByteString, value=abc\n" +
      "   path=/child/child/int, class=java.lang.Integer, value=1\n" +
      "   path=/child/child/string, class=java.lang.String, value=foo\n" +
      "   path=/child/child/boolean, class=java.lang.Boolean, value=false\n" +
      "   path=/child/child/double, class=java.lang.Double, value=4.0\n" +
      "   path=/child/child/long, class=java.lang.Long, value=2\n" +
      "   path=/child/child/float, class=java.lang.Float, value=3.0\n";

    //Possible postExpected output:
    String postExpected =
      "   path=/child/child/bytes, class=com.linkedin.data.ByteString, value=abc\n" +
      "   path=/child/child/int, class=java.lang.Integer, value=1\n" +
      "   path=/child/child/string, class=java.lang.String, value=foo\n" +
      "   path=/child/child/boolean, class=java.lang.Boolean, value=false\n" +
      "   path=/child/child/double, class=java.lang.Double, value=4.0\n" +
      "   path=/child/child/long, class=java.lang.Long, value=2\n" +
      "   path=/child/child/float, class=java.lang.Float, value=3.0\n" +
      "  path=/child/child, class=com.linkedin.data.DataMap\n";
      */
    final Set<String> commonValues = new HashSet<String>();
    commonValues.add("path=/child/child/bytes, class=com.linkedin.data.ByteString, value=abc");
    commonValues.add("path=/child/child/int, class=java.lang.Integer, value=1");
    commonValues.add("path=/child/child/string, class=java.lang.String, value=foo");
    commonValues.add("path=/child/child/boolean, class=java.lang.Boolean, value=false");
    commonValues.add("path=/child/child/double, class=java.lang.Double, value=4.0");
    commonValues.add("path=/child/child/long, class=java.lang.Long, value=2");
    commonValues.add("path=/child/child/float, class=java.lang.Float, value=3.0");
    List<String> preOrderTraversal = traverseWithDataElement(element, IterationOrder.PRE_ORDER, true);
    Set<String> preOrderTraversalWithoutRoot = new HashSet<String>(preOrderTraversal.subList(1, preOrderTraversal.size()));
    Assert.assertEquals(preOrderTraversal.get(0), "path=/child/child, class=com.linkedin.data.DataMap", "The first node in the pre order traversal should be: com.linkedin.data.DataMap");
    List<String> postOrderTraversal = traverseWithDataElement(element, IterationOrder.POST_ORDER, true);
    Set<String> postOrderTraversalWithoutRoot = new HashSet<String>(postOrderTraversal.subList(0, postOrderTraversal.size() - 1));
    Assert.assertEquals(postOrderTraversal.get(postOrderTraversal.size() - 1), "path=/child/child, class=com.linkedin.data.DataMap", "The last node in the post order traversal should be: com.linkedin.data.DataMap");
    Assert.assertEquals(preOrderTraversalWithoutRoot, postOrderTraversalWithoutRoot, "The traversals without the root should match each other");
    Assert.assertEquals(preOrderTraversalWithoutRoot, commonValues, "The traversals should cover all the leaves");
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) SimpleDataElement(com.linkedin.data.element.SimpleDataElement) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 19 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class RestLiDataValidator method checkDeletesAreValid.

private void checkDeletesAreValid(DataSchema schema, MessageList<Message> messages, ValidationErrorResult result) {
    for (Message message : messages) {
        Object[] path = message.getPath();
        if (path[path.length - 1].toString().equals(PatchConstants.DELETE_COMMAND)) {
            // Replace $delete with the field name to get the full path
            path[path.length - 1] = message.getFormat();
            RecordDataSchema.Field field = DataSchemaUtil.getField(schema, path);
            if (field != null && !field.getOptional() && field.getDefault() == null) {
                result.addMessage(new Message(path, "cannot delete a required field"));
            }
            DataElement fakeElement = hollowElementFromPath(path);
            if (_readOnlyDescendantPredicate.evaluate(fakeElement)) {
                result.addMessage(new Message(path, "cannot delete a ReadOnly field or its descendants"));
            } else if (_createOnlyDescendantPredicate.evaluate(fakeElement)) {
                result.addMessage(new Message(path, "cannot delete a CreateOnly field or its descendants"));
            }
        }
    }
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) Message(com.linkedin.data.message.Message) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 20 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class RestLiDataValidator method hollowElementFromPath.

/**
   * Create a hollow data element in which only getName() and getParent() work correctly.
   * This method is used to test $delete partial update paths against {@link PathMatchesPatternPredicate}.
   *
   * @param path the path from the root to the element, including the name of the element
   * @return a hollow data element
   */
private static DataElement hollowElementFromPath(Object[] path) {
    DataElement root = new SimpleDataElement(null, null);
    DataElement current = root;
    for (Object component : path) {
        DataElement child = new SimpleDataElement(null, component.toString(), null, current);
        current = child;
    }
    return current;
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) SimpleDataElement(com.linkedin.data.element.SimpleDataElement)

Aggregations

DataElement (com.linkedin.data.element.DataElement)20 SimpleDataElement (com.linkedin.data.element.SimpleDataElement)11 ByteString (com.linkedin.data.ByteString)7 Test (org.testng.annotations.Test)7 DataMap (com.linkedin.data.DataMap)5 Message (com.linkedin.data.message.Message)5 DataSchema (com.linkedin.data.schema.DataSchema)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)4 ArrayList (java.util.ArrayList)4 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)3 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)2 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)2 DataComplex (com.linkedin.data.DataComplex)1 TestUtil.asList (com.linkedin.data.TestUtil.asList)1 Predicate (com.linkedin.data.it.Predicate)1 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)1 CoercionMode (com.linkedin.data.schema.validation.CoercionMode)1 RequiredMode (com.linkedin.data.schema.validation.RequiredMode)1