2ee3a2965fa57bef9633fc5a967a0697761d23c3
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / StoreTreeNodesTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
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;
18
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.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class StoreTreeNodesTest extends AbstractTestModelTest {
37     private static final Logger LOG = LoggerFactory.getLogger(StoreTreeNodesTest.class);
38
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";
43
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)
47             .build();
48
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)
52             .build();
53
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)
57             .build();
58
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))
63                     .build())
64                     .build();
65
66     private RootApplyStrategy rootOper;
67
68     @Before
69     public void prepare() {
70         rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
71             DataTreeConfiguration.DEFAULT_OPERATIONAL));
72     }
73
74     public NormalizedNode<?, ?> createDocumentOne() {
75         return ImmutableContainerNodeBuilder
76                 .create()
77                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(SCHEMA_CONTEXT.getQName()))
78                 .withChild(createTestContainer()).build();
79
80     }
81
82     @Test
83     public void findNodeTestNodeFound() {
84         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
85                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
86         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
87         final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
88         assertPresentAndType(node, TreeNode.class);
89     }
90
91     @Test
92     public void findNodeTestNodeNotFound() {
93         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
94                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
95         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
96         final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
97                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
98                 .build();
99         final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
100         assertFalse(node.isPresent());
101     }
102
103     @Test
104     public void findNodeCheckedTestNodeFound() {
105         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
106                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
107         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
108         TreeNode foundNode = null;
109         try {
110             foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
111         } catch (final IllegalArgumentException e) {
112             fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
113         }
114         assertNotNull(foundNode);
115     }
116
117     @Test
118     public void findNodeCheckedTestNodeNotFound() {
119         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
120                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
121         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
122         final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
123                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
124                 .build();
125         try {
126             StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
127             fail("Illegal argument exception should have been thrown");
128         } catch (final IllegalArgumentException e) {
129             LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
130         }
131     }
132
133     @Test
134     public void findClosestOrFirstMatchTestNodeExists() {
135         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
136                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
137         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
138         final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
139         assertPresentAndType(expectedNode, TreeNode.class);
140
141         final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
142         assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
143     }
144
145     @Test
146     public void findClosestOrFirstMatchTestNodeDoesNotExist() {
147         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
148                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
149         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
150         final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
151                 .node(TestModel.INNER_LIST_QNAME)
152                 .build();
153         final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
154                 .node(TestModel.INNER_LIST_QNAME)
155                 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
156                 .build();
157         final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
158         assertPresentAndType(expectedNode, TreeNode.class);
159
160         final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
161             twoTwoInvalidPath);
162         assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
163     }
164
165     @Test
166     public void getChildTestChildFound() {
167         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
168                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
169         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
170         final Optional<? extends TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
171                 TestModel.TEST_PATH.getLastPathArgument());
172         assertPresentAndType(node, TreeNode.class);
173     }
174
175     @Test
176     public void getChildTestChildNotFound() {
177         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
178                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
179         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
180         final Optional<? extends TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
181                 TestModel.OUTER_LIST_PATH.getLastPathArgument());
182         assertFalse(node.isPresent());
183     }
184
185     private static ContainerNode createTestContainer() {
186         return ImmutableContainerNodeBuilder
187                 .create()
188                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
189                 .withChild(
190                         mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
191                         .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
192                         .withChild(BAR_NODE).build()).build();
193     }
194
195     private static <T extends TreeNode> T assertPresentAndType(final Optional<? extends TreeNode> potential,
196             final Class<T> type) {
197         assertNotNull(potential);
198         assertTrue(potential.isPresent());
199         assertTrue(type.isInstance(potential.get()));
200         return type.cast(potential.get());
201     }
202
203     private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
204         assertEquals(expected.getIdentifier(), actual.getIdentifier());
205         assertEquals(expected.getVersion(), actual.getVersion());
206         assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
207         assertEquals(expected.getData(), actual.getData());
208         assertEquals(expected.toString(), actual.toString());
209     }
210 }