e861805730c832965cc85f05a3c058fa9bf0b736
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / OrderingTest.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.stmt;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.util.Collection;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23
24 public class OrderingTest extends AbstractModelTest {
25     @Test
26     public void testOrderingTypedef() throws Exception {
27         final Collection<? extends TypeDefinition<?>> typedefs = BAR.getTypeDefinitions();
28         final String[] expectedOrder = { "int32-ext1", "int32-ext2", "string-ext1", "string-ext2", "string-ext3",
29             "string-ext4", "multiple-pattern-string", "my-decimal-type", "my-union", "my-union-ext", "nested-union2"
30         };
31         final String[] actualOrder = new String[typedefs.size()];
32
33         int offset = 0;
34         for (final TypeDefinition<?> type : typedefs) {
35             actualOrder[offset] = type.getQName().getLocalName();
36             offset++;
37         }
38         assertArrayEquals(expectedOrder, actualOrder);
39     }
40
41     @Test
42     public void testOrderingChildNodes() throws Exception {
43         AugmentationSchemaNode augment1 = null;
44         for (final AugmentationSchemaNode as : FOO.getAugmentations()) {
45             if (as.getChildNodes().size() == 5) {
46                 augment1 = as;
47                 break;
48             }
49         }
50         assertNotNull(augment1);
51
52         final String[] expectedOrder = { "ds0ChannelNumber", "interface-id", "my-type", "schemas", "odl" };
53         final String[] actualOrder = new String[expectedOrder.length];
54
55         int offset = 0;
56         for (final DataSchemaNode augmentChild : augment1.getChildNodes()) {
57             actualOrder[offset] = augmentChild.getQName().getLocalName();
58             offset++;
59         }
60
61         assertArrayEquals(expectedOrder, actualOrder);
62     }
63
64     @Test
65     public void testOrderingNestedChildNodes1() throws Exception {
66         final Collection<? extends DataSchemaNode> childNodes = FOO.getChildNodes();
67         final String[] expectedOrder = { "int32-leaf", "string-leaf", "multiple-pattern-string-leaf",
68             "multiple-pattern-direct-string-def-leaf", "length-leaf", "decimal-leaf", "decimal-leaf2", "ext",
69             "union-leaf", "custom-union-leaf", "transfer", "datas", "mycont", "data", "how", "address", "port",
70             "addresses", "peer", "id", "foo-id", "sub-ext", "sub-transfer", "sub-datas"
71         };
72         final String[] actualOrder = new String[childNodes.size()];
73
74         int offset = 0;
75         for (final DataSchemaNode child : childNodes) {
76             actualOrder[offset] = child.getQName().getLocalName();
77             offset++;
78         }
79         assertArrayEquals(expectedOrder, actualOrder);
80     }
81
82     @Test
83     public void testOrderingNestedChildNodes2() throws Exception {
84         final Collection<? extends GroupingDefinition> groupings = BAZ.getGroupings();
85         assertEquals(1, groupings.size());
86         final GroupingDefinition target = groupings.iterator().next();
87
88         final Collection<? extends DataSchemaNode> childNodes = target.getChildNodes();
89         final String[] expectedOrder = { "data", "how", "address", "port", "addresses" };
90         final String[] actualOrder = new String[childNodes.size()];
91
92         int offset = 0;
93         for (final DataSchemaNode child : childNodes) {
94             actualOrder[offset] = child.getQName().getLocalName();
95             offset++;
96         }
97         assertArrayEquals(expectedOrder, actualOrder);
98     }
99
100     @Test
101     public void testOrderingNestedChildNodes3() throws Exception {
102         final Module justFoo = StmtTestUtils.parseYangSource("/ordering/foo.yang").getModules().iterator().next();
103         final ContainerSchemaNode x = (ContainerSchemaNode) justFoo
104                 .getDataChildByName(QName.create(justFoo.getQNameModule(), "x"));
105         final Collection<? extends DataSchemaNode> childNodes = x.getChildNodes();
106
107         final String[] expectedOrder = { "x15", "x10", "x5", "x1", "a5", "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" };
108         final String[] actualOrder = new String[childNodes.size()];
109
110         int offset = 0;
111         for (final DataSchemaNode child : childNodes) {
112             actualOrder[offset] = child.getQName().getLocalName();
113             offset++;
114         }
115         assertArrayEquals(expectedOrder, actualOrder);
116     }
117 }