bbcb8937730cea8a157a5f705d173f5108ed49e4
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / 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.impl;
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(NodeIdentifierWithPredicates.of(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).values().contains(TOP_LEVEL_LIST_KEY_VALUE));
110         assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
111     }
112
113     @Test
114     public void testBindingAwareIIToYangIIAugmentation() {
115         final PathArgument lastArg = registry.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
116         assertTrue(lastArg instanceof AugmentationIdentifier);
117     }
118
119     @Test
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));
124     }
125
126     @Test
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);
132
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);
138
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);
145
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);
150
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);
155     }
156
157     @Test
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);
163
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);
168
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);
172
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);
177     }
178 }