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;
14 import com.google.common.collect.Sets;
15 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.text.ParseException;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
23 import javax.xml.transform.dom.DOMSource;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
33 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
45 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
47 public class ImmutableNormalizedNodeStreamWriterTest {
49 private QNameModule bazModule;
51 private QName outerContainer;
53 private QName myContainer1;
54 private QName myKeyedList;
55 private QName myKeyLeaf;
56 private QName myLeafInList1;
57 private QName myLeafInList2;
58 private QName myOrderedList;
59 private QName myKeyLeafInOrderedList;
60 private QName myLeafInOrderedList1;
61 private QName myLeafInOrderedList2;
62 private QName myUnkeyedList;
63 private QName myLeafInUnkeyedList;
64 private QName myLeaf1;
65 private QName myLeafList;
66 private QName myOrderedLeafList;
68 private QName myContainer2;
69 private QName innerContainer;
70 private QName myLeaf2;
71 private QName myLeaf3;
72 private QName myChoice;
73 private QName myLeafInCase2;
74 private QName myAnyxml;
76 private QName myContainer3;
77 private QName myDoublyKeyedList;
78 private QName myFirstKeyLeaf;
79 private QName mySecondKeyLeaf;
80 private QName myLeafInList3;
82 private DOMSource anyxmlDomSource;
85 public void setup() throws URISyntaxException, ParseException {
86 bazModule = QNameModule.create(new URI("baz-namespace"), SimpleDateFormatUtil.getRevisionFormat()
87 .parse("1970-01-01"));
89 outerContainer = QName.create(bazModule, "outer-container");
91 myContainer1 = QName.create(bazModule, "my-container-1");
92 myKeyedList = QName.create(bazModule, "my-keyed-list");
93 myKeyLeaf = QName.create(bazModule, "my-key-leaf");
94 myLeafInList1 = QName.create(bazModule, "my-leaf-in-list-1");
95 myLeafInList2 = QName.create(bazModule, "my-leaf-in-list-2");
96 myOrderedList = QName.create(bazModule, "my-ordered-list");
97 myKeyLeafInOrderedList = QName.create(bazModule, "my-key-leaf-in-ordered-list");
98 myLeafInOrderedList1 = QName.create(bazModule, "my-leaf-in-ordered-list-1");
99 myLeafInOrderedList2 = QName.create(bazModule, "my-leaf-in-ordered-list-2");
100 myUnkeyedList = QName.create(bazModule, "my-unkeyed-list");
101 myLeafInUnkeyedList = QName.create(bazModule, "my-leaf-in-unkeyed-list");
102 myLeaf1 = QName.create(bazModule, "my-leaf-1");
103 myLeafList = QName.create(bazModule, "my-leaf-list");
104 myOrderedLeafList = QName.create(bazModule, "my-ordered-leaf-list");
106 myContainer2 = QName.create(bazModule, "my-container-2");
107 innerContainer = QName.create(bazModule, "inner-container");
108 myLeaf2 = QName.create(bazModule, "my-leaf-2");
109 myLeaf3 = QName.create(bazModule, "my-leaf-3");
110 myChoice = QName.create(bazModule, "my-choice");
111 myLeafInCase2 = QName.create(bazModule, "my-leaf-in-case-2");
112 myAnyxml = QName.create(bazModule, "my-anyxml");
114 myContainer3 = QName.create(bazModule, "my-container-3");
115 myDoublyKeyedList = QName.create(bazModule, "my-doubly-keyed-list");
116 myFirstKeyLeaf = QName.create(bazModule, "my-first-key-leaf");
117 mySecondKeyLeaf = QName.create(bazModule, "my-second-key-leaf");
118 myLeafInList3 = QName.create(bazModule, "my-leaf-in-list-3");
120 anyxmlDomSource = new DOMSource();
124 public void testImmutableNormalizedNodeStreamWriter() throws IOException {
125 final NormalizedNodeResult result = new NormalizedNodeResult();
126 final NormalizedNodeStreamWriter immutableNormalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter
128 final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter
129 .forStreamWriter(immutableNormalizedNodeStreamWriter);
130 normalizedNodeWriter.write(buildOuterContainerNode());
132 final NormalizedNode<?, ?> output = result.getResult();
133 assertNotNull(output);
135 final NormalizedNode<?, ?> expectedNormalizedNode = buildOuterContainerNode();
136 assertNotNull(expectedNormalizedNode);
138 assertEquals(expectedNormalizedNode, output);
141 private NormalizedNode<?, ?> buildOuterContainerNode() {
143 MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(myKeyedList))
144 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
145 new NodeIdentifierWithPredicates(myKeyedList, myKeyLeaf, "listkeyvalue1"))
146 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList1))
147 .withValue("listleafvalue1").build())
148 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList2))
149 .withValue("listleafvalue2").build()).build())
150 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
151 new NodeIdentifierWithPredicates(myKeyedList, myKeyLeaf, "listkeyvalue2"))
152 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList1))
153 .withValue("listleafvalue12").build())
154 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList2))
155 .withValue("listleafvalue22").build()).build()).build();
157 OrderedMapNode myOrderedListNode = Builders.orderedMapBuilder().withNodeIdentifier(
158 new NodeIdentifier(myOrderedList))
159 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
160 new NodeIdentifierWithPredicates(myOrderedList, myKeyLeafInOrderedList, "olistkeyvalue1"))
161 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInOrderedList1))
162 .withValue("olistleafvalue1").build())
163 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInOrderedList2))
164 .withValue("olistleafvalue2").build()).build())
165 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
166 new NodeIdentifierWithPredicates(myOrderedList, myKeyLeafInOrderedList, "olistkeyvalue2"))
167 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInOrderedList1))
168 .withValue("olistleafvalue12").build())
169 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInOrderedList2))
170 .withValue("olistleafvalue22").build()).build()).build();
172 final UnkeyedListEntryNode unkeyedListEntry = Builders.unkeyedListEntryBuilder()
173 .withNodeIdentifier(new NodeIdentifier(myLeafInUnkeyedList))
174 .withChild(ImmutableNodes.leafNode(myLeafInUnkeyedList, "foo")).build();
176 final List<UnkeyedListEntryNode> unkeyedListEntries = new ArrayList<>();
177 unkeyedListEntries.add(unkeyedListEntry);
179 UnkeyedListNode myUnkeyedListNode = Builders.unkeyedListBuilder().withNodeIdentifier(
180 new NodeIdentifier(myUnkeyedList))
181 .withValue(unkeyedListEntries).build();
183 LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeaf1))
184 .withValue("value1").build();
186 LeafSetNode<?> myLeafListNode = Builders.leafSetBuilder().withNodeIdentifier(new NodeIdentifier(myLeafList))
187 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
188 new NodeWithValue<>(myLeafList, "lflvalue1")).withValue("lflvalue1").build())
189 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
190 new NodeWithValue<>(myLeafList, "lflvalue2")).withValue("lflvalue2").build()).build();
192 LeafSetNode<?> myOrderedLeafListNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
193 new NodeIdentifier(myOrderedLeafList))
194 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
195 new NodeWithValue<>(myOrderedLeafList, "olflvalue1")).withValue("olflvalue1").build())
196 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
197 new NodeWithValue<>(myOrderedLeafList, "olflvalue2")).withValue("olflvalue2").build()).build();
199 ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(
200 new NodeIdentifier(myContainer1))
201 .withChild(myKeyedListNode)
202 .withChild(myOrderedListNode)
203 .withChild(myUnkeyedListNode)
204 .withChild(myLeaf1Node)
205 .withChild(myLeafListNode)
206 .withChild(myOrderedLeafListNode)
210 ContainerNode innerContainerNode = Builders.containerBuilder().withNodeIdentifier(
211 new NodeIdentifier(innerContainer))
212 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeaf2))
213 .withValue("value2").build()).build();
215 LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeaf3))
216 .withValue("value3").build();
218 ChoiceNode myChoiceNode = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(myChoice))
219 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInCase2))
220 .withValue("case2value").build()).build();
222 AnyXmlNode myAnyxmlNode = Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(myAnyxml))
223 .withValue(anyxmlDomSource).build();
225 ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(
226 new NodeIdentifier(myContainer2))
227 .withChild(innerContainerNode)
228 .withChild(myLeaf3Node)
229 .withChild(myChoiceNode)
230 .withChild(myAnyxmlNode).build();
233 Map<QName, Object> keys = new HashMap<>();
234 keys.put(myFirstKeyLeaf, "listkeyvalue1");
235 keys.put(mySecondKeyLeaf, "listkeyvalue2");
237 MapNode myDoublyKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(myDoublyKeyedList))
238 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
239 new NodeIdentifierWithPredicates(myDoublyKeyedList, keys))
240 .withChild(Builders.leafBuilder().withNodeIdentifier(
241 new NodeIdentifier(myLeafInList3)).withValue("listleafvalue1").build()).build())
244 AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(
245 new AugmentationIdentifier(Sets.newHashSet(myDoublyKeyedList)))
246 .withChild(myDoublyKeyedListNode).build();
248 ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(
249 new NodeIdentifier(myContainer3))
250 .withChild(myDoublyKeyedListAugNode).build();
252 AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(
253 new AugmentationIdentifier(Sets.newHashSet(myContainer3)))
254 .withChild(myContainer3Node).build();
256 ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(
257 new NodeIdentifier(outerContainer))
258 .withChild(myContainer1Node)
259 .withChild(myContainer2Node)
260 .withChild(myContainer3AugNode).build();
262 return outerContainerNode;