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.junit.jupiter.api.Assertions.assertArrayEquals;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.fromInstanceId;
15 import com.google.common.collect.ImmutableMap;
17 import org.junit.jupiter.api.AfterAll;
18 import org.junit.jupiter.api.BeforeAll;
19 import org.junit.jupiter.api.Test;
20 import org.opendaylight.yangtools.util.ImmutableOffsetMap;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
27 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 class InstanceIdToNodesTest {
34 private static final String NS = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:normalization:test";
35 private static final String REVISION = "2014-03-13";
36 private static final QName ID = QName.create(NS, REVISION, "id");
37 private static final QName FOO = QName.create(ID, "foo");
38 private static final QName BAR = QName.create(ID, "bar");
39 private static final NodeIdentifier TWO_KEY_LIST = NodeIdentifier.create(QName.create(ID, "two-key-list"));
41 private static EffectiveModelContext ctx;
43 private final NodeIdentifier rootContainer = new NodeIdentifier(QName.create(NS, REVISION, "test"));
44 private final NodeIdentifier outerList = new NodeIdentifier(QName.create(NS, REVISION, "outer-list"));
45 private final NodeIdentifierWithPredicates outerListWithKey = NodeIdentifierWithPredicates.of(
46 QName.create(NS, REVISION, "outer-list"), ID, 1);
48 private final NodeIdentifier leafList = new NodeIdentifier(QName.create(NS, REVISION, "ordered-leaf-list"));
49 private final NodeWithValue<String> leafListWithValue = new NodeWithValue<>(leafList.getNodeType(), "abcd");
53 ctx = YangParserTestUtils.parseYangResources(InstanceIdToNodesTest.class, "/filter-test.yang");
57 static void teardown() {
62 void testListLastChildOverride() {
63 assertEquals(ImmutableNodes.newContainerBuilder()
64 .withNodeIdentifier(rootContainer)
65 .withChild(ImmutableNodes.newSystemMapBuilder()
66 .withNodeIdentifier(outerList)
67 .withChild(ImmutableNodes.newMapEntryBuilder()
68 .withNodeIdentifier(outerListWithKey)
69 .withChild(ImmutableNodes.leafNode(new NodeIdentifier(ID), 1))
73 fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, outerList, outerListWithKey)));
78 assertEquals(ImmutableNodes.newContainerBuilder()
79 .withNodeIdentifier(rootContainer)
80 .withChild(Builders.<String>orderedLeafSetBuilder()
81 .withNodeIdentifier(leafList)
82 .withChild(Builders.<String>leafSetEntryBuilder()
83 .withNodeIdentifier(leafListWithValue)
84 .withValue(leafListWithValue.getValue())
88 fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, leafList, leafListWithValue)));
92 void testEmptyInstanceIdentifier() {
93 assertEquals(ImmutableNodes.newContainerBuilder()
94 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
95 .build(), fromInstanceId(ctx, YangInstanceIdentifier.of()));
99 void testKeyOrdering() {
100 final Map<QName, Object> misordered = ImmutableOffsetMap.orderedCopyOf(ImmutableMap.of(BAR, "bar", FOO, "foo"));
101 final var id = NodeIdentifierWithPredicates.of(TWO_KEY_LIST.getNodeType(), misordered);
102 assertArrayEquals(new Object[] { BAR, FOO }, id.keySet().toArray());
104 final var filter = fromInstanceId(ctx, YangInstanceIdentifier.of(TWO_KEY_LIST, id));
105 final var value = assertInstanceOf(MapNode.class, filter).body();
106 assertEquals(1, value.size());
107 final var entry = value.iterator().next();
109 // The entry must have a the proper order
110 assertArrayEquals(new Object[] { FOO, BAR }, entry.name().keySet().toArray());