2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.impl.TestUtils;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
28 @RunWith(MockitoJUnitRunner.class)
29 public class InstanceIdentifierForXmlCodecTest {
31 public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:iid:test\">" + "<a>/cont</a>"
34 private static final String NS = "urn:opendaylight:controller:iid:test";
35 private static final String REVISION = "2014-07-28";
37 private SchemaContext schemaContext;
38 private Element elementOrig;
41 public void setup() throws Exception {
42 final File rpcTestYang = new File(getClass().getResource("iid-test.yang").toURI());
43 this.schemaContext = TestUtils.parseYangSources(rpcTestYang);
45 final YangInstanceIdentifier.NodeIdentifier container = new YangInstanceIdentifier.NodeIdentifier(
46 QName.create(InstanceIdentifierForXmlCodecTest.NS, InstanceIdentifierForXmlCodecTest.REVISION, "cont"));
47 final NormalizedNode<?, ?> data = ImmutableNodes.fromInstanceId(this.schemaContext,
48 YangInstanceIdentifier.create(container));
51 final Document doc = XmlDocumentUtilsTest.readXmlToDocument(XML_CONTENT);
52 this.elementOrig = XmlDocumentUtils.createElementFor(doc, data);
53 assertNotNull(this.elementOrig);
57 public void deserializeTest() throws Exception {
58 final YangInstanceIdentifier deserialize = InstanceIdentifierForXmlCodec.deserialize(this.elementOrig,
60 assertEquals("/", deserialize.toString());
64 public void serializeTest() throws Exception {
66 final QName name = QName.create(InstanceIdentifierForXmlCodecTest.NS,
67 InstanceIdentifierForXmlCodecTest.REVISION, "cont");
68 final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(name).build();
70 final Element el = InstanceIdentifierForXmlCodec.serialize(id, this.elementOrig, this.schemaContext);
71 assertEquals(this.elementOrig, el);