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.odlext.model.api.YangModeledAnyxmlSchemaNode;
24 import org.opendaylight.yangtools.util.UnmodifiableCollection;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
29 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
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.api.schema.UserLeafSetNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeSchemaAwareBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeSchemaAwareBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeSchemaAwareBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
53 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
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.ImmutableUserLeafSetNodeBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserLeafSetNodeSchemaAwareBuilder;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeBuilder;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeSchemaAwareBuilder;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableYangModeledAnyXmlNodeBuilder;
61 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
64 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
65 import org.opendaylight.yangtools.yang.model.api.Module;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
69 public class BuilderTest {
70 private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01",
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 NodeIdentifier NODE_IDENTIFIER_LIST = NodeIdentifier.create(LIST_MAIN);
76 private static final NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = NodeIdentifier.create(LEAF_LIST_MAIN);
77 private static final NodeIdentifier NODE_IDENTIFIER_LEAF = NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
78 private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
80 private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
82 private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
84 private static final int SIZE = 3;
85 private static final NodeWithValue<String> BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
86 private static final LeafSetEntryNode<String> LEAF_SET_ENTRY_NODE =
87 ImmutableLeafSetEntryNodeBuilder.<String>create()
88 .withNodeIdentifier(BAR_PATH)
91 private ListSchemaNode list;
92 private LeafListSchemaNode leafList;
95 public void setup() throws URISyntaxException {
96 final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
98 final SchemaContext schema = YangParserTestUtils.parseYangFiles(leafRefTestYang);
99 final Module module = schema.getModules().iterator().next();
100 final DataSchemaNode root = module.findDataChildByName(ROOT_CONTAINER).get();
101 list = (ListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LIST_MAIN).get();
102 leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LEAF_LIST_MAIN).get();
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 NodeIdentifierWithPredicates mapEntryPath = NodeIdentifierWithPredicates.of(LIST_MAIN, keys);
112 final UserMapNode orderedMapNodeCreateNull = ImmutableUserMapNodeBuilder.create()
113 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
114 .withChild(LIST_MAIN_CHILD_1)
115 .addChild(LIST_MAIN_CHILD_2)
116 .withValue(mapEntryNodeColl)
118 final UserMapNode orderedMapNodeCreateSize = ImmutableUserMapNodeBuilder.create(SIZE)
119 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
121 final UserMapNode orderedMapNodeCreateNode = ImmutableUserMapNodeBuilder.create(orderedMapNodeCreateNull)
122 .removeChild(mapEntryPath)
124 final UserMapNode orderedMapNodeSchemaAware = ImmutableUserMapNodeSchemaAwareBuilder.create(list)
125 .withChild(LIST_MAIN_CHILD_1)
127 final UserMapNode orderedMapNodeSchemaAwareMapNodeConst = ImmutableUserMapNodeSchemaAwareBuilder.create(
128 list, getImmutableUserMapNode())
131 assertNotNull(Builders.orderedMapBuilder(list));
132 assertEquals(SIZE, orderedMapNodeCreateNull.size());
133 assertEquals(orderedMapNodeCreateNode.size(), orderedMapNodeCreateNull.size() - 1);
134 assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
135 assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.getChild(0));
136 assertEquals(SIZE, orderedMapNodeCreateNull.size());
137 assertEquals(orderedMapNodeSchemaAware.getChild(0), orderedMapNodeSchemaAwareMapNodeConst.getChild(0));
141 public void immutableUserLeafSetNodeBuilderTest() {
142 final UserLeafSetNode<String> orderedLeafSet = ImmutableUserLeafSetNodeBuilder.<String>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(orderedLeafSet);
150 final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.body();
151 final NormalizedNode orderedMapNodeSchemaAware = ImmutableUserLeafSetNodeSchemaAwareBuilder.create(
152 leafList).withChildValue("baz").build();
153 final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
154 (UnmodifiableCollection<?>) orderedMapNodeSchemaAware.body();
155 final NormalizedNode orderedLeafSetShemaAware = ImmutableUserLeafSetNodeSchemaAwareBuilder.create(
156 leafList, (UserLeafSetNode<?>) orderedLeafSet).build();
158 assertNotNull(Builders.orderedLeafSetBuilder(leafList));
159 assertNotNull(Builders.anyXmlBuilder());
160 assertNotNull(orderedLeafSetShemaAware);
161 assertEquals(1, ((UserLeafSetNode<?>)orderedLeafSet).size());
162 assertEquals("baz", orderedLeafSet.getChild(0).body());
163 assertNull(orderedLeafSet.childByArg(BAR_PATH));
164 assertEquals(1, leafSetCollection.size());
165 assertEquals(1, SchemaAwareleafSetCollection.size());
169 public void immutableMapNodeBuilderTest() {
170 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
171 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
172 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> collectionNodeBuilder =
173 ImmutableMapNodeBuilder.create(1);
174 assertNotNull(collectionNodeBuilder);
175 collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
176 collectionNodeBuilder.withValue(mapEntryNodeColl);
177 final SystemMapNode mapNode = collectionNodeBuilder.build();
178 final SystemMapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list,
179 getImmutableMapNode()).build();
180 assertNotNull(mapNodeSchemaAware);
181 assertNotNull(Builders.mapBuilder(mapNode));
185 public void immutableUnkeyedListEntryNodeBuilderTest() {
186 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
187 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
189 final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
190 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
192 final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
193 .create(unkeyedListEntryNode).build();
194 assertEquals(unkeyedListEntryNode.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
196 assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
201 public void immutableUnkeyedListNodeBuilderTest() {
202 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
203 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
205 final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
206 ImmutableUnkeyedListNodeBuilder.create();
207 final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
208 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
209 .addChild(unkeyedListEntryNode)
211 final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
212 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
214 final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
217 unkeyedListNodeSize.getChild(1);
218 } catch (IndexOutOfBoundsException e) {
219 // Ignored on purpose
222 assertNotNull(unkeyedListNodeSize.body());
223 assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.getChild(0));
224 assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType()
226 assertNotNull(unkeyedListNodeCreated);
230 public void immutableChoiceNodeBuilderTest() {
231 final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
233 final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
234 assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
238 @Test(expected = NullPointerException.class)
239 public void immutableAugmentationNodeBuilderExceptionTest() {
240 ImmutableAugmentationNodeBuilder.create(1).build();
243 @Test(expected = NullPointerException.class)
244 public void immutableContainerNodeBuilderExceptionTest() {
245 final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
246 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
248 assertNotNull(immutableContainerNode);
249 final ContainerSchemaNode containerSchemaNode = mock(ContainerSchemaNode.class);
250 ImmutableContainerNodeSchemaAwareBuilder.create(containerSchemaNode, immutableContainerNode)
251 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
255 @Test(expected = NullPointerException.class)
256 public void immutableLeafSetNodeBuilderExceptionTest() {
257 final SystemLeafSetNode<Object> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
258 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
259 assertNotNull(leafSetNode);
260 ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
263 @Test(expected = UnsupportedOperationException.class)
264 public void immutableLeafSetEntryNodeSchemaAwareBuilderExceptionTest() {
265 final LeafListSchemaNode leafListSchemaNode = mock(LeafListSchemaNode.class);
266 ImmutableLeafSetEntryNodeSchemaAwareBuilder.create(leafListSchemaNode).withNodeIdentifier(BAR_PATH).build();
269 @Test(expected = NullPointerException.class)
270 public void immutableMapEntryNodeBuilderExceptionTest() {
271 ImmutableMapEntryNodeBuilder.create(1).build();
274 @Test(expected = NullPointerException.class)
275 public void immutableYangModeledAnyXmlNodeBuilderExceptionTest() {
276 ImmutableYangModeledAnyXmlNodeBuilder.create(mock(YangModeledAnyxmlSchemaNode.class), 1);
279 @Test(expected = UnsupportedOperationException.class)
280 public void immutableUnkeyedListNodeBuilderExceptionTest() {
281 ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF)
282 .removeChild(NODE_IDENTIFIER_LIST).build();
285 @Test(expected = UnsupportedOperationException.class)
286 public void immutableMapNodeSchemaAwareExceptionTest() {
287 ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST)
291 @Test(expected = UnsupportedOperationException.class)
292 public void immutableOrderedMapSchemaAwareExceptionTest1() {
293 ImmutableUserMapNodeSchemaAwareBuilder.create(list).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
296 @Test(expected = UnsupportedOperationException.class)
297 public void immutableUserLeafSetNodeSchemaAwareExceptionTest1() {
298 ImmutableUserLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
302 private static SystemLeafSetNode<String> getImmutableLeafSetNode() {
303 final ListNodeBuilder<String, SystemLeafSetNode<String>> leafSetBuilder = Builders.leafSetBuilder();
304 leafSetBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
305 leafSetBuilder.addChild(LEAF_SET_ENTRY_NODE);
306 return leafSetBuilder.build();
309 private static SystemMapNode getImmutableMapNode() {
310 return ImmutableMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1)
314 private static UserMapNode getImmutableUserMapNode() {
315 return ImmutableUserMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST)
316 .withChild(LIST_MAIN_CHILD_1).build();