Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / 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.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.AugmentationSchemaNode;
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 = { "int32-ext1", "int32-ext2", "string-ext1", "string-ext2", "string-ext3",
50             "string-ext4", "invalid-string-pattern", "multiple-pattern-string", "my-decimal-type", "my-union",
51             "my-union-ext", "nested-union2"
52         };
53         final String[] actualOrder = new String[typedefs.size()];
54
55         int offset = 0;
56         for (final TypeDefinition<?> type : typedefs) {
57             actualOrder[offset] = type.getQName().getLocalName();
58             offset++;
59         }
60         assertArrayEquals(expectedOrder, actualOrder);
61     }
62
63     @Test
64     public void testOrderingChildNodes() throws Exception {
65         AugmentationSchemaNode augment1 = null;
66         for (final AugmentationSchemaNode as : foo.getAugmentations()) {
67             if (as.getChildNodes().size() == 5) {
68                 augment1 = as;
69                 break;
70             }
71         }
72         assertNotNull(augment1);
73
74         final String[] expectedOrder = { "ds0ChannelNumber", "interface-id", "my-type", "schemas", "odl" };
75         final String[] actualOrder = new String[expectedOrder.length];
76
77         int offset = 0;
78         for (final DataSchemaNode augmentChild : augment1.getChildNodes()) {
79             actualOrder[offset] = augmentChild.getQName().getLocalName();
80             offset++;
81         }
82
83         assertArrayEquals(expectedOrder, actualOrder);
84     }
85
86     @Test
87     public void testOrderingNestedChildNodes1() throws Exception {
88         final Collection<DataSchemaNode> childNodes = foo.getChildNodes();
89         final String[] expectedOrder = { "int32-leaf", "string-leaf", "invalid-pattern-string-leaf",
90             "invalid-direct-string-pattern-def-leaf", "multiple-pattern-string-leaf",
91             "multiple-pattern-direct-string-def-leaf", "length-leaf", "decimal-leaf", "decimal-leaf2", "ext",
92             "union-leaf", "custom-union-leaf", "transfer", "datas", "mycont", "data", "how", "address", "port",
93             "addresses", "peer", "id", "foo-id", "sub-ext", "sub-transfer", "sub-datas"
94         };
95         final String[] actualOrder = new String[childNodes.size()];
96
97         int offset = 0;
98         for (final DataSchemaNode child : childNodes) {
99             actualOrder[offset] = child.getQName().getLocalName();
100             offset++;
101         }
102         assertArrayEquals(expectedOrder, actualOrder);
103     }
104
105     @Test
106     public void testOrderingNestedChildNodes2() throws Exception {
107         final Set<GroupingDefinition> groupings = baz.getGroupings();
108         assertEquals(1, groupings.size());
109         final GroupingDefinition target = groupings.iterator().next();
110
111         final Collection<DataSchemaNode> childNodes = target.getChildNodes();
112         final String[] expectedOrder = { "data", "how", "address", "port", "addresses" };
113         final String[] actualOrder = new String[childNodes.size()];
114
115         int offset = 0;
116         for (final DataSchemaNode child : childNodes) {
117             actualOrder[offset] = child.getQName().getLocalName();
118             offset++;
119         }
120         assertArrayEquals(expectedOrder, actualOrder);
121     }
122
123     @Test
124     public void testOrderingNestedChildNodes3() throws Exception {
125         final Module baz = TestUtils.loadModuleResources(getClass(), "/ordering/foo.yang")
126                 .getModules().iterator().next();
127         final ContainerSchemaNode x = (ContainerSchemaNode) baz
128                 .getDataChildByName(QName.create(baz.getQNameModule(), "x"));
129         final Collection<DataSchemaNode> childNodes = x.getChildNodes();
130
131         final String[] expectedOrder = { "x15", "x10", "x5", "x1", "a5", "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" };
132         final String[] actualOrder = new String[childNodes.size()];
133
134         int offset = 0;
135         for (final DataSchemaNode child : childNodes) {
136             actualOrder[offset] = child.getQName().getLocalName();
137             offset++;
138         }
139         assertArrayEquals(expectedOrder, actualOrder);
140     }
141 }