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