Make sure we compare key members via their property name
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / test / InstanceIdentifierSerializeDeserializeTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  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.mdsal.binding.dom.codec.test;
9
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;
14
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;
38
39 public class InstanceIdentifierSerializeDeserializeTest extends AbstractBindingCodecTest {
40     public static final String TOP_LEVEL_LIST_KEY_VALUE = "foo";
41
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);
49
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");
54
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(new NodeIdentifierWithPredicates(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY,
59                 TOP_LEVEL_LIST_KEY_VALUE));
60
61     @Test
62     public void testYangIIToBindingAwareII() {
63         final InstanceIdentifier<?> instanceIdentifier = registry.fromYangInstanceIdentifier(BI_TOP_PATH);
64         assertEquals(Top.class, instanceIdentifier.getTargetType());
65     }
66
67     @Test
68     public void testYangIIToBindingAwareIIListWildcarded() {
69         final InstanceIdentifier<?> instanceIdentifier = registry.fromYangInstanceIdentifier(BI_TOP_LEVEL_LIST_PATH);
70         assertNull(instanceIdentifier);
71     }
72
73     @Test
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());
83     }
84
85     @Test
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());
92     }
93
94     @Test
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());
101     }
102
103     @Test
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).getKeyValues().containsValue(
110             TOP_LEVEL_LIST_KEY_VALUE));
111         assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
112     }
113
114     @Test
115     public void testBindingAwareIIToYangIIAugmentation() {
116         final PathArgument lastArg = registry.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
117         assertTrue(lastArg instanceof AugmentationIdentifier);
118     }
119
120     @Test
121     public void testBindingAwareIIToYangIILeafOnlyAugmentation() {
122         final PathArgument leafOnlyLastArg = registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY).getLastPathArgument();
123         assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
124         assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
125     }
126
127     @Test
128     public void testChoiceCaseGroupingFromBinding() {
129         final YangInstanceIdentifier contBase = registry.toYangInstanceIdentifier(
130             InstanceIdentifier.builder(Cont.class).child(ContBase.class, GrpCont.class).build());
131         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
132             NodeIdentifier.create(ContChoice.QNAME), NodeIdentifier.create(GrpCont.QNAME)), contBase);
133
134         final YangInstanceIdentifier contAug = registry.toYangInstanceIdentifier(
135             InstanceIdentifier.builder(Cont.class).child(ContAug.class, GrpCont.class).build());
136         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
137             NodeIdentifier.create(ContChoice.QNAME),
138             NodeIdentifier.create(GrpCont.QNAME.withModule(ContAug.QNAME.getModule()))), contAug);
139
140         // Legacy: downcast the child to Class, losing type safety but still working. Faced with ambiguity, it will
141         //         select the lexically-lower class
142         assertEquals(1, ContBase.class.getCanonicalName().compareTo(ContAug.class.getCanonicalName()));
143         final YangInstanceIdentifier contAugLegacy = registry.toYangInstanceIdentifier(
144             InstanceIdentifier.builder(Cont.class).child((Class) GrpCont.class).build());
145         assertEquals(contAug, contAugLegacy);
146
147         final YangInstanceIdentifier rootBase = registry.toYangInstanceIdentifier(
148             InstanceIdentifier.builder(RootBase.class, GrpCont.class).build());
149         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
150             NodeIdentifier.create(GrpCont.QNAME)), rootBase);
151
152         final YangInstanceIdentifier rootAug = registry.toYangInstanceIdentifier(
153             InstanceIdentifier.builder(RootAug.class, GrpCont.class).build());
154         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
155             NodeIdentifier.create(GrpCont.QNAME.withModule(RootAug.QNAME.getModule()))), rootAug);
156     }
157
158     @Test
159     public void testChoiceCaseGroupingToBinding() {
160         final InstanceIdentifier<?> contBase = registry.fromYangInstanceIdentifier(
161             YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
162             NodeIdentifier.create(ContChoice.QNAME), NodeIdentifier.create(GrpCont.QNAME)));
163         assertEquals(InstanceIdentifier.builder(Cont.class).child(ContBase.class, GrpCont.class).build(), contBase);
164
165         final InstanceIdentifier<?> contAug = registry.fromYangInstanceIdentifier(
166             YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME), NodeIdentifier.create(ContChoice.QNAME),
167                 NodeIdentifier.create(GrpCont.QNAME.withModule(ContAug.QNAME.getModule()))));
168         assertEquals(InstanceIdentifier.builder(Cont.class).child(ContAug.class, GrpCont.class).build(), contAug);
169
170         final InstanceIdentifier<?> rootBase = registry.fromYangInstanceIdentifier(
171             YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME), NodeIdentifier.create(GrpCont.QNAME)));
172         assertEquals(InstanceIdentifier.builder(RootBase.class, GrpCont.class).build(), rootBase);
173
174         final InstanceIdentifier<?> rootAug = registry.fromYangInstanceIdentifier(
175             YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
176                 NodeIdentifier.create(GrpCont.QNAME.withModule(RootAug.QNAME.getModule()))));
177         assertEquals(InstanceIdentifier.builder(RootAug.class, GrpCont.class).build(), rootAug);
178     }
179 }