Mark methods as static
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / StoreTreeNodesTest.java
1 /*
2  * Copyright (c) 2014 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 import com.google.common.base.Optional;
19 import java.util.Map;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class StoreTreeNodesTest {
36     private static final Logger LOG = LoggerFactory.getLogger(StoreTreeNodesTest.class);
37
38     private static final Short ONE_ID = 1;
39     private static final Short TWO_ID = 2;
40     private static final String TWO_ONE_NAME = "one";
41     private static final String TWO_TWO_NAME = "two";
42
43     private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
44             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
45             .build();
46
47     private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
48             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
49             .build();
50
51     private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
52             .node(TestModel.INNER_LIST_QNAME) //
53             .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
54             .build();
55
56     private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
57             .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
58                     .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
59                     .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)) //
60                     .build()) //
61                     .build();
62
63     private SchemaContext schemaContext;
64     private RootModificationApplyOperation rootOper;
65
66     @Before
67     public void prepare() {
68         schemaContext = TestModel.createTestContext();
69         assertNotNull("Schema context must not be null.", schemaContext);
70         rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext));
71     }
72
73     public NormalizedNode<?, ?> createDocumentOne() {
74         return ImmutableContainerNodeBuilder
75                 .create()
76                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
77                 .withChild(createTestContainer()).build();
78
79     }
80
81     private static ContainerNode createTestContainer() {
82         return ImmutableContainerNodeBuilder
83                 .create()
84                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
85                 .withChild(
86                         mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
87                         .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
88                         .withChild(BAR_NODE).build()).build();
89     }
90
91     private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
92         assertNotNull(potential);
93         assertTrue(potential.isPresent());
94         assertTrue(type.isInstance(potential.get()));
95         return type.cast(potential.get());
96     }
97
98     @Test
99     public void findNodeTestNodeFound() {
100         InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
101                 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
102         TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
103         Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
104         assertPresentAndType(node, TreeNode.class);
105     }
106
107     @Test
108     public void findNodeTestNodeNotFound() {
109         InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
110                 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
111         TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
112         final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
113                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
114                 .build();
115         Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
116         assertFalse(node.isPresent());
117     }
118
119     @Test
120     public void findNodeCheckedTestNodeFound() {
121         InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
122                 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
123         TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
124         TreeNode foundNode = null;
125         try {
126             foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
127         } catch (IllegalArgumentException e) {
128             fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
129         }
130         assertNotNull(foundNode);
131     }
132
133     @Test
134     public void findNodeCheckedTestNodeNotFound() {
135         InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
136                 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
137         TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
138         final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
139                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
140                 .build();
141         try {
142             StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
143             fail("Illegal argument exception should have been thrown");
144         } catch (IllegalArgumentException e) {
145             LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
146         }
147     }
148
149     @Test
150     public void findClosestOrFirstMatchTestNodeExists() {
151         InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
152                 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
153         TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
154         Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
155         assertPresentAndType(expectedNode, TreeNode.class);
156         Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
157         assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
158     }
159
160     @Test
161     public void findClosestOrFirstMatchTestNodeDoesNotExist() {
162         InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
163                 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
164         TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
165         final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
166                 .node(TestModel.INNER_LIST_QNAME)
167                 .build();
168         final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
169                 .node(TestModel.INNER_LIST_QNAME) //
170                 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three") //
171                 .build();
172         Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
173         assertPresentAndType(expectedNode, TreeNode.class);
174         Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, twoTwoInvalidPath);
175         assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
176     }
177
178     @Test
179     public void getChildTestChildFound() {
180         InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
181                 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
182         TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
183         Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.fromNullable(rootNode),
184                 TestModel.TEST_PATH.getLastPathArgument());
185         assertPresentAndType(node, TreeNode.class);
186     }
187
188     @Test
189     public void getChildTestChildNotFound() {
190         InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
191                 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
192         TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
193         Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.fromNullable(rootNode),
194                 TestModel.OUTER_LIST_PATH.getLastPathArgument());
195         assertFalse(node.isPresent());
196     }
197 }