2 * Copyright (c) 2015 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.schema.tree;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
19 import java.util.Map.Entry;
20 import java.util.Optional;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class StoreTreeNodesTest extends AbstractTestModelTest {
37 private static final Logger LOG = LoggerFactory.getLogger(StoreTreeNodesTest.class);
39 private static final Short ONE_ID = 1;
40 private static final Short TWO_ID = 2;
41 private static final String TWO_ONE_NAME = "one";
42 private static final String TWO_TWO_NAME = "two";
44 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(
45 TestModel.OUTER_LIST_PATH)
46 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
49 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(
50 TestModel.OUTER_LIST_PATH)
51 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
54 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
55 .node(TestModel.INNER_LIST_QNAME)
56 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
59 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
60 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
61 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
62 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME))
66 private RootApplyStrategy rootOper;
69 public void prepare() {
70 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
71 DataTreeConfiguration.DEFAULT_OPERATIONAL));
74 public NormalizedNode<?, ?> createDocumentOne() {
75 return ImmutableContainerNodeBuilder
77 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(SCHEMA_CONTEXT.getQName()))
78 .withChild(createTestContainer()).build();
83 public void findNodeTestNodeFound() {
84 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
85 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
86 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
87 final Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
88 assertPresentAndType(node, TreeNode.class);
92 public void findNodeTestNodeNotFound() {
93 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
94 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
95 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
96 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
97 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
99 final Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
100 assertFalse(node.isPresent());
104 public void findNodeCheckedTestNodeFound() {
105 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
106 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
107 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
108 TreeNode foundNode = null;
110 foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
111 } catch (final IllegalArgumentException e) {
112 fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
114 assertNotNull(foundNode);
118 public void findNodeCheckedTestNodeNotFound() {
119 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
120 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
121 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
122 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
123 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
126 StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
127 fail("Illegal argument exception should have been thrown");
128 } catch (final IllegalArgumentException e) {
129 LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
134 public void findClosestOrFirstMatchTestNodeExists() {
135 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
136 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
137 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
138 final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
139 assertPresentAndType(expectedNode, TreeNode.class);
141 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
142 assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
146 public void findClosestOrFirstMatchTestNodeDoesNotExist() {
147 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
148 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
149 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
150 final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
151 .node(TestModel.INNER_LIST_QNAME)
153 final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
154 .node(TestModel.INNER_LIST_QNAME)
155 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
157 final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
158 assertPresentAndType(expectedNode, TreeNode.class);
160 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
162 assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
166 public void getChildTestChildFound() {
167 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
168 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
169 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
170 final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
171 TestModel.TEST_PATH.getLastPathArgument());
172 assertPresentAndType(node, TreeNode.class);
176 public void getChildTestChildNotFound() {
177 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
178 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
179 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
180 final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
181 TestModel.OUTER_LIST_PATH.getLastPathArgument());
182 assertFalse(node.isPresent());
185 private static ContainerNode createTestContainer() {
186 return ImmutableContainerNodeBuilder
188 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
190 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
191 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
192 .withChild(BAR_NODE).build()).build();
195 private static <T extends TreeNode> T assertPresentAndType(final Optional<TreeNode> potential,
196 final Class<T> type) {
197 assertNotNull(potential);
198 assertTrue(potential.isPresent());
199 assertTrue(type.isInstance(potential.get()));
200 return type.cast(potential.get());
203 private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
204 assertEquals(expected.getIdentifier(), actual.getIdentifier());
205 assertEquals(expected.getVersion(), actual.getVersion());
206 assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
207 assertEquals(expected.getData(), actual.getData());
208 assertEquals(expected.toString(), actual.toString());