Bug 1781: Fix serialization of leafrefs
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / NodeFactoryTest.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.yangtools.yang.data.impl;
9
10 import java.net.URI;
11 import java.util.ArrayList;
12 import java.util.Date;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
22 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
23 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.Node;
25 import org.opendaylight.yangtools.yang.data.api.NodeModification;
26 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
27 import org.w3c.dom.Document;
28
29 /**
30  * @author michal.rehak
31  *
32  */
33 @Deprecated
34 public class NodeFactoryTest {
35
36     private QName qName;
37     private CompositeNode network;
38
39     private String ns;
40     private Document networkShadow;
41
42
43     /**
44      * @throws Exception
45      */
46     @Before
47     public void setUp() throws Exception {
48         ns = "urn:ietf:params:xml:ns:netconf:base:1.0";
49         qName = QName.create(
50                 new URI(ns),
51                 new Date(42), "node");
52         network = NodeHelper.buildTestConfigTree(qName);
53         networkShadow = NodeUtils.buildShadowDomTree(network);
54         NodeHelper.compareXmlTree(networkShadow, "./config02-shadow.xml", getClass());
55     }
56
57     /**
58      * Test method for methods creating immutable nodes in
59      * {@link org.opendaylight.yangtools.yang.data.impl.NodeFactory}.
60      * @throws Exception
61      */
62     @Test
63     public void testImmutableNodes() throws Exception {
64         Assert.assertEquals(2, network.getValue().size());
65         CompositeNode tpList = NodeUtils.findNodeByXpath(networkShadow,
66                 NodeHelper.AddNamespaceToPattern(
67                         "//{0}node[{0}node-id/text()='nodeId_19']/{0}termination-points", ns));
68
69
70         Assert.assertEquals(2, tpList.getCompositesByName("termination-point").size());
71     }
72
73     /**
74      * Test method for methods creating immutable and mutable nodes:
75      * {@link NodeFactory#createMutableCompositeNode(QName, CompositeNode, List, ModifyAction, CompositeNode)},
76      * {@link NodeFactory#createMutableSimpleNode(QName, CompositeNode, Object, ModifyAction, SimpleNode)}
77      * @throws Exception
78      */
79     @Test
80     public void testMutableNodes() throws Exception {
81         // <config>
82         //   <top xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
83         //     <interface xc:operation="delete">
84         //       <name>Ethernet0/0</name>
85         //       <mtu>1500</mtu>
86         //     </interface>
87         //     <interface>
88         //       <name>Ethernet0/1</name>
89         //       <mtu>1501</mtu>
90         //     </interface>
91         //   </top>
92         // </config>
93
94
95         List<Node<?>> value = new ArrayList<Node<?>>();
96         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "name"), null, "Ethernet0/0"));
97         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "mtu"), null, 1500));
98
99         MutableCompositeNode ifNode = NodeFactory.createMutableCompositeNode(
100                 QName.create(qName, "interface"), null,
101                 value, ModifyAction.DELETE, null);
102         ifNode.init();
103         NodeHelper.assignParentToChildren(ifNode);
104
105         value = new ArrayList<Node<?>>();
106         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "name"), null, "Ethernet1/0"));
107         value.add(NodeFactory.createMutableSimpleNode(QName.create(qName, "mtu"), null, 1501, ModifyAction.REMOVE, null));
108
109         CompositeNode ifNode2 = NodeFactory.createImmutableCompositeNode(QName.create(qName, "interface"), null, value);
110         NodeHelper.assignParentToChildren(ifNode2);
111
112         value = new ArrayList<Node<?>>();
113         value.add(ifNode);
114         value.add(ifNode2);
115
116         CompositeNode topNode = NodeFactory.createImmutableCompositeNode(QName.create(qName, "top"), null, value);
117         NodeHelper.assignParentToChildren(topNode);
118         value = new ArrayList<Node<?>>();
119         value.add(topNode);
120
121         CompositeNode root = NodeFactory.createImmutableCompositeNode(QName.create(qName, "config"), null, value);
122         Document shadowConfig = NodeUtils.buildShadowDomTree(root);
123         NodeHelper.compareXmlTree(shadowConfig, "./mutableNodesConfig.xml", getClass());
124
125         Assert.assertEquals(1, root.getValue().size());
126         Assert.assertEquals(1, ifNode.getSimpleNodesByName("name").size());
127         Assert.assertEquals(1, ifNode.getSimpleNodesByName("mtu").size());
128         Assert.assertEquals(2, topNode.getCompositesByName("interface").size());
129         NodeModification interfaceMod = topNode.getCompositesByName("interface").get(0);
130         Assert.assertEquals(ModifyAction.DELETE, interfaceMod.getModificationAction());
131     }
132
133     /**
134      * test of {@link NodeFactory#copyDeepAsMutable(CompositeNode, Map)}
135      * @throws Exception
136      */
137     @Test
138     public void testCopyDeepAsMutable() throws Exception {
139         Map<Node<?>, Node<?>> mutableToOrig = new HashMap<>();
140         CompositeNode mutableNetwork = NodeFactory.copyDeepAsMutable(network, mutableToOrig);
141
142         Document mutableNetworkShadow = NodeUtils.buildShadowDomTree(mutableNetwork);
143
144         NodeHelper.compareXmlTree(mutableNetworkShadow, "./config02-shadow.xml", getClass());
145
146         CompositeNode immutableNetwork = NodeFactory.copyDeepAsImmutable(mutableNetwork, null);
147         Assert.assertEquals(network, immutableNetwork);
148     }
149
150
151     /**
152      * test of {@link NodeFactory#copyDeepAsImmutable(CompositeNode, Map)}
153      * @throws Exception
154      */
155     @Test
156     public void testCopyDeepAsImmutable() throws Exception {
157         Map<Node<?>, Node<?>> mutableToOrig = new HashMap<>();
158         CompositeNode immutableNetwork = NodeFactory.copyDeepAsImmutable(network, mutableToOrig);
159
160         Document mutableNetworkShadow = NodeUtils.buildShadowDomTree(immutableNetwork);
161         NodeHelper.compareXmlTree(mutableNetworkShadow, "./config02-shadow.xml", getClass());
162
163         Assert.assertEquals(network, immutableNetwork);
164     }
165
166 }