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;
14 import com.google.common.collect.ImmutableMap;
16 import org.junit.jupiter.api.AfterAll;
17 import org.junit.jupiter.api.BeforeAll;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.yangtools.util.ImmutableOffsetMap;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30 class InstanceIdToNodesTest {
32 private static final String NS = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:normalization:test";
33 private static final String REVISION = "2014-03-13";
34 private static final QName ID = QName.create(NS, REVISION, "id");
35 private static final QName FOO = QName.create(ID, "foo");
36 private static final QName BAR = QName.create(ID, "bar");
37 private static final NodeIdentifier TWO_KEY_LIST = NodeIdentifier.create(QName.create(ID, "two-key-list"));
39 private static EffectiveModelContext ctx;
41 private final NodeIdentifier rootContainer = new NodeIdentifier(QName.create(NS, REVISION, "test"));
42 private final NodeIdentifier outerList = new NodeIdentifier(QName.create(NS, REVISION, "outer-list"));
43 private final NodeIdentifierWithPredicates outerListWithKey = NodeIdentifierWithPredicates.of(
44 QName.create(NS, REVISION, "outer-list"), ID, 1);
46 private final NodeIdentifier leafList = new NodeIdentifier(QName.create(NS, REVISION, "ordered-leaf-list"));
47 private final NodeWithValue<?> leafListWithValue = new NodeWithValue<>(leafList.getNodeType(), "abcd");
51 ctx = YangParserTestUtils.parseYangResources(InstanceIdToNodesTest.class, "/filter-test.yang");
55 static void teardown() {
60 void testListLastChildOverride() {
61 assertEquals(Builders.containerBuilder()
62 .withNodeIdentifier(rootContainer)
63 .withChild(Builders.mapBuilder()
64 .withNodeIdentifier(outerList)
65 .withChild(Builders.mapEntryBuilder()
66 .withNodeIdentifier(outerListWithKey)
67 .withChild(Builders.leafBuilder()
68 .withNodeIdentifier(new NodeIdentifier(ID))
74 ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, outerList, outerListWithKey)));
79 assertEquals(Builders.containerBuilder()
80 .withNodeIdentifier(rootContainer)
81 .withChild(Builders.orderedLeafSetBuilder()
82 .withNodeIdentifier(leafList)
83 .withChild(Builders.leafSetEntryBuilder()
84 .withNodeIdentifier(leafListWithValue)
85 .withValue(leafListWithValue.getValue())
89 ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, leafList, leafListWithValue)));
93 void testEmptyInstanceIdentifier() {
94 assertEquals(ImmutableNodes.containerNode(SchemaContext.NAME),
95 ImmutableNodes.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 = ImmutableNodes.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());