2 * Copyright (c) 2014 Brocade Communications 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
8 package org.opendaylight.controller.md.sal.common.impl.util.compat;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.Lists;
18 import com.google.common.collect.Maps;
19 import com.google.common.collect.Sets;
20 import java.util.AbstractMap;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.List;
26 import java.util.Map.Entry;
28 import org.junit.Test;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
31 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
35 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
36 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.Node;
38 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
53 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
54 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
55 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
56 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
58 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
59 import org.opendaylight.yangtools.yang.model.api.Module;
60 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
61 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
63 public class DataNormalizerTest {
65 static class NormalizedNodeData {
68 Object nodeData; // List for a container, value Object for a leaf
70 NormalizedNodeData(PathArgument nodeID, Class<?> nodeClass, Object nodeData) {
72 this.nodeClass = nodeClass;
73 this.nodeData = nodeData;
77 static class LegacyNodeData {
79 Object nodeData; // List for a CompositeNode, value Object for a
82 LegacyNodeData(QName nodeKey, Object nodeData) {
83 this.nodeKey = nodeKey;
84 this.nodeData = nodeData;
88 public String toString() {
89 return nodeKey.toString();
93 static final QName TEST_QNAME = QName.create(
94 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:normalization:test", "2014-03-13", "test");
95 static final QName OUTER_LIST_QNAME = QName.create(TEST_QNAME, "outer-list");
96 static final QName INNER_LIST_QNAME = QName.create(TEST_QNAME, "inner-list");
97 static final QName OUTER_CHOICE_QNAME = QName.create(TEST_QNAME, "outer-choice");
98 static final QName ID_QNAME = QName.create(TEST_QNAME, "id");
99 static final QName NAME_QNAME = QName.create(TEST_QNAME, "name");
100 static final QName VALUE_QNAME = QName.create(TEST_QNAME, "value");
102 static final InstanceIdentifier TEST_PATH = InstanceIdentifier.of(TEST_QNAME);
103 static final InstanceIdentifier OUTER_LIST_PATH = InstanceIdentifier.builder(TEST_PATH).node(OUTER_LIST_QNAME)
105 static final QName ONE_QNAME = QName.create(TEST_QNAME, "one");
106 static final QName TWO_QNAME = QName.create(TEST_QNAME, "two");
107 static final QName THREE_QNAME = QName.create(TEST_QNAME, "three");
109 static final QName ANY_XML_DATA_QNAME = QName.create(TEST_QNAME, "any-xml-data");
110 static final QName OUTER_CONTAINER_QNAME = QName.create(TEST_QNAME, "outer-container");
111 static final QName AUGMENTED_LEAF_QNAME = QName.create(TEST_QNAME, "augmented-leaf");
112 static final QName UNKEYED_LIST_QNAME = QName.create(TEST_QNAME, "unkeyed-list");
113 static final QName UNORDERED_LEAF_LIST_QNAME = QName.create(TEST_QNAME, "unordered-leaf-list");
114 static final QName ORDERED_LEAF_LIST_QNAME = QName.create(TEST_QNAME, "ordered-leaf-list");
116 static final Short OUTER_LIST_ID = (short) 10;
118 static final InstanceIdentifier OUTER_LIST_PATH_LEGACY = InstanceIdentifier.builder(TEST_QNAME)
119 .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, OUTER_LIST_ID).build();
121 static final InstanceIdentifier LEAF_TWO_PATH_LEGACY = InstanceIdentifier.builder(OUTER_LIST_PATH_LEGACY)
122 .node(TWO_QNAME).build();
124 static final QName ANY_XML_LEAF_QNAME = QName.create(TEST_QNAME, "leaf");;
125 static final QName ANY_XML_INNER_QNAME = QName.create(TEST_QNAME, "inner");
126 static final QName ANY_XML_INNER_LEAF_QNAME = QName.create(TEST_QNAME, "inner-leaf");
128 SchemaContext createTestContext() {
129 YangParserImpl parser = new YangParserImpl();
130 Set<Module> modules = parser.parseYangModelsFromStreams(Collections.singletonList(DataNormalizerTest.class
131 .getResourceAsStream("/normalization-test.yang")));
132 return parser.resolveSchemaContext(modules);
136 public void testToNormalizedInstanceIdentifier() {
137 SchemaContext testCtx = createTestContext();
138 DataNormalizer normalizer = new DataNormalizer(testCtx);
140 InstanceIdentifier normalizedPath = normalizer.toNormalized(LEAF_TWO_PATH_LEGACY);
142 verifyNormalizedInstanceIdentifier(normalizedPath, TEST_QNAME, OUTER_LIST_QNAME, new Object[] {
143 OUTER_LIST_QNAME, ID_QNAME, OUTER_LIST_ID }, OUTER_CHOICE_QNAME, TWO_QNAME);
146 private void verifyNormalizedInstanceIdentifier(InstanceIdentifier actual, Object... expPath) {
148 assertNotNull("Actual InstanceIdentifier is null", actual);
149 assertEquals("InstanceIdentifier path length", expPath.length, actual.getPath().size());
151 for (int i = 0; i < expPath.length; i++) {
152 PathArgument actualArg = actual.getPath().get(i);
153 if (expPath[i] instanceof Object[]) { // NodeIdentifierWithPredicates
154 Object[] exp = (Object[]) expPath[i];
155 assertEquals("Actual path arg " + (i + 1) + " class", NodeIdentifierWithPredicates.class,
156 actualArg.getClass());
157 NodeIdentifierWithPredicates actualNode = (NodeIdentifierWithPredicates) actualArg;
158 assertEquals("Actual path arg " + (i + 1) + " node type", exp[0], actualNode.getNodeType());
159 assertEquals("Actual path arg " + (i + 1) + " key values map size", 1, actualNode.getKeyValues().size());
160 Entry<QName, Object> keyValuesEntry = actualNode.getKeyValues().entrySet().iterator().next();
161 assertEquals("Actual path arg " + (i + 1) + " key values map key", exp[1], keyValuesEntry.getKey());
162 assertEquals("Actual path arg " + (i + 1) + " key values map value", exp[2], keyValuesEntry.getValue());
163 } else if (expPath[i] instanceof Set) { // AugmentationIdentifier
164 assertEquals("Actual path arg " + (i + 1) + " class", AugmentationIdentifier.class,
165 actualArg.getClass());
166 AugmentationIdentifier actualNode = (AugmentationIdentifier) actualArg;
167 assertEquals("Actual path arg " + (i + 1) + " PossibleChildNames", expPath[i],
168 actualNode.getPossibleChildNames());
170 assertEquals("Actual path arg " + (i + 1) + " node type", expPath[i], actualArg.getNodeType());
176 public void testToLegacyInstanceIdentifier() throws DataNormalizationException {
178 DataNormalizer normalizer = new DataNormalizer(createTestContext());
180 InstanceIdentifier normalized = InstanceIdentifier.builder().node(TEST_QNAME).node(OUTER_LIST_QNAME)
181 .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, OUTER_LIST_ID).node(OUTER_CHOICE_QNAME).node(TWO_QNAME)
184 InstanceIdentifier legacy = normalizer.toLegacy(normalized);
186 assertEquals("Legacy InstanceIdentifier", LEAF_TWO_PATH_LEGACY, legacy);
190 public void testToLegacyNormalizedNode() {
192 ChoiceNode choiceNode1 = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(OUTER_CHOICE_QNAME))
193 .withChild(ImmutableNodes.leafNode(TWO_QNAME, "two"))
194 .withChild(ImmutableNodes.leafNode(THREE_QNAME, "three")).build();
196 MapEntryNode innerListEntryNode1 = Builders.mapEntryBuilder()
197 .withNodeIdentifier(new NodeIdentifierWithPredicates(INNER_LIST_QNAME, NAME_QNAME, "inner-name1"))
198 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "inner-name1"))
199 .withChild(ImmutableNodes.leafNode(VALUE_QNAME, "inner-value1")).build();
201 MapEntryNode innerListEntryNode2 = Builders.mapEntryBuilder()
202 .withNodeIdentifier(new NodeIdentifierWithPredicates(INNER_LIST_QNAME, NAME_QNAME, "inner-name2"))
203 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "inner-name2"))
204 .withChild(ImmutableNodes.leafNode(VALUE_QNAME, "inner-value2")).build();
206 OrderedMapNode innerListNode = Builders.orderedMapBuilder()
207 .withNodeIdentifier(new NodeIdentifier(INNER_LIST_QNAME)).withChild(innerListEntryNode1)
208 .withChild(innerListEntryNode2).build();
210 Short outerListID1 = Short.valueOf((short) 10);
211 MapEntryNode outerListEntryNode1 = Builders.mapEntryBuilder()
212 .withNodeIdentifier(new NodeIdentifierWithPredicates(OUTER_LIST_QNAME, ID_QNAME, outerListID1))
213 .withChild(ImmutableNodes.leafNode(ID_QNAME, outerListID1)).withChild(choiceNode1)
214 .withChild(innerListNode).build();
216 ChoiceNode choiceNode2 = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(OUTER_CHOICE_QNAME))
217 .withChild(ImmutableNodes.leafNode(ONE_QNAME, "one")).build();
219 Short outerListID2 = Short.valueOf((short) 20);
220 MapEntryNode outerListEntryNode2 = Builders.mapEntryBuilder()
221 .withNodeIdentifier(new NodeIdentifierWithPredicates(OUTER_LIST_QNAME, ID_QNAME, outerListID2))
222 .withChild(ImmutableNodes.leafNode(ID_QNAME, outerListID2)).withChild(choiceNode2).build();
224 MapNode outerListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(OUTER_LIST_QNAME))
225 .withChild(outerListEntryNode1).withChild(outerListEntryNode2).build();
227 UnkeyedListEntryNode unkeyedListEntryNode1 = Builders.unkeyedListEntryBuilder()
228 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
229 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "unkeyed1")).build();
231 UnkeyedListEntryNode unkeyedListEntryNode2 = Builders.unkeyedListEntryBuilder()
232 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
233 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "unkeyed2")).build();
235 UnkeyedListNode unkeyedListNode = Builders.unkeyedListBuilder()
236 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME)).withChild(unkeyedListEntryNode1)
237 .withChild(unkeyedListEntryNode2).build();
239 ContainerNode testContainerNode = Builders.containerBuilder()
240 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME)).withChild(outerListNode).withChild(unkeyedListNode)
243 Node<?> legacyNode = DataNormalizer.toLegacy(testContainerNode);
251 expectSimpleNode(ID_QNAME, outerListID1),
252 expectSimpleNode(TWO_QNAME, "two"),
253 expectSimpleNode(THREE_QNAME, "three"),
255 expectCompositeNode(INNER_LIST_QNAME, expectSimpleNode(NAME_QNAME, "inner-name1"),
256 expectSimpleNode(VALUE_QNAME, "inner-value1")),
258 expectCompositeNode(INNER_LIST_QNAME, expectSimpleNode(NAME_QNAME, "inner-name2"),
259 expectSimpleNode(VALUE_QNAME, "inner-value2"))),
260 expectCompositeNode(OUTER_LIST_QNAME, expectSimpleNode(ID_QNAME, outerListID2),
261 expectSimpleNode(ONE_QNAME, "one")),
262 expectCompositeNode(UNKEYED_LIST_QNAME, expectSimpleNode(NAME_QNAME, "unkeyed1")),
263 expectCompositeNode(UNKEYED_LIST_QNAME, expectSimpleNode(NAME_QNAME, "unkeyed2"))));
265 // Conversion of Mixin type nodes is not supported.
267 assertNull("Expected null returned for Mixin type node", DataNormalizer.toLegacy(outerListNode));
271 * Following data are constructed: <any-xml-data> <inner>
272 * <inner-leaf>inner-leaf-value</inner-leaf> </inner>
273 * <leaf>leaf-value</leaf> <any-xml-data>
276 public void testToLegacyNormalizedNodeWithAnyXml() {
278 Node<?> innerLeafChild = NodeFactory.createImmutableSimpleNode(ANY_XML_INNER_LEAF_QNAME, null,
280 CompositeNode innerContainer = NodeFactory.createImmutableCompositeNode(ANY_XML_INNER_QNAME, null,
281 Collections.<Node<?>> singletonList(innerLeafChild));
283 Node<?> leafChild = NodeFactory.createImmutableSimpleNode(ANY_XML_LEAF_QNAME, null, "leaf-value");
284 CompositeNode anyXmlNodeValue = NodeFactory.createImmutableCompositeNode(ANY_XML_DATA_QNAME, null,
285 Arrays.asList(leafChild, innerContainer));
287 AnyXmlNode testAnyXmlNode = Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TEST_QNAME))
288 .withValue(anyXmlNodeValue).build();
290 ContainerNode testContainerNode = Builders.containerBuilder()
291 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME)).withChild(testAnyXmlNode).build();
293 DataNormalizer normalizer = new DataNormalizer(createTestContext());
294 Node<?> legacyNode = normalizer.toLegacy(InstanceIdentifier.builder(TEST_QNAME).build(), testContainerNode);
302 expectSimpleNode(ANY_XML_LEAF_QNAME, "leaf-value"),
303 expectCompositeNode(ANY_XML_INNER_QNAME,
304 expectSimpleNode(ANY_XML_INNER_LEAF_QNAME, "inner-leaf-value")))));
308 public void testToLegacyNormalizedNodeWithLeafLists() {
310 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
311 testBuilder.setQName(TEST_QNAME);
313 ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders.leafSetBuilder()
314 .withNodeIdentifier(new NodeIdentifier(UNORDERED_LEAF_LIST_QNAME));
315 for (int i = 1; i <= 3; i++) {
316 leafSetBuilder.withChildValue("unordered-value" + i);
319 ListNodeBuilder<Object, LeafSetEntryNode<Object>> orderedLeafSetBuilder = Builders.orderedLeafSetBuilder()
320 .withNodeIdentifier(new NodeIdentifier(ORDERED_LEAF_LIST_QNAME));
321 for (int i = 3; i > 0; i--) {
322 orderedLeafSetBuilder.withChildValue("ordered-value" + i);
325 ContainerNode testContainerNode = Builders.containerBuilder()
326 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME)).withChild(leafSetBuilder.build())
327 .withChild(orderedLeafSetBuilder.build()).build();
329 DataNormalizer normalizer = new DataNormalizer(createTestContext());
331 Node<?> legacyNode = normalizer.toLegacy(InstanceIdentifier.builder(TEST_QNAME).build(), testContainerNode);
335 expectCompositeNode(TEST_QNAME, expectSimpleNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value1"),
336 expectSimpleNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value2"),
337 expectSimpleNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value3"),
338 expectSimpleNode(ORDERED_LEAF_LIST_QNAME, "ordered-value3"),
339 expectSimpleNode(ORDERED_LEAF_LIST_QNAME, "ordered-value2"),
340 expectSimpleNode(ORDERED_LEAF_LIST_QNAME, "ordered-value1")));
344 public void testToLegacyNormalizedNodeWithAugmentation() {
346 AugmentationNode augmentationNode = Builders.augmentationBuilder()
347 .withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(AUGMENTED_LEAF_QNAME)))
348 .withChild(ImmutableNodes.leafNode(AUGMENTED_LEAF_QNAME, "augmented-value")).build();
350 ContainerNode outerContainerNode = Builders.containerBuilder()
351 .withNodeIdentifier(new NodeIdentifier(OUTER_CONTAINER_QNAME)).withChild(augmentationNode).build();
353 ContainerNode testContainerNode = Builders.containerBuilder()
354 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME)).withChild(outerContainerNode).build();
356 DataNormalizer normalizer = new DataNormalizer(createTestContext());
358 Node<?> legacyNode = normalizer.toLegacy(InstanceIdentifier.builder(TEST_QNAME).build(), testContainerNode);
364 expectCompositeNode(OUTER_CONTAINER_QNAME,
365 expectSimpleNode(AUGMENTED_LEAF_QNAME, "augmented-value"))));
368 private boolean isOrdered(QName nodeName) {
369 return ORDERED_LEAF_LIST_QNAME.equals(nodeName) || INNER_LIST_QNAME.equals(nodeName);
372 @SuppressWarnings("unchecked")
373 private void verifyLegacyNode(Node<?> actual, LegacyNodeData expNodeData) {
375 assertNotNull("Actual Node is null", actual);
376 assertTrue("Expected CompositeNode instance", actual instanceof CompositeNode);
377 CompositeNode actualCN = (CompositeNode) actual;
378 assertEquals("Node key", expNodeData.nodeKey, actualCN.getKey());
380 List<LegacyNodeData> expChildData = Lists.newArrayList();
381 List<LegacyNodeData> unorderdChildData = Lists.newArrayList();
382 for (LegacyNodeData data : (List<LegacyNodeData>) expNodeData.nodeData) {
383 if (isOrdered(data.nodeKey)) {
384 expChildData.add(data);
386 unorderdChildData.add(data);
390 Collections.sort(unorderdChildData, new Comparator<LegacyNodeData>() {
392 public int compare(LegacyNodeData arg1, LegacyNodeData arg2) {
393 String str1 = arg1.nodeKey.getLocalName();
394 if (!(arg1.nodeData instanceof List))
395 str1 += arg1.nodeData; // add simple node value
397 String str2 = arg2.nodeKey.getLocalName();
398 if (!(arg2.nodeData instanceof List))
399 str2 += arg2.nodeData; // add simple node value
401 return str1.compareTo(str2);
405 expChildData.addAll(unorderdChildData);
407 List<Node<?>> actualChildNodes = Lists.newArrayList();
408 List<Node<?>> unorderedChildNodes = Lists.newArrayList();
409 for (Node<?> node : actualCN.getValue()) {
410 if (isOrdered(node.getKey())) {
411 actualChildNodes.add(node);
413 unorderedChildNodes.add(node);
417 Collections.sort(unorderedChildNodes, new Comparator<Node<?>>() {
419 public int compare(Node<?> n1, Node<?> n2) {
420 String str1 = n1.getKey().getLocalName();
421 if (n1 instanceof SimpleNode)
422 str1 += ((SimpleNode<?>) n1).getValue();
424 String str2 = n2.getKey().getLocalName();
425 if (n2 instanceof SimpleNode)
426 str2 += ((SimpleNode<?>) n2).getValue();
428 return str1.compareTo(str2);
432 actualChildNodes.addAll(unorderedChildNodes);
434 for (Node<?> actualChild : actualChildNodes) {
435 LegacyNodeData expData = expChildData.isEmpty() ? null : expChildData.remove(0);
436 assertNotNull("Unexpected child node with key " + actualChild.getKey(), expData);
437 assertEquals("Child node QName", expData.nodeKey, actualChild.getKey());
439 if (expData.nodeData instanceof List) { // List represents a
441 verifyLegacyNode(actualChild, expData);
442 } else { // else a simple node
443 assertTrue("Expected SimpleNode instance", actualChild instanceof SimpleNode);
444 assertEquals("Child node value with key " + actualChild.getKey(), expData.nodeData,
445 ((SimpleNode<?>) actualChild).getValue());
449 if (!expChildData.isEmpty()) {
450 fail("Missing child nodes: " + expChildData);
454 private LegacyNodeData expectCompositeNode(QName key, LegacyNodeData... childData) {
455 return new LegacyNodeData(key, Lists.newArrayList(childData));
458 private LegacyNodeData expectSimpleNode(QName key, Object value) {
459 return new LegacyNodeData(key, value);
463 public void testToNormalizedCompositeNode() {
464 SchemaContext testCtx = createTestContext();
465 DataNormalizer normalizer = new DataNormalizer(testCtx);
467 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
468 testBuilder.setQName(TEST_QNAME);
470 CompositeNodeBuilder<ImmutableCompositeNode> outerListBuilder = ImmutableCompositeNode.builder();
471 outerListBuilder.setQName(OUTER_LIST_QNAME);
472 outerListBuilder.addLeaf(ID_QNAME, 10);
473 outerListBuilder.addLeaf(ONE_QNAME, "one");
475 for (int i = 3; i > 0; i--) {
476 CompositeNodeBuilder<ImmutableCompositeNode> innerListBuilder = ImmutableCompositeNode.builder();
477 innerListBuilder.setQName(INNER_LIST_QNAME);
478 innerListBuilder.addLeaf(NAME_QNAME, "inner-name" + i);
479 innerListBuilder.addLeaf(VALUE_QNAME, "inner-value" + i);
480 outerListBuilder.add(innerListBuilder.toInstance());
483 testBuilder.add(outerListBuilder.toInstance());
485 outerListBuilder = ImmutableCompositeNode.builder();
486 outerListBuilder.setQName(OUTER_LIST_QNAME);
487 outerListBuilder.addLeaf(ID_QNAME, 20);
488 outerListBuilder.addLeaf(TWO_QNAME, "two");
489 outerListBuilder.addLeaf(THREE_QNAME, "three");
490 testBuilder.add(outerListBuilder.toInstance());
492 for (int i = 1; i <= 2; i++) {
493 CompositeNodeBuilder<ImmutableCompositeNode> unkeyedListBuilder = ImmutableCompositeNode.builder();
494 unkeyedListBuilder.setQName(UNKEYED_LIST_QNAME);
495 unkeyedListBuilder.addLeaf(NAME_QNAME, "unkeyed-name" + i);
496 testBuilder.add(unkeyedListBuilder.toInstance());
499 Entry<InstanceIdentifier, NormalizedNode<?, ?>> normalizedNodeEntry = normalizer
500 .toNormalized(new AbstractMap.SimpleEntry<InstanceIdentifier, CompositeNode>(new InstanceIdentifier(
501 ImmutableList.<PathArgument> of(new NodeIdentifier(TEST_QNAME))), testBuilder.toInstance()));
503 verifyNormalizedInstanceIdentifier(normalizedNodeEntry.getKey(), TEST_QNAME);
505 verifyNormalizedNode(
506 normalizedNodeEntry.getValue(),
515 expectLeafNode(ID_QNAME, 10),
516 expectChoiceNode(OUTER_CHOICE_QNAME, expectLeafNode(ONE_QNAME, "one")),
517 expectOrderedMapNode(
519 expectMapEntryNode(INNER_LIST_QNAME, NAME_QNAME, "inner-name3",
520 expectLeafNode(NAME_QNAME, "inner-name3"),
521 expectLeafNode(VALUE_QNAME, "inner-value3")),
522 expectMapEntryNode(INNER_LIST_QNAME, NAME_QNAME, "inner-name2",
523 expectLeafNode(NAME_QNAME, "inner-name2"),
524 expectLeafNode(VALUE_QNAME, "inner-value2")),
525 expectMapEntryNode(INNER_LIST_QNAME, NAME_QNAME, "inner-name1",
526 expectLeafNode(NAME_QNAME, "inner-name1"),
527 expectLeafNode(VALUE_QNAME, "inner-value1")))),
532 expectLeafNode(ID_QNAME, 20),
533 expectChoiceNode(OUTER_CHOICE_QNAME, expectLeafNode(TWO_QNAME, "two"),
534 expectLeafNode(THREE_QNAME, "three")))),
535 expectUnkeyedListNode(
537 expectUnkeyedListEntryNode(UNKEYED_LIST_QNAME,
538 expectLeafNode(NAME_QNAME, "unkeyed-name1")),
539 expectUnkeyedListEntryNode(UNKEYED_LIST_QNAME,
540 expectLeafNode(NAME_QNAME, "unkeyed-name2")))));
544 public void testToNormalizedCompositeNodeWithAnyXml() {
545 SchemaContext testCtx = createTestContext();
546 DataNormalizer normalizer = new DataNormalizer(testCtx);
548 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
549 testBuilder.setQName(TEST_QNAME);
551 CompositeNodeBuilder<ImmutableCompositeNode> anyXmlBuilder = ImmutableCompositeNode.builder();
552 anyXmlBuilder.setQName(ANY_XML_DATA_QNAME);
553 anyXmlBuilder.addLeaf(ANY_XML_LEAF_QNAME, "leaf-value");
555 CompositeNodeBuilder<ImmutableCompositeNode> innerBuilder = ImmutableCompositeNode.builder();
556 innerBuilder.setQName(ANY_XML_INNER_QNAME);
557 innerBuilder.addLeaf(ANY_XML_INNER_LEAF_QNAME, "inner-leaf-value");
559 anyXmlBuilder.add(innerBuilder.toInstance());
560 CompositeNode anyXmlLegacy = anyXmlBuilder.toInstance();
561 testBuilder.add(anyXmlLegacy);
563 Entry<InstanceIdentifier, NormalizedNode<?, ?>> normalizedNodeEntry = normalizer
564 .toNormalized(new AbstractMap.SimpleEntry<InstanceIdentifier, CompositeNode>(new InstanceIdentifier(
565 ImmutableList.<PathArgument> of(new NodeIdentifier(TEST_QNAME))), testBuilder.toInstance()));
567 verifyNormalizedInstanceIdentifier(normalizedNodeEntry.getKey(), TEST_QNAME);
569 verifyNormalizedNode(normalizedNodeEntry.getValue(),
570 expectContainerNode(TEST_QNAME, expectAnyXmlNode(ANY_XML_DATA_QNAME, anyXmlLegacy)));
574 public void testToNormalizedCompositeNodeWithAugmentation() {
575 SchemaContext testCtx = createTestContext();
576 DataNormalizer normalizer = new DataNormalizer(testCtx);
578 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
579 testBuilder.setQName(TEST_QNAME);
581 CompositeNodeBuilder<ImmutableCompositeNode> outerContBuilder = ImmutableCompositeNode.builder();
582 outerContBuilder.setQName(OUTER_CONTAINER_QNAME);
583 outerContBuilder.addLeaf(AUGMENTED_LEAF_QNAME, "augmented-value");
585 testBuilder.add(outerContBuilder.toInstance());
587 Entry<InstanceIdentifier, NormalizedNode<?, ?>> normalizedNodeEntry = normalizer
588 .toNormalized(new AbstractMap.SimpleEntry<InstanceIdentifier, CompositeNode>(new InstanceIdentifier(
589 ImmutableList.<PathArgument> of(new NodeIdentifier(TEST_QNAME))), testBuilder.toInstance()));
591 verifyNormalizedInstanceIdentifier(normalizedNodeEntry.getKey(), TEST_QNAME);
593 NormalizedNodeData expAugmentation = expectAugmentation(AUGMENTED_LEAF_QNAME,
594 expectLeafNode(AUGMENTED_LEAF_QNAME, "augmented-value"));
596 verifyNormalizedNode(normalizedNodeEntry.getValue(),
597 expectContainerNode(TEST_QNAME, expectContainerNode(OUTER_CONTAINER_QNAME, expAugmentation)));
599 normalizedNodeEntry = normalizer.toNormalized(new AbstractMap.SimpleEntry<InstanceIdentifier, CompositeNode>(
600 new InstanceIdentifier(Lists.newArrayList(new NodeIdentifier(TEST_QNAME), new NodeIdentifier(
601 OUTER_CONTAINER_QNAME))), outerContBuilder.toInstance()));
603 verifyNormalizedInstanceIdentifier(normalizedNodeEntry.getKey(), TEST_QNAME, OUTER_CONTAINER_QNAME,
604 Sets.newHashSet(AUGMENTED_LEAF_QNAME));
606 verifyNormalizedNode(normalizedNodeEntry.getValue(), expAugmentation);
610 public void testToNormalizedCompositeNodeWithLeafLists() {
611 SchemaContext testCtx = createTestContext();
612 DataNormalizer normalizer = new DataNormalizer(testCtx);
614 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
615 testBuilder.setQName(TEST_QNAME);
617 for (int i = 1; i <= 3; i++) {
618 testBuilder.addLeaf(UNORDERED_LEAF_LIST_QNAME, "unordered-value" + i);
621 for (int i = 3; i > 0; i--) {
622 testBuilder.addLeaf(ORDERED_LEAF_LIST_QNAME, "ordered-value" + i);
625 Entry<InstanceIdentifier, NormalizedNode<?, ?>> normalizedNodeEntry = normalizer
626 .toNormalized(new AbstractMap.SimpleEntry<InstanceIdentifier, CompositeNode>(new InstanceIdentifier(
627 ImmutableList.<PathArgument> of(new NodeIdentifier(TEST_QNAME))), testBuilder.toInstance()));
629 verifyNormalizedInstanceIdentifier(normalizedNodeEntry.getKey(), TEST_QNAME);
631 verifyNormalizedNode(
632 normalizedNodeEntry.getValue(),
635 expectLeafSetNode(UNORDERED_LEAF_LIST_QNAME,
636 expectLeafSetEntryNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value1"),
637 expectLeafSetEntryNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value2"),
638 expectLeafSetEntryNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value3")),
639 expectOrderedLeafSetNode(ORDERED_LEAF_LIST_QNAME,
640 expectLeafSetEntryNode(ORDERED_LEAF_LIST_QNAME, "ordered-value3"),
641 expectLeafSetEntryNode(ORDERED_LEAF_LIST_QNAME, "ordered-value2"),
642 expectLeafSetEntryNode(ORDERED_LEAF_LIST_QNAME, "ordered-value1"))));
645 @SuppressWarnings("unchecked")
646 private void verifyNormalizedNode(NormalizedNode<?, ?> actual, NormalizedNodeData expNodeData) {
648 Class<?> expNodeClass = expNodeData.nodeClass;
649 PathArgument expNodeID = expNodeData.nodeID;
651 assertNotNull("Actual NormalizedNode is null", actual);
652 assertTrue("NormalizedNode instance " + actual.getClass() + " is not derived from " + expNodeClass,
653 expNodeClass.isAssignableFrom(actual.getClass()));
654 assertEquals("NormalizedNode identifier", expNodeID, actual.getIdentifier());
656 if (expNodeData.nodeData instanceof List) {
657 Map<PathArgument, Integer> orderingMap = null;
658 if (expNodeClass.equals(OrderedMapNode.class) || expNodeClass.equals(OrderedLeafSetNode.class)) {
659 orderingMap = Maps.newHashMap();
663 Map<PathArgument, NormalizedNodeData> expChildDataMap = Maps.newHashMap();
664 List<NormalizedNodeData> expChildDataList = (List<NormalizedNodeData>) expNodeData.nodeData;
665 for (NormalizedNodeData data : expChildDataList) {
666 expChildDataMap.put(data.nodeID, data);
668 if (orderingMap != null) {
669 orderingMap.put(data.nodeID, i++);
673 assertNotNull("Actual value is null for node " + actual.getIdentifier(), actual.getValue());
674 assertTrue("Expected value instance Iterable for node " + actual.getIdentifier(),
675 Iterable.class.isAssignableFrom(actual.getValue().getClass()));
678 for (NormalizedNode<?, ?> actualChild : (Iterable<NormalizedNode<?, ?>>) actual.getValue()) {
679 NormalizedNodeData expChildData = expNodeClass.equals(UnkeyedListNode.class) ? expChildDataList
680 .remove(0) : expChildDataMap.remove(actualChild.getIdentifier());
683 "Unexpected child node " + actualChild.getClass() + " with identifier "
684 + actualChild.getIdentifier() + " for parent node " + actual.getClass()
685 + " with identifier " + actual.getIdentifier(), expChildData);
687 if (orderingMap != null) {
688 assertEquals("Order index for child node " + actualChild.getIdentifier(),
689 orderingMap.get(actualChild.getIdentifier()), Integer.valueOf(i));
692 verifyNormalizedNode(actualChild, expChildData);
696 if (expNodeClass.equals(UnkeyedListNode.class)) {
697 if (expChildDataList.size() > 0) {
698 fail("Missing " + expChildDataList.size() + " child nodes for parent " + actual.getIdentifier());
701 if (!expChildDataMap.isEmpty()) {
702 fail("Missing child nodes for parent " + actual.getIdentifier() + ": " + expChildDataMap.keySet());
706 assertEquals("Leaf value for node " + actual.getIdentifier(), expNodeData.nodeData, actual.getValue());
710 private NormalizedNodeData expectOrderedLeafSetNode(QName nodeName, NormalizedNodeData... childData) {
711 return new NormalizedNodeData(new NodeIdentifier(nodeName), OrderedLeafSetNode.class,
712 Lists.newArrayList(childData));
715 private NormalizedNodeData expectLeafSetNode(QName nodeName, NormalizedNodeData... childData) {
716 return new NormalizedNodeData(new NodeIdentifier(nodeName), LeafSetNode.class, Lists.newArrayList(childData));
719 private NormalizedNodeData expectLeafSetEntryNode(QName nodeName, Object value) {
720 return new NormalizedNodeData(new NodeWithValue(nodeName, value), LeafSetEntryNode.class, value);
723 private NormalizedNodeData expectUnkeyedListNode(QName nodeName, NormalizedNodeData... childData) {
724 return new NormalizedNodeData(new NodeIdentifier(nodeName), UnkeyedListNode.class,
725 Lists.newArrayList(childData));
728 private NormalizedNodeData expectUnkeyedListEntryNode(QName nodeName, NormalizedNodeData... childData) {
729 return new NormalizedNodeData(new NodeIdentifier(nodeName), UnkeyedListEntryNode.class,
730 Lists.newArrayList(childData));
733 private NormalizedNodeData expectAugmentation(QName augmentedNodeName, NormalizedNodeData... childData) {
734 return new NormalizedNodeData(new AugmentationIdentifier(Sets.newHashSet(augmentedNodeName)),
735 AugmentationNode.class, Lists.newArrayList(childData));
738 private NormalizedNodeData expectAnyXmlNode(QName nodeName, Object value) {
739 return new NormalizedNodeData(new NodeIdentifier(nodeName), AnyXmlNode.class, value);
742 private NormalizedNodeData expectContainerNode(QName nodeName, NormalizedNodeData... childData) {
743 return new NormalizedNodeData(new NodeIdentifier(nodeName), ContainerNode.class, Lists.newArrayList(childData));
746 private NormalizedNodeData expectChoiceNode(QName nodeName, NormalizedNodeData... childData) {
747 return new NormalizedNodeData(new NodeIdentifier(nodeName), ChoiceNode.class, Lists.newArrayList(childData));
750 private NormalizedNodeData expectLeafNode(QName nodeName, Object value) {
751 return new NormalizedNodeData(new NodeIdentifier(nodeName), LeafNode.class, value);
755 private NormalizedNodeData expectMapEntryNode(QName nodeName, QName key, Object value,
756 NormalizedNodeData... childData) {
757 return new NormalizedNodeData(new NodeIdentifierWithPredicates(nodeName, key, value), MapEntryNode.class,
758 Lists.newArrayList(childData));
761 private NormalizedNodeData expectMapNode(QName key, NormalizedNodeData... childData) {
762 return new NormalizedNodeData(new NodeIdentifier(key), MapNode.class, Lists.newArrayList(childData));
765 private NormalizedNodeData expectOrderedMapNode(QName key, NormalizedNodeData... childData) {
766 return new NormalizedNodeData(new NodeIdentifier(key), OrderedMapNode.class, Lists.newArrayList(childData));