50492810e01c218a6478f4ef18808f19e5e770a9
[yangtools.git] / data / 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.junit.Assert.assertThrows;
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.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserLeafSetNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeBuilder;
52 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
55 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
56 import org.opendaylight.yangtools.yang.model.api.Module;
57 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
58
59 public class BuilderTest {
60     private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01",
61         "root-container");
62     private static final QName LIST_MAIN = QName.create(ROOT_CONTAINER, "list-ordered-by-user-with-key");
63     private static final QName LEAF_LIST_MAIN = QName.create(ROOT_CONTAINER, "leaf-list-ordered-by-user");
64     private static final QName LIST_MAIN_CHILD_QNAME_1 = QName.create(ROOT_CONTAINER, "leaf-a");
65     private static final NodeIdentifier NODE_IDENTIFIER_LIST = NodeIdentifier.create(LIST_MAIN);
66     private static final NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = NodeIdentifier.create(LEAF_LIST_MAIN);
67     private static final NodeIdentifier NODE_IDENTIFIER_LEAF = NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
68     private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
69             1);
70     private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
71             2);
72     private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
73             3);
74     private static final int SIZE = 3;
75     private static final NodeWithValue<String> BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
76     private static final LeafSetEntryNode<String> LEAF_SET_ENTRY_NODE =
77             ImmutableLeafSetEntryNodeBuilder.<String>create()
78             .withNodeIdentifier(BAR_PATH)
79             .withValue("bar")
80             .build();
81     private ListSchemaNode list;
82     private LeafListSchemaNode leafList;
83
84     @Before
85     public void setup() throws URISyntaxException {
86         final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
87                 .toURI());
88         final var schema = YangParserTestUtils.parseYangFiles(leafRefTestYang);
89         final Module module = schema.getModules().iterator().next();
90         final DataSchemaNode root = module.getDataChildByName(ROOT_CONTAINER);
91         list = (ListSchemaNode)((ContainerSchemaNode) root).getDataChildByName(LIST_MAIN);
92         leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).getDataChildByName(LEAF_LIST_MAIN);
93     }
94
95     @Test
96     public void immutableOrderedMapBuilderTest() {
97         final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
98         mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
99         final Map<QName, Object> keys = new HashMap<>();
100         keys.put(LIST_MAIN_CHILD_QNAME_1, 1);
101         final NodeIdentifierWithPredicates mapEntryPath = NodeIdentifierWithPredicates.of(LIST_MAIN, keys);
102         final UserMapNode orderedMapNodeCreateNull = ImmutableUserMapNodeBuilder.create()
103                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
104                 .withChild(LIST_MAIN_CHILD_1)
105                 .addChild(LIST_MAIN_CHILD_2)
106                 .withValue(mapEntryNodeColl)
107                 .build();
108         final UserMapNode orderedMapNodeCreateSize = ImmutableUserMapNodeBuilder.create(SIZE)
109                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
110                 .build();
111         final UserMapNode orderedMapNodeCreateNode = ImmutableUserMapNodeBuilder.create(orderedMapNodeCreateNull)
112                 .removeChild(mapEntryPath)
113                 .build();
114         final UserMapNode orderedMapNodeSchemaAware = ImmutableUserMapNodeBuilder.create()
115                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
116                 .withChild(LIST_MAIN_CHILD_1)
117                 .build();
118         final UserMapNode orderedMapNodeSchemaAwareMapNodeConst =
119                 ImmutableUserMapNodeBuilder.create(getImmutableUserMapNode())
120                 .build();
121
122         assertEquals(SIZE, orderedMapNodeCreateNull.size());
123         assertEquals(orderedMapNodeCreateNode.size(), orderedMapNodeCreateNull.size() - 1);
124         assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
125         assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.childAt(0));
126         assertEquals(SIZE, orderedMapNodeCreateNull.size());
127         assertEquals(orderedMapNodeSchemaAware.childAt(0), orderedMapNodeSchemaAwareMapNodeConst.childAt(0));
128     }
129
130     @Test
131     public void immutableUserLeafSetNodeBuilderTest() {
132         final UserLeafSetNode<String> orderedLeafSet = ImmutableUserLeafSetNodeBuilder.<String>create()
133                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
134                 .withChild(LEAF_SET_ENTRY_NODE)
135                 .withChildValue("baz")
136                 .removeChild(BAR_PATH)
137                 .build();
138         final LinkedList<LeafSetNode<?>> mapEntryNodeColl = new LinkedList<>();
139         mapEntryNodeColl.add(orderedLeafSet);
140         final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.body();
141         final NormalizedNode orderedMapNodeSchemaAware = ImmutableUserLeafSetNodeBuilder.create()
142             .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
143             .withChildValue("baz")
144             .build();
145         final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
146                 (UnmodifiableCollection<?>) orderedMapNodeSchemaAware.body();
147
148         assertNotNull(Builders.anyXmlBuilder());
149         assertEquals(1, ((UserLeafSetNode<?>)orderedLeafSet).size());
150         assertEquals("baz", orderedLeafSet.childAt(0).body());
151         assertNull(orderedLeafSet.childByArg(BAR_PATH));
152         assertEquals(1, leafSetCollection.size());
153         assertEquals(1, SchemaAwareleafSetCollection.size());
154     }
155
156     @Test
157     public void immutableMapNodeBuilderTest() {
158         final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
159         mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
160         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> collectionNodeBuilder =
161             ImmutableMapNodeBuilder.create(1);
162         assertNotNull(collectionNodeBuilder);
163         collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
164         collectionNodeBuilder.withValue(mapEntryNodeColl);
165         final SystemMapNode mapNode = collectionNodeBuilder.build();
166         assertNotNull(Builders.mapBuilder(mapNode));
167     }
168
169     @Test
170     public void immutableUnkeyedListEntryNodeBuilderTest() {
171         final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
172                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
173                 .build();
174         final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
175                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
176                 .build();
177         final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
178                 .create(unkeyedListEntryNode).build();
179         assertEquals(unkeyedListEntryNode.getIdentifier(), unkeyedListEntryNodeSize.getIdentifier());
180         assertEquals(unkeyedListEntryNodeSize.getIdentifier(), unkeyedListEntryNodeNode.getIdentifier());
181     }
182
183     @Test
184     public void immutableUnkeyedListNodeBuilderTest() {
185         final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
186                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
187                 .build();
188         final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
189                 ImmutableUnkeyedListNodeBuilder.create();
190         final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
191                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
192                 .addChild(unkeyedListEntryNode)
193                 .build();
194         final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
195                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
196                 .build();
197         final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
198                 .build();
199
200         assertThrows(IndexOutOfBoundsException.class, () -> unkeyedListNodeSize.childAt(1));
201
202         assertNotNull(unkeyedListNodeSize.body());
203         assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.childAt(0));
204         assertEquals(unkeyedListNode.getIdentifier(), unkeyedListNodeSize.getIdentifier());
205         assertNotNull(unkeyedListNodeCreated);
206     }
207
208     @Test
209     public void immutableChoiceNodeBuilderTest() {
210         final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
211                 .build();
212         final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
213         assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
214     }
215
216
217     @Test
218     public void immutableAugmentationNodeBuilderExceptionTest() {
219         final var builder = ImmutableAugmentationNodeBuilder.create(1);
220         assertThrows(NullPointerException.class, builder::build);
221     }
222
223     @Test
224     public void immutableContainerNodeBuilderExceptionTest() {
225         final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
226                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
227                 .build();
228         assertNotNull(immutableContainerNode);
229     }
230
231     @Test
232     public void immutableLeafSetNodeBuilderExceptionTest() {
233         final SystemLeafSetNode<Object> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
234                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
235                 .build();
236         assertNotNull(leafSetNode);
237     }
238
239     @Test
240     public void immutableMapEntryNodeBuilderExceptionTest() {
241         final var builder = ImmutableMapEntryNodeBuilder.create(1);
242         assertThrows(NullPointerException.class, builder::build);
243     }
244
245     @Test
246     public void immutableUnkeyedListNodeBuilderExceptionTest() {
247         final var builder = ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF);
248         assertThrows(UnsupportedOperationException.class, () -> builder.removeChild(NODE_IDENTIFIER_LIST));
249     }
250
251     private static SystemMapNode getImmutableMapNode() {
252         return ImmutableMapNodeBuilder.create()
253             .withNodeIdentifier(NODE_IDENTIFIER_LIST)
254             .withChild(LIST_MAIN_CHILD_1)
255             .build();
256     }
257
258     private static UserMapNode getImmutableUserMapNode() {
259         return ImmutableUserMapNodeBuilder.create()
260             .withNodeIdentifier(NODE_IDENTIFIER_LIST)
261             .withChild(LIST_MAIN_CHILD_1)
262             .build();
263     }
264 }