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.assertTrue;
14 import static org.junit.jupiter.api.Assertions.fail;
15 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
18 import java.util.Optional;
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Test;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
26 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
28 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
29 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class StoreTreeNodesTest extends AbstractTestModelTest {
35 private static final Logger LOG = LoggerFactory.getLogger(StoreTreeNodesTest.class);
37 private static final Short ONE_ID = 1;
38 private static final Short TWO_ID = 2;
39 private static final String TWO_ONE_NAME = "one";
40 private static final String TWO_TWO_NAME = "two";
42 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(
43 TestModel.OUTER_LIST_PATH)
44 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
47 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(
48 TestModel.OUTER_LIST_PATH)
49 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
52 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
53 .node(TestModel.INNER_LIST_QNAME)
54 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
57 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
58 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
59 .withChild(ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
60 .withChild(ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME))
64 private RootApplyStrategy rootOper;
67 void prepare() throws ExcludedDataSchemaNodeException {
68 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
69 DataTreeConfiguration.DEFAULT_OPERATIONAL));
72 public static ContainerNode createDocumentOne() {
73 return ImmutableNodes.newContainerBuilder()
74 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
75 .withChild(createTestContainer())
80 void findNodeTestNodeFound() {
81 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
82 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
83 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
84 final var node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
85 assertPresentAndType(node, TreeNode.class);
89 void findNodeTestNodeNotFound() {
90 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
91 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
92 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
93 final var outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
94 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
96 final var node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
97 assertFalse(node.isPresent());
101 void findNodeCheckedTestNodeFound() {
102 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
103 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
104 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
105 TreeNode foundNode = null;
107 foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
108 } catch (final IllegalArgumentException e) {
109 fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
111 assertNotNull(foundNode);
115 void findNodeCheckedTestNodeNotFound() {
116 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
117 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
118 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
119 final var outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
120 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
123 StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
124 fail("Illegal argument exception should have been thrown");
125 } catch (final IllegalArgumentException e) {
126 LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
131 void findClosestOrFirstMatchTestNodeExists() {
132 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
133 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
134 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
135 final var expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
136 assertPresentAndType(expectedNode, TreeNode.class);
138 final var actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
139 assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
143 void findClosestOrFirstMatchTestNodeDoesNotExist() {
144 final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
145 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
146 final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
147 final var outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
148 .node(TestModel.INNER_LIST_QNAME)
150 final var twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
151 .node(TestModel.INNER_LIST_QNAME)
152 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
154 final var expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
155 assertPresentAndType(expectedNode, TreeNode.class);
157 final var actualNode = StoreTreeNodes.findClosest(rootNode,
159 assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
162 private static ContainerNode createTestContainer() {
163 return ImmutableNodes.newContainerBuilder()
164 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
165 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
166 .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
172 private static <T extends TreeNode> T assertPresentAndType(final Optional<? extends TreeNode> potential,
173 final Class<T> type) {
174 assertNotNull(potential);
175 assertTrue(potential.isPresent());
176 assertTrue(type.isInstance(potential.orElseThrow()));
177 return type.cast(potential.orElseThrow());
180 private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
181 assertEquals(expected.getIdentifier(), actual.getIdentifier());
182 assertEquals(expected.getVersion(), actual.getVersion());
183 assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
184 assertEquals(expected.getData(), actual.getData());
185 assertEquals(expected.toString(), actual.toString());