a837d961279e32b65303ec1a2a63a5455513a010
[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 = codecContext.fromYangInstanceIdentifier(BI_TOP_PATH);
64         assertEquals(Top.class, instanceIdentifier.getTargetType());
65     }
66
67     @Test
68     public void testYangIIToBindingAwareIIListWildcarded() {
69         final InstanceIdentifier<?> instanceIdentifier = codecContext.fromYangInstanceIdentifier(
70             BI_TOP_LEVEL_LIST_PATH);
71         assertNull(instanceIdentifier);
72     }
73
74     @Test
75     public void testYangIIToBindingAwareIIListWithKey() {
76         final InstanceIdentifier<?> instanceIdentifier = codecContext.fromYangInstanceIdentifier(
77             BI_TOP_LEVEL_LIST_1_PATH);
78         final InstanceIdentifier.PathArgument last = Iterables.getLast(instanceIdentifier.getPathArguments());
79         assertEquals(TopLevelList.class, instanceIdentifier.getTargetType());
80         assertFalse(instanceIdentifier.isWildcarded());
81         assertTrue(last instanceof InstanceIdentifier.IdentifiableItem);
82         final Identifier<?> key = ((InstanceIdentifier.IdentifiableItem<?, ?>) last).getKey();
83         assertEquals(TopLevelListKey.class, key.getClass());
84         assertEquals(TOP_LEVEL_LIST_KEY_VALUE, ((TopLevelListKey)key).getName());
85     }
86
87     @Test
88     public void testBindingAwareIIToYangIContainer() {
89         final YangInstanceIdentifier yangInstanceIdentifier = codecContext.toYangInstanceIdentifier(
90                 InstanceIdentifier.create(Top.class).child(TopLevelList.class));
91         final PathArgument lastPathArgument = yangInstanceIdentifier.getLastPathArgument();
92         assertTrue(lastPathArgument instanceof NodeIdentifier);
93         assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
94     }
95
96     @Test
97     public void testBindingAwareIIToYangIIWildcard() {
98         final YangInstanceIdentifier yangInstanceIdentifier = codecContext.toYangInstanceIdentifier(
99                 InstanceIdentifier.create(Top.class).child(TopLevelList.class));
100         final PathArgument lastPathArgument = yangInstanceIdentifier.getLastPathArgument();
101         assertTrue(lastPathArgument instanceof NodeIdentifier);
102         assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
103     }
104
105     @Test
106     public void testBindingAwareIIToYangIIListWithKey() {
107         final YangInstanceIdentifier yangInstanceIdentifier = codecContext.toYangInstanceIdentifier(
108                 InstanceIdentifier.create(Top.class).child(TopLevelList.class, TOP_FOO_KEY));
109         final PathArgument lastPathArgument = yangInstanceIdentifier.getLastPathArgument();
110         assertTrue(lastPathArgument instanceof NodeIdentifierWithPredicates);
111         assertTrue(((NodeIdentifierWithPredicates) lastPathArgument).values().contains(TOP_LEVEL_LIST_KEY_VALUE));
112         assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
113     }
114
115     @Test
116     public void testBindingAwareIIToYangIIAugmentation() {
117         final PathArgument lastArg = codecContext.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
118         assertTrue(lastArg instanceof AugmentationIdentifier);
119     }
120
121     @Test
122     public void testBindingAwareIIToYangIILeafOnlyAugmentation() {
123         final PathArgument leafOnlyLastArg = codecContext.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY)
124                 .getLastPathArgument();
125         assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
126         assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
127     }
128
129     @Test
130     public void testChoiceCaseGroupingFromBinding() {
131         final YangInstanceIdentifier contBase = codecContext.toYangInstanceIdentifier(
132             InstanceIdentifier.builder(Cont.class).child(ContBase.class, GrpCont.class).build());
133         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
134             NodeIdentifier.create(ContChoice.QNAME), NodeIdentifier.create(GrpCont.QNAME)), contBase);
135
136         final YangInstanceIdentifier contAug = codecContext.toYangInstanceIdentifier(
137             InstanceIdentifier.builder(Cont.class).child(ContAug.class, GrpCont.class).build());
138         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
139             NodeIdentifier.create(ContChoice.QNAME),
140             NodeIdentifier.create(GrpCont.QNAME.bindTo(ContAug.QNAME.getModule()))), contAug);
141
142         // Legacy: downcast the child to Class, losing type safety but still working. Faced with ambiguity, it will
143         //         select the lexically-lower class
144         assertEquals(1, ContBase.class.getCanonicalName().compareTo(ContAug.class.getCanonicalName()));
145         final YangInstanceIdentifier contAugLegacy = codecContext.toYangInstanceIdentifier(
146             InstanceIdentifier.builder(Cont.class).child((Class) GrpCont.class).build());
147         assertEquals(contAug, contAugLegacy);
148
149         final YangInstanceIdentifier rootBase = codecContext.toYangInstanceIdentifier(
150             InstanceIdentifier.builder(RootBase.class, GrpCont.class).build());
151         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
152             NodeIdentifier.create(GrpCont.QNAME)), rootBase);
153
154         final YangInstanceIdentifier rootAug = codecContext.toYangInstanceIdentifier(
155             InstanceIdentifier.builder(RootAug.class, GrpCont.class).build());
156         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
157             NodeIdentifier.create(GrpCont.QNAME.bindTo(RootAug.QNAME.getModule()))), rootAug);
158     }
159
160     @Test
161     public void testChoiceCaseGroupingToBinding() {
162         final InstanceIdentifier<?> contBase = codecContext.fromYangInstanceIdentifier(
163             YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
164             NodeIdentifier.create(ContChoice.QNAME), NodeIdentifier.create(GrpCont.QNAME)));
165         assertEquals(InstanceIdentifier.builder(Cont.class).child(ContBase.class, GrpCont.class).build(), contBase);
166
167         final InstanceIdentifier<?> contAug = codecContext.fromYangInstanceIdentifier(
168             YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME), NodeIdentifier.create(ContChoice.QNAME),
169                 NodeIdentifier.create(GrpCont.QNAME.bindTo(ContAug.QNAME.getModule()))));
170         assertEquals(InstanceIdentifier.builder(Cont.class).child(ContAug.class, GrpCont.class).build(), contAug);
171
172         final InstanceIdentifier<?> rootBase = codecContext.fromYangInstanceIdentifier(
173             YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME), NodeIdentifier.create(GrpCont.QNAME)));
174         assertEquals(InstanceIdentifier.builder(RootBase.class, GrpCont.class).build(), rootBase);
175
176         final InstanceIdentifier<?> rootAug = codecContext.fromYangInstanceIdentifier(
177             YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
178                 NodeIdentifier.create(GrpCont.QNAME.bindTo(RootAug.QNAME.getModule()))));
179         assertEquals(InstanceIdentifier.builder(RootAug.class, GrpCont.class).build(), rootAug);
180     }
181 }