2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl;
11 import java.util.Date;
13 import junit.framework.Assert;
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;
29 * @author michal.rehak
33 public class NodeModificationBuilderImplTest {
35 private static final Logger LOG = LoggerFactory.getLogger(NodeModificationBuilderImplTest.class);
37 private SchemaContext schemaCtx;
39 private CompositeNode network;
40 private NodeModificationBuilderImpl nodeModificationBuilder;
47 private void dumpResult() throws Exception {
48 CompositeNode diffTree = nodeModificationBuilder.buildDiffTree();
49 CompositeNode diffTreeImmutable = NodeFactory.copyDeepAsImmutable(diffTree, null);
51 Document diffShadow = NodeUtils.buildShadowDomTree(diffTreeImmutable);
52 NodeHelper.dumpDoc(diffShadow, LOG);
56 * prepare schemaContext
61 public void setUp() throws Exception {
62 schemaCtx = NodeHelper.loadSchemaContext();
64 ns = "urn:opendaylight:controller:network";
65 qName = QName.create(new URI(ns), new Date(1369000800000L), "topos");
66 network = NodeHelper.buildTestConfigTree(qName);
68 nodeModificationBuilder = new NodeModificationBuilderImpl(schemaCtx);
73 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#getMutableEquivalent(org.opendaylight.yangtools.yang.data.api.Node)}
77 public void testGetMutableEquivalent() {
78 MutableCompositeNode rootMutable = (MutableCompositeNode) nodeModificationBuilder.getMutableEquivalent(network);
80 CompositeNode topologies = network.getCompositesByName("topologies").iterator().next();
81 Node<?> mutableEquivalent = nodeModificationBuilder.getMutableEquivalent(topologies);
82 CompositeNode topologiesMutable = rootMutable.getCompositesByName("topologies").iterator().next();
84 Assert.assertSame(topologiesMutable, mutableEquivalent);
89 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
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));
101 MutableCompositeNode mutableParent = (MutableCompositeNode) nodeModificationBuilder
102 .getMutableEquivalent(needle);
104 MutableSimpleNode<String> newMutable = NodeFactory.createMutableSimpleNode(
105 QName.create(needle.getNodeType(),
106 "anySubNode"), mutableParent, "42", null, null);
108 nodeModificationBuilder.addNode(newMutable);
114 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
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));
126 MutableCompositeNode mutableParent = (MutableCompositeNode) nodeModificationBuilder
127 .getMutableEquivalent(needle);
129 MutableSimpleNode<String> newMutable = NodeFactory.createMutableSimpleNode(
130 QName.create(needle.getNodeType(),
131 "anySubNode"), null, "42", null, null);
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();
139 nodeModificationBuilder.addNode(newMutableCom);
145 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
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));
157 @SuppressWarnings("unchecked")
158 MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
159 .getMutableEquivalent(needle);
161 nodeModificationBuilder.deleteNode(mutableNeedle.getParent().asMutable());
167 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
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));
179 @SuppressWarnings("unchecked")
180 MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
181 .getMutableEquivalent(needle);
183 nodeModificationBuilder.deleteNode(mutableNeedle);
189 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
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));
201 @SuppressWarnings("unchecked")
202 MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
203 .getMutableEquivalent(needle);
205 mutableNeedle.setValue("tpId_18x");
206 nodeModificationBuilder.mergeNode(mutableNeedle.getParent().asMutable());
212 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
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));
224 @SuppressWarnings("unchecked")
225 MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
226 .getMutableEquivalent(needle);
228 nodeModificationBuilder.removeNode(mutableNeedle.getParent().asMutable());
234 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
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));
246 @SuppressWarnings("unchecked")
247 MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
248 .getMutableEquivalent(needle);
250 nodeModificationBuilder.removeNode(mutableNeedle);
256 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
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));
268 @SuppressWarnings("unchecked")
269 MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
270 .getMutableEquivalent(needle);
272 mutableNeedle.setValue("tpId_18x");
273 nodeModificationBuilder.replaceNode(mutableNeedle.getParent().asMutable());
279 * {@link org.opendaylight.yangtools.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()}
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));
291 @SuppressWarnings("unchecked")
292 MutableSimpleNode<String> mutableNeedle = (MutableSimpleNode<String>) nodeModificationBuilder
293 .getMutableEquivalent(needle);
295 mutableNeedle.setValue("tpId_18x");
296 nodeModificationBuilder.replaceNode(mutableNeedle);