Merge "Remove unused imports"
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XmlStreamUtilsTest.java
index 1a35875eadaa51e8752b8e26b006f4541d4daf22..88e202d5cf7988fb5d7b51784cd772bfe22c6e41 100644 (file)
@@ -8,20 +8,24 @@
 
 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import com.google.common.base.Optional;
 import com.google.common.collect.Maps;
 import java.io.ByteArrayOutputStream;
 import java.net.URI;
 import java.util.AbstractMap;
+import java.util.Collections;
 import java.util.Map;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamWriter;
-import junit.framework.Assert;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
 import org.w3c.dom.Document;
 
 public class XmlStreamUtilsTest {
@@ -51,10 +55,10 @@ public class XmlStreamUtilsTest {
         final String xmlAsString = new String(out.toByteArray());
 
         final Map<String, String> mappedPrefixes = mapPrefixed(randomPrefix.getPrefixes());
-        Assert.assertEquals(2, mappedPrefixes.size());
+        assertEquals(2, mappedPrefixes.size());
         final String randomPrefixValue = mappedPrefixes.get("namespace2");
 
-        final String expectedXmlAsString = "<element xmlns:prefix=\"namespace\" prefix:attr=\"value\" xmlns:" + randomPrefixValue + "=\"namespace2\" " + randomPrefixValue + ":attr=\"value\"></element>";
+        final String expectedXmlAsString = "<element xmlns:a=\"namespace\" a:attr=\"value\" xmlns:" + randomPrefixValue + "=\"namespace2\" " + randomPrefixValue + ":attr=\"value\"></element>";
 
         XMLUnit.setIgnoreAttributeOrder(true);
         final Document control = XMLUnit.buildControlDocument(expectedXmlAsString);
@@ -62,7 +66,35 @@ public class XmlStreamUtilsTest {
         final Diff diff = XMLUnit.compareXML(control, test);
 
         final boolean identical = diff.identical();
-        Assert.assertTrue("Xml differs: " + diff.toString(), identical);
+        assertTrue("Xml differs: " + diff.toString(), identical);
+    }
+
+    @Test
+    public void testEmptyNodeWithAttribute() throws Exception {
+        final ByteArrayOutputStream out = new ByteArrayOutputStream();
+        final XMLStreamWriter writer =  XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
+
+        final Map<QName, String> attrs = Maps.newHashMap();
+        attrs.put(QName.create("namespaceAttr", "2012-12-12", "attr1"), "value");
+        final QName qName = QName.create("urn:opendaylight:controller:rpc:test", "2014-07-28", "cont");
+        final ImmutableCompositeNode dataAttributes = ImmutableCompositeNode.create(qName, attrs, Collections.<Node<?>>emptyList());
+        XmlStreamUtils.create(XmlUtils.DEFAULT_XML_CODEC_PROVIDER).writeDocument(writer, dataAttributes);
+
+        writer.close();
+        out.close();
+
+        final String xmlAsString = new String(out.toByteArray());
+
+        // TODO why resulting xml does not have namespace definition ? If sending xml by e.g. netconf the namespace is there but not here in test
+        final String expectedXmlAsString = "<cont xmlns:a=\"namespaceAttr\" a:attr1=\"value\"></cont>";
+
+        XMLUnit.setIgnoreAttributeOrder(true);
+        final Document control = XMLUnit.buildControlDocument(expectedXmlAsString);
+        final Document test = XMLUnit.buildTestDocument(xmlAsString);
+        final Diff diff = XMLUnit.compareXML(control, test);
+
+        final boolean identical = diff.identical();
+        assertTrue("Xml differs: " + diff.toString(), identical);
     }
 
     private Map<String, String> mapPrefixed(final Iterable<Map.Entry<URI, String>> prefixes) {