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;
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.odlext.model.api.YangModeledAnyxmlSchemaNode;
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.MapNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
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.ImmutableContainerNodeSchemaAwareBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeSchemaAwareBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeSchemaAwareBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
53 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeSchemaAwareBuilder;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeSchemaAwareBuilder;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableYangModeledAnyXmlNodeBuilder;
59 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.Module;
64 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
65 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
67 public class BuilderTest {
68 private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01",
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 NodeIdentifier NODE_IDENTIFIER_LIST = NodeIdentifier.create(LIST_MAIN);
74 private static final NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = NodeIdentifier.create(LEAF_LIST_MAIN);
75 private static final NodeIdentifier NODE_IDENTIFIER_LEAF = NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
76 private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
78 private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
80 private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
82 private static final int SIZE = 3;
83 private static final NodeWithValue<String> BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
84 private static final LeafSetEntryNode<String> LEAF_SET_ENTRY_NODE =
85 ImmutableLeafSetEntryNodeBuilder.<String>create()
86 .withNodeIdentifier(BAR_PATH)
89 private ListSchemaNode list;
90 private LeafListSchemaNode leafList;
93 public void setup() throws URISyntaxException {
94 final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
96 final SchemaContext schema = YangParserTestUtils.parseYangFiles(leafRefTestYang);
97 final Module module = schema.getModules().iterator().next();
98 final DataSchemaNode root = module.findDataChildByName(ROOT_CONTAINER).get();
99 list = (ListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LIST_MAIN).get();
100 leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LEAF_LIST_MAIN).get();
104 public void immutableOrderedMapBuilderTest() {
105 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
106 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
107 final Map<QName, Object> keys = new HashMap<>();
108 keys.put(LIST_MAIN_CHILD_QNAME_1, 1);
109 final NodeIdentifierWithPredicates mapEntryPath = NodeIdentifierWithPredicates.of(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, orderedMapNodeCreateNull.getSize());
131 assertEquals(orderedMapNodeCreateNode.getSize(), orderedMapNodeCreateNull.getSize() - 1);
132 assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
133 assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.getChild(0));
134 assertEquals(SIZE, orderedMapNodeCreateNull.getValue().size());
135 assertEquals(orderedMapNodeSchemaAware.getChild(0), orderedMapNodeSchemaAwareMapNodeConst.getChild(0));
139 public void immutableOrderedLeafSetNodeBuilderTest() {
140 final NormalizedNode<?, ?> orderedLeafSet = ImmutableOrderedLeafSetNodeBuilder.<String>create()
141 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
142 .withChild(LEAF_SET_ENTRY_NODE)
143 .withChildValue("baz")
144 .removeChild(BAR_PATH)
146 final LinkedList<LeafSetNode<?>> mapEntryNodeColl = new LinkedList<>();
147 mapEntryNodeColl.add((LeafSetNode<?>)orderedLeafSet);
148 final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.getValue();
149 final NormalizedNode<?, ?> orderedMapNodeSchemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(
150 leafList).withChildValue("baz").build();
151 final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
152 (UnmodifiableCollection<?>)orderedMapNodeSchemaAware.getValue();
153 final NormalizedNode<?, ?> orderedLeafSetShemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(
154 leafList,(LeafSetNode<?>)orderedLeafSet).build();
156 assertNotNull(Builders.orderedLeafSetBuilder(leafList));
157 assertNotNull(Builders.anyXmlBuilder());
158 assertNotNull(orderedLeafSetShemaAware);
159 assertEquals(1, ((OrderedLeafSetNode<?>)orderedLeafSet).getSize());
160 assertEquals("baz", ((OrderedLeafSetNode<?>)orderedLeafSet).getChild(0).getValue());
161 assertNotNull(((OrderedLeafSetNode<?>)orderedLeafSet).getChild(BAR_PATH));
162 assertEquals(1, leafSetCollection.size());
163 assertEquals(1, SchemaAwareleafSetCollection.size());
167 public void immutableMapNodeBuilderTest() {
168 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
169 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
170 final CollectionNodeBuilder<MapEntryNode, MapNode> collectionNodeBuilder = ImmutableMapNodeBuilder.create(1);
171 assertNotNull(collectionNodeBuilder);
172 collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
173 collectionNodeBuilder.withValue(mapEntryNodeColl);
174 final MapNode mapNode = collectionNodeBuilder.build();
175 final MapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode())
177 assertNotNull(mapNodeSchemaAware);
178 assertNotNull(Builders.mapBuilder(mapNode));
182 public void immutableUnkeyedListEntryNodeBuilderTest() {
183 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
184 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
186 final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
187 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
189 final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
190 .create(unkeyedListEntryNode).build();
191 assertEquals(unkeyedListEntryNode.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
193 assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
198 public void immutableUnkeyedListNodeBuilderTest() {
199 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
200 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
202 final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
203 ImmutableUnkeyedListNodeBuilder.create();
204 final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
205 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
206 .addChild(unkeyedListEntryNode)
208 final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
209 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
211 final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
214 unkeyedListNodeSize.getChild(1);
215 } catch (IndexOutOfBoundsException e) {
216 // Ignored on purpose
219 assertNotNull(unkeyedListNodeSize.getValue());
220 assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.getChild(0));
221 assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType()
223 assertNotNull(unkeyedListNodeCreated);
227 public void immutableChoiceNodeBuilderTest() {
228 final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
230 final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
231 assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
235 @Test(expected = NullPointerException.class)
236 public void immutableAugmentationNodeBuilderExceptionTest() {
237 ImmutableAugmentationNodeBuilder.create(1).build();
240 @Test(expected = NullPointerException.class)
241 public void immutableContainerNodeBuilderExceptionTest() {
242 final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
243 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
245 assertNotNull(immutableContainerNode);
246 final ContainerSchemaNode containerSchemaNode = mock(ContainerSchemaNode.class);
247 ImmutableContainerNodeSchemaAwareBuilder.create(containerSchemaNode, immutableContainerNode)
248 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
252 @Test(expected = NullPointerException.class)
253 public void immutableLeafSetNodeBuilderExceptionTest() {
254 final LeafSetNode<?> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
255 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
256 assertNotNull(leafSetNode);
257 ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
260 @Test(expected = UnsupportedOperationException.class)
261 public void immutableLeafSetEntryNodeSchemaAwareBuilderExceptionTest() {
262 final LeafListSchemaNode leafListSchemaNode = mock(LeafListSchemaNode.class);
263 ImmutableLeafSetEntryNodeSchemaAwareBuilder.create(leafListSchemaNode).withNodeIdentifier(BAR_PATH).build();
266 @Test(expected = NullPointerException.class)
267 public void immutableMapEntryNodeBuilderExceptionTest() {
268 ImmutableMapEntryNodeBuilder.create(1).build();
271 @Test(expected = NullPointerException.class)
272 public void immutableYangModeledAnyXmlNodeBuilderExceptionTest() {
273 ImmutableYangModeledAnyXmlNodeBuilder.create(mock(YangModeledAnyxmlSchemaNode.class), 1);
276 @Test(expected = UnsupportedOperationException.class)
277 public void immutableUnkeyedListNodeBuilderExceptionTest() {
278 ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF)
279 .removeChild(NODE_IDENTIFIER_LIST).build();
282 @Test(expected = UnsupportedOperationException.class)
283 public void immutableOrderedMapNotSchemaAwareExceptionTest1() {
284 ImmutableOrderedMapNodeBuilder.create(getImmutableMapNode()).build();
287 @Test(expected = UnsupportedOperationException.class)
288 public void immutableMapNodeSchemaAwareExceptionTest() {
289 ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST)
293 @Test(expected = UnsupportedOperationException.class)
294 public void immutableOrderedMapSchemaAwareExceptionTest1() {
295 ImmutableOrderedMapNodeSchemaAwareBuilder.create(list).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
298 @Test(expected = UnsupportedOperationException.class)
299 public void immutableOrderedMapSchemaAwareExceptionTest2() {
300 ImmutableOrderedMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).build();
303 @Test(expected = UnsupportedOperationException.class)
304 public void immutableOrderedLeafSetNodeExceptionTest1() {
305 ImmutableOrderedLeafSetNodeBuilder.create(getImmutableLeafSetNode()).build();
308 @Test(expected = UnsupportedOperationException.class)
309 public void immutableOrderedLeafSetNodeSchemaAwareExceptionTest1() {
310 ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
314 private static LeafSetNode<?> getImmutableLeafSetNode() {
315 final ListNodeBuilder<String, LeafSetEntryNode<String>> leafSetBuilder = Builders.leafSetBuilder();
316 leafSetBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
317 leafSetBuilder.addChild(LEAF_SET_ENTRY_NODE);
318 return leafSetBuilder.build();
321 private static MapNode getImmutableMapNode() {
322 return ImmutableMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1)
326 private static MapNode getImmutableOrderedMapNode() {
327 return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST)
328 .withChild(LIST_MAIN_CHILD_1).build();