Split out OpenDaylight yang-ext metamodel
[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.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.MapNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeSchemaAwareBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeSchemaAwareBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeSchemaAwareBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
53 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeSchemaAwareBuilder;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeSchemaAwareBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableYangModeledAnyXmlNodeBuilder;
60 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
64 import org.opendaylight.yangtools.yang.model.api.Module;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
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 FileNotFoundException, ReactorException, 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.getDataChildByName(ROOT_CONTAINER);
101         list = (ListSchemaNode)((ContainerSchemaNode) root).getDataChildByName(LIST_MAIN);
102         leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).getDataChildByName(LEAF_LIST_MAIN);
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 = new NodeIdentifierWithPredicates(LIST_MAIN, keys);
112         final OrderedMapNode orderedMapNodeCreateNull = ImmutableOrderedMapNodeBuilder.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 OrderedMapNode orderedMapNodeCreateSize = ImmutableOrderedMapNodeBuilder.create(SIZE)
119                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
120                 .build();
121         final OrderedMapNode orderedMapNodeCreateNode = ImmutableOrderedMapNodeBuilder.create(orderedMapNodeCreateNull)
122                 .removeChild(mapEntryPath)
123                 .build();
124         final OrderedMapNode orderedMapNodeSchemaAware = ImmutableOrderedMapNodeSchemaAwareBuilder.create(list)
125                 .withChild(LIST_MAIN_CHILD_1)
126                 .build();
127         final OrderedMapNode orderedMapNodeSchemaAwareMapNodeConst = ImmutableOrderedMapNodeSchemaAwareBuilder.create(
128                 list, getImmutableOrderedMapNode())
129                 .build();
130
131         assertNotNull(Builders.orderedMapBuilder(list));
132         assertEquals(SIZE, orderedMapNodeCreateNull.getSize());
133         assertEquals(orderedMapNodeCreateNode.getSize(), orderedMapNodeCreateNull.getSize() - 1);
134         assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
135         assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.getChild(0));
136         assertEquals(SIZE, orderedMapNodeCreateNull.getValue().size());
137         assertEquals(orderedMapNodeSchemaAware.getChild(0), orderedMapNodeSchemaAwareMapNodeConst.getChild(0));
138     }
139
140     @Test
141     public void immutableOrderedLeafSetNodeBuilderTest() {
142         final NormalizedNode<?, ?> orderedLeafSet = ImmutableOrderedLeafSetNodeBuilder.<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((LeafSetNode<?>)orderedLeafSet);
150         final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.getValue();
151         final NormalizedNode<?, ?> orderedMapNodeSchemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(
152             leafList).withChildValue("baz").build();
153         final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
154                 (UnmodifiableCollection<?>)orderedMapNodeSchemaAware.getValue();
155         final NormalizedNode<?, ?> orderedLeafSetShemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(
156             leafList,(LeafSetNode<?>)orderedLeafSet).build();
157
158         assertNotNull(Builders.orderedLeafSetBuilder(leafList));
159         assertNotNull(Builders.anyXmlBuilder());
160         assertNotNull(orderedLeafSetShemaAware);
161         assertEquals(1, ((OrderedLeafSetNode<?>)orderedLeafSet).getSize());
162         assertEquals("baz", ((OrderedLeafSetNode<?>)orderedLeafSet).getChild(0).getValue());
163         assertNotNull(((OrderedLeafSetNode<?>)orderedLeafSet).getChild(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, MapNode> collectionNodeBuilder = ImmutableMapNodeBuilder.create(1);
173         assertNotNull(collectionNodeBuilder);
174         collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
175         collectionNodeBuilder.withValue(mapEntryNodeColl);
176         final MapNode mapNode = collectionNodeBuilder.build();
177         final MapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode())
178                 .build();
179         assertNotNull(mapNodeSchemaAware);
180         assertNotNull(Builders.mapBuilder(mapNode));
181     }
182
183     @Test
184     public void immutableUnkeyedListEntryNodeBuilderTest() {
185         final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
186                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
187                 .build();
188         final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
189                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
190                 .build();
191         final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
192                 .create(unkeyedListEntryNode).build();
193         assertEquals(unkeyedListEntryNode.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
194                 .getLocalName());
195         assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
196                 .getLocalName());
197     }
198
199     @Test
200     public void immutableUnkeyedListNodeBuilderTest() {
201         final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
202                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
203                 .build();
204         final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
205                 ImmutableUnkeyedListNodeBuilder.create();
206         final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
207                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
208                 .addChild(unkeyedListEntryNode)
209                 .build();
210         final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
211                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
212                 .build();
213         final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
214                 .build();
215         try {
216             unkeyedListNodeSize.getChild(1);
217         } catch (IndexOutOfBoundsException e) {
218             // Ignored on purpose
219         }
220
221         assertNotNull(unkeyedListNodeSize.getValue());
222         assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.getChild(0));
223         assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType()
224                 .getLocalName());
225         assertNotNull(unkeyedListNodeCreated);
226     }
227
228     @Test
229     public void immutableChoiceNodeBuilderTest() {
230         final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
231                 .build();
232         final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
233         assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
234     }
235
236
237     @Test(expected = NullPointerException.class)
238     public void immutableAugmentationNodeBuilderExceptionTest() {
239         ImmutableAugmentationNodeBuilder.create(1).build();
240     }
241
242     @Test(expected = NullPointerException.class)
243     public void immutableContainerNodeBuilderExceptionTest() {
244         final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
245                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
246                 .build();
247         assertNotNull(immutableContainerNode);
248         final ContainerSchemaNode containerSchemaNode = mock(ContainerSchemaNode.class);
249         ImmutableContainerNodeSchemaAwareBuilder.create(containerSchemaNode, immutableContainerNode)
250                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
251                 .build();
252     }
253
254     @Test(expected = NullPointerException.class)
255     public void immutableLeafSetNodeBuilderExceptionTest() {
256         final LeafSetNode<?> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
257                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
258         assertNotNull(leafSetNode);
259         ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
260     }
261
262     @Test(expected = UnsupportedOperationException.class)
263     public void immutableLeafSetEntryNodeSchemaAwareBuilderExceptionTest() {
264         final LeafListSchemaNode leafListSchemaNode = mock(LeafListSchemaNode.class);
265         ImmutableLeafSetEntryNodeSchemaAwareBuilder.create(leafListSchemaNode).withNodeIdentifier(BAR_PATH).build();
266     }
267
268     @Test(expected = NullPointerException.class)
269     public void immutableMapEntryNodeBuilderExceptionTest() {
270         ImmutableMapEntryNodeBuilder.create(1).build();
271     }
272
273     @Test(expected = NullPointerException.class)
274     public void immutableYangModeledAnyXmlNodeBuilderExceptionTest() {
275         ImmutableYangModeledAnyXmlNodeBuilder.create(mock(YangModeledAnyXmlSchemaNode.class), 1);
276     }
277
278     @Test(expected = UnsupportedOperationException.class)
279     public void immutableUnkeyedListNodeBuilderExceptionTest() {
280         ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF)
281                 .removeChild(NODE_IDENTIFIER_LIST).build();
282     }
283
284     @Test(expected = UnsupportedOperationException.class)
285     public void immutableOrderedMapNotSchemaAwareExceptionTest1() {
286         ImmutableOrderedMapNodeBuilder.create(getImmutableMapNode()).build();
287     }
288
289     @Test(expected = UnsupportedOperationException.class)
290     public void immutableMapNodeSchemaAwareExceptionTest() {
291         ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST)
292         .build();
293     }
294
295     @Test(expected = UnsupportedOperationException.class)
296     public void immutableOrderedMapSchemaAwareExceptionTest1() {
297         ImmutableOrderedMapNodeSchemaAwareBuilder.create(list).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
298     }
299
300     @Test(expected = UnsupportedOperationException.class)
301     public void immutableOrderedMapSchemaAwareExceptionTest2() {
302         ImmutableOrderedMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).build();
303     }
304
305     @Test(expected = UnsupportedOperationException.class)
306     public void immutableOrderedLeafSetNodeExceptionTest1() {
307         ImmutableOrderedLeafSetNodeBuilder.create(getImmutableLeafSetNode()).build();
308     }
309
310     @Test(expected = UnsupportedOperationException.class)
311     public void immutableOrderedLeafSetNodeSchemaAwareExceptionTest1() {
312         ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
313         .build();
314     }
315
316     private static LeafSetNode<?> getImmutableLeafSetNode() {
317         final ListNodeBuilder<String, LeafSetEntryNode<String>> leafSetBuilder = Builders.<String>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)
325                 .build();
326     }
327
328     private static MapNode getImmutableOrderedMapNode() {
329         return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST)
330                 .withChild(LIST_MAIN_CHILD_1).build();
331     }
332 }