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.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
31 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNodeFactory;
32 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
33 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class StoreTreeNodesTest extends AbstractTestModelTest {
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 RootApplyStrategy rootOper;
71 public void prepare() throws ExcludedDataSchemaNodeException {
72 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
73 DataTreeConfiguration.DEFAULT_OPERATIONAL));
76 public NormalizedNode createDocumentOne() {
77 return ImmutableContainerNodeBuilder
79 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
80 .withChild(createTestContainer()).build();
85 public void findNodeTestNodeFound() {
86 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
87 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
88 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
89 final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
90 assertPresentAndType(node, TreeNode.class);
94 public void findNodeTestNodeNotFound() {
95 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
96 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
97 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
98 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
99 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
101 final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
102 assertFalse(node.isPresent());
106 public void findNodeCheckedTestNodeFound() {
107 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
108 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
109 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
110 TreeNode foundNode = null;
112 foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
113 } catch (final IllegalArgumentException e) {
114 fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
116 assertNotNull(foundNode);
120 public void findNodeCheckedTestNodeNotFound() {
121 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
122 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
123 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
124 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
125 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
128 StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
129 fail("Illegal argument exception should have been thrown");
130 } catch (final IllegalArgumentException e) {
131 LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
136 public void findClosestOrFirstMatchTestNodeExists() {
137 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
138 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
139 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
140 final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
141 assertPresentAndType(expectedNode, TreeNode.class);
143 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
144 assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
148 public void findClosestOrFirstMatchTestNodeDoesNotExist() {
149 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
150 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
151 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
152 final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
153 .node(TestModel.INNER_LIST_QNAME)
155 final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
156 .node(TestModel.INNER_LIST_QNAME)
157 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
159 final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
160 assertPresentAndType(expectedNode, TreeNode.class);
162 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
164 assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
167 private static ContainerNode createTestContainer() {
168 return ImmutableContainerNodeBuilder
170 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
172 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
173 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
174 .withChild(BAR_NODE).build()).build();
177 private static <T extends TreeNode> T assertPresentAndType(final Optional<? extends TreeNode> potential,
178 final Class<T> type) {
179 assertNotNull(potential);
180 assertTrue(potential.isPresent());
181 assertTrue(type.isInstance(potential.get()));
182 return type.cast(potential.get());
185 private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
186 assertEquals(expected.getIdentifier(), actual.getIdentifier());
187 assertEquals(expected.getVersion(), actual.getVersion());
188 assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
189 assertEquals(expected.getData(), actual.getData());
190 assertEquals(expected.toString(), actual.toString());