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.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class StoreTreeNodesTest {
38 private static final Logger LOG = LoggerFactory.getLogger(StoreTreeNodesTest.class);
40 private static final Short ONE_ID = 1;
41 private static final Short TWO_ID = 2;
42 private static final String TWO_ONE_NAME = "one";
43 private static final String TWO_TWO_NAME = "two";
45 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(
46 TestModel.OUTER_LIST_PATH)
47 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
50 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(
51 TestModel.OUTER_LIST_PATH)
52 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
55 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
56 .node(TestModel.INNER_LIST_QNAME)
57 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
60 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
61 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
62 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
63 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME))
67 private SchemaContext schemaContext;
68 private RootModificationApplyOperation rootOper;
71 public void prepare() {
72 schemaContext = TestModel.createTestContext();
73 assertNotNull("Schema context must not be null.", schemaContext);
74 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext,
75 DataTreeConfiguration.DEFAULT_OPERATIONAL));
78 public NormalizedNode<?, ?> createDocumentOne() {
79 return ImmutableContainerNodeBuilder
81 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
82 .withChild(createTestContainer()).build();
87 public void findNodeTestNodeFound() {
88 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
89 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
90 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
91 final Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
92 assertPresentAndType(node, TreeNode.class);
96 public void findNodeTestNodeNotFound() {
97 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
98 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
99 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
100 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
101 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
103 final Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
104 assertFalse(node.isPresent());
108 public void findNodeCheckedTestNodeFound() {
109 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
110 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
111 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
112 TreeNode foundNode = null;
114 foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
115 } catch (final IllegalArgumentException e) {
116 fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
118 assertNotNull(foundNode);
122 public void findNodeCheckedTestNodeNotFound() {
123 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
124 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
125 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
126 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
127 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
130 StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
131 fail("Illegal argument exception should have been thrown");
132 } catch (final IllegalArgumentException e) {
133 LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
138 public void findClosestOrFirstMatchTestNodeExists() {
139 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
140 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
141 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
142 final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
143 assertPresentAndType(expectedNode, TreeNode.class);
145 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
146 assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
150 public void findClosestOrFirstMatchTestNodeDoesNotExist() {
151 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
152 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
153 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
154 final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
155 .node(TestModel.INNER_LIST_QNAME)
157 final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
158 .node(TestModel.INNER_LIST_QNAME)
159 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
161 final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
162 assertPresentAndType(expectedNode, TreeNode.class);
164 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
166 assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
170 public void getChildTestChildFound() {
171 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
172 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
173 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
174 final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
175 TestModel.TEST_PATH.getLastPathArgument());
176 assertPresentAndType(node, TreeNode.class);
180 public void getChildTestChildNotFound() {
181 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
182 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
183 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
184 final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
185 TestModel.OUTER_LIST_PATH.getLastPathArgument());
186 assertFalse(node.isPresent());
189 private static ContainerNode createTestContainer() {
190 return ImmutableContainerNodeBuilder
192 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
194 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
195 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
196 .withChild(BAR_NODE).build()).build();
199 private static <T extends TreeNode> T assertPresentAndType(final Optional<TreeNode> potential,
200 final Class<T> type) {
201 assertNotNull(potential);
202 assertTrue(potential.isPresent());
203 assertTrue(type.isInstance(potential.get()));
204 return type.cast(potential.get());
207 private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
208 assertEquals(expected.getIdentifier(), actual.getIdentifier());
209 assertEquals(expected.getVersion(), actual.getVersion());
210 assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
211 assertEquals(expected.getData(), actual.getData());
212 assertEquals(expected.toString(), actual.toString());