Moved binding MD-SAL APIs to binding folder
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / yangtools / binding / data / 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.yangtools.binding.data.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 javassist.ClassPool;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
24 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
25 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
26 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
27 import org.opendaylight.yangtools.yang.binding.Identifier;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31
32 public class InstanceIdentifierSerializeDeserializeTest extends AbstractBindingRuntimeTest{
33     public static final String TOP_LEVEL_LIST_KEY_VALUE = "foo";
34
35     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
36     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
37             .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
38     private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY =
39             BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
40     private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES =
41             BA_TOP_LEVEL_LIST.augmentation(TreeComplexUsesAugment.class);
42
43     public static final QName TOP_QNAME = Top.QNAME;
44     public static final QName TOP_LEVEL_LIST_QNAME = QName.create(TOP_QNAME, "top-level-list");
45     public static final QName TOP_LEVEL_LIST_KEY = QName.create(TOP_QNAME, "name");
46     private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
47
48     public static final YangInstanceIdentifier BI_TOP_PATH = YangInstanceIdentifier.builder().node(TOP_QNAME).build();
49     public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_PATH = BI_TOP_PATH.node(TOP_LEVEL_LIST_QNAME);
50     public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_1_PATH = BI_TOP_LEVEL_LIST_PATH
51             .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY, TOP_LEVEL_LIST_KEY_VALUE));
52
53     private BindingNormalizedNodeCodecRegistry registry;
54
55     @Override
56     @Before
57     public void setup() {
58         super.setup();
59         final JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
60         registry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(utils));
61         registry.onBindingRuntimeContextUpdated(getRuntimeContext());
62     }
63
64     @Test
65     public void testYangIIToBindingAwareII() {
66         final InstanceIdentifier<?> instanceIdentifier = registry.fromYangInstanceIdentifier(BI_TOP_PATH);
67         assertEquals(Top.class, instanceIdentifier.getTargetType());
68     }
69
70     @Test
71     public void testYangIIToBindingAwareIIListWildcarded() {
72         final InstanceIdentifier<?> instanceIdentifier = registry.fromYangInstanceIdentifier(BI_TOP_LEVEL_LIST_PATH);
73         assertNull(instanceIdentifier);
74     }
75
76     @Test
77     public void testYangIIToBindingAwareIIListWithKey() {
78         final InstanceIdentifier<?> instanceIdentifier = registry.fromYangInstanceIdentifier(BI_TOP_LEVEL_LIST_1_PATH);
79         final InstanceIdentifier.PathArgument last = Iterables.getLast(instanceIdentifier.getPathArguments());
80         assertEquals(TopLevelList.class, instanceIdentifier.getTargetType());
81         assertFalse(instanceIdentifier.isWildcarded());
82         assertTrue(last instanceof InstanceIdentifier.IdentifiableItem);
83         final Identifier<?> key = ((InstanceIdentifier.IdentifiableItem<?, ?>) last).getKey();
84         assertEquals(TopLevelListKey.class, key.getClass());
85         assertEquals(TOP_LEVEL_LIST_KEY_VALUE, ((TopLevelListKey)key).getName());
86     }
87
88     @Test
89     public void testBindingAwareIIToYangIContainer() {
90         final YangInstanceIdentifier yangInstanceIdentifier = registry.toYangInstanceIdentifier(
91                 InstanceIdentifier.create(Top.class).child(TopLevelList.class));
92         final YangInstanceIdentifier.PathArgument lastPathArgument = yangInstanceIdentifier.getLastPathArgument();
93         assertTrue(lastPathArgument instanceof YangInstanceIdentifier.NodeIdentifier);
94         assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
95     }
96
97     @Test
98     public void testBindingAwareIIToYangIIWildcard() {
99         final YangInstanceIdentifier yangInstanceIdentifier = registry.toYangInstanceIdentifier(
100                 InstanceIdentifier.create(Top.class).child(TopLevelList.class));
101         final YangInstanceIdentifier.PathArgument lastPathArgument = yangInstanceIdentifier.getLastPathArgument();
102         assertTrue(lastPathArgument instanceof YangInstanceIdentifier.NodeIdentifier);
103         assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
104     }
105
106     @Test
107     public void testBindingAwareIIToYangIIListWithKey() {
108         final YangInstanceIdentifier yangInstanceIdentifier = registry.toYangInstanceIdentifier(
109                 InstanceIdentifier.create(Top.class).child(TopLevelList.class, TOP_FOO_KEY));
110         final YangInstanceIdentifier.PathArgument lastPathArgument = yangInstanceIdentifier.getLastPathArgument();
111         assertTrue(lastPathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates);
112         assertTrue(((YangInstanceIdentifier.NodeIdentifierWithPredicates) lastPathArgument).getKeyValues().containsValue(TOP_LEVEL_LIST_KEY_VALUE));
113         assertEquals(TopLevelList.QNAME, lastPathArgument.getNodeType());
114     }
115
116     @Test
117     public void testBindingAwareIIToYangIIAugmentation() {
118         final YangInstanceIdentifier.PathArgument lastArg = registry.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
119         assertTrue(lastArg instanceof YangInstanceIdentifier.AugmentationIdentifier);
120     }
121
122     @Test
123     public void testBindingAwareIIToYangIILeafOnlyAugmentation() {
124         final YangInstanceIdentifier.PathArgument leafOnlyLastArg = registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY).getLastPathArgument();
125         assertTrue(leafOnlyLastArg instanceof YangInstanceIdentifier.AugmentationIdentifier);
126         assertTrue(((YangInstanceIdentifier.AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
127     }
128 }