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