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.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.tree.api.DataTreeConfiguration;
31 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
32 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class StoreTreeNodesTest extends AbstractTestModelTest {
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 RootApplyStrategy rootOper;
70 public void prepare() throws ExcludedDataSchemaNodeException {
71 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
72 DataTreeConfiguration.DEFAULT_OPERATIONAL));
75 public NormalizedNode createDocumentOne() {
76 return ImmutableContainerNodeBuilder
78 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
79 .withChild(createTestContainer()).build();
84 public void findNodeTestNodeFound() {
85 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
86 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
87 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
88 final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
89 assertPresentAndType(node, TreeNode.class);
93 public void findNodeTestNodeNotFound() {
94 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
95 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
96 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
97 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
98 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
100 final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
101 assertFalse(node.isPresent());
105 public void findNodeCheckedTestNodeFound() {
106 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
107 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
108 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
109 TreeNode foundNode = null;
111 foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
112 } catch (final IllegalArgumentException e) {
113 fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
115 assertNotNull(foundNode);
119 public void findNodeCheckedTestNodeNotFound() {
120 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
121 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
122 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
123 final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
124 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
127 StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
128 fail("Illegal argument exception should have been thrown");
129 } catch (final IllegalArgumentException e) {
130 LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
135 public void findClosestOrFirstMatchTestNodeExists() {
136 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
137 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
138 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
139 final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
140 assertPresentAndType(expectedNode, TreeNode.class);
142 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
143 assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
147 public void findClosestOrFirstMatchTestNodeDoesNotExist() {
148 final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
149 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
150 final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
151 final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
152 .node(TestModel.INNER_LIST_QNAME)
154 final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
155 .node(TestModel.INNER_LIST_QNAME)
156 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
158 final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
159 assertPresentAndType(expectedNode, TreeNode.class);
161 final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
163 assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
166 private static ContainerNode createTestContainer() {
167 return ImmutableContainerNodeBuilder
169 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
171 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
172 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
173 .withChild(BAR_NODE).build()).build();
176 private static <T extends TreeNode> T assertPresentAndType(final Optional<? extends TreeNode> potential,
177 final Class<T> type) {
178 assertNotNull(potential);
179 assertTrue(potential.isPresent());
180 assertTrue(type.isInstance(potential.get()));
181 return type.cast(potential.get());
184 private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
185 assertEquals(expected.getIdentifier(), actual.getIdentifier());
186 assertEquals(expected.getVersion(), actual.getVersion());
187 assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
188 assertEquals(expected.getData(), actual.getData());
189 assertEquals(expected.toString(), actual.toString());