bd44ddb54505f8e7868f72107b151f00988a09f2
[yangtools.git] / yang / yang-parser-impl / 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 java.util.Set;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24
25 public class OrderingTest {
26
27     @Test
28     public void testOrderingTypedef() throws Exception {
29         final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
30         final Module bar = TestUtils.findModule(modules, "bar");
31         final Set<TypeDefinition<?>> typedefs = bar.getTypeDefinitions();
32         final String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2",
33                 "string-ext1", "string-ext2", "string-ext3", "string-ext4",
34                 "invalid-string-pattern", "multiple-pattern-string",
35                 "my-decimal-type", "my-union", "my-union-ext", "nested-union2" };
36         final String[] actualOrder = new String[typedefs.size()];
37
38         int i = 0;
39         for (final TypeDefinition<?> type : typedefs) {
40             actualOrder[i] = type.getQName().getLocalName();
41             i++;
42         }
43         assertArrayEquals(expectedOrder, actualOrder);
44     }
45
46     @Test
47     public void testOrderingChildNodes() throws Exception {
48         final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
49         final Module foo = TestUtils.findModule(modules, "foo");
50         AugmentationSchema augment1 = null;
51         for (final AugmentationSchema as : foo.getAugmentations()) {
52             if (as.getChildNodes().size() == 5) {
53                 augment1 = as;
54                 break;
55             }
56         }
57         assertNotNull(augment1);
58
59         final String[] expectedOrder = new String[] { "ds0ChannelNumber",
60                 "interface-id", "my-type", "schemas", "odl" };
61         final String[] actualOrder = new String[expectedOrder.length];
62
63         int i = 0;
64         for (final DataSchemaNode augmentChild : augment1.getChildNodes()) {
65             actualOrder[i] = augmentChild.getQName().getLocalName();
66             i++;
67         }
68
69         assertArrayEquals(expectedOrder, actualOrder);
70     }
71
72     @Test
73     public void testOrderingNestedChildNodes1() throws Exception {
74         final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
75         final Module foo = TestUtils.findModule(modules, "foo");
76
77         final Collection<DataSchemaNode> childNodes = foo.getChildNodes();
78         final String[] expectedOrder = new String[] { "int32-leaf", "string-leaf",
79                 "invalid-pattern-string-leaf",
80                 "invalid-direct-string-pattern-def-leaf",
81                 "multiple-pattern-string-leaf",
82                 "multiple-pattern-direct-string-def-leaf", "length-leaf",
83                 "decimal-leaf", "decimal-leaf2", "ext", "union-leaf",
84                 "custom-union-leaf", "transfer", "datas", "mycont", "data",
85                 "how", "address", "port", "addresses", "peer", "id", "foo-id",
86                 "sub-ext", "sub-transfer", "sub-datas" };
87         final String[] actualOrder = new String[childNodes.size()];
88
89         int i = 0;
90         for (final DataSchemaNode child : childNodes) {
91             actualOrder[i] = child.getQName().getLocalName();
92             i++;
93         }
94         assertArrayEquals(expectedOrder, actualOrder);
95     }
96
97     @Test
98     public void testOrderingNestedChildNodes2() throws Exception {
99         final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
100         final Module baz = TestUtils.findModule(modules, "baz");
101         final Set<GroupingDefinition> groupings = baz.getGroupings();
102         assertEquals(1, groupings.size());
103         final GroupingDefinition target = groupings.iterator().next();
104
105         final Collection<DataSchemaNode> childNodes = target.getChildNodes();
106         final String[] expectedOrder = new String[] { "data", "how", "address",
107                 "port", "addresses" };
108         final String[] actualOrder = new String[childNodes.size()];
109
110         int i = 0;
111         for (final DataSchemaNode child : childNodes) {
112             actualOrder[i] = child.getQName().getLocalName();
113             i++;
114         }
115         assertArrayEquals(expectedOrder, actualOrder);
116     }
117
118     @Test
119     public void testOrderingNestedChildNodes3() throws Exception {
120         final Module baz = TestUtils.loadModule(getClass().getResourceAsStream(
121                 "/ordering/foo.yang"));
122         final ContainerSchemaNode x = (ContainerSchemaNode) baz
123                 .getDataChildByName(QName.create(baz.getQNameModule(), "x"));
124         final Collection<DataSchemaNode> childNodes = x.getChildNodes();
125
126         final String[] expectedOrder = new String[] { "x15", "x10", "x5", "x1", "a5",
127                 "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" };
128         final String[] actualOrder = new String[childNodes.size()];
129
130         int i = 0;
131         for (final DataSchemaNode child : childNodes) {
132             actualOrder[i] = child.getQName().getLocalName();
133             i++;
134         }
135         assertArrayEquals(expectedOrder, actualOrder);
136     }
137
138 }