e4ede1f76e8ed059e3df8750755747a7958ee74f
[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.YangInstanceIdentifier.NodeWithValue;
27 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
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.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 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", "root-container");
71     private static final QName LIST_MAIN = QName.create(ROOT_CONTAINER, "list-ordered-by-user-with-key");
72     private static final QName LEAF_LIST_MAIN = QName.create(ROOT_CONTAINER, "leaf-list-ordered-by-user");
73     private static final QName LIST_MAIN_CHILD_QNAME_1 = QName.create(ROOT_CONTAINER, "leaf-a");
74     private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LIST = YangInstanceIdentifier.NodeIdentifier
75             .create(LIST_MAIN);
76     private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = YangInstanceIdentifier.NodeIdentifier
77             .create(LEAF_LIST_MAIN);
78     private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LEAF = YangInstanceIdentifier
79             .NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
80     private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 1);
81     private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 2);
82     private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 3);
83     private static final int SIZE = 3;
84     private static final NodeWithValue<String> BAR_PATH = new 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 = YangParserTestUtils.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, orderedMapNodeCreateNull.getSize());
132         assertEquals(orderedMapNodeCreateNode.getSize(), orderedMapNodeCreateNull.getSize() - 1);
133         assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
134         assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.getChild(0));
135         assertEquals(SIZE, orderedMapNodeCreateNull.getValue().size());
136         assertEquals(orderedMapNodeSchemaAware.getChild(0), orderedMapNodeSchemaAwareMapNodeConst.getChild(0));
137     }
138
139     @Test
140     public void immutableOrderedLeafSetNodeBuilderTest() {
141         final NormalizedNode<?, ?> orderedLeafSet = ImmutableOrderedLeafSetNodeBuilder.create()
142                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
143                 .withChild(LEAF_SET_ENTRY_NODE)
144                 .withChildValue("baz")
145                 .removeChild(BAR_PATH)
146                 .build();
147         final LinkedList<LeafSetNode<?>> mapEntryNodeColl = new LinkedList<>();
148         mapEntryNodeColl.add((LeafSetNode<?>)orderedLeafSet);
149         final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.getValue();
150         final NormalizedNode<?, ?> orderedMapNodeSchemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList)
151                 .withChildValue("baz")
152                 .build();
153         final UnmodifiableCollection<?> SchemaAwareleafSetCollection = (UnmodifiableCollection<?>)orderedMapNodeSchemaAware
154                 .getValue();
155         final NormalizedNode<?, ?> orderedLeafSetShemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList,
156                 (LeafSetNode<?>)orderedLeafSet)
157                 .build();
158
159         assertNotNull(Builders.orderedLeafSetBuilder(leafList));
160         assertNotNull(Builders.anyXmlBuilder());
161         assertNotNull(orderedLeafSetShemaAware);
162         assertEquals(1, ((OrderedLeafSetNode<?>)orderedLeafSet).getSize());
163         assertEquals("baz", ((OrderedLeafSetNode<?>)orderedLeafSet).getChild(0).getValue());
164         assertNotNull(((OrderedLeafSetNode<?>)orderedLeafSet).getChild(BAR_PATH));
165         assertEquals(1, leafSetCollection.size());
166         assertEquals(1, SchemaAwareleafSetCollection.size());
167     }
168
169     @Test
170     public void immutableMapNodeBuilderTest() {
171         final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
172         mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
173         final CollectionNodeBuilder<MapEntryNode, MapNode> collectionNodeBuilder = ImmutableMapNodeBuilder.create(1);
174         assertNotNull(collectionNodeBuilder);
175         collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
176         collectionNodeBuilder.withValue(mapEntryNodeColl);
177         final MapNode mapNode = collectionNodeBuilder.build();
178         final MapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).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.create(unkeyedListEntryNode)
192                 .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.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         }
218
219         assertNotNull(unkeyedListNodeSize.getValue());
220         assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.getChild(0));
221         assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType()
222                 .getLocalName());
223         assertNotNull(unkeyedListNodeCreated);
224     }
225
226     @Test
227     public void immutableChoiceNodeBuilderTest() {
228         final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
229                 .build();
230         final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
231         assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
232     }
233
234
235     @Test(expected=NullPointerException.class)
236     public void immutableAugmentationNodeBuilderExceptionTest() {
237         ImmutableAugmentationNodeBuilder.create(1).build();
238     }
239
240     @Test(expected=NullPointerException.class)
241     public void immutableContainerNodeBuilderExceptionTest() {
242         final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
243                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
244                 .build();
245         assertNotNull(immutableContainerNode);
246         final ContainerSchemaNode containerSchemaNode = mock(ContainerSchemaNode.class);
247         ImmutableContainerNodeSchemaAwareBuilder.create(containerSchemaNode, immutableContainerNode)
248                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
249                 .build();
250     }
251
252     @Test(expected=NullPointerException.class)
253     public void immutableLeafSetNodeBuilderExceptionTest() {
254         final LeafSetNode<?> leafSetNode = ImmutableLeafSetNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
255                 .build();
256         assertNotNull(leafSetNode);
257         ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
258     }
259
260     @Test(expected=UnsupportedOperationException.class)
261     public void immutableLeafSetEntryNodeSchemaAwareBuilderExceptionTest() {
262         final LeafListSchemaNode leafListSchemaNode = mock(LeafListSchemaNode.class);
263         ImmutableLeafSetEntryNodeSchemaAwareBuilder.create(leafListSchemaNode).withNodeIdentifier(BAR_PATH).build();
264     }
265
266     @Test(expected=NullPointerException.class)
267     public void immutableMapEntryNodeBuilderExceptionTest() {
268         ImmutableMapEntryNodeBuilder.create(1).build();
269     }
270
271     @Test(expected=NullPointerException.class)
272     public void immutableYangModeledAnyXmlNodeBuilderExceptionTest() {
273         ImmutableYangModeledAnyXmlNodeBuilder.create(mock(YangModeledAnyXmlSchemaNode.class), 1);
274     }
275
276     @Test(expected=UnsupportedOperationException.class)
277     public void immutableUnkeyedListNodeBuilderExceptionTest() {
278         ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF)
279                 .removeChild(NODE_IDENTIFIER_LIST).build();
280     }
281
282     @Test(expected=UnsupportedOperationException.class)
283     public void immutableOrderedMapNotSchemaAwareExceptionTest1() {
284         ImmutableOrderedMapNodeBuilder.create(getImmutableMapNode()).build();
285     }
286
287     @Test(expected=UnsupportedOperationException.class)
288     public void immutableMapNodeSchemaAwareExceptionTest() {
289         ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
290     }
291
292     @Test(expected=UnsupportedOperationException.class)
293     public void immutableOrderedMapSchemaAwareExceptionTest1() {
294         ImmutableOrderedMapNodeSchemaAwareBuilder.create(list).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
295     }
296
297     @Test(expected=UnsupportedOperationException.class)
298     public void immutableOrderedMapSchemaAwareExceptionTest2() {
299         ImmutableOrderedMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).build();
300     }
301
302     @Test(expected=UnsupportedOperationException.class)
303     public void immutableOrderedLeafSetNodeExceptionTest1() {
304         ImmutableOrderedLeafSetNodeBuilder.create(getImmutableLeafSetNode()).build();
305     }
306
307     @Test(expected=UnsupportedOperationException.class)
308     public void immutableOrderedLeafSetNodeSchemaAwareExceptionTest1() {
309         ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
310     }
311
312     private static LeafSetNode<?> getImmutableLeafSetNode() {
313         final ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders.leafSetBuilder();
314         leafSetBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
315         leafSetBuilder.addChild(LEAF_SET_ENTRY_NODE);
316         return leafSetBuilder.build();
317     }
318
319     private static MapNode getImmutableMapNode() {
320         return ImmutableMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1).build();
321     }
322
323     private static MapNode getImmutableOrderedMapNode() {
324         return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1)
325                 .build();
326     }
327 }