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