Migrate yang-data-impl to JUnit5
[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
14 import com.google.common.collect.ImmutableMap;
15 import java.util.Map;
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;
29
30 class InstanceIdToNodesTest {
31
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"));
38
39     private static EffectiveModelContext ctx;
40
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);
45
46     private final NodeIdentifier leafList = new NodeIdentifier(QName.create(NS, REVISION, "ordered-leaf-list"));
47     private final NodeWithValue<?> leafListWithValue = new NodeWithValue<>(leafList.getNodeType(), "abcd");
48
49     @BeforeAll
50     static void setUp() {
51         ctx = YangParserTestUtils.parseYangResources(InstanceIdToNodesTest.class, "/filter-test.yang");
52     }
53
54     @AfterAll
55     static void teardown() {
56         ctx = null;
57     }
58
59     @Test
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))
69                         .withValue(1)
70                         .build())
71                     .build())
72                 .build())
73             .build(),
74             ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, outerList, outerListWithKey)));
75     }
76
77     @Test
78     void testLeafList() {
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())
86                     .build())
87                 .build())
88             .build(),
89             ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, leafList, leafListWithValue)));
90     }
91
92     @Test
93     void testEmptyInstanceIdentifier() {
94         assertEquals(ImmutableNodes.containerNode(SchemaContext.NAME),
95             ImmutableNodes.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 = 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();
108
109         // The entry must have a the proper order
110         assertArrayEquals(new Object[] { FOO, BAR }, entry.name().keySet().toArray());
111     }
112 }