Bug 3899: Milestone: Increase test coverage for Yangtools 24/34424/2
authorJakub Toth <jatoth@cisco.com>
Fri, 5 Feb 2016 16:02:14 +0000 (17:02 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 11 Feb 2016 09:10:46 +0000 (09:10 +0000)
test XmlDocumentUtilsTest

Change-Id: I8d4958b048e1bf392182b409d4998429712781b6
Signed-off-by: Jakub Toth <jatoth@cisco.com>
(cherry picked from commit a3f2b690663731627b1d93a41cb447d93a60ba5f)

yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtilsTest.java
yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/xml-doc-test.yang [moved from yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/rpc-test.yang with 89% similarity]
yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/xml-doc-test2.yang [new file with mode: 0644]

index 4145ea8d79201af817aa385f65efd9fc25f34839..99a32465a19f7a8eb64f58f2526d6bb3f96bd50a 100644 (file)
@@ -9,24 +9,50 @@
 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
 
 import com.google.common.base.Charsets;
+import com.google.common.base.Optional;
 import com.google.common.collect.Lists;
-import com.google.common.io.ByteSource;
 import java.io.ByteArrayInputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import org.junit.Assert;
 import org.junit.Before;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.ModifyAction;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.RetestUtils;
+import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
 
+@RunWith(MockitoJUnitRunner.class)
 public class XmlDocumentUtilsTest {
-
+    private static final String NS = "urn:opendaylight:controller:xml:doc:test";
+    private static final String NS2 = "urn:opendaylight:controller:xml:doc:test2";
+    private static final String NS3 = "urn:opendaylight:controller:xml:doc:test3";
+    private static final String REVISION = "2014-07-28";
     private static final DocumentBuilderFactory BUILDERFACTORY;
 
     static {
@@ -38,31 +64,23 @@ public class XmlDocumentUtilsTest {
         BUILDERFACTORY = factory;
     }
 
-    public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:rpc:test\">\n" +
+    private static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:xml:doc:test\">\n"
+            +
             "<a>value</a>\n" +
-            "<ref xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">"+
+ "<ref xmlns:ltha=\"urn:opendaylight:controller:xml:doc:test\">"
+            +
             "/ltha:cont/ltha:l[  ltha:id='id/foo/bar'  ]"+
             "</ref>\n" +
             "</input>";
 
-    public static final String RPC_REPLY = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-1\">\n" +
-            " <ok/>\n" +
-            "</rpc-reply>";
-
     private SchemaContext schema;
-    private RpcDefinition testRpc;
 
     @Before
     public void setUp() throws Exception {
-        final ByteSource byteSource = new ByteSource() {
-            @Override
-            public InputStream openStream() throws IOException {
-                return XmlDocumentUtilsTest.this.getClass().getResourceAsStream("rpc-test.yang");
-            }
-        };
-        schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
-        final Module rpcTestModule = schema.getModules().iterator().next();
-        testRpc = rpcTestModule.getRpcs().iterator().next();
+        final File rpcTestYang1 = new File(getClass().getResource("xml-doc-test.yang").toURI());
+        final File rpcTestYang2 = new File(getClass().getResource("xml-doc-test2.yang").toURI());
+
+        this.schema = RetestUtils.parseYangSources(rpcTestYang1, rpcTestYang2);
     }
 
     public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
@@ -81,4 +99,124 @@ public class XmlDocumentUtilsTest {
         doc.getDocumentElement().normalize();
         return doc;
     }
+
+    @Test
+    public void xmlDocCreateElementForContWithoutAttrTest() throws Exception {
+        final Document doc = readXmlToDocument(XML_CONTENT);
+        final YangInstanceIdentifier.NodeIdentifier container = new YangInstanceIdentifier.NodeIdentifier(
+                QName.create(XmlDocumentUtilsTest.NS, XmlDocumentUtilsTest.REVISION, "cont"));
+
+        final NormalizedNode<?, ?> data = ImmutableNodes.fromInstanceId(this.schema,
+                YangInstanceIdentifier.create(container));
+        Assert.assertNotNull(data);
+
+        final Element element = XmlDocumentUtils.createElementFor(doc, data);
+        Assert.assertNotNull(element);
+        Assert.assertEquals(element.getTagName(), "cont");
+    }
+
+    @Test
+    public void xmlDocCreateElementForContiWithAttrTest() throws Exception {
+        final Document doc = readXmlToDocument(XML_CONTENT);
+        final YangInstanceIdentifier.NodeIdentifier container = new YangInstanceIdentifier.NodeIdentifier(
+                QName.create(XmlDocumentUtilsTest.NS, XmlDocumentUtilsTest.REVISION, "cont"));
+
+        final Map<QName, String> attributes = new HashMap<>();
+        attributes.put(QName.create(XmlDocumentUtilsTest.NS, XmlDocumentUtilsTest.REVISION, "l"), "list");
+
+        final ContainerNode node = Builders.containerBuilder().withNodeIdentifier(container).withAttributes(attributes)
+                .build();
+        final NormalizedNode<?, ?> data = node;
+        Assert.assertNotNull(data);
+
+        final Element element = XmlDocumentUtils.createElementFor(doc, data);
+        Assert.assertNotNull(element);
+        Assert.assertEquals("cont", element.getTagName());
+        Assert.assertEquals("list", element.getAttribute("l"));
+    }
+
+    @Test
+    public void xmlDocgetModifyOperationWithAttributesTest() throws Exception {
+        final Document doc = readXmlToDocument(XML_CONTENT);
+        final YangInstanceIdentifier.NodeIdentifier container = new YangInstanceIdentifier.NodeIdentifier(
+                QName.create(XmlDocumentUtilsTest.NS, XmlDocumentUtilsTest.REVISION, "cont"));
+
+        final Map<QName, String> attributes = new HashMap<>();
+        attributes.put(QName.create(XmlDocumentUtilsTest.NS, XmlDocumentUtilsTest.REVISION, "l"), "list");
+
+        final ContainerNode node = Builders.containerBuilder().withNodeIdentifier(container).withAttributes(attributes)
+                .build();
+        final NormalizedNode<?, ?> data = node;
+        Assert.assertNotNull(data);
+
+        final Element element = XmlDocumentUtils.createElementFor(doc, data);
+        final Optional<ModifyAction> modifyOperationFromAttributes = XmlDocumentUtils
+                .getModifyOperationFromAttributes(element);
+        Assert.assertFalse(modifyOperationFromAttributes.isPresent());
+    }
+
+    @Test
+    public void xmlDocgetModifyOperationWithoutAttributesTest() {
+        final Element element = Mockito.mock(Element.class);
+        Mockito.when(element.getAttributeNS(Mockito.anyString(), Mockito.anyString())).thenReturn("hello");
+
+        final Optional<ModifyAction> modifyOperationFromAttributes = XmlDocumentUtils
+                .getModifyOperationFromAttributes(element);
+        Assert.assertFalse(modifyOperationFromAttributes.isPresent());
+    }
+
+    @Test
+    public void xmlDocFindFirstSchemaContTest() {
+        final QName ID = QName.create(XmlDocumentUtilsTest.NS, XmlDocumentUtilsTest.REVISION, "cont");
+        final Optional<DataSchemaNode> findFirstSchema = XmlDocumentUtils.findFirstSchema(ID,
+                this.schema.getChildNodes());
+        Assert.assertNotNull(findFirstSchema);
+        Assert.assertEquals(ID, findFirstSchema.get().getQName());
+    }
+
+    @Test
+    public void xmlDocFindFirstSchemaCont2Test() {
+        final QName ID = QName.create(XmlDocumentUtilsTest.NS2, XmlDocumentUtilsTest.REVISION, "cont2");
+        final Optional<DataSchemaNode> findFirstSchema = XmlDocumentUtils.findFirstSchema(ID,
+                this.schema.getChildNodes());
+        final DataSchemaNode dataSchemaNode = findFirstSchema.get();
+        Assert.assertNotNull(findFirstSchema);
+        Assert.assertEquals(ID, dataSchemaNode.getQName());
+    }
+
+    @Test
+    public void xmlDocFindFirstSchemaNullParamsTest() {
+        Optional<DataSchemaNode> findFirstSchema = XmlDocumentUtils.findFirstSchema(null,
+                this.schema.getChildNodes());
+        Assert.assertFalse(findFirstSchema.isPresent());
+
+        final QName ID = QName.create(XmlDocumentUtilsTest.NS2, XmlDocumentUtilsTest.REVISION, "cont2");
+        findFirstSchema = XmlDocumentUtils.findFirstSchema(ID, null);
+        Assert.assertFalse(findFirstSchema.isPresent());
+    }
+
+    @Test
+    public void xmlDocFindFirstSchemaContChoicTest() {
+        final QName ID = QName.create(XmlDocumentUtilsTest.NS3, XmlDocumentUtilsTest.REVISION, "cont");
+        final DataSchemaNode sch = Mockito.mock(ChoiceSchemaNode.class);
+        final Set<ChoiceCaseNode> setCases = new HashSet<>();
+        final ChoiceCaseNode choice = Mockito.mock(ChoiceCaseNode.class);
+        setCases.add(choice);
+        Mockito.when(((ChoiceSchemaNode) sch).getCases()).thenReturn(setCases);
+        Mockito.when(sch.getQName()).thenReturn(QName.create("badNamespace", "badLocalName"));
+        final Iterable<DataSchemaNode> colls = Lists.newArrayList(sch);
+        final Optional<DataSchemaNode> findFirstSchema = XmlDocumentUtils.findFirstSchema(ID, colls);
+        Assert.assertNotNull(findFirstSchema);
+    }
+
+    @Test
+    public void codecProviderTest() {
+        final XmlCodecProvider provider = XmlDocumentUtils.defaultValueCodecProvider();
+        Assert.assertNotNull(provider);
+        Assert.assertEquals(XmlUtils.DEFAULT_XML_CODEC_PROVIDER, provider);
+
+        final TypeDefinition<?> baseType = Mockito.mock(BinaryTypeDefinition.class);
+        final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = provider.codecFor(baseType);
+        Assert.assertNotNull(codec);
+    }
 }
similarity index 89%
rename from yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/rpc-test.yang
rename to yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/xml-doc-test.yang
index 94596f3152fd8b94d1443ccdf90a3d5bc80b17a5..8efbea4e862c07f8743ab6460f8d539531de2fd0 100644 (file)
@@ -5,10 +5,10 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-module rpc-test {
+module xml-doc-test {
     yang-version 1;
-    namespace "urn:opendaylight:controller:rpc:test";
-    prefix "rpct";
+    namespace "urn:opendaylight:controller:xml:doc:test";
+    prefix "xmldt";
 
     revision 2014-07-28 {
        description "Initial test";
diff --git a/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/xml-doc-test2.yang b/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/xml-doc-test2.yang
new file mode 100644 (file)
index 0000000..88b8f15
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+module xml-doc-test-2 {
+    yang-version 1;
+    namespace "urn:opendaylight:controller:xml:doc:test2";
+    prefix "xmldt";
+
+    revision 2014-07-28 {
+       description "Initial test";
+    }
+
+    container cont2 {
+
+        list l {
+            key "id";
+
+            leaf id {
+                type string;
+            }
+        }
+    }
+}