Fixing sonar issues 4
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / NodeModificationBuilderImplTest.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.Date;
12
13 import junit.framework.Assert;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
21 import org.opendaylight.yangtools.yang.data.api.Node;
22 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27
28 /**
29  * @author michal.rehak
30  * 
31  */
32 public class NodeModificationBuilderImplTest {
33
34     private static final Logger LOG = LoggerFactory.getLogger(NodeModificationBuilderImplTest.class);
35
36     private SchemaContext schemaCtx;
37     private QName qName;
38     private CompositeNode network;
39     private NodeModificationBuilderImpl nodeModificationBuilder;
40
41     private String ns;
42
43     /**
44      * @throws Exception
45      */
46     private void dumpResult() throws Exception {
47         CompositeNode diffTree = nodeModificationBuilder.buildDiffTree();
48         CompositeNode diffTreeImmutable = NodeFactory.copyDeepAsImmutable(diffTree, null);
49
50         Document diffShadow = NodeUtils.buildShadowDomTree(diffTreeImmutable);
51         NodeHelper.dumpDoc(diffShadow, System.out);
52     }
53
54     /**
55      * prepare schemaContext
56      * 
57      * @throws Exception
58      */
59     @Before
60     public void setUp() throws Exception {
61         schemaCtx = NodeHelper.loadSchemaContext();
62
63         ns = "urn:opendaylight:controller:network";
64         qName = new QName(new URI(ns), new Date(1369000800000L), "topos");
65         network = NodeHelper.buildTestConfigTree(qName);
66
67         nodeModificationBuilder = new NodeModificationBuilderImpl(schemaCtx);
68     }
69
70     /**
71      * Test method for
72      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#getMutableEquivalent(org.opendaylight.yangtools.yang.data.api.Node)}
73      * .
74      */
75     @Test
76     public void testGetMutableEquivalent() {
77         MutableCompositeNode rootMutable = (MutableCompositeNode) nodeModificationBuilder.getMutableEquivalent(network);
78
79         CompositeNode topologies = network.getCompositesByName("topologies").iterator().next();
80         Node<?> mutableEquivalent = nodeModificationBuilder.getMutableEquivalent(topologies);
81         CompositeNode topologiesMutable = rootMutable.getCompositesByName("topologies").iterator().next();
82
83         Assert.assertSame(topologiesMutable, mutableEquivalent);
84     }
85
86     /**
87      * Test method for
88      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
89      * .
90      * 
91      * @throws Exception
92      */
93     @Test
94     public void testBuildDiffTreeAddSimple() throws Exception {
95         LOG.debug("testBuildDiffTreeAddSimple");
96         Document networkShadow = NodeUtils.buildShadowDomTree(network);
97         CompositeNode needle = NodeUtils.findNodeByXpath(networkShadow,
98                 NodeHelper.AddNamespaceToPattern("//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]", ns));
99
100         MutableCompositeNode mutableParent = (MutableCompositeNode) nodeModificationBuilder
101                 .getMutableEquivalent(needle);
102
103         MutableSimpleNode<String> newMutable = NodeFactory.createMutableSimpleNode(new QName(needle.getNodeType(),
104                 "anySubNode"), mutableParent, "42", null, null);
105
106         nodeModificationBuilder.addNode(newMutable);
107         dumpResult();
108     }
109
110     /**
111      * Test method for
112      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
113      * .
114      * 
115      * @throws Exception
116      */
117     @Test
118     public void testBuildDiffTreeAddComposite() throws Exception {
119         LOG.debug("testBuildDiffTreeAddComposite");
120         Document networkShadow = NodeUtils.buildShadowDomTree(network);
121         CompositeNode needle = NodeUtils.findNodeByXpath(networkShadow,
122                 NodeHelper.AddNamespaceToPattern("//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]", ns));
123
124         MutableCompositeNode mutableParent = (MutableCompositeNode) nodeModificationBuilder
125                 .getMutableEquivalent(needle);
126
127         MutableSimpleNode<String> newMutable = NodeFactory.createMutableSimpleNode(new QName(needle.getNodeType(),
128                 "anySubNode"), null, "42", null, null);
129
130         MutableCompositeNode newMutableCom = NodeFactory.createMutableCompositeNode(new QName(needle.getNodeType(),
131                 "anySubNode"), mutableParent, NodeUtils.buildChildrenList(newMutable), null, null);
132         NodeUtils.fixChildrenRelation(newMutableCom);
133         newMutableCom.init();
134
135         nodeModificationBuilder.addNode(newMutableCom);
136         dumpResult();
137     }
138
139     /**
140      * Test method for
141      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
142      * .
143      * 
144      * @throws Exception
145      */
146     @Test
147     public void testBuildDiffTreeDeleteComposite() throws Exception {
148         LOG.debug("testBuildDiffTreeDeleteComposite");
149         Document networkShadow = NodeUtils.buildShadowDomTree(network);
150         SimpleNode<?> needle = NodeUtils.findNodeByXpath(networkShadow, NodeHelper.AddNamespaceToPattern(
151                 "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns));
152
153         @SuppressWarnings("unchecked")
154         MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
155                 .getMutableEquivalent(needle);
156
157         nodeModificationBuilder.deleteNode(mutableNeedle.getParent().asMutable());
158         dumpResult();
159     }
160
161     /**
162      * Test method for
163      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
164      * .
165      * 
166      * @throws Exception
167      */
168     @Test
169     public void testBuildDiffTreeDeleteSimple() throws Exception {
170         LOG.debug("testBuildDiffTreeDeleteSimple");
171         Document networkShadow = NodeUtils.buildShadowDomTree(network);
172         SimpleNode<?> needle = NodeUtils.findNodeByXpath(networkShadow, NodeHelper.AddNamespaceToPattern(
173                 "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns));
174
175         @SuppressWarnings("unchecked")
176         MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
177                 .getMutableEquivalent(needle);
178
179         nodeModificationBuilder.deleteNode(mutableNeedle);
180         dumpResult();
181     }
182
183     /**
184      * Test method for
185      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
186      * .
187      * 
188      * @throws Exception
189      */
190     @Test
191     public void testBuildDiffTreeMerge() throws Exception {
192         LOG.debug("testBuildDiffTreeMerge");
193         Document networkShadow = NodeUtils.buildShadowDomTree(network);
194         SimpleNode<?> needle = NodeUtils.findNodeByXpath(networkShadow, NodeHelper.AddNamespaceToPattern(
195                 "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns));
196
197         @SuppressWarnings("unchecked")
198         MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
199                 .getMutableEquivalent(needle);
200
201         mutableNeedle.setValue("tpId_18x");
202         nodeModificationBuilder.mergeNode(mutableNeedle.getParent().asMutable());
203         dumpResult();
204     }
205
206     /**
207      * Test method for
208      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
209      * .
210      * 
211      * @throws Exception
212      */
213     @Test
214     public void testBuildDiffTreeRemoveComposite() throws Exception {
215         LOG.debug("testBuildDiffTreeRemoveComposite");
216         Document networkShadow = NodeUtils.buildShadowDomTree(network);
217         SimpleNode<?> needle = NodeUtils.findNodeByXpath(networkShadow, NodeHelper.AddNamespaceToPattern(
218                 "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns));
219
220         @SuppressWarnings("unchecked")
221         MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
222                 .getMutableEquivalent(needle);
223
224         nodeModificationBuilder.removeNode(mutableNeedle.getParent().asMutable());
225         dumpResult();
226     }
227
228     /**
229      * Test method for
230      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
231      * .
232      * 
233      * @throws Exception
234      */
235     @Test
236     public void testBuildDiffTreeRemoveSimple() throws Exception {
237         LOG.debug("testBuildDiffTreeRemoveSimple");
238         Document networkShadow = NodeUtils.buildShadowDomTree(network);
239         SimpleNode<?> needle = NodeUtils.findNodeByXpath(networkShadow, NodeHelper.AddNamespaceToPattern(
240                 "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns));
241
242         @SuppressWarnings("unchecked")
243         MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
244                 .getMutableEquivalent(needle);
245
246         nodeModificationBuilder.removeNode(mutableNeedle);
247         dumpResult();
248     }
249
250     /**
251      * Test method for
252      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
253      * .
254      * 
255      * @throws Exception
256      */
257     @Test
258     public void testBuildDiffTreeReplaceComposite() throws Exception {
259         LOG.debug("testBuildDiffTreeReplaceComposite");
260         Document networkShadow = NodeUtils.buildShadowDomTree(network);
261         SimpleNode<?> needle = NodeUtils.findNodeByXpath(networkShadow, NodeHelper.AddNamespaceToPattern(
262                 "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns));
263
264         @SuppressWarnings("unchecked")
265         MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
266                 .getMutableEquivalent(needle);
267
268         mutableNeedle.setValue("tpId_18x");
269         nodeModificationBuilder.replaceNode(mutableNeedle.getParent().asMutable());
270         dumpResult();
271     }
272
273     /**
274      * Test method for
275      * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
276      * .
277      * 
278      * @throws Exception
279      */
280     @Test
281     public void testBuildDiffTreeReplaceSimple() throws Exception {
282         LOG.debug("testBuildDiffTreeReplaceSimple");
283         Document networkShadow = NodeUtils.buildShadowDomTree(network);
284         SimpleNode<?> needle = NodeUtils.findNodeByXpath(networkShadow, NodeHelper.AddNamespaceToPattern(
285                 "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns));
286
287         @SuppressWarnings("unchecked")
288         MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
289                 .getMutableEquivalent(needle);
290
291         mutableNeedle.setValue("tpId_18x");
292         nodeModificationBuilder.replaceNode(mutableNeedle);
293         dumpResult();
294     }
295
296 }