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