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.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.binding.KeyedInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.Uint8;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 public class InstanceIdentifierTest extends AbstractBindingCodecTest {
33 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
34 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
35 .child(TopLevelList.class, TOP_FOO_KEY).build();
36 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
37 .augmentation(TreeLeafOnlyAugment.class);
38 private static final InstanceIdentifier<ListViaUses> BA_TREE_COMPLEX_USES = BA_TOP_LEVEL_LIST
39 .augmentation(TreeComplexUsesAugment.class).child(ListViaUses.class, new ListViaUsesKey("bar"));
42 public void testComplexAugmentationSerialization() {
43 // augmentation child pointer fully recoverable after reverse transformation
44 final YangInstanceIdentifier yii = codecContext.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES);
45 final InstanceIdentifier<?> converted = codecContext.fromYangInstanceIdentifier(yii);
46 assertEquals(BA_TREE_COMPLEX_USES, converted);
50 public void testLeafOnlyAugmentationSerialization() {
51 // augmentation only pointer translated to parent node being augmented,
52 // because of augmentation only have no corresponding yang identifier
53 final YangInstanceIdentifier yii = codecContext.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
54 final InstanceIdentifier<?> converted = codecContext.fromYangInstanceIdentifier(yii);
55 assertEquals(BA_TOP_LEVEL_LIST, converted);
59 public void testCamelCaseKeys() {
60 final InstanceIdentifier<?> result = codecContext.fromYangInstanceIdentifier(YangInstanceIdentifier.of(
61 NodeIdentifier.create(OspfStatLsdbBrief.QNAME),
62 NodeIdentifierWithPredicates.of(OspfStatLsdbBrief.QNAME, ImmutableMap.of(
63 QName.create(OspfStatLsdbBrief.QNAME, "AreaIndex"), 1,
64 QName.create(OspfStatLsdbBrief.QNAME, "LsaType"), Uint8.valueOf(2),
65 QName.create(OspfStatLsdbBrief.QNAME, "LsId"), 3,
66 QName.create(OspfStatLsdbBrief.QNAME, "AdvRtr"), "foo"))));
67 assertTrue(result instanceof KeyedInstanceIdentifier);
68 final var key = ((KeyedInstanceIdentifier<?, ?>) result).getKey();
69 assertEquals(new OspfStatLsdbBriefKey("foo", 1, 3, Uint8.valueOf(2)), key);