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.assertTrue;
13 import com.google.common.collect.ImmutableMap;
14 import org.junit.Test;
15 import org.opendaylight.yang.gen.v1.mdsal._355.norev.OspfStatLsdbBrief;
16 import org.opendaylight.yang.gen.v1.mdsal._355.norev.OspfStatLsdbBriefKey;
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.augment.rev140709.complex.from.grouping.ListViaUses;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.complex.from.grouping.ListViaUsesKey;
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.yang.binding.Identifier;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.Uint8;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 public class InstanceIdentifierTest extends AbstractBindingCodecTest {
34 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
35 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
36 .child(TopLevelList.class, TOP_FOO_KEY).build();
37 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
38 .augmentation(TreeLeafOnlyAugment.class);
39 private static final InstanceIdentifier<ListViaUses> BA_TREE_COMPLEX_USES = BA_TOP_LEVEL_LIST
40 .augmentation(TreeComplexUsesAugment.class).child(ListViaUses.class, new ListViaUsesKey("bar"));
43 public void testComplexAugmentationSerialization() {
44 // augmentation child pointer fully recoverable after reverse transformation
45 final YangInstanceIdentifier yii = codecContext.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES);
46 final InstanceIdentifier<?> converted = codecContext.fromYangInstanceIdentifier(yii);
47 assertEquals(BA_TREE_COMPLEX_USES, converted);
51 public void testLeafOnlyAugmentationSerialization() {
52 // augmentation only pointer translated to parent node being augmented,
53 // because of augmentation only have no corresponding yang identifier
54 final YangInstanceIdentifier yii = codecContext.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
55 final InstanceIdentifier<?> converted = codecContext.fromYangInstanceIdentifier(yii);
56 assertEquals(BA_TOP_LEVEL_LIST, converted);
60 public void testCamelCaseKeys() {
61 final InstanceIdentifier<?> result = codecContext.fromYangInstanceIdentifier(YangInstanceIdentifier.create(
62 NodeIdentifier.create(OspfStatLsdbBrief.QNAME),
63 NodeIdentifierWithPredicates.of(OspfStatLsdbBrief.QNAME, ImmutableMap.of(
64 QName.create(OspfStatLsdbBrief.QNAME, "AreaIndex"), 1,
65 QName.create(OspfStatLsdbBrief.QNAME, "LsaType"), Uint8.valueOf(2),
66 QName.create(OspfStatLsdbBrief.QNAME, "LsId"), 3,
67 QName.create(OspfStatLsdbBrief.QNAME, "AdvRtr"), "foo"))));
68 assertTrue(result instanceof KeyedInstanceIdentifier);
69 final Identifier<?> key = ((KeyedInstanceIdentifier<?, ?>) result).getKey();
70 assertEquals(new OspfStatLsdbBriefKey("foo", 1, 3, Uint8.valueOf(2)), key);