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.model.api.SchemaContext;
58 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
60 public class BuilderTest {
61 private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01",
63 private static final QName LIST_MAIN = QName.create(ROOT_CONTAINER, "list-ordered-by-user-with-key");
64 private static final QName LEAF_LIST_MAIN = QName.create(ROOT_CONTAINER, "leaf-list-ordered-by-user");
65 private static final QName LIST_MAIN_CHILD_QNAME_1 = QName.create(ROOT_CONTAINER, "leaf-a");
66 private static final NodeIdentifier NODE_IDENTIFIER_LIST = NodeIdentifier.create(LIST_MAIN);
67 private static final NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = NodeIdentifier.create(LEAF_LIST_MAIN);
68 private static final NodeIdentifier NODE_IDENTIFIER_LEAF = NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
69 private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
71 private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
73 private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
75 private static final int SIZE = 3;
76 private static final NodeWithValue<String> BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
77 private static final LeafSetEntryNode<String> LEAF_SET_ENTRY_NODE =
78 ImmutableLeafSetEntryNodeBuilder.<String>create()
79 .withNodeIdentifier(BAR_PATH)
82 private ListSchemaNode list;
83 private LeafListSchemaNode leafList;
86 public void setup() throws URISyntaxException {
87 final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
89 final SchemaContext schema = YangParserTestUtils.parseYangFiles(leafRefTestYang);
90 final Module module = schema.getModules().iterator().next();
91 final DataSchemaNode root = module.findDataChildByName(ROOT_CONTAINER).get();
92 list = (ListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LIST_MAIN).get();
93 leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LEAF_LIST_MAIN).get();
97 public void immutableOrderedMapBuilderTest() {
98 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
99 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
100 final Map<QName, Object> keys = new HashMap<>();
101 keys.put(LIST_MAIN_CHILD_QNAME_1, 1);
102 final NodeIdentifierWithPredicates mapEntryPath = NodeIdentifierWithPredicates.of(LIST_MAIN, keys);
103 final UserMapNode orderedMapNodeCreateNull = ImmutableUserMapNodeBuilder.create()
104 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
105 .withChild(LIST_MAIN_CHILD_1)
106 .addChild(LIST_MAIN_CHILD_2)
107 .withValue(mapEntryNodeColl)
109 final UserMapNode orderedMapNodeCreateSize = ImmutableUserMapNodeBuilder.create(SIZE)
110 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
112 final UserMapNode orderedMapNodeCreateNode = ImmutableUserMapNodeBuilder.create(orderedMapNodeCreateNull)
113 .removeChild(mapEntryPath)
115 final UserMapNode orderedMapNodeSchemaAware = ImmutableUserMapNodeBuilder.create()
116 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
117 .withChild(LIST_MAIN_CHILD_1)
119 final UserMapNode orderedMapNodeSchemaAwareMapNodeConst =
120 ImmutableUserMapNodeBuilder.create(getImmutableUserMapNode())
123 assertEquals(SIZE, orderedMapNodeCreateNull.size());
124 assertEquals(orderedMapNodeCreateNode.size(), orderedMapNodeCreateNull.size() - 1);
125 assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
126 assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.childAt(0));
127 assertEquals(SIZE, orderedMapNodeCreateNull.size());
128 assertEquals(orderedMapNodeSchemaAware.childAt(0), orderedMapNodeSchemaAwareMapNodeConst.childAt(0));
132 public void immutableUserLeafSetNodeBuilderTest() {
133 final UserLeafSetNode<String> orderedLeafSet = ImmutableUserLeafSetNodeBuilder.<String>create()
134 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
135 .withChild(LEAF_SET_ENTRY_NODE)
136 .withChildValue("baz")
137 .removeChild(BAR_PATH)
139 final LinkedList<LeafSetNode<?>> mapEntryNodeColl = new LinkedList<>();
140 mapEntryNodeColl.add(orderedLeafSet);
141 final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.body();
142 final NormalizedNode orderedMapNodeSchemaAware = ImmutableUserLeafSetNodeBuilder.create()
143 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
144 .withChildValue("baz")
146 final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
147 (UnmodifiableCollection<?>) orderedMapNodeSchemaAware.body();
149 assertNotNull(Builders.anyXmlBuilder());
150 assertEquals(1, ((UserLeafSetNode<?>)orderedLeafSet).size());
151 assertEquals("baz", orderedLeafSet.childAt(0).body());
152 assertNull(orderedLeafSet.childByArg(BAR_PATH));
153 assertEquals(1, leafSetCollection.size());
154 assertEquals(1, SchemaAwareleafSetCollection.size());
158 public void immutableMapNodeBuilderTest() {
159 final LinkedList<MapEntryNode> mapEntryNodeColl = new LinkedList<>();
160 mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
161 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> collectionNodeBuilder =
162 ImmutableMapNodeBuilder.create(1);
163 assertNotNull(collectionNodeBuilder);
164 collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
165 collectionNodeBuilder.withValue(mapEntryNodeColl);
166 final SystemMapNode mapNode = collectionNodeBuilder.build();
167 assertNotNull(Builders.mapBuilder(mapNode));
171 public void immutableUnkeyedListEntryNodeBuilderTest() {
172 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
173 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
175 final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
176 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
178 final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
179 .create(unkeyedListEntryNode).build();
180 assertEquals(unkeyedListEntryNode.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
182 assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
187 public void immutableUnkeyedListNodeBuilderTest() {
188 final UnkeyedListEntryNode unkeyedListEntryNode = ImmutableUnkeyedListEntryNodeBuilder.create()
189 .withNodeIdentifier(NODE_IDENTIFIER_LEAF)
191 final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
192 ImmutableUnkeyedListNodeBuilder.create();
193 final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
194 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
195 .addChild(unkeyedListEntryNode)
197 final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
198 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
200 final UnkeyedListNode unkeyedListNodeCreated = ImmutableUnkeyedListNodeBuilder.create(unkeyedListNode)
203 assertThrows(IndexOutOfBoundsException.class, () -> unkeyedListNodeSize.childAt(1));
205 assertNotNull(unkeyedListNodeSize.body());
206 assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.childAt(0));
207 assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType().getLocalName());
208 assertNotNull(unkeyedListNodeCreated);
212 public void immutableChoiceNodeBuilderTest() {
213 final ChoiceNode choiceNode = ImmutableChoiceNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LIST)
215 final ChoiceNode choiceNodeCreated = ImmutableChoiceNodeBuilder.create(choiceNode).build();
216 assertEquals(choiceNodeCreated.getIdentifier(), choiceNode.getIdentifier());
221 public void immutableAugmentationNodeBuilderExceptionTest() {
222 final var builder = ImmutableAugmentationNodeBuilder.create(1);
223 assertThrows(NullPointerException.class, builder::build);
227 public void immutableContainerNodeBuilderExceptionTest() {
228 final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
229 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
231 assertNotNull(immutableContainerNode);
235 public void immutableLeafSetNodeBuilderExceptionTest() {
236 final SystemLeafSetNode<Object> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
237 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
239 assertNotNull(leafSetNode);
243 public void immutableMapEntryNodeBuilderExceptionTest() {
244 final var builder = ImmutableMapEntryNodeBuilder.create(1);
245 assertThrows(NullPointerException.class, builder::build);
249 public void immutableUnkeyedListNodeBuilderExceptionTest() {
250 final var builder = ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF);
251 assertThrows(UnsupportedOperationException.class, () -> builder.removeChild(NODE_IDENTIFIER_LIST));
254 private static SystemMapNode getImmutableMapNode() {
255 return ImmutableMapNodeBuilder.create()
256 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
257 .withChild(LIST_MAIN_CHILD_1)
261 private static UserMapNode getImmutableUserMapNode() {
262 return ImmutableUserMapNodeBuilder.create()
263 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
264 .withChild(LIST_MAIN_CHILD_1)