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.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.tree.StoreTreeNodes;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
30 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
31 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class StoreTreeNodesTest extends AbstractTestModelTest {
37 private static final Logger LOG = LoggerFactory.getLogger(StoreTreeNodesTest.class);
39 private static final Short ONE_ID = 1;
40 private static final Short TWO_ID = 2;
41 private static final String TWO_ONE_NAME = "one";
42 private static final String TWO_TWO_NAME = "two";
44 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(
45 TestModel.OUTER_LIST_PATH)
46 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
49 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(
50 TestModel.OUTER_LIST_PATH)
51 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
54 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
55 .node(TestModel.INNER_LIST_QNAME)
56 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
59 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
60 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
61 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
62 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME))
66 private RootApplyStrategy rootOper;
69 public void prepare() throws ExcludedDataSchemaNodeException {
70 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
71 DataTreeConfiguration.DEFAULT_OPERATIONAL));
74 public static ContainerNode createDocumentOne() {
75 return Builders.containerBuilder()
76 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
77 .withChild(createTestContainer())
82 public void findNodeTestNodeFound() {
83 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
84 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
85 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
86 final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
87 assertPresentAndType(node, TreeNode.class);
91 public void findNodeTestNodeNotFound() {
92 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
93 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
94 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
95 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
96 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
98 final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
99 assertFalse(node.isPresent());
103 public void findNodeCheckedTestNodeFound() {
104 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
105 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
106 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
107 TreeNode foundNode = null;
109 foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
110 } catch (final IllegalArgumentException e) {
111 fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
113 assertNotNull(foundNode);
117 public void findNodeCheckedTestNodeNotFound() {
118 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
119 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
120 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
121 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
122 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
125 StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
126 fail("Illegal argument exception should have been thrown");
127 } catch (final IllegalArgumentException e) {
128 LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
133 public void findClosestOrFirstMatchTestNodeExists() {
134 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
135 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
136 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
137 final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
138 assertPresentAndType(expectedNode, TreeNode.class);
140 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
141 assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
145 public void findClosestOrFirstMatchTestNodeDoesNotExist() {
146 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
147 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
148 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
149 final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
150 .node(TestModel.INNER_LIST_QNAME)
152 final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
153 .node(TestModel.INNER_LIST_QNAME)
154 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
156 final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
157 assertPresentAndType(expectedNode, TreeNode.class);
159 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
161 assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
164 private static ContainerNode createTestContainer() {
165 return Builders.containerBuilder()
166 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
167 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
168 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, 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.getIdentifier(), actual.getIdentifier());
184 assertEquals(expected.getVersion(), actual.getVersion());
185 assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
186 assertEquals(expected.getData(), actual.getData());
187 assertEquals(expected.toString(), actual.toString());