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 com.google.common.base.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.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class StoreTreeNodesTest {
39 private static final Logger LOG = LoggerFactory.getLogger(StoreTreeNodesTest.class);
41 private static final Short ONE_ID = 1;
42 private static final Short TWO_ID = 2;
43 private static final String TWO_ONE_NAME = "one";
44 private static final String TWO_TWO_NAME = "two";
46 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(
47 TestModel.OUTER_LIST_PATH)
48 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
51 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(
52 TestModel.OUTER_LIST_PATH)
53 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
56 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
57 .node(TestModel.INNER_LIST_QNAME)
58 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
61 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
62 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
63 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
64 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME))
68 private SchemaContext schemaContext;
69 private RootModificationApplyOperation rootOper;
72 public void prepare() throws ReactorException {
73 schemaContext = TestModel.createTestContext();
74 assertNotNull("Schema context must not be null.", schemaContext);
75 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext,
76 DataTreeConfiguration.DEFAULT_OPERATIONAL));
79 public NormalizedNode<?, ?> createDocumentOne() {
80 return ImmutableContainerNodeBuilder
82 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
83 .withChild(createTestContainer()).build();
87 private static ContainerNode createTestContainer() {
88 return ImmutableContainerNodeBuilder
90 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
92 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
93 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
94 .withChild(BAR_NODE).build()).build();
97 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
98 assertNotNull(potential);
99 assertTrue(potential.isPresent());
100 assertTrue(type.isInstance(potential.get()));
101 return type.cast(potential.get());
105 public void findNodeTestNodeFound() {
106 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
107 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
108 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
109 final Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
110 assertPresentAndType(node, TreeNode.class);
114 public void findNodeTestNodeNotFound() {
115 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
116 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
117 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
118 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
119 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
121 final Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
122 assertFalse(node.isPresent());
126 public void findNodeCheckedTestNodeFound() {
127 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
128 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
129 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
130 TreeNode foundNode = null;
132 foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
133 } catch (final IllegalArgumentException e) {
134 fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
136 assertNotNull(foundNode);
140 public void findNodeCheckedTestNodeNotFound() {
141 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
142 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
143 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
144 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
145 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
148 StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
149 fail("Illegal argument exception should have been thrown");
150 } catch (final IllegalArgumentException e) {
151 LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
156 public void findClosestOrFirstMatchTestNodeExists() {
157 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
158 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
159 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
160 final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
161 assertPresentAndType(expectedNode, TreeNode.class);
162 final Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
164 assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
168 public void findClosestOrFirstMatchTestNodeDoesNotExist() {
169 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
170 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
171 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
172 final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
173 .node(TestModel.INNER_LIST_QNAME)
175 final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
176 .node(TestModel.INNER_LIST_QNAME) //
177 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three") //
179 final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
180 assertPresentAndType(expectedNode, TreeNode.class);
181 final Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
183 assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
187 public void getChildTestChildFound() {
188 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
189 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
190 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
191 final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.fromNullable(rootNode),
192 TestModel.TEST_PATH.getLastPathArgument());
193 assertPresentAndType(node, TreeNode.class);
197 public void getChildTestChildNotFound() {
198 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
199 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
200 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
201 final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.fromNullable(rootNode),
202 TestModel.OUTER_LIST_PATH.getLastPathArgument());
203 assertFalse(node.isPresent());