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