935947f9f6187de022b5ee3d75dff06f986ba222
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / InstanceIdentifierForXmlCodecTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.io.File;
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;
27
28 @RunWith(MockitoJUnitRunner.class)
29 public class InstanceIdentifierForXmlCodecTest {
30
31     public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:iid:test\">" + "<a>/cont</a>"
32             + "</input>";
33
34     private static final String NS = "urn:opendaylight:controller:iid:test";
35     private static final String REVISION = "2014-07-28";
36
37     private SchemaContext schemaContext;
38     private Element elementOrig;
39
40     @Before
41     public void setup() throws Exception {
42         final File rpcTestYang = new File(getClass().getResource("iid-test.yang").toURI());
43         this.schemaContext = TestUtils.parseYangSources(rpcTestYang);
44
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));
49         assertNotNull(data);
50
51         final Document doc = XmlDocumentUtilsTest.readXmlToDocument(XML_CONTENT);
52         this.elementOrig = XmlDocumentUtils.createElementFor(doc, data);
53         assertNotNull(this.elementOrig);
54     }
55
56     @Test
57     public void deserializeTest() throws Exception {
58         final YangInstanceIdentifier deserialize = InstanceIdentifierForXmlCodec.deserialize(this.elementOrig,
59                 this.schemaContext);
60         assertEquals("/", deserialize.toString());
61     }
62
63     @Test
64     public void serializeTest() throws Exception {
65
66         final QName name = QName.create(InstanceIdentifierForXmlCodecTest.NS,
67                 InstanceIdentifierForXmlCodecTest.REVISION, "cont");
68         final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(name).build();
69
70         final Element el = InstanceIdentifierForXmlCodec.serialize(id, this.elementOrig, this.schemaContext);
71         assertEquals(this.elementOrig, el);
72     }
73 }