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