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.junit.Assert.assertNull;
14 import static org.mockito.Mockito.mock;
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.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.api.schema.builder.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.ImmutableUnkeyedListEntryNodeBuilder;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserLeafSetNodeBuilder;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserLeafSetNodeSchemaAwareBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeBuilder;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeSchemaAwareBuilder;
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 UserMapNode orderedMapNodeCreateNull = ImmutableUserMapNodeBuilder.create()
111 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
112 .withChild(LIST_MAIN_CHILD_1)
113 .addChild(LIST_MAIN_CHILD_2)
114 .withValue(mapEntryNodeColl)
116 final UserMapNode orderedMapNodeCreateSize = ImmutableUserMapNodeBuilder.create(SIZE)
117 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
119 final UserMapNode orderedMapNodeCreateNode = ImmutableUserMapNodeBuilder.create(orderedMapNodeCreateNull)
120 .removeChild(mapEntryPath)
122 final UserMapNode orderedMapNodeSchemaAware = ImmutableUserMapNodeSchemaAwareBuilder.create(list)
123 .withChild(LIST_MAIN_CHILD_1)
125 final UserMapNode orderedMapNodeSchemaAwareMapNodeConst = ImmutableUserMapNodeSchemaAwareBuilder.create(
126 list, getImmutableUserMapNode())
129 assertNotNull(Builders.orderedMapBuilder(list));
130 assertEquals(SIZE, orderedMapNodeCreateNull.size());
131 assertEquals(orderedMapNodeCreateNode.size(), orderedMapNodeCreateNull.size() - 1);
132 assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
133 assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.getChild(0));
134 assertEquals(SIZE, orderedMapNodeCreateNull.size());
135 assertEquals(orderedMapNodeSchemaAware.getChild(0), orderedMapNodeSchemaAwareMapNodeConst.getChild(0));
139 public void immutableUserLeafSetNodeBuilderTest() {
140 final UserLeafSetNode<String> orderedLeafSet = ImmutableUserLeafSetNodeBuilder.<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(orderedLeafSet);
148 final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.body();
149 final NormalizedNode orderedMapNodeSchemaAware = ImmutableUserLeafSetNodeSchemaAwareBuilder.create(
150 leafList).withChildValue("baz").build();
151 final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
152 (UnmodifiableCollection<?>) orderedMapNodeSchemaAware.body();
153 final NormalizedNode orderedLeafSetShemaAware = ImmutableUserLeafSetNodeSchemaAwareBuilder.create(
154 leafList, (UserLeafSetNode<?>) orderedLeafSet).build();
156 assertNotNull(Builders.orderedLeafSetBuilder(leafList));
157 assertNotNull(Builders.anyXmlBuilder());
158 assertNotNull(orderedLeafSetShemaAware);
159 assertEquals(1, ((UserLeafSetNode<?>)orderedLeafSet).size());
160 assertEquals("baz", orderedLeafSet.getChild(0).body());
161 assertNull(orderedLeafSet.childByArg(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, SystemMapNode> collectionNodeBuilder =
171 ImmutableMapNodeBuilder.create(1);
172 assertNotNull(collectionNodeBuilder);
173 collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
174 collectionNodeBuilder.withValue(mapEntryNodeColl);
175 final SystemMapNode mapNode = collectionNodeBuilder.build();
176 final SystemMapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list,
177 getImmutableMapNode()).build();
178 assertNotNull(mapNodeSchemaAware);
179 assertNotNull(Builders.mapBuilder(mapNode));
183 public void immutableUnkeyedListEntryNodeBuilderTest() {
184 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
185 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
187 final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
188 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
190 final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
191 .create(unkeyedListEntryNode).build();
192 assertEquals(unkeyedListEntryNode.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
194 assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
199 public void immutableUnkeyedListNodeBuilderTest() {
200 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
201 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
203 final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
204 ImmutableUnkeyedListNodeBuilder.create();
205 final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
206 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
207 .addChild(unkeyedListEntryNode)
209 final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
210 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
212 final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
215 unkeyedListNodeSize.getChild(1);
216 } catch (IndexOutOfBoundsException e) {
217 // Ignored on purpose
220 assertNotNull(unkeyedListNodeSize.body());
221 assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.getChild(0));
222 assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType()
224 assertNotNull(unkeyedListNodeCreated);
228 public void immutableChoiceNodeBuilderTest() {
229 final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
231 final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
232 assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
236 @Test(expected = NullPointerException.class)
237 public void immutableAugmentationNodeBuilderExceptionTest() {
238 ImmutableAugmentationNodeBuilder.create(1).build();
241 @Test(expected = NullPointerException.class)
242 public void immutableContainerNodeBuilderExceptionTest() {
243 final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
244 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
246 assertNotNull(immutableContainerNode);
247 final ContainerSchemaNode containerSchemaNode = mock(ContainerSchemaNode.class);
248 ImmutableContainerNodeSchemaAwareBuilder.create(containerSchemaNode, immutableContainerNode)
249 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
253 @Test(expected = NullPointerException.class)
254 public void immutableLeafSetNodeBuilderExceptionTest() {
255 final SystemLeafSetNode<Object> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
256 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
257 assertNotNull(leafSetNode);
258 ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
261 @Test(expected = UnsupportedOperationException.class)
262 public void immutableLeafSetEntryNodeSchemaAwareBuilderExceptionTest() {
263 final LeafListSchemaNode leafListSchemaNode = mock(LeafListSchemaNode.class);
264 ImmutableLeafSetEntryNodeSchemaAwareBuilder.create(leafListSchemaNode).withNodeIdentifier(BAR_PATH).build();
267 @Test(expected = NullPointerException.class)
268 public void immutableMapEntryNodeBuilderExceptionTest() {
269 ImmutableMapEntryNodeBuilder.create(1).build();
272 @Test(expected = UnsupportedOperationException.class)
273 public void immutableUnkeyedListNodeBuilderExceptionTest() {
274 ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF)
275 .removeChild(NODE_IDENTIFIER_LIST).build();
278 @Test(expected = UnsupportedOperationException.class)
279 public void immutableMapNodeSchemaAwareExceptionTest() {
280 ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST)
284 @Test(expected = UnsupportedOperationException.class)
285 public void immutableOrderedMapSchemaAwareExceptionTest1() {
286 ImmutableUserMapNodeSchemaAwareBuilder.create(list).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
289 @Test(expected = UnsupportedOperationException.class)
290 public void immutableUserLeafSetNodeSchemaAwareExceptionTest1() {
291 ImmutableUserLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
295 private static SystemLeafSetNode<String> getImmutableLeafSetNode() {
296 final ListNodeBuilder<String, SystemLeafSetNode<String>> leafSetBuilder = Builders.leafSetBuilder();
297 leafSetBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
298 leafSetBuilder.addChild(LEAF_SET_ENTRY_NODE);
299 return leafSetBuilder.build();
302 private static SystemMapNode getImmutableMapNode() {
303 return ImmutableMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1)
307 private static UserMapNode getImmutableUserMapNode() {
308 return ImmutableUserMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST)
309 .withChild(LIST_MAIN_CHILD_1).build();