From: Jakub Toth Date: Fri, 5 Feb 2016 16:02:14 +0000 (+0100) Subject: Bug 3899: Milestone: Increase test coverage for Yangtools X-Git-Tag: release/beryllium~2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=yangtools.git;a=commitdiff_plain;h=55ce5d9463add9c9c6d8ecde62f4d323d07bdcc1 Bug 3899: Milestone: Increase test coverage for Yangtools test XmlDocumentUtilsTest Change-Id: I8d4958b048e1bf392182b409d4998429712781b6 Signed-off-by: Jakub Toth (cherry picked from commit a3f2b690663731627b1d93a41cb447d93a60ba5f) --- diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtilsTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtilsTest.java index 4145ea8d79..99a32465a1 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtilsTest.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtilsTest.java @@ -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 = "\n" + + private static final String XML_CONTENT = "\n" + + "value\n" + - ""+ + "" + + "/ltha:cont/ltha:l[ ltha:id='id/foo/bar' ]"+ "\n" + ""; - public static final String RPC_REPLY = "\n" + - " \n" + - ""; - 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 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 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 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 modifyOperationFromAttributes = XmlDocumentUtils + .getModifyOperationFromAttributes(element); + Assert.assertFalse(modifyOperationFromAttributes.isPresent()); + } + + @Test + public void xmlDocFindFirstSchemaContTest() { + final QName ID = QName.create(XmlDocumentUtilsTest.NS, XmlDocumentUtilsTest.REVISION, "cont"); + final Optional 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 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 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 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 colls = Lists.newArrayList(sch); + final Optional 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> codec = provider.codecFor(baseType); + Assert.assertNotNull(codec); + } } diff --git a/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/rpc-test.yang b/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/xml-doc-test.yang 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 94596f3152..8efbea4e86 100644 --- a/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/rpc-test.yang +++ b/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/xml-doc-test.yang @@ -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 index 0000000000..88b8f153f9 --- /dev/null +++ b/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/xml-doc-test2.yang @@ -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; + } + } + } +}