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