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