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