b89d1820887612d21b5ea5b8b4b44f686d87a4fc
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / AbstractPrettyTreeTest.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech s.r.o. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.tree.impl;
9
10 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
14
15 import java.util.List;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
21 import org.opendaylight.yangtools.yang.data.api.schema.AnydataNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36
37 public abstract class AbstractPrettyTreeTest {
38     protected static final QName ROOT_QNAME = QName.create(
39             "urn:opendaylight:controller:sal:dom:store:test", "2014-03-13", "root");
40     protected static final QName ANOTHER_QNAME = QName.create(
41             "urn:opendaylight:controller:sal:dom:store:another", "another");
42
43     protected static final QName LIST_A_QNAME = QName.create(ROOT_QNAME, "list-a");
44     protected static final QName LEAF_A_QNAME = QName.create(ROOT_QNAME, "leaf-a");
45     protected static final QName LIST_B_QNAME = QName.create(ROOT_QNAME, "list-b");
46     protected static final QName LEAF_B_QNAME = QName.create(ROOT_QNAME, "leaf-b");
47
48     protected static final QName CHOICE_QNAME = QName.create(ROOT_QNAME, "choice");
49     protected static final QName AUGMENT_QNAME = QName.create(ROOT_QNAME, "augment");
50
51     protected static final QName LIST_ANOTHER_NAMESPACE_QNAME = QName.create(ANOTHER_QNAME,
52             "list-from-another-namespace");
53     protected static final QName LEAF_ANOTHER_NAMESPACE_QNAME = QName.create(ANOTHER_QNAME,
54             "leaf-from-another-namespace");
55
56     protected static final QName LEAF_QNAME = QName.create(ROOT_QNAME, "leaf");
57     protected static final QName LEAF_SET_QNAME = QName.create(ROOT_QNAME, "leaf-set");
58
59     protected static final QName USER_LEAF_SET_QNAME = QName.create(ROOT_QNAME, "user-leaf-set");
60     protected static final QName USER_MAP_QNAME = QName.create(ROOT_QNAME, "user-map");
61     protected static final QName USER_MAP_ENTRY_QNAME = QName.create(ROOT_QNAME, "user-map-entry");
62
63     protected static final QName UNKEYED_LIST_QNAME = QName.create(ROOT_QNAME,
64             "unkeyed-list");
65     protected static final QName UNKEYED_LIST_ENTRY_QNAME = QName.create(ROOT_QNAME,
66             "unkeyed-list-entry");
67     protected static final QName UNKEYED_LIST_LEAF_QNAME = QName.create(ROOT_QNAME,
68             "unkeyed-list-leaf");
69
70     protected static final QName ANY_DATA_QNAME = QName.create(ROOT_QNAME, "any-data");
71
72     /**
73      * Return a test node.
74      *
75      * <pre>
76      * root
77      *     list-a
78      *          leaf-a "foo"
79      *     list-a
80      *          leaf-a "bar"
81      *          list-b
82      *                  leaf-b "one"
83      *          list-b
84      *                  leaf-b "two"
85      *     choice
86      *          augment
87      *                  augmented-leaf "Augmented leaf value"
88      *     another
89      *          list-from-another-namespace
90      *               leaf-from-another-namespace "Leaf from another namespace value"
91      *     leaf "Leaf value"
92      *     leaf-set "Leaf set value"
93      *     user-leaf-set "User leaf set value"
94      *     user-map
95      *          user-map-entry "User map entry value"
96      *     unkeyed-list
97      *          unkeyed-list-entry
98      *               unkeyed-list-leaf "Unkeyed list leaf value"
99      *     any-data "Any data value"
100      *
101      * </pre>
102      *
103      * @return A test node
104      */
105     protected static ContainerNode createContainerNode() {
106         return Builders.containerBuilder()
107             .withNodeIdentifier(new NodeIdentifier(ROOT_QNAME))
108             .withChild(createMapNode())
109             .withChild(createChoiceNode())
110             .withChild(createContainerFromAnotherNamespace())
111             .withChild(createLeafNode())
112             .withChild(createLeafSetNode())
113             .withChild(createUserLeafSetNode())
114             .withChild(createUserMapNode())
115             .withChild(createUnkeyedListNode())
116             .withChild(createAnyDataNode())
117             .build();
118     }
119
120     protected static MapNode createMapNode() {
121         return mapNodeBuilder(LIST_A_QNAME)
122                 .withChild(mapEntry(LIST_A_QNAME, LEAF_A_QNAME, "foo"))
123                 .withChild(createMapEntryNode()).build();
124     }
125
126     protected static MapEntryNode createMapEntryNode() {
127         return mapEntryBuilder(LIST_A_QNAME, LEAF_A_QNAME, "bar")
128                 .withChild(mapNodeBuilder(LIST_B_QNAME)
129                         .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, "one"))
130                         .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, "two"))
131                         .build()).build();
132     }
133
134     protected static ChoiceNode createChoiceNode() {
135         return Builders.choiceBuilder()
136                 .withNodeIdentifier(NodeIdentifier.create(CHOICE_QNAME))
137                 .withChild(createAugmentationNode())
138                 .build();
139     }
140
141     protected static AugmentationNode createAugmentationNode() {
142         return Builders.augmentationBuilder()
143                 .withNodeIdentifier(AugmentationIdentifier
144                         .create(Set.of(AUGMENT_QNAME)))
145                 .withChild(createAugmentedLeafNode())
146                 .build();
147     }
148
149     protected static LeafNode<String> createAugmentedLeafNode() {
150         return leafNode(AUGMENT_QNAME, "Augmented leaf value");
151     }
152
153     protected static ContainerNode createContainerFromAnotherNamespace() {
154         return Builders.containerBuilder()
155             .withNodeIdentifier(new NodeIdentifier(ANOTHER_QNAME))
156             .withChild(mapNodeBuilder(LIST_ANOTHER_NAMESPACE_QNAME)
157                 .withChild(mapEntry(LIST_ANOTHER_NAMESPACE_QNAME, LEAF_ANOTHER_NAMESPACE_QNAME,
158                     "Leaf from another namespace value"))
159                 .build())
160             .build();
161     }
162
163     protected static LeafNode<String> createLeafNode() {
164         return ImmutableNodes.leafNode(LEAF_QNAME, "Leaf value");
165     }
166
167     protected static LeafSetNode<String> createLeafSetNode() {
168         final String value = "Leaf set value";
169         return Builders.<String>leafSetBuilder()
170             .withNodeIdentifier(NodeIdentifier.create(LEAF_SET_QNAME))
171             .withValue(List.of(Builders.<String>leafSetEntryBuilder()
172                 .withNodeIdentifier(new NodeWithValue<>(LEAF_SET_QNAME, value))
173                 .withValue(value)
174                 .build()))
175             .build();
176     }
177
178     protected static UserLeafSetNode<String> createUserLeafSetNode() {
179         final String value = "User leaf set value";
180         final LeafSetEntryNode<String> leafSetValue = Builders.<String>leafSetEntryBuilder()
181                 .withNodeIdentifier(new NodeWithValue<>(USER_LEAF_SET_QNAME, value))
182                 .withValue(value)
183                 .build();
184         return Builders.<String>orderedLeafSetBuilder()
185                 .withNodeIdentifier(NodeIdentifier.create(USER_LEAF_SET_QNAME))
186                 .withValue(List.of(leafSetValue))
187                 .build();
188     }
189
190     protected static UserMapNode createUserMapNode() {
191         return Builders.orderedMapBuilder()
192                 .withNodeIdentifier(NodeIdentifier.create(USER_MAP_QNAME))
193                 .withValue(List.of(createUserMapEntryNode()))
194                 .build();
195     }
196
197     protected static MapEntryNode createUserMapEntryNode() {
198         return mapEntry(USER_MAP_QNAME, USER_MAP_ENTRY_QNAME, "User map entry value");
199     }
200
201     protected static UnkeyedListNode createUnkeyedListNode() {
202         return Builders.unkeyedListBuilder()
203                 .withNodeIdentifier(NodeIdentifier.create(UNKEYED_LIST_QNAME))
204                 .withChild(createUnkeyedListEntryNode())
205                 .build();
206     }
207
208     protected static UnkeyedListEntryNode createUnkeyedListEntryNode() {
209         return Builders.unkeyedListEntryBuilder()
210                 .withNodeIdentifier(NodeIdentifier.create(UNKEYED_LIST_ENTRY_QNAME))
211                 .withChild(leafNode(UNKEYED_LIST_LEAF_QNAME, "Unkeyed list leaf value"))
212                 .build();
213     }
214
215     protected static AnydataNode<String> createAnyDataNode() {
216         return Builders.anydataBuilder(String.class)
217                 .withNodeIdentifier(NodeIdentifier.create(ANY_DATA_QNAME))
218                 .withValue("Any data value")
219                 .build();
220     }
221 }