From 10b12a2d5ae1cf6bbf2305372342635ac741c94c Mon Sep 17 00:00:00 2001 From: Jakub Toth Date: Mon, 8 Feb 2016 16:08:11 +0100 Subject: [PATCH 1/1] Bug 3899: Milestone: Increase test coverage for Yangtools test InstanceIdentifierForXmlCodecTest Change-Id: I3dc7f58309ae900825a10d5ea0a878cf037c24e5 Signed-off-by: Jakub Toth (cherry picked from commit 5b4fa9654c9013694ab57b404f0798411fdbabc4) --- .../InstanceIdentifierForXmlCodecTest.java | 73 +++++++++++++++++++ .../yang/data/impl/codec/xml/iid-test.yang | 27 +++++++ 2 files changed, 100 insertions(+) create mode 100644 yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/InstanceIdentifierForXmlCodecTest.java create mode 100644 yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/iid-test.yang diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/InstanceIdentifierForXmlCodecTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/InstanceIdentifierForXmlCodecTest.java new file mode 100644 index 0000000000..d6e412d4f3 --- /dev/null +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/InstanceIdentifierForXmlCodecTest.java @@ -0,0 +1,73 @@ +/* + * 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 + */ + +package org.opendaylight.yangtools.yang.data.impl.codec.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.File; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.impl.RetestUtils; +import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +@RunWith(MockitoJUnitRunner.class) +public class InstanceIdentifierForXmlCodecTest { + + public static final String XML_CONTENT = "" + "/cont" + + ""; + + private static final String NS = "urn:opendaylight:controller:iid:test"; + private static final String REVISION = "2014-07-28"; + + private SchemaContext schemaContext; + private Element elementOrig; + + @Before + public void setup() throws Exception { + final File rpcTestYang = new File(getClass().getResource("iid-test.yang").toURI()); + this.schemaContext = RetestUtils.parseYangSources(rpcTestYang); + + final YangInstanceIdentifier.NodeIdentifier container = new YangInstanceIdentifier.NodeIdentifier( + QName.create(InstanceIdentifierForXmlCodecTest.NS, InstanceIdentifierForXmlCodecTest.REVISION, "cont")); + final NormalizedNode data = ImmutableNodes.fromInstanceId(this.schemaContext, + YangInstanceIdentifier.create(container)); + assertNotNull(data); + + final Document doc = XmlDocumentUtilsTest.readXmlToDocument(XML_CONTENT); + this.elementOrig = XmlDocumentUtils.createElementFor(doc, data); + assertNotNull(this.elementOrig); + } + + @Test + public void deserializeTest() throws Exception { + final YangInstanceIdentifier deserialize = InstanceIdentifierForXmlCodec.deserialize(this.elementOrig, + this.schemaContext); + assertEquals("/", deserialize.toString()); + } + + @Test + public void serializeTest() throws Exception { + + final QName name = QName.create(InstanceIdentifierForXmlCodecTest.NS, + InstanceIdentifierForXmlCodecTest.REVISION, "cont"); + final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(name).build(); + + final Element el = InstanceIdentifierForXmlCodec.serialize(id, this.elementOrig, this.schemaContext); + assertEquals(this.elementOrig, el); + } +} diff --git a/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/iid-test.yang b/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/iid-test.yang new file mode 100644 index 0000000000..2265d2d326 --- /dev/null +++ b/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/iid-test.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 iid-test { + yang-version 1; + namespace "urn:opendaylight:controller:iid:test"; + prefix "iidt"; + + revision 2014-07-28 { + description "Initial test"; + } + + container cont { + + list l { + key "id"; + + leaf id { + type string; + } + } + } +} -- 2.36.6