1 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.fail;
8 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
9 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
10 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
12 import com.google.common.base.Optional;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class TreeNodeUtilsTest {
32 private static final Logger LOG = LoggerFactory.getLogger(TreeNodeUtilsTest.class);
34 private static final Short ONE_ID = 1;
35 private static final Short TWO_ID = 2;
36 private static final String TWO_ONE_NAME = "one";
37 private static final String TWO_TWO_NAME = "two";
39 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
40 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
43 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
44 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
47 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
48 .node(TestModel.INNER_LIST_QNAME) //
49 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
52 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
53 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
54 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
55 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)) //
59 private SchemaContext schemaContext;
60 private RootModificationApplyOperation rootOper;
63 public void prepare() {
64 schemaContext = TestModel.createTestContext();
65 assertNotNull("Schema context must not be null.", schemaContext);
66 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext));
69 public NormalizedNode<?, ?> createDocumentOne() {
70 return ImmutableContainerNodeBuilder
72 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
73 .withChild(createTestContainer()).build();
77 private ContainerNode createTestContainer() {
78 return ImmutableContainerNodeBuilder
80 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
82 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
83 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
84 .withChild(BAR_NODE).build()).build();
87 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
88 assertNotNull(potential);
89 assertTrue(potential.isPresent());
90 assertTrue(type.isInstance(potential.get()));
91 return type.cast(potential.get());
95 public void findNodeTestNodeFound() {
96 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
97 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
98 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
99 Optional<TreeNode> node = TreeNodeUtils.findNode(rootNode, OUTER_LIST_1_PATH);
100 assertPresentAndType(node, TreeNode.class);
104 public void findNodeTestNodeNotFound() {
105 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
106 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
107 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
108 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
109 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
111 Optional<TreeNode> node = TreeNodeUtils.findNode(rootNode, outerList1InvalidPath);
112 assertFalse(node.isPresent());
116 public void findNodeCheckedTestNodeFound() {
117 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
118 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
119 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
120 TreeNode foundNode = null;
122 foundNode = TreeNodeUtils.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
123 } catch (IllegalArgumentException e) {
124 fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
126 assertNotNull(foundNode);
130 public void findNodeCheckedTestNodeNotFound() {
131 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
132 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
133 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
134 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
135 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
138 TreeNodeUtils.findNodeChecked(rootNode, outerList1InvalidPath);
139 fail("Illegal argument exception should have been thrown");
140 } catch (IllegalArgumentException e) {
141 LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
146 public void findClosestOrFirstMatchTestNodeExists() {
147 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
148 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
149 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
150 Optional<TreeNode> expectedNode = TreeNodeUtils.findNode(rootNode, TWO_TWO_PATH);
151 assertPresentAndType(expectedNode, TreeNode.class);
152 Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = TreeNodeUtils.findClosest(rootNode, TWO_TWO_PATH);
153 assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
157 public void findClosestOrFirstMatchTestNodeDoesNotExist() {
158 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
159 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
160 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
161 final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
162 .node(TestModel.INNER_LIST_QNAME)
164 final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
165 .node(TestModel.INNER_LIST_QNAME) //
166 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three") //
168 Optional<TreeNode> expectedNode = TreeNodeUtils.findNode(rootNode, outerListInnerListPath);
169 assertPresentAndType(expectedNode, TreeNode.class);
170 Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = TreeNodeUtils.findClosest(rootNode, twoTwoInvalidPath);
171 assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
175 public void getChildTestChildFound() {
176 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
177 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
178 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
179 Optional<TreeNode> node = TreeNodeUtils.getChild(Optional.fromNullable(rootNode),
180 TestModel.TEST_PATH.getLastPathArgument());
181 assertPresentAndType(node, TreeNode.class);
185 public void getChildTestChildNotFound() {
186 InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
187 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
188 TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
189 Optional<TreeNode> node = TreeNodeUtils.getChild(Optional.fromNullable(rootNode),
190 TestModel.OUTER_LIST_PATH.getLastPathArgument());
191 assertFalse(node.isPresent());