2 * Copyright (c) 2016 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.yangtools.yang.data.impl.schema;
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.isA;
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.Collection;
18 import org.junit.AfterClass;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.util.ImmutableOffsetMap;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34 public class InstanceIdToNodesTest {
36 private static final String NS = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:normalization:test";
37 private static final String REVISION = "2014-03-13";
38 private static final QName ID = QName.create(NS, REVISION, "id");
39 private static final QName FOO = QName.create(ID, "foo");
40 private static final QName BAR = QName.create(ID, "bar");
41 private static final NodeIdentifier TWO_KEY_LIST = NodeIdentifier.create(QName.create(ID, "two-key-list"));
43 private static EffectiveModelContext ctx;
45 private final NodeIdentifier rootContainer = new NodeIdentifier(QName.create(NS, REVISION, "test"));
46 private final NodeIdentifier outerList = new NodeIdentifier(QName.create(NS, REVISION, "outer-list"));
47 private final NodeIdentifierWithPredicates outerListWithKey = NodeIdentifierWithPredicates.of(
48 QName.create(NS, REVISION, "outer-list"), ID, 1);
50 private final NodeIdentifier leafList = new NodeIdentifier(QName.create(NS, REVISION, "ordered-leaf-list"));
51 private final NodeWithValue<?> leafListWithValue = new NodeWithValue<>(leafList.getNodeType(), "abcd");
54 public static void setUp() {
55 ctx = YangParserTestUtils.parseYangResources(InstanceIdToNodesTest.class, "/filter-test.yang");
59 public static void teardown() {
64 public void testListLastChildOverride() {
65 assertEquals(Builders.containerBuilder()
66 .withNodeIdentifier(rootContainer)
67 .withChild(Builders.mapBuilder()
68 .withNodeIdentifier(outerList)
69 .withChild(Builders.mapEntryBuilder()
70 .withNodeIdentifier(outerListWithKey)
71 .withChild(Builders.leafBuilder()
72 .withNodeIdentifier(new NodeIdentifier(ID))
78 ImmutableNodes.fromInstanceId(ctx,
79 YangInstanceIdentifier.create(rootContainer, outerList, outerListWithKey)));
83 public void testLeafList() {
84 assertEquals(Builders.containerBuilder()
85 .withNodeIdentifier(rootContainer)
86 .withChild(Builders.orderedLeafSetBuilder()
87 .withNodeIdentifier(leafList)
88 .withChild(Builders.leafSetEntryBuilder()
89 .withNodeIdentifier(leafListWithValue)
90 .withValue(leafListWithValue.getValue())
94 ImmutableNodes.fromInstanceId(ctx,
95 YangInstanceIdentifier.create(rootContainer, leafList, leafListWithValue)));
99 public void testEmptyInstanceIdentifier() {
100 assertEquals(ImmutableNodes.containerNode(SchemaContext.NAME),
101 ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.empty()));
105 public void testKeyOrdering() {
106 final Map<QName, Object> misordered = ImmutableOffsetMap.orderedCopyOf(ImmutableMap.of(BAR, "bar", FOO, "foo"));
107 final NodeIdentifierWithPredicates id = NodeIdentifierWithPredicates.of(TWO_KEY_LIST.getNodeType(), misordered);
108 assertArrayEquals(new Object[] { BAR, FOO }, id.keySet().toArray());
110 final NormalizedNode filter = ImmutableNodes.fromInstanceId(ctx,
111 YangInstanceIdentifier.create(TWO_KEY_LIST, id));
112 assertThat(filter, isA(MapNode.class));
113 final Collection<MapEntryNode> value = ((MapNode) filter).body();
114 assertEquals(1, value.size());
115 final MapEntryNode entry = value.iterator().next();
117 // The entry must have a the proper order
118 assertArrayEquals(new Object[] { FOO, BAR }, entry.getIdentifier().keySet().toArray());