1 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
3 import com.google.common.base.Optional;
4 import junit.framework.Assert;
5 import org.junit.Before;
7 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
8 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
9 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
10 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
11 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
12 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
14 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
15 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
21 import static org.junit.Assert.*;
22 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.*;
25 public class TreeNodeUtilsTest {
26 private static final Logger LOG = LoggerFactory.getLogger(TreeNodeUtilsTest.class);
28 private static final Short ONE_ID = 1;
29 private static final Short TWO_ID = 2;
30 private static final String TWO_ONE_NAME = "one";
31 private static final String TWO_TWO_NAME = "two";
33 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
34 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
37 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
38 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
41 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
42 .node(TestModel.INNER_LIST_QNAME) //
43 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
46 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
47 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
48 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
49 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)) //
53 private SchemaContext schemaContext;
54 private RootModificationApplyOperation rootOper;
57 public void prepare() {
58 schemaContext = TestModel.createTestContext();
59 assertNotNull("Schema context must not be null.", schemaContext);
60 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext));
63 public NormalizedNode<?, ?> createDocumentOne() {
64 return ImmutableContainerNodeBuilder
66 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
67 .withChild(createTestContainer()).build();
71 private ContainerNode createTestContainer() {
72 return ImmutableContainerNodeBuilder
74 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
76 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
77 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
78 .withChild(BAR_NODE).build()).build();
81 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
82 assertNotNull(potential);
83 assertTrue(potential.isPresent());
84 assertTrue(type.isInstance(potential.get()));
85 return type.cast(potential.get());
89 public void findNodeTestNodeFound() {
90 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
91 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
92 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
93 Optional<TreeNode> node = TreeNodeUtils.findNode(rootNode, OUTER_LIST_1_PATH);
94 assertPresentAndType(node, TreeNode.class);
98 public void findNodeTestNodeNotFound() {
99 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
100 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
101 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
102 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
103 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
105 Optional<TreeNode> node = TreeNodeUtils.findNode(rootNode, outerList1InvalidPath);
106 Assert.assertFalse(node.isPresent());
110 public void findNodeCheckedTestNodeFound() {
111 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
112 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
113 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
114 TreeNode foundNode = null;
116 foundNode = TreeNodeUtils.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
117 } catch (IllegalArgumentException e) {
118 fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
120 Assert.assertNotNull(foundNode);
124 public void findNodeCheckedTestNodeNotFound() {
125 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
126 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
127 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
128 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
129 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
132 TreeNodeUtils.findNodeChecked(rootNode, outerList1InvalidPath);
133 fail("Illegal argument exception should have been thrown");
134 } catch (IllegalArgumentException e) {
135 LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
140 public void findClosestOrFirstMatchTestNodeExists() {
141 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
142 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
143 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
144 Optional<TreeNode> expectedNode = TreeNodeUtils.findNode(rootNode, TWO_TWO_PATH);
145 assertPresentAndType(expectedNode, TreeNode.class);
146 Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = TreeNodeUtils.findClosest(rootNode, TWO_TWO_PATH);
147 Assert.assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
151 public void findClosestOrFirstMatchTestNodeDoesNotExist() {
152 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
153 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
154 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
155 final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
156 .node(TestModel.INNER_LIST_QNAME)
158 final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
159 .node(TestModel.INNER_LIST_QNAME) //
160 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three") //
162 Optional<TreeNode> expectedNode = TreeNodeUtils.findNode(rootNode, outerListInnerListPath);
163 assertPresentAndType(expectedNode, TreeNode.class);
164 Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = TreeNodeUtils.findClosest(rootNode, twoTwoInvalidPath);
165 Assert.assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
169 public void getChildTestChildFound() {
170 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
171 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
172 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
173 Optional<TreeNode> node = TreeNodeUtils.getChild(Optional.fromNullable(rootNode),
174 TestModel.TEST_PATH.getLastPathArgument());
175 assertPresentAndType(node, TreeNode.class);
179 public void getChildTestChildNotFound() {
180 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
181 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
182 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
183 Optional<TreeNode> node = TreeNodeUtils.getChild(Optional.fromNullable(rootNode),
184 TestModel.OUTER_LIST_PATH.getLastPathArgument());
185 Assert.assertFalse(node.isPresent());