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