Rework NormalizedNode type hierarchy
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / BuilderTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.yangtools.yang.data.impl.schema;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.Mockito.mock;
15
16 import java.io.File;
17 import java.net.URISyntaxException;
18 import java.util.HashMap;
19 import java.util.LinkedList;
20 import java.util.Map;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyxmlSchemaNode;
24 import org.opendaylight.yangtools.util.UnmodifiableCollection;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
29 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeSchemaAwareBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeSchemaAwareBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeSchemaAwareBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
53 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserLeafSetNodeBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserLeafSetNodeSchemaAwareBuilder;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeBuilder;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeSchemaAwareBuilder;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableYangModeledAnyXmlNodeBuilder;
61 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
64 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
65 import org.opendaylight.yangtools.yang.model.api.Module;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
68
69 public class BuilderTest {
70     private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01",
71         "root-container");
72     private static final QName LIST_MAIN = QName.create(ROOT_CONTAINER, "list-ordered-by-user-with-key");
73     private static final QName LEAF_LIST_MAIN = QName.create(ROOT_CONTAINER, "leaf-list-ordered-by-user");
74     private static final QName LIST_MAIN_CHILD_QNAME_1 = QName.create(ROOT_CONTAINER, "leaf-a");
75     private static final NodeIdentifier NODE_IDENTIFIER_LIST = NodeIdentifier.create(LIST_MAIN);
76     private static final NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = NodeIdentifier.create(LEAF_LIST_MAIN);
77     private static final NodeIdentifier NODE_IDENTIFIER_LEAF = NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
78     private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
79             1);
80     private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
81             2);
82     private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
83             3);
84     private static final int SIZE = 3;
85     private static final NodeWithValue<String> BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
86     private static final LeafSetEntryNode<String> LEAF_SET_ENTRY_NODE =
87             ImmutableLeafSetEntryNodeBuilder.<String>create()
88             .withNodeIdentifier(BAR_PATH)
89             .withValue("bar")
90             .build();
91     private ListSchemaNode list;
92     private LeafListSchemaNode leafList;
93
94     @Before
95     public void setup() throws URISyntaxException {
96         final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
97                 .toURI());
98         final SchemaContext schema = YangParserTestUtils.parseYangFiles(leafRefTestYang);
99         final Module module = schema.getModules().iterator().next();
100         final DataSchemaNode root = module.findDataChildByName(ROOT_CONTAINER).get();
101         list = (ListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LIST_MAIN).get();
102         leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LEAF_LIST_MAIN).get();
103     }
104
105     @Test
106     public void immutableOrderedMapBuilderTest() {
107         final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
108         mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
109         final Map<QName, Object> keys = new HashMap<>();
110         keys.put(LIST_MAIN_CHILD_QNAME_1, 1);
111         final NodeIdentifierWithPredicates mapEntryPath = NodeIdentifierWithPredicates.of(LIST_MAIN, keys);
112         final UserMapNode orderedMapNodeCreateNull = ImmutableUserMapNodeBuilder.create()
113                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
114                 .withChild(LIST_MAIN_CHILD_1)
115                 .addChild(LIST_MAIN_CHILD_2)
116                 .withValue(mapEntryNodeColl)
117                 .build();
118         final UserMapNode orderedMapNodeCreateSize = ImmutableUserMapNodeBuilder.create(SIZE)
119                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
120                 .build();
121         final UserMapNode orderedMapNodeCreateNode = ImmutableUserMapNodeBuilder.create(orderedMapNodeCreateNull)
122                 .removeChild(mapEntryPath)
123                 .build();
124         final UserMapNode orderedMapNodeSchemaAware = ImmutableUserMapNodeSchemaAwareBuilder.create(list)
125                 .withChild(LIST_MAIN_CHILD_1)
126                 .build();
127         final UserMapNode orderedMapNodeSchemaAwareMapNodeConst = ImmutableUserMapNodeSchemaAwareBuilder.create(
128                 list, getImmutableUserMapNode())
129                 .build();
130
131         assertNotNull(Builders.orderedMapBuilder(list));
132         assertEquals(SIZE, orderedMapNodeCreateNull.size());
133         assertEquals(orderedMapNodeCreateNode.size(), orderedMapNodeCreateNull.size() - 1);
134         assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
135         assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.getChild(0));
136         assertEquals(SIZE, orderedMapNodeCreateNull.size());
137         assertEquals(orderedMapNodeSchemaAware.getChild(0), orderedMapNodeSchemaAwareMapNodeConst.getChild(0));
138     }
139
140     @Test
141     public void immutableUserLeafSetNodeBuilderTest() {
142         final UserLeafSetNode<String> orderedLeafSet = ImmutableUserLeafSetNodeBuilder.<String>create()
143                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
144                 .withChild(LEAF_SET_ENTRY_NODE)
145                 .withChildValue("baz")
146                 .removeChild(BAR_PATH)
147                 .build();
148         final LinkedList<LeafSetNode<?>> mapEntryNodeColl = new LinkedList<>();
149         mapEntryNodeColl.add(orderedLeafSet);
150         final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.body();
151         final NormalizedNode orderedMapNodeSchemaAware = ImmutableUserLeafSetNodeSchemaAwareBuilder.create(
152             leafList).withChildValue("baz").build();
153         final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
154                 (UnmodifiableCollection<?>) orderedMapNodeSchemaAware.body();
155         final NormalizedNode orderedLeafSetShemaAware = ImmutableUserLeafSetNodeSchemaAwareBuilder.create(
156             leafList, (UserLeafSetNode<?>) orderedLeafSet).build();
157
158         assertNotNull(Builders.orderedLeafSetBuilder(leafList));
159         assertNotNull(Builders.anyXmlBuilder());
160         assertNotNull(orderedLeafSetShemaAware);
161         assertEquals(1, ((UserLeafSetNode<?>)orderedLeafSet).size());
162         assertEquals("baz", orderedLeafSet.getChild(0).body());
163         assertNull(orderedLeafSet.childByArg(BAR_PATH));
164         assertEquals(1, leafSetCollection.size());
165         assertEquals(1, SchemaAwareleafSetCollection.size());
166     }
167
168     @Test
169     public void immutableMapNodeBuilderTest() {
170         final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
171         mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
172         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> collectionNodeBuilder =
173             ImmutableMapNodeBuilder.create(1);
174         assertNotNull(collectionNodeBuilder);
175         collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
176         collectionNodeBuilder.withValue(mapEntryNodeColl);
177         final SystemMapNode mapNode = collectionNodeBuilder.build();
178         final SystemMapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list,
179             getImmutableMapNode()).build();
180         assertNotNull(mapNodeSchemaAware);
181         assertNotNull(Builders.mapBuilder(mapNode));
182     }
183
184     @Test
185     public void immutableUnkeyedListEntryNodeBuilderTest() {
186         final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
187                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
188                 .build();
189         final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
190                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
191                 .build();
192         final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
193                 .create(unkeyedListEntryNode).build();
194         assertEquals(unkeyedListEntryNode.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
195                 .getLocalName());
196         assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
197                 .getLocalName());
198     }
199
200     @Test
201     public void immutableUnkeyedListNodeBuilderTest() {
202         final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
203                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
204                 .build();
205         final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
206                 ImmutableUnkeyedListNodeBuilder.create();
207         final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
208                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
209                 .addChild(unkeyedListEntryNode)
210                 .build();
211         final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
212                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
213                 .build();
214         final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
215                 .build();
216         try {
217             unkeyedListNodeSize.getChild(1);
218         } catch (IndexOutOfBoundsException e) {
219             // Ignored on purpose
220         }
221
222         assertNotNull(unkeyedListNodeSize.body());
223         assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.getChild(0));
224         assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType()
225                 .getLocalName());
226         assertNotNull(unkeyedListNodeCreated);
227     }
228
229     @Test
230     public void immutableChoiceNodeBuilderTest() {
231         final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
232                 .build();
233         final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
234         assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
235     }
236
237
238     @Test(expected = NullPointerException.class)
239     public void immutableAugmentationNodeBuilderExceptionTest() {
240         ImmutableAugmentationNodeBuilder.create(1).build();
241     }
242
243     @Test(expected = NullPointerException.class)
244     public void immutableContainerNodeBuilderExceptionTest() {
245         final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
246                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
247                 .build();
248         assertNotNull(immutableContainerNode);
249         final ContainerSchemaNode containerSchemaNode = mock(ContainerSchemaNode.class);
250         ImmutableContainerNodeSchemaAwareBuilder.create(containerSchemaNode, immutableContainerNode)
251                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
252                 .build();
253     }
254
255     @Test(expected = NullPointerException.class)
256     public void immutableLeafSetNodeBuilderExceptionTest() {
257         final SystemLeafSetNode<Object> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
258                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
259         assertNotNull(leafSetNode);
260         ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
261     }
262
263     @Test(expected = UnsupportedOperationException.class)
264     public void immutableLeafSetEntryNodeSchemaAwareBuilderExceptionTest() {
265         final LeafListSchemaNode leafListSchemaNode = mock(LeafListSchemaNode.class);
266         ImmutableLeafSetEntryNodeSchemaAwareBuilder.create(leafListSchemaNode).withNodeIdentifier(BAR_PATH).build();
267     }
268
269     @Test(expected = NullPointerException.class)
270     public void immutableMapEntryNodeBuilderExceptionTest() {
271         ImmutableMapEntryNodeBuilder.create(1).build();
272     }
273
274     @Test(expected = NullPointerException.class)
275     public void immutableYangModeledAnyXmlNodeBuilderExceptionTest() {
276         ImmutableYangModeledAnyXmlNodeBuilder.create(mock(YangModeledAnyxmlSchemaNode.class), 1);
277     }
278
279     @Test(expected = UnsupportedOperationException.class)
280     public void immutableUnkeyedListNodeBuilderExceptionTest() {
281         ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF)
282                 .removeChild(NODE_IDENTIFIER_LIST).build();
283     }
284
285     @Test(expected = UnsupportedOperationException.class)
286     public void immutableMapNodeSchemaAwareExceptionTest() {
287         ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST)
288         .build();
289     }
290
291     @Test(expected = UnsupportedOperationException.class)
292     public void immutableOrderedMapSchemaAwareExceptionTest1() {
293         ImmutableUserMapNodeSchemaAwareBuilder.create(list).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
294     }
295
296     @Test(expected = UnsupportedOperationException.class)
297     public void immutableUserLeafSetNodeSchemaAwareExceptionTest1() {
298         ImmutableUserLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
299         .build();
300     }
301
302     private static SystemLeafSetNode<String> getImmutableLeafSetNode() {
303         final ListNodeBuilder<String, SystemLeafSetNode<String>> leafSetBuilder = Builders.leafSetBuilder();
304         leafSetBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
305         leafSetBuilder.addChild(LEAF_SET_ENTRY_NODE);
306         return leafSetBuilder.build();
307     }
308
309     private static SystemMapNode getImmutableMapNode() {
310         return ImmutableMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1)
311                 .build();
312     }
313
314     private static UserMapNode getImmutableUserMapNode() {
315         return ImmutableUserMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST)
316                 .withChild(LIST_MAIN_CHILD_1).build();
317     }
318 }