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