3827b5a413416786aec90fa40a1ac8b00e0c18ee
[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.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.isA;
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14
15 import com.google.common.collect.ImmutableMap;
16 import java.util.Collection;
17 import java.util.Map;
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;
33
34 public class InstanceIdToNodesTest {
35
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"));
42
43     private static EffectiveModelContext ctx;
44
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);
49
50     private final NodeIdentifier leafList = new NodeIdentifier(QName.create(NS, REVISION, "ordered-leaf-list"));
51     private final NodeWithValue<?> leafListWithValue = new NodeWithValue<>(leafList.getNodeType(), "abcd");
52
53     @BeforeClass
54     public static void setUp() {
55         ctx = YangParserTestUtils.parseYangResources(InstanceIdToNodesTest.class, "/filter-test.yang");
56     }
57
58     @AfterClass
59     public static void teardown() {
60         ctx = null;
61     }
62
63     @Test
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))
73                         .withValue(1)
74                         .build())
75                     .build())
76                 .build())
77             .build(),
78             ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, outerList, outerListWithKey)));
79     }
80
81     @Test
82     public void testLeafList() {
83         assertEquals(Builders.containerBuilder()
84             .withNodeIdentifier(rootContainer)
85             .withChild(Builders.orderedLeafSetBuilder()
86                 .withNodeIdentifier(leafList)
87                 .withChild(Builders.leafSetEntryBuilder()
88                     .withNodeIdentifier(leafListWithValue)
89                     .withValue(leafListWithValue.getValue())
90                     .build())
91                 .build())
92             .build(),
93             ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.of(rootContainer, leafList, leafListWithValue)));
94     }
95
96     @Test
97     public void testEmptyInstanceIdentifier() {
98         assertEquals(ImmutableNodes.containerNode(SchemaContext.NAME),
99             ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.of()));
100     }
101
102     @Test
103     public void testKeyOrdering() {
104         final Map<QName, Object> misordered = ImmutableOffsetMap.orderedCopyOf(ImmutableMap.of(BAR, "bar", FOO, "foo"));
105         final NodeIdentifierWithPredicates id = NodeIdentifierWithPredicates.of(TWO_KEY_LIST.getNodeType(), misordered);
106         assertArrayEquals(new Object[] { BAR, FOO }, id.keySet().toArray());
107
108         final NormalizedNode filter = ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.of(TWO_KEY_LIST, id));
109         assertThat(filter, isA(MapNode.class));
110         final Collection<MapEntryNode> value = ((MapNode) filter).body();
111         assertEquals(1, value.size());
112         final MapEntryNode entry = value.iterator().next();
113
114         // The entry must have a the proper order
115         assertArrayEquals(new Object[] { FOO, BAR }, entry.name().keySet().toArray());
116     }
117 }