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;
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.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.ImmutableLeafSetEntryNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserLeafSetNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeBuilder;
52 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
55 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
56 import org.opendaylight.yangtools.yang.model.api.Module;
57 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
59 public class BuilderTest {
60 private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01",
62 private static final QName LIST_MAIN = QName.create(ROOT_CONTAINER, "list-ordered-by-user-with-key");
63 private static final QName LEAF_LIST_MAIN = QName.create(ROOT_CONTAINER, "leaf-list-ordered-by-user");
64 private static final QName LIST_MAIN_CHILD_QNAME_1 = QName.create(ROOT_CONTAINER, "leaf-a");
65 private static final NodeIdentifier NODE_IDENTIFIER_LIST = NodeIdentifier.create(LIST_MAIN);
66 private static final NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = NodeIdentifier.create(LEAF_LIST_MAIN);
67 private static final NodeIdentifier NODE_IDENTIFIER_LEAF = NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
68 private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
70 private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
72 private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
74 private static final int SIZE = 3;
75 private static final NodeWithValue<String> BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
76 private static final LeafSetEntryNode<String> LEAF_SET_ENTRY_NODE =
77 ImmutableLeafSetEntryNodeBuilder.<String>create()
78 .withNodeIdentifier(BAR_PATH)
81 private ListSchemaNode list;
82 private LeafListSchemaNode leafList;
85 public void setup() throws URISyntaxException {
86 final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
88 final var schema = YangParserTestUtils.parseYangFiles(leafRefTestYang);
89 final Module module = schema.getModules().iterator().next();
90 final DataSchemaNode root = module.getDataChildByName(ROOT_CONTAINER);
91 list = (ListSchemaNode)((ContainerSchemaNode) root).getDataChildByName(LIST_MAIN);
92 leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).getDataChildByName(LEAF_LIST_MAIN);
96 public void immutableOrderedMapBuilderTest() {
97 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
98 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
99 final Map<QName, Object> keys = new HashMap<>();
100 keys.put(LIST_MAIN_CHILD_QNAME_1, 1);
101 final NodeIdentifierWithPredicates mapEntryPath = NodeIdentifierWithPredicates.of(LIST_MAIN, keys);
102 final UserMapNode orderedMapNodeCreateNull = ImmutableUserMapNodeBuilder.create()
103 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
104 .withChild(LIST_MAIN_CHILD_1)
105 .addChild(LIST_MAIN_CHILD_2)
106 .withValue(mapEntryNodeColl)
108 final UserMapNode orderedMapNodeCreateSize = ImmutableUserMapNodeBuilder.create(SIZE)
109 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
111 final UserMapNode orderedMapNodeCreateNode = ImmutableUserMapNodeBuilder.create(orderedMapNodeCreateNull)
112 .removeChild(mapEntryPath)
114 final UserMapNode orderedMapNodeSchemaAware = ImmutableUserMapNodeBuilder.create()
115 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
116 .withChild(LIST_MAIN_CHILD_1)
118 final UserMapNode orderedMapNodeSchemaAwareMapNodeConst =
119 ImmutableUserMapNodeBuilder.create(getImmutableUserMapNode())
122 assertEquals(SIZE, orderedMapNodeCreateNull.size());
123 assertEquals(orderedMapNodeCreateNode.size(), orderedMapNodeCreateNull.size() - 1);
124 assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
125 assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.childAt(0));
126 assertEquals(SIZE, orderedMapNodeCreateNull.size());
127 assertEquals(orderedMapNodeSchemaAware.childAt(0), orderedMapNodeSchemaAwareMapNodeConst.childAt(0));
131 public void immutableUserLeafSetNodeBuilderTest() {
132 final UserLeafSetNode<String> orderedLeafSet = ImmutableUserLeafSetNodeBuilder.<String>create()
133 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
134 .withChild(LEAF_SET_ENTRY_NODE)
135 .withChildValue("baz")
136 .removeChild(BAR_PATH)
138 final LinkedList<LeafSetNode<?>> mapEntryNodeColl = new LinkedList<>();
139 mapEntryNodeColl.add(orderedLeafSet);
140 final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.body();
141 final NormalizedNode orderedMapNodeSchemaAware = ImmutableUserLeafSetNodeBuilder.create()
142 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
143 .withChildValue("baz")
145 final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
146 (UnmodifiableCollection<?>) orderedMapNodeSchemaAware.body();
148 assertNotNull(Builders.anyXmlBuilder());
149 assertEquals(1, ((UserLeafSetNode<?>)orderedLeafSet).size());
150 assertEquals("baz", orderedLeafSet.childAt(0).body());
151 assertNull(orderedLeafSet.childByArg(BAR_PATH));
152 assertEquals(1, leafSetCollection.size());
153 assertEquals(1, SchemaAwareleafSetCollection.size());
157 public void immutableMapNodeBuilderTest() {
158 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
159 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
160 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> collectionNodeBuilder =
161 ImmutableMapNodeBuilder.create(1);
162 assertNotNull(collectionNodeBuilder);
163 collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
164 collectionNodeBuilder.withValue(mapEntryNodeColl);
165 final SystemMapNode mapNode = collectionNodeBuilder.build();
166 assertNotNull(Builders.mapBuilder(mapNode));
170 public void immutableUnkeyedListEntryNodeBuilderTest() {
171 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
172 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
174 final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
175 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
177 final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
178 .create(unkeyedListEntryNode).build();
179 assertEquals(unkeyedListEntryNode.getIdentifier(), unkeyedListEntryNodeSize.getIdentifier());
180 assertEquals(unkeyedListEntryNodeSize.getIdentifier(), unkeyedListEntryNodeNode.getIdentifier());
184 public void immutableUnkeyedListNodeBuilderTest() {
185 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
186 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
188 final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
189 ImmutableUnkeyedListNodeBuilder.create();
190 final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
191 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
192 .addChild(unkeyedListEntryNode)
194 final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
195 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
197 final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
200 assertThrows(IndexOutOfBoundsException.class, () -> unkeyedListNodeSize.childAt(1));
202 assertNotNull(unkeyedListNodeSize.body());
203 assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.childAt(0));
204 assertEquals(unkeyedListNode.getIdentifier(), unkeyedListNodeSize.getIdentifier());
205 assertNotNull(unkeyedListNodeCreated);
209 public void immutableChoiceNodeBuilderTest() {
210 final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
212 final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
213 assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
218 public void immutableAugmentationNodeBuilderExceptionTest() {
219 final var builder = ImmutableAugmentationNodeBuilder.create(1);
220 assertThrows(NullPointerException.class, builder::build);
224 public void immutableContainerNodeBuilderExceptionTest() {
225 final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
226 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
228 assertNotNull(immutableContainerNode);
232 public void immutableLeafSetNodeBuilderExceptionTest() {
233 final SystemLeafSetNode<Object> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
234 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
236 assertNotNull(leafSetNode);
240 public void immutableMapEntryNodeBuilderExceptionTest() {
241 final var builder = ImmutableMapEntryNodeBuilder.create(1);
242 assertThrows(NullPointerException.class, builder::build);
246 public void immutableUnkeyedListNodeBuilderExceptionTest() {
247 final var builder = ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF);
248 assertThrows(UnsupportedOperationException.class, () -> builder.removeChild(NODE_IDENTIFIER_LIST));
251 private static SystemMapNode getImmutableMapNode() {
252 return ImmutableMapNodeBuilder.create()
253 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
254 .withChild(LIST_MAIN_CHILD_1)
258 private static UserMapNode getImmutableUserMapNode() {
259 return ImmutableUserMapNodeBuilder.create()
260 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
261 .withChild(LIST_MAIN_CHILD_1)