2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.data.impl.schema;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.mock;
15 import java.io.FileNotFoundException;
16 import java.net.URISyntaxException;
17 import java.util.HashMap;
18 import java.util.LinkedList;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.util.UnmodifiableCollection;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.MapNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeSchemaAwareBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeSchemaAwareBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeSchemaAwareBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeSchemaAwareBuilder;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
53 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeSchemaAwareBuilder;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableYangModeledAnyXmlNodeBuilder;
57 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
58 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.Module;
62 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
63 import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
64 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
65 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ContainerEffectiveStatementImpl;
66 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
68 public class BuilderTest {
69 private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01", "root-container");
70 private static final QName LIST_MAIN = QName.create(ROOT_CONTAINER, "list-ordered-by-user-with-key");
71 private static final QName LEAF_LIST_MAIN = QName.create(ROOT_CONTAINER, "leaf-list-ordered-by-user");
72 private static final QName LIST_MAIN_CHILD_QNAME_1 = QName.create(ROOT_CONTAINER, "leaf-a");
73 private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LIST = YangInstanceIdentifier.NodeIdentifier
75 private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = YangInstanceIdentifier.NodeIdentifier
76 .create(LEAF_LIST_MAIN);
77 private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LEAF = YangInstanceIdentifier
78 .NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
79 private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 1);
80 private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 2);
81 private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 3);
82 private static final Integer SIZE = 3;
83 private static final NodeWithValue BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
84 private static final LeafSetEntryNode LEAF_SET_ENTRY_NODE = ImmutableLeafSetEntryNodeBuilder.create()
85 .withNodeIdentifier(BAR_PATH)
88 private ListSchemaNode list;
89 private LeafListSchemaNode leafList;
92 public void setup() throws FileNotFoundException, ReactorException, URISyntaxException {
93 final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
95 final SchemaContext schema = YangParserTestUtils.parseYangSources(leafRefTestYang);
96 final Module module = schema.getModules().iterator().next();
97 final DataSchemaNode root = module.getDataChildByName(ROOT_CONTAINER);
98 list = (ListSchemaNode)((ContainerEffectiveStatementImpl) root).getDataChildByName(LIST_MAIN);
99 leafList = (LeafListSchemaNode)((ContainerEffectiveStatementImpl) root).getDataChildByName(LEAF_LIST_MAIN);
103 public void immutableOrderedMapBuilderTest() {
104 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
105 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
106 final Map<QName, Object> keys = new HashMap<>();
107 keys.put(LIST_MAIN_CHILD_QNAME_1, 1);
108 final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath = new YangInstanceIdentifier
109 .NodeIdentifierWithPredicates(LIST_MAIN, keys);
110 final OrderedMapNode orderedMapNodeCreateNull = ImmutableOrderedMapNodeBuilder.create()
111 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
112 .withChild(LIST_MAIN_CHILD_1)
113 .addChild(LIST_MAIN_CHILD_2)
114 .withValue(mapEntryNodeColl)
116 final OrderedMapNode orderedMapNodeCreateSize = ImmutableOrderedMapNodeBuilder.create(SIZE)
117 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
119 final OrderedMapNode orderedMapNodeCreateNode = ImmutableOrderedMapNodeBuilder.create(orderedMapNodeCreateNull)
120 .removeChild(mapEntryPath)
122 final OrderedMapNode orderedMapNodeSchemaAware = ImmutableOrderedMapNodeSchemaAwareBuilder.create(list)
123 .withChild(LIST_MAIN_CHILD_1)
125 final OrderedMapNode orderedMapNodeSchemaAwareMapNodeConst = ImmutableOrderedMapNodeSchemaAwareBuilder.create
126 (list, getImmutableOrderedMapNode())
129 assertNotNull(Builders.orderedMapBuilder(list));
130 assertEquals(SIZE, (Integer) orderedMapNodeCreateNull.getSize());
131 assertEquals(orderedMapNodeCreateNode.getSize(), orderedMapNodeCreateNull.getSize() - 1);
132 assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
133 assertEquals(orderedMapNodeCreateNull.getChild(0), LIST_MAIN_CHILD_1);
134 assertEquals((Integer) orderedMapNodeCreateNull.getValue().size(), SIZE);
135 assertNotNull(orderedMapNodeCreateNull.hashCode());
136 assertEquals((Integer) orderedMapNodeCreateNull.getValue().size(), SIZE);
137 assertEquals(orderedMapNodeSchemaAware.getChild(0), orderedMapNodeSchemaAwareMapNodeConst.getChild(0));
141 public void immutableOrderedLeafSetNodeBuilderTest() {
142 final NormalizedNode<?, ?> orderedLeafSet = ImmutableOrderedLeafSetNodeBuilder.create()
143 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
144 .withChild(LEAF_SET_ENTRY_NODE)
145 .withChildValue("baz")
146 .removeChild(BAR_PATH)
148 final LinkedList<LeafSetNode<?>> mapEntryNodeColl = new LinkedList<>();
149 mapEntryNodeColl.add((LeafSetNode<?>)orderedLeafSet);
150 final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.getValue();
151 final NormalizedNode<?, ?> orderedMapNodeSchemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList)
152 .withChildValue("baz")
154 final UnmodifiableCollection<?> SchemaAwareleafSetCollection = (UnmodifiableCollection<?>)orderedMapNodeSchemaAware
156 final NormalizedNode<?, ?> orderedLeafSetShemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList,
157 (LeafSetNode<?>)orderedLeafSet)
160 assertNotNull(Builders.orderedLeafSetBuilder(leafList));
161 assertNotNull(Builders.anyXmlBuilder());
162 assertNotNull(orderedLeafSetShemaAware.hashCode());
163 assertNotNull(orderedLeafSetShemaAware);
164 assertEquals(1, ((OrderedLeafSetNode<?>)orderedLeafSet).getSize());
165 assertEquals("baz", ((OrderedLeafSetNode<?>)orderedLeafSet).getChild(0).getValue());
166 assertNotNull(((OrderedLeafSetNode<?>)orderedLeafSet).getChild(BAR_PATH));
167 assertEquals(1, leafSetCollection.size());
168 assertEquals(1, SchemaAwareleafSetCollection.size());
172 public void immutableMapNodeBuilderTest() {
173 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
174 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
175 final CollectionNodeBuilder<MapEntryNode, MapNode> collectionNodeBuilder = ImmutableMapNodeBuilder.create(1);
176 assertNotNull(collectionNodeBuilder);
177 collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
178 collectionNodeBuilder.withValue(mapEntryNodeColl);
179 final MapNode mapNode = collectionNodeBuilder.build();
180 assertNotNull(mapNode.hashCode());
181 final MapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).build();
182 assertNotNull(mapNodeSchemaAware);
183 assertNotNull(Builders.mapBuilder(mapNode));
187 public void immutableUnkeyedListEntryNodeBuilderTest() {
188 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
189 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
191 final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
192 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
194 final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder.create(unkeyedListEntryNode)
196 assertEquals(unkeyedListEntryNode.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
198 assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
203 public void immutableUnkeyedListNodeBuilderTest() {
204 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
205 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
207 final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
208 ImmutableUnkeyedListNodeBuilder.create();
209 final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
210 .addChild(unkeyedListEntryNode)
212 final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
213 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
215 final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
218 unkeyedListNodeSize.getChild(1);
219 } catch (IndexOutOfBoundsException e) {
222 assertNotNull(unkeyedListNodeSize.getSize());
223 assertNotNull(unkeyedListNodeSize.getValue());
224 assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.getChild(0));
225 assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType()
227 assertNotNull(unkeyedListNodeCreated);
231 public void immutableChoiceNodeBuilderTest() {
232 final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
234 final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
235 assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
239 @Test(expected=NullPointerException.class)
240 public void immutableAugmentationNodeBuilderExceptionTest() {
241 ImmutableAugmentationNodeBuilder.create(1).build();
244 @Test(expected=NullPointerException.class)
245 public void immutableContainerNodeBuilderExceptionTest() {
246 final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
247 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
249 assertNotNull(immutableContainerNode);
250 final ContainerSchemaNode containerSchemaNode = mock(ContainerSchemaNode.class);
251 ImmutableContainerNodeSchemaAwareBuilder.create(containerSchemaNode, immutableContainerNode)
252 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
256 @Test(expected=NullPointerException.class)
257 public void immutableLeafSetNodeBuilderExceptionTest() {
258 final LeafSetNode<?> leafSetNode = ImmutableLeafSetNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
260 assertNotNull(leafSetNode);
261 ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
264 @Test(expected=UnsupportedOperationException.class)
265 public void immutableLeafSetEntryNodeSchemaAwareBuilderExceptionTest() {
266 final LeafListSchemaNode leafListSchemaNode = mock(LeafListSchemaNode.class);
267 ImmutableLeafSetEntryNodeSchemaAwareBuilder.create(leafListSchemaNode).withNodeIdentifier(BAR_PATH).build();
270 @Test(expected=NullPointerException.class)
271 public void immutableMapEntryNodeBuilderExceptionTest() {
272 ImmutableMapEntryNodeBuilder.create(1).build();
275 @Test(expected=NullPointerException.class)
276 public void immutableYangModeledAnyXmlNodeBuilderExceptionTest() {
277 ImmutableYangModeledAnyXmlNodeBuilder.create(mock(YangModeledAnyXmlSchemaNode.class), 1);
280 @Test(expected=UnsupportedOperationException.class)
281 public void immutableUnkeyedListNodeBuilderExceptionTest() {
282 ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF)
283 .removeChild(NODE_IDENTIFIER_LIST).build();
286 @Test(expected=UnsupportedOperationException.class)
287 public void immutableOrderedMapNotSchemaAwareExceptionTest1() {
288 ImmutableOrderedMapNodeBuilder.create(getImmutableMapNode()).build();
291 @Test(expected=UnsupportedOperationException.class)
292 public void immutableMapNodeSchemaAwareExceptionTest() {
293 ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
296 @Test(expected=UnsupportedOperationException.class)
297 public void immutableOrderedMapSchemaAwareExceptionTest1() {
298 ImmutableOrderedMapNodeSchemaAwareBuilder.create(list).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
301 @Test(expected=UnsupportedOperationException.class)
302 public void immutableOrderedMapSchemaAwareExceptionTest2() {
303 ImmutableOrderedMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).build();
306 @Test(expected=UnsupportedOperationException.class)
307 public void immutableOrderedLeafSetNodeExceptionTest1() {
308 ImmutableOrderedLeafSetNodeBuilder.create(getImmutableLeafSetNode()).build();
311 @Test(expected=UnsupportedOperationException.class)
312 public void immutableOrderedLeafSetNodeSchemaAwareExceptionTest1() {
313 ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
316 private static LeafSetNode<?> getImmutableLeafSetNode() {
317 final ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders.leafSetBuilder();
318 leafSetBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
319 leafSetBuilder.addChild(LEAF_SET_ENTRY_NODE);
320 return leafSetBuilder.build();
323 private static MapNode getImmutableMapNode() {
324 return ImmutableMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1).build();
327 private static MapNode getImmutableOrderedMapNode() {
328 return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1)