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