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