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.junit.Assert.assertThrows;
16 import java.net.URISyntaxException;
17 import java.util.HashMap;
18 import java.util.LinkedList;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.util.UnmodifiableCollection;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserLeafSetNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeBuilder;
49 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.Module;
54 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
56 public class BuilderTest {
57 private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01",
59 private static final QName LIST_MAIN = QName.create(ROOT_CONTAINER, "list-ordered-by-user-with-key");
60 private static final QName LEAF_LIST_MAIN = QName.create(ROOT_CONTAINER, "leaf-list-ordered-by-user");
61 private static final QName LIST_MAIN_CHILD_QNAME_1 = QName.create(ROOT_CONTAINER, "leaf-a");
62 private static final NodeIdentifier NODE_IDENTIFIER_LIST = NodeIdentifier.create(LIST_MAIN);
63 private static final NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = NodeIdentifier.create(LEAF_LIST_MAIN);
64 private static final NodeIdentifier NODE_IDENTIFIER_LEAF = NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
65 private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
67 private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
69 private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
71 private static final int SIZE = 3;
72 private static final NodeWithValue<String> BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
73 private static final LeafSetEntryNode<String> LEAF_SET_ENTRY_NODE =
74 ImmutableLeafSetEntryNodeBuilder.<String>create()
75 .withNodeIdentifier(BAR_PATH)
78 private ListSchemaNode list;
79 private LeafListSchemaNode leafList;
82 public void setup() throws URISyntaxException {
83 final var schema = YangParserTestUtils.parseYang("""
84 module immutable-ordered-map-node {
86 namespace "test.namespace.builder.test";
89 revision "2016-01-01" {
90 description "Initial revision.";
93 container root-container {
94 list list-ordered-by-user-with-key {
101 leaf-list leaf-list-ordered-by-user {
107 final Module module = schema.getModules().iterator().next();
108 final DataSchemaNode root = module.getDataChildByName(ROOT_CONTAINER);
109 list = (ListSchemaNode)((ContainerSchemaNode) root).getDataChildByName(LIST_MAIN);
110 leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).getDataChildByName(LEAF_LIST_MAIN);
114 public void immutableOrderedMapBuilderTest() {
115 final var mapEntryNodeColl = new LinkedList<MapEntryNode>();
116 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
117 final var keys = new HashMap<QName, Object>();
118 keys.put(LIST_MAIN_CHILD_QNAME_1, 1);
119 final NodeIdentifierWithPredicates mapEntryPath = NodeIdentifierWithPredicates.of(LIST_MAIN, keys);
120 final UserMapNode orderedMapNodeCreateNull = ImmutableUserMapNodeBuilder.create()
121 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
122 .withChild(LIST_MAIN_CHILD_1)
123 .addChild(LIST_MAIN_CHILD_2)
124 .withValue(mapEntryNodeColl)
126 final UserMapNode orderedMapNodeCreateSize = ImmutableUserMapNodeBuilder.create(SIZE)
127 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
129 final UserMapNode orderedMapNodeCreateNode = ImmutableUserMapNodeBuilder.create(orderedMapNodeCreateNull)
130 .removeChild(mapEntryPath)
132 final UserMapNode orderedMapNodeSchemaAware = ImmutableUserMapNodeBuilder.create()
133 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
134 .withChild(LIST_MAIN_CHILD_1)
136 final UserMapNode orderedMapNodeSchemaAwareMapNodeConst =
137 ImmutableUserMapNodeBuilder.create(getImmutableUserMapNode())
140 assertEquals(SIZE, orderedMapNodeCreateNull.size());
141 assertEquals(orderedMapNodeCreateNode.size(), orderedMapNodeCreateNull.size() - 1);
142 assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.name());
143 assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.childAt(0));
144 assertEquals(SIZE, orderedMapNodeCreateNull.size());
145 assertEquals(orderedMapNodeSchemaAware.childAt(0), orderedMapNodeSchemaAwareMapNodeConst.childAt(0));
149 public void immutableUserLeafSetNodeBuilderTest() {
150 final UserLeafSetNode<String> orderedLeafSet = ImmutableUserLeafSetNodeBuilder.<String>create()
151 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
152 .withChild(LEAF_SET_ENTRY_NODE)
153 .withChildValue("baz")
154 .removeChild(BAR_PATH)
156 final LinkedList<LeafSetNode<?>> mapEntryNodeColl = new LinkedList<>();
157 mapEntryNodeColl.add(orderedLeafSet);
158 final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.body();
159 final NormalizedNode orderedMapNodeSchemaAware = ImmutableUserLeafSetNodeBuilder.create()
160 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
161 .withChildValue("baz")
163 final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
164 (UnmodifiableCollection<?>) orderedMapNodeSchemaAware.body();
166 assertNotNull(Builders.anyXmlBuilder());
167 assertEquals(1, ((UserLeafSetNode<?>)orderedLeafSet).size());
168 assertEquals("baz", orderedLeafSet.childAt(0).body());
169 assertNull(orderedLeafSet.childByArg(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, SystemMapNode> collectionNodeBuilder =
179 ImmutableMapNodeBuilder.create(1);
180 assertNotNull(collectionNodeBuilder);
181 collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
182 collectionNodeBuilder.withValue(mapEntryNodeColl);
183 final SystemMapNode mapNode = collectionNodeBuilder.build();
184 assertNotNull(Builders.mapBuilder(mapNode));
188 public void immutableUnkeyedListEntryNodeBuilderTest() {
189 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
190 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
192 final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
193 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
195 final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
196 .create(unkeyedListEntryNode).build();
197 assertEquals(unkeyedListEntryNode.name(), unkeyedListEntryNodeSize.name());
198 assertEquals(unkeyedListEntryNodeSize.name(), unkeyedListEntryNodeNode.name());
202 public void immutableUnkeyedListNodeBuilderTest() {
203 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
204 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
206 final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
207 ImmutableUnkeyedListNodeBuilder.create();
208 final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
209 .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 assertThrows(IndexOutOfBoundsException.class, () -> unkeyedListNodeSize.childAt(1));
220 assertNotNull(unkeyedListNodeSize.body());
221 assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.childAt(0));
222 assertEquals(unkeyedListNode.name(), unkeyedListNodeSize.name());
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.name(), choiceNode.name());
235 public void immutableContainerNodeBuilderExceptionTest() {
236 final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
237 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
239 assertNotNull(immutableContainerNode);
243 public void immutableLeafSetNodeBuilderExceptionTest() {
244 final SystemLeafSetNode<Object> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
245 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
247 assertNotNull(leafSetNode);
251 public void immutableMapEntryNodeBuilderExceptionTest() {
252 final var builder = ImmutableMapEntryNodeBuilder.create(1);
253 assertThrows(NullPointerException.class, builder::build);
257 public void immutableUnkeyedListNodeBuilderExceptionTest() {
258 final var builder = ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF);
259 assertThrows(UnsupportedOperationException.class, () -> builder.removeChild(NODE_IDENTIFIER_LIST));
262 private static SystemMapNode getImmutableMapNode() {
263 return ImmutableMapNodeBuilder.create()
264 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
265 .withChild(LIST_MAIN_CHILD_1)
269 private static UserMapNode getImmutableUserMapNode() {
270 return ImmutableUserMapNodeBuilder.create()
271 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
272 .withChild(LIST_MAIN_CHILD_1)