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.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.mock;
16 import java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.util.HashMap;
19 import java.util.LinkedList;
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;
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.RetestUtils;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeSchemaAwareBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeSchemaAwareBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeSchemaAwareBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
53 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeSchemaAwareBuilder;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeSchemaAwareBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableYangModeledAnyXmlNodeBuilder;
60 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
64 import org.opendaylight.yangtools.yang.model.api.Module;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66 import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
67 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
68 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ContainerEffectiveStatementImpl;
70 public class BuilderTest {
71 private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01", "root-container");
72 private static final QName LIST_MAIN = QName.create(ROOT_CONTAINER, "list-ordered-by-user-with-key");
73 private static final QName LEAF_LIST_MAIN = QName.create(ROOT_CONTAINER, "leaf-list-ordered-by-user");
74 private static final QName LIST_MAIN_CHILD_QNAME_1 = QName.create(ROOT_CONTAINER, "leaf-a");
75 private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LIST = YangInstanceIdentifier.NodeIdentifier
77 private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = YangInstanceIdentifier.NodeIdentifier
78 .create(LEAF_LIST_MAIN);
79 private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LEAF = YangInstanceIdentifier
80 .NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
81 private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 1);
82 private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 2);
83 private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 3);
84 private static final Integer SIZE = 3;
85 private static final YangInstanceIdentifier.NodeWithValue BAR_PATH = new YangInstanceIdentifier
86 .NodeWithValue(LEAF_LIST_MAIN, "bar");
87 private static final LeafSetEntryNode LEAF_SET_ENTRY_NODE = ImmutableLeafSetEntryNodeBuilder.create()
88 .withNodeIdentifier(BAR_PATH)
91 private ListSchemaNode list;
92 private LeafListSchemaNode leafList;
95 public void setup() throws FileNotFoundException, ReactorException, URISyntaxException {
96 final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
98 final SchemaContext schema = RetestUtils.parseYangSources(leafRefTestYang);
99 final Module module = schema.getModules().iterator().next();
100 final DataSchemaNode root = module.getDataChildByName(ROOT_CONTAINER);
101 list = (ListSchemaNode)((ContainerEffectiveStatementImpl) root).getDataChildByName(LIST_MAIN);
102 leafList = (LeafListSchemaNode)((ContainerEffectiveStatementImpl) root).getDataChildByName(LEAF_LIST_MAIN);
106 public void immutableOrderedMapBuilderTest() {
107 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList();
108 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
109 final Map<QName, Object> keys = new HashMap<>();
110 keys.put(LIST_MAIN_CHILD_QNAME_1, 1);
111 final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath = new YangInstanceIdentifier
112 .NodeIdentifierWithPredicates(LIST_MAIN, keys);
113 final OrderedMapNode orderedMapNodeCreateNull = ImmutableOrderedMapNodeBuilder.create()
114 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
115 .withChild(LIST_MAIN_CHILD_1)
116 .addChild(LIST_MAIN_CHILD_2)
117 .withValue(mapEntryNodeColl)
119 final OrderedMapNode orderedMapNodeCreateSize = ImmutableOrderedMapNodeBuilder.create(SIZE)
120 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
122 final OrderedMapNode orderedMapNodeCreateNode = ImmutableOrderedMapNodeBuilder.create(orderedMapNodeCreateNull)
123 .removeChild(mapEntryPath)
125 final OrderedMapNode orderedMapNodeSchemaAware = ImmutableOrderedMapNodeSchemaAwareBuilder.create(list)
126 .withChild(LIST_MAIN_CHILD_1)
128 final OrderedMapNode orderedMapNodeSchemaAwareMapNodeConst = ImmutableOrderedMapNodeSchemaAwareBuilder.create
129 (list, getImmutableOrderedMapNode())
132 assertNotNull(Builders.orderedMapBuilder(list));
133 assertEquals(SIZE, (Integer) orderedMapNodeCreateNull.getSize());
134 assertEquals(orderedMapNodeCreateNode.getSize(), orderedMapNodeCreateNull.getSize() - 1);
135 assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
136 assertEquals(orderedMapNodeCreateNull.getChild(0), LIST_MAIN_CHILD_1);
137 assertEquals((Integer) orderedMapNodeCreateNull.getValue().size(), SIZE);
138 assertNotNull(orderedMapNodeCreateNull.hashCode());
139 assertEquals((Integer) orderedMapNodeCreateNull.getValue().size(), SIZE);
140 assertEquals(orderedMapNodeSchemaAware.getChild(0), orderedMapNodeSchemaAwareMapNodeConst.getChild(0));
144 public void immutableOrderedLeafSetNodeBuilderTest() {
145 final NormalizedNode orderedLeafSet = ImmutableOrderedLeafSetNodeBuilder.create()
146 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
147 .withChild(LEAF_SET_ENTRY_NODE)
148 .withChildValue("baz")
149 .removeChild(BAR_PATH)
151 final LinkedList<LeafSetNode> mapEntryNodeColl = new LinkedList();
152 mapEntryNodeColl.add((LeafSetNode)orderedLeafSet);
153 final UnmodifiableCollection leafSetCollection = (UnmodifiableCollection)orderedLeafSet.getValue();
154 final NormalizedNode orderedMapNodeSchemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList)
155 .withChildValue("baz")
157 final UnmodifiableCollection SchemaAwareleafSetCollection = (UnmodifiableCollection)orderedMapNodeSchemaAware
159 final NormalizedNode orderedLeafSetShemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList,
160 (LeafSetNode)orderedLeafSet)
163 assertNotNull(Builders.orderedLeafSetBuilder(leafList));
164 assertNotNull(Builders.anyXmlBuilder());
165 assertNotNull(orderedLeafSetShemaAware.hashCode());
166 assertNotNull(orderedLeafSetShemaAware);
167 assertEquals(1, ((OrderedLeafSetNode)orderedLeafSet).getSize());
168 assertEquals("baz", ((OrderedLeafSetNode)orderedLeafSet).getChild(0).getValue());
169 assertNotNull(((OrderedLeafSetNode)orderedLeafSet).getChild(BAR_PATH));
170 assertEquals(1, leafSetCollection.size());
171 assertEquals(1, SchemaAwareleafSetCollection.size());
175 public void immutableMapNodeBuilderTest() {
176 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList();
177 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
178 final CollectionNodeBuilder<MapEntryNode, MapNode> collectionNodeBuilder = ImmutableMapNodeBuilder.create(1);
179 assertNotNull(collectionNodeBuilder);
180 collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
181 collectionNodeBuilder.withValue(mapEntryNodeColl);
182 final MapNode mapNode = collectionNodeBuilder.build();
183 assertNotNull(mapNode.hashCode());
184 final MapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).build();
185 assertNotNull(mapNodeSchemaAware);
186 assertNotNull(Builders.mapBuilder(mapNode));
190 public void immutableUnkeyedListEntryNodeBuilderTest() {
191 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
192 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
194 final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
195 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
197 final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder.create(unkeyedListEntryNode)
199 assertEquals(unkeyedListEntryNode.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
201 assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
206 public void immutableUnkeyedListNodeBuilderTest() {
207 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
208 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
210 final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
211 ImmutableUnkeyedListNodeBuilder.create();
212 final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
213 .addChild(unkeyedListEntryNode)
215 final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
216 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
218 final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
221 unkeyedListNodeSize.getChild(1);
222 } catch (IndexOutOfBoundsException e) {
225 assertNotNull(unkeyedListNodeSize.getSize());
226 assertNotNull(unkeyedListNodeSize.getValue());
227 assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.getChild(0));
228 assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType()
230 assertNotNull(unkeyedListNodeCreated);
234 public void immutableChoiceNodeBuilderTest() {
235 final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
237 final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
238 assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
242 @Test(expected=NullPointerException.class)
243 public void immutableAugmentationNodeBuilderExceptionTest() {
244 ImmutableAugmentationNodeBuilder.create(1).build();
247 @Test(expected=NullPointerException.class)
248 public void immutableContainerNodeBuilderExceptionTest() {
249 final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
250 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
252 assertNotNull(immutableContainerNode);
253 final ContainerSchemaNode containerSchemaNode = mock(ContainerSchemaNode.class);
254 ImmutableContainerNodeSchemaAwareBuilder.create(containerSchemaNode, immutableContainerNode)
255 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
259 @Test(expected=NullPointerException.class)
260 public void immutableLeafSetNodeBuilderExceptionTest() {
261 final LeafSetNode leafSetNode = ImmutableLeafSetNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
263 assertNotNull(leafSetNode);
264 ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
267 @Test(expected=UnsupportedOperationException.class)
268 public void immutableLeafSetEntryNodeSchemaAwareBuilderExceptionTest() {
269 final LeafListSchemaNode leafListSchemaNode = mock(LeafListSchemaNode.class);
270 ImmutableLeafSetEntryNodeSchemaAwareBuilder.create(leafListSchemaNode).withNodeIdentifier(BAR_PATH).build();
273 @Test(expected=NullPointerException.class)
274 public void immutableMapEntryNodeBuilderExceptionTest() {
275 ImmutableMapEntryNodeBuilder.create(1).build();
278 @Test(expected=NullPointerException.class)
279 public void immutableYangModeledAnyXmlNodeBuilderExceptionTest() {
280 ImmutableYangModeledAnyXmlNodeBuilder.create(mock(YangModeledAnyXmlSchemaNode.class), 1);
283 @Test(expected=UnsupportedOperationException.class)
284 public void immutableUnkeyedListNodeBuilderExceptionTest() {
285 ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF)
286 .removeChild(NODE_IDENTIFIER_LIST).build();
289 @Test(expected=UnsupportedOperationException.class)
290 public void immutableOrderedMapNotSchemaAwareExceptionTest1() {
291 ImmutableOrderedMapNodeBuilder.create(getImmutableMapNode()).build();
294 @Test(expected=UnsupportedOperationException.class)
295 public void immutableMapNodeSchemaAwareExceptionTest() {
296 ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
299 @Test(expected=UnsupportedOperationException.class)
300 public void immutableOrderedMapSchemaAwareExceptionTest1() {
301 ImmutableOrderedMapNodeSchemaAwareBuilder.create(list).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
304 @Test(expected=UnsupportedOperationException.class)
305 public void immutableOrderedMapSchemaAwareExceptionTest2() {
306 ImmutableOrderedMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).build();
309 @Test(expected=UnsupportedOperationException.class)
310 public void immutableOrderedLeafSetNodeExceptionTest1() {
311 ImmutableOrderedLeafSetNodeBuilder.create(getImmutableLeafSetNode()).build();
314 @Test(expected=UnsupportedOperationException.class)
315 public void immutableOrderedLeafSetNodeSchemaAwareExceptionTest1() {
316 ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
319 private static LeafSetNode getImmutableLeafSetNode() {
320 final ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders.leafSetBuilder();
321 leafSetBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
322 leafSetBuilder.addChild(LEAF_SET_ENTRY_NODE);
323 return leafSetBuilder.build();
326 private static MapNode getImmutableMapNode() {
327 return ImmutableMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1).build();
330 private static MapNode getImmutableOrderedMapNode() {
331 return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1)