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