2 * Copyright (c) 2014 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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.Iterables;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
22 import org.opendaylight.yang.gen.v1.urn.test.opendaylight.mdsal45.aug.norev.cont.cont.choice.ContAug;
23 import org.opendaylight.yang.gen.v1.urn.test.opendaylight.mdsal45.aug.norev.root.RootAug;
24 import org.opendaylight.yang.gen.v1.urn.test.opendaylight.mdsal45.base.norev.Cont;
25 import org.opendaylight.yang.gen.v1.urn.test.opendaylight.mdsal45.base.norev.Root;
26 import org.opendaylight.yang.gen.v1.urn.test.opendaylight.mdsal45.base.norev.cont.ContChoice;
27 import org.opendaylight.yang.gen.v1.urn.test.opendaylight.mdsal45.base.norev.cont.cont.choice.ContBase;
28 import org.opendaylight.yang.gen.v1.urn.test.opendaylight.mdsal45.base.norev.grp.GrpCont;
29 import org.opendaylight.yang.gen.v1.urn.test.opendaylight.mdsal45.base.norev.root.RootBase;
30 import org.opendaylight.yangtools.yang.binding.Identifier;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
39 public class InstanceIdentifierSerializeDeserializeTest extends AbstractBindingCodecTest {
40 public static final String TOP_LEVEL_LIST_KEY_VALUE = "foo";
42 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
43 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
44 .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
45 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY =
46 BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
47 private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES =
48 BA_TOP_LEVEL_LIST.augmentation(TreeComplexUsesAugment.class);
50 public static final QName TOP_QNAME = Top.QNAME;
51 public static final QName TOP_LEVEL_LIST_QNAME = QName.create(TOP_QNAME, "top-level-list");
52 public static final QName TOP_LEVEL_LIST_KEY = QName.create(TOP_QNAME, "name");
53 private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
55 public static final YangInstanceIdentifier BI_TOP_PATH = YangInstanceIdentifier.of(TOP_QNAME);
56 public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_PATH = BI_TOP_PATH.node(TOP_LEVEL_LIST_QNAME);
57 public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_1_PATH = BI_TOP_LEVEL_LIST_PATH
58 .node(NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY,
59 TOP_LEVEL_LIST_KEY_VALUE));
62 public void testYangIIToBindingAwareII() {
63 final InstanceIdentifier<?> instanceIdentifier = registry.fromYangInstanceIdentifier(BI_TOP_PATH);
64 assertEquals(Top.class, instanceIdentifier.getTargetType());
68 public void testYangIIToBindingAwareIIListWildcarded() {
69 final InstanceIdentifier<?> instanceIdentifier = registry.fromYangInstanceIdentifier(BI_TOP_LEVEL_LIST_PATH);
70 assertNull(instanceIdentifier);
74 public void testYangIIToBindingAwareIIListWithKey() {
75 final InstanceIdentifier<?> instanceIdentifier = registry.fromYangInstanceIdentifier(BI_TOP_LEVEL_LIST_1_PATH);
76 final InstanceIdentifier.PathArgument last = Iterables.getLast(instanceIdentifier.getPathArguments());
77 assertEquals(TopLevelList.class, instanceIdentifier.getTargetType());
78 assertFalse(instanceIdentifier.isWildcarded());
79 assertTrue(last instanceof InstanceIdentifier.IdentifiableItem);
80 final Identifier<?> key = ((InstanceIdentifier.IdentifiableItem<?, ?>) last).getKey();
81 assertEquals(TopLevelListKey.class, key.getClass());
82 assertEquals(TOP_LEVEL_LIST_KEY_VALUE, ((TopLevelListKey)key).getName());
86 public void testBindingAwareIIToYangIContainer() {
87 final YangInstanceIdentifier yangInstanceIdentifier = registry.toYangInstanceIdentifier(
88 InstanceIdentifier.create(Top.class).child(TopLevelList.class));
89 final PathArgument lastPathArgument = yangInstanceIdentifier.getLastPathArgument();
90 assertTrue(lastPathArgument instanceof NodeIdentifier);
91 assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
95 public void testBindingAwareIIToYangIIWildcard() {
96 final YangInstanceIdentifier yangInstanceIdentifier = registry.toYangInstanceIdentifier(
97 InstanceIdentifier.create(Top.class).child(TopLevelList.class));
98 final PathArgument lastPathArgument = yangInstanceIdentifier.getLastPathArgument();
99 assertTrue(lastPathArgument instanceof NodeIdentifier);
100 assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
104 public void testBindingAwareIIToYangIIListWithKey() {
105 final YangInstanceIdentifier yangInstanceIdentifier = registry.toYangInstanceIdentifier(
106 InstanceIdentifier.create(Top.class).child(TopLevelList.class, TOP_FOO_KEY));
107 final PathArgument lastPathArgument = yangInstanceIdentifier.getLastPathArgument();
108 assertTrue(lastPathArgument instanceof NodeIdentifierWithPredicates);
109 assertTrue(((NodeIdentifierWithPredicates) lastPathArgument).values().contains(TOP_LEVEL_LIST_KEY_VALUE));
110 assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
114 public void testBindingAwareIIToYangIIAugmentation() {
115 final PathArgument lastArg = registry.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
116 assertTrue(lastArg instanceof AugmentationIdentifier);
120 public void testBindingAwareIIToYangIILeafOnlyAugmentation() {
121 final PathArgument leafOnlyLastArg = registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY).getLastPathArgument();
122 assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
123 assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
127 public void testChoiceCaseGroupingFromBinding() {
128 final YangInstanceIdentifier contBase = registry.toYangInstanceIdentifier(
129 InstanceIdentifier.builder(Cont.class).child(ContBase.class, GrpCont.class).build());
130 assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
131 NodeIdentifier.create(ContChoice.QNAME), NodeIdentifier.create(GrpCont.QNAME)), contBase);
133 final YangInstanceIdentifier contAug = registry.toYangInstanceIdentifier(
134 InstanceIdentifier.builder(Cont.class).child(ContAug.class, GrpCont.class).build());
135 assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
136 NodeIdentifier.create(ContChoice.QNAME),
137 NodeIdentifier.create(GrpCont.QNAME.withModule(ContAug.QNAME.getModule()))), contAug);
139 // Legacy: downcast the child to Class, losing type safety but still working. Faced with ambiguity, it will
140 // select the lexically-lower class
141 assertEquals(1, ContBase.class.getCanonicalName().compareTo(ContAug.class.getCanonicalName()));
142 final YangInstanceIdentifier contAugLegacy = registry.toYangInstanceIdentifier(
143 InstanceIdentifier.builder(Cont.class).child((Class) GrpCont.class).build());
144 assertEquals(contAug, contAugLegacy);
146 final YangInstanceIdentifier rootBase = registry.toYangInstanceIdentifier(
147 InstanceIdentifier.builder(RootBase.class, GrpCont.class).build());
148 assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
149 NodeIdentifier.create(GrpCont.QNAME)), rootBase);
151 final YangInstanceIdentifier rootAug = registry.toYangInstanceIdentifier(
152 InstanceIdentifier.builder(RootAug.class, GrpCont.class).build());
153 assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
154 NodeIdentifier.create(GrpCont.QNAME.withModule(RootAug.QNAME.getModule()))), rootAug);
158 public void testChoiceCaseGroupingToBinding() {
159 final InstanceIdentifier<?> contBase = registry.fromYangInstanceIdentifier(
160 YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
161 NodeIdentifier.create(ContChoice.QNAME), NodeIdentifier.create(GrpCont.QNAME)));
162 assertEquals(InstanceIdentifier.builder(Cont.class).child(ContBase.class, GrpCont.class).build(), contBase);
164 final InstanceIdentifier<?> contAug = registry.fromYangInstanceIdentifier(
165 YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME), NodeIdentifier.create(ContChoice.QNAME),
166 NodeIdentifier.create(GrpCont.QNAME.withModule(ContAug.QNAME.getModule()))));
167 assertEquals(InstanceIdentifier.builder(Cont.class).child(ContAug.class, GrpCont.class).build(), contAug);
169 final InstanceIdentifier<?> rootBase = registry.fromYangInstanceIdentifier(
170 YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME), NodeIdentifier.create(GrpCont.QNAME)));
171 assertEquals(InstanceIdentifier.builder(RootBase.class, GrpCont.class).build(), rootBase);
173 final InstanceIdentifier<?> rootAug = registry.fromYangInstanceIdentifier(
174 YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
175 NodeIdentifier.create(GrpCont.QNAME.withModule(RootAug.QNAME.getModule()))));
176 assertEquals(InstanceIdentifier.builder(RootAug.class, GrpCont.class).build(), rootAug);