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.ArrayList;
12 import java.util.Date;
13 import java.util.HashMap;
14 import java.util.List;
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;
30 * @author michal.rehak
34 public class NodeFactoryTest {
37 private CompositeNode network;
40 private Document networkShadow;
47 public void setUp() throws Exception {
48 ns = "urn:ietf:params:xml:ns:netconf:base:1.0";
51 new Date(42), "node");
52 network = NodeHelper.buildTestConfigTree(qName);
53 networkShadow = NodeUtils.buildShadowDomTree(network);
54 NodeHelper.compareXmlTree(networkShadow, "./config02-shadow.xml", getClass());
58 * Test method for methods creating immutable nodes in
59 * {@link org.opendaylight.yangtools.yang.data.impl.NodeFactory}.
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));
70 Assert.assertEquals(2, tpList.getCompositesByName("termination-point").size());
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)}
80 public void testMutableNodes() throws Exception {
82 // <top xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
83 // <interface xc:operation="delete">
84 // <name>Ethernet0/0</name>
88 // <name>Ethernet0/1</name>
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));
99 MutableCompositeNode ifNode = NodeFactory.createMutableCompositeNode(
100 QName.create(qName, "interface"), null,
101 value, ModifyAction.DELETE, null);
103 NodeHelper.assignParentToChildren(ifNode);
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));
109 CompositeNode ifNode2 = NodeFactory.createImmutableCompositeNode(QName.create(qName, "interface"), null, value);
110 NodeHelper.assignParentToChildren(ifNode2);
112 value = new ArrayList<Node<?>>();
116 CompositeNode topNode = NodeFactory.createImmutableCompositeNode(QName.create(qName, "top"), null, value);
117 NodeHelper.assignParentToChildren(topNode);
118 value = new ArrayList<Node<?>>();
121 CompositeNode root = NodeFactory.createImmutableCompositeNode(QName.create(qName, "config"), null, value);
122 Document shadowConfig = NodeUtils.buildShadowDomTree(root);
123 NodeHelper.compareXmlTree(shadowConfig, "./mutableNodesConfig.xml", getClass());
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());
134 * test of {@link NodeFactory#copyDeepAsMutable(CompositeNode, Map)}
138 public void testCopyDeepAsMutable() throws Exception {
139 Map<Node<?>, Node<?>> mutableToOrig = new HashMap<>();
140 CompositeNode mutableNetwork = NodeFactory.copyDeepAsMutable(network, mutableToOrig);
142 Document mutableNetworkShadow = NodeUtils.buildShadowDomTree(mutableNetwork);
144 NodeHelper.compareXmlTree(mutableNetworkShadow, "./config02-shadow.xml", getClass());
146 CompositeNode immutableNetwork = NodeFactory.copyDeepAsImmutable(mutableNetwork, null);
147 Assert.assertEquals(network, immutableNetwork);
152 * test of {@link NodeFactory#copyDeepAsImmutable(CompositeNode, Map)}
156 public void testCopyDeepAsImmutable() throws Exception {
157 Map<Node<?>, Node<?>> mutableToOrig = new HashMap<>();
158 CompositeNode immutableNetwork = NodeFactory.copyDeepAsImmutable(network, mutableToOrig);
160 Document mutableNetworkShadow = NodeUtils.buildShadowDomTree(immutableNetwork);
161 NodeHelper.compareXmlTree(mutableNetworkShadow, "./config02-shadow.xml", getClass());
163 Assert.assertEquals(network, immutableNetwork);