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.tree.impl;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import java.util.Optional;
17 import org.junit.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
25 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
27 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
28 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 class StoreTreeNodesTest extends AbstractTestModelTest {
32 private static final Short ONE_ID = 1;
33 private static final Short TWO_ID = 2;
34 private static final String TWO_ONE_NAME = "one";
35 private static final String TWO_TWO_NAME = "two";
37 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(
38 TestModel.OUTER_LIST_PATH)
39 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
42 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(
43 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 = ImmutableNodes.newMapEntryBuilder()
53 .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID))
54 .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, TWO_ID))
55 .withChild(ImmutableNodes.newSystemMapBuilder()
56 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
57 .withChild(ImmutableNodes.newMapEntryBuilder()
59 NodeIdentifierWithPredicates.of(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
60 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, TWO_ONE_NAME))
62 .withChild(ImmutableNodes.newMapEntryBuilder()
64 NodeIdentifierWithPredicates.of(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME))
65 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, TWO_TWO_NAME))
70 private RootApplyStrategy rootOper;
73 void prepare() throws ExcludedDataSchemaNodeException {
74 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
75 DataTreeConfiguration.DEFAULT_OPERATIONAL));
78 public static ContainerNode createDocumentOne() {
79 return ImmutableNodes.newContainerBuilder()
80 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
81 .withChild(createTestContainer())
86 void findNodeTestNodeFound() {
87 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
88 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
89 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
90 final var node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
91 assertPresentAndType(node, TreeNode.class);
95 void findNodeTestNodeNotFound() {
96 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
97 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
98 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
99 final var outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
100 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
102 final var node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
103 assertFalse(node.isPresent());
107 void findNodeCheckedTestNodeFound() {
108 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
109 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
110 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
111 TreeNode foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
112 assertNotNull(foundNode);
116 void findNodeCheckedTestNodeNotFound() {
117 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
118 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
119 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
120 final var outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
121 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
123 assertThrows(IllegalArgumentException.class,
124 () -> StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath));
128 void findClosestOrFirstMatchTestNodeExists() {
129 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
130 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
131 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
132 final var expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
133 assertPresentAndType(expectedNode, TreeNode.class);
135 final var actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
136 assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
140 void findClosestOrFirstMatchTestNodeDoesNotExist() {
141 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
142 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
143 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
144 final var outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
145 .node(TestModel.INNER_LIST_QNAME)
147 final var twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
148 .node(TestModel.INNER_LIST_QNAME)
149 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
151 final var expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
152 assertPresentAndType(expectedNode, TreeNode.class);
154 final var actualNode = StoreTreeNodes.findClosest(rootNode,
156 assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
159 private static ContainerNode createTestContainer() {
160 return ImmutableNodes.newContainerBuilder()
161 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
162 .withChild(ImmutableNodes.newSystemMapBuilder()
163 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
164 .withChild(ImmutableNodes.newMapEntryBuilder()
166 NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
167 .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, ONE_ID))
174 private static <T extends TreeNode> T assertPresentAndType(final Optional<? extends TreeNode> potential,
175 final Class<T> type) {
176 assertNotNull(potential);
177 assertTrue(potential.isPresent());
178 assertTrue(type.isInstance(potential.orElseThrow()));
179 return type.cast(potential.orElseThrow());
182 private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
183 assertEquals(expected.version(), actual.version());
184 assertEquals(expected.subtreeVersion(), actual.subtreeVersion());
185 assertEquals(expected.data(), actual.data());
186 assertEquals(expected.toString(), actual.toString());