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