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