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