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