xml_serializer empty container and leaf validation fix
Change-Id: Ice4cba3800d22ffa0be78bf3322e85b710bb227c
diff --git a/serializers/xml/src/main/java/org/onosproject/yang/serializers/xml/XmlSerializerListener.java b/serializers/xml/src/main/java/org/onosproject/yang/serializers/xml/XmlSerializerListener.java
index b062ac9..bdf4625 100644
--- a/serializers/xml/src/main/java/org/onosproject/yang/serializers/xml/XmlSerializerListener.java
+++ b/serializers/xml/src/main/java/org/onosproject/yang/serializers/xml/XmlSerializerListener.java
@@ -91,11 +91,6 @@
return;
}
- if (nodeType == OBJECT_NODE && element.content() == null ||
- element.content().isEmpty()) {
- nodeType = TEXT_NODE;
- }
-
if (nodeType == OBJECT_NODE) {
if (dnBuilder != null) {
dnBuilder = addDataNode(dnBuilder, element.getName(),
diff --git a/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/XmlSerializerTest.java b/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/XmlSerializerTest.java
index 3c84ef8..7b557ce 100644
--- a/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/XmlSerializerTest.java
+++ b/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/XmlSerializerTest.java
@@ -91,6 +91,34 @@
}
/**
+ * Validates data node in which XML element is of type YANG empty
+ * container and empty leaf inside a container.
+ */
+ @Test
+ public void testEmptyContainer() {
+ String path = "src/test/resources/emptyContainers.xml";
+
+ DefaultCompositeStream external =
+ new DefaultCompositeStream(null, parseInput(path));
+ CompositeData compositeData = xmlSerializer.decode(external, context);
+ DataNode rootNode = validateRootDataNode(compositeData.resourceData());
+ DataNode animal = validateContainerDataNode(rootNode, "animal",
+ "yrt:animal");
+ DataNode c3 = validateContainerDataNode(animal, "c3",
+ "yrt:animal");
+ DataNode c4 = validateContainerDataNode(rootNode, "c4", "yrt:animal");
+ DataNode c2 = validateContainerDataNode(rootNode, "c2",
+ "yrt:animal");
+ validateNullLeafDataNode(c2, "p3", "yrt:animal", null);
+
+ // encode test
+ CompositeStream compositeStream = xmlSerializer.encode(compositeData,
+ context);
+ InputStream inputStream = compositeStream.resourceData();
+ assertThat(convertInputStreamToString(inputStream), is(parseXml(path)));
+ }
+
+ /**
* Validates data node in which XML element is of type YANG leaf.
*/
@Test
@@ -331,6 +359,26 @@
}
/**
+ * Validates an empty leaf data node.
+ *
+ * @param parent data node holding leaf
+ * @param name name of the leaf
+ * @param namespace namespace of the leaf
+ * @param value leaf value
+ */
+ private static void validateNullLeafDataNode(DataNode parent,
+ String name, String namespace,
+ Object value) {
+ Map<NodeKey, DataNode> childNodes = ((InnerNode) parent).childNodes();
+ NodeKey key = NodeKey.builder().schemaId(name, namespace).build();
+ LeafNode dataNode = ((LeafNode) childNodes.get(key));
+ SchemaId schemaId = dataNode.key().schemaId();
+ assertThat(schemaId.name(), is(name));
+ assertThat(schemaId.namespace(), is(namespace));
+ assertThat(dataNode.value(), is(value));
+ }
+
+ /**
* Validates leaf-list data node.
*
* @param parent data node holding leaf-list
diff --git a/serializers/xml/src/test/resources/animal.yang b/serializers/xml/src/test/resources/animal.yang
new file mode 100644
index 0000000..db50494
--- /dev/null
+++ b/serializers/xml/src/test/resources/animal.yang
@@ -0,0 +1,41 @@
+module animal {
+
+ yang-version 1;
+
+ namespace "yrt:animal";
+
+ prefix "foodType";
+
+ organization "ON-LAB";
+
+ description "This module defines for food.";
+
+ revision "2016-06-24" {
+ description "Initial revision.";
+ }
+
+ container animal {
+ container c3 {
+ }
+ leaf p7 {
+ type string;
+ }
+ }
+
+ container c4 {
+ }
+
+ container c2 {
+ leaf p3 {
+ type string;
+ }
+ }
+
+ leaf bool {
+ type boolean;
+ }
+
+ leaf-list boolean {
+ type boolean;
+ }
+}
\ No newline at end of file
diff --git a/serializers/xml/src/test/resources/emptyContainers.xml b/serializers/xml/src/test/resources/emptyContainers.xml
new file mode 100644
index 0000000..d7a789d
--- /dev/null
+++ b/serializers/xml/src/test/resources/emptyContainers.xml
@@ -0,0 +1,7 @@
+<animal xmlns="yrt:animal">
+<c3/>
+</animal>
+<c4 xmlns="yrt:animal"></c4>
+<c2 xmlns="yrt:animal">
+<p3/>
+</c2>