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