Remove schema-aware builders
[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.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.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
181                 .getLocalName());
182         assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
183                 .getLocalName());
184     }
185
186     @Test
187     public void immutableUnkeyedListNodeBuilderTest() {
188         final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
189                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
190                 .build();
191         final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
192                 ImmutableUnkeyedListNodeBuilder.create();
193         final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
194                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
195                 .addChild(unkeyedListEntryNode)
196                 .build();
197         final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
198                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
199                 .build();
200         final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
201                 .build();
202
203         assertThrows(IndexOutOfBoundsException.class, () -> unkeyedListNodeSize.childAt(1));
204
205         assertNotNull(unkeyedListNodeSize.body());
206         assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.childAt(0));
207         assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType().getLocalName());
208         assertNotNull(unkeyedListNodeCreated);
209     }
210
211     @Test
212     public void immutableChoiceNodeBuilderTest() {
213         final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
214                 .build();
215         final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
216         assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
217     }
218
219
220     @Test
221     public void immutableAugmentationNodeBuilderExceptionTest() {
222         final var builder = ImmutableAugmentationNodeBuilder.create(1);
223         assertThrows(NullPointerException.class, builder::build);
224     }
225
226     @Test
227     public void immutableContainerNodeBuilderExceptionTest() {
228         final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
229                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
230                 .build();
231         assertNotNull(immutableContainerNode);
232     }
233
234     @Test
235     public void immutableLeafSetNodeBuilderExceptionTest() {
236         final SystemLeafSetNode<Object> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
237                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
238                 .build();
239         assertNotNull(leafSetNode);
240     }
241
242     @Test
243     public void immutableMapEntryNodeBuilderExceptionTest() {
244         final var builder = ImmutableMapEntryNodeBuilder.create(1);
245         assertThrows(NullPointerException.class, builder::build);
246     }
247
248     @Test
249     public void immutableUnkeyedListNodeBuilderExceptionTest() {
250         final var builder = ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF);
251         assertThrows(UnsupportedOperationException.class, () -> builder.removeChild(NODE_IDENTIFIER_LIST));
252     }
253
254     private static SystemMapNode getImmutableMapNode() {
255         return ImmutableMapNodeBuilder.create()
256             .withNodeIdentifier(NODE_IDENTIFIER_LIST)
257             .withChild(LIST_MAIN_CHILD_1)
258             .build();
259     }
260
261     private static UserMapNode getImmutableUserMapNode() {
262         return ImmutableUserMapNodeBuilder.create()
263             .withNodeIdentifier(NODE_IDENTIFIER_LIST)
264             .withChild(LIST_MAIN_CHILD_1)
265             .build();
266     }
267 }