Introduce binding.EntryObject
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / InstanceIdToNodesTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema;
9
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;
14
15 import com.google.common.collect.ImmutableMap;
16 import java.util.Map;
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;
31
32 class InstanceIdToNodesTest {
33
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"));
40
41     private static EffectiveModelContext ctx;
42
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);
47
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");
50
51     @BeforeAll
52     static void setUp() {
53         ctx = YangParserTestUtils.parseYangResources(InstanceIdToNodesTest.class, "/filter-test.yang");
54     }
55
56     @AfterAll
57     static void teardown() {
58         ctx = null;
59     }
60
61     @Test
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))
70                     .build())
71                 .build())
72             .build(),
73             fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, outerList, outerListWithKey)));
74     }
75
76     @Test
77     void testLeafList() {
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())
85                     .build())
86                 .build())
87             .build(),
88             fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, leafList, leafListWithValue)));
89     }
90
91     @Test
92     void testEmptyInstanceIdentifier() {
93         assertEquals(ImmutableNodes.newContainerBuilder()
94             .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
95             .build(), fromInstanceId(ctx, YangInstanceIdentifier.of()));
96     }
97
98     @Test
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());
103
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();
108
109         // The entry must have a the proper order
110         assertArrayEquals(new Object[] { FOO, BAR }, entry.name().keySet().toArray());
111     }
112 }