2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
14 import java.util.Collection;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 public class OrderingTest {
28 public void testOrderingTypedef() throws Exception {
29 final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
30 final Module bar = TestUtils.findModule(modules, "bar");
31 final Set<TypeDefinition<?>> typedefs = bar.getTypeDefinitions();
32 final String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2",
33 "string-ext1", "string-ext2", "string-ext3", "string-ext4",
34 "invalid-string-pattern", "multiple-pattern-string",
35 "my-decimal-type", "my-union", "my-union-ext", "nested-union2" };
36 final String[] actualOrder = new String[typedefs.size()];
39 for (final TypeDefinition<?> type : typedefs) {
40 actualOrder[i] = type.getQName().getLocalName();
43 assertArrayEquals(expectedOrder, actualOrder);
47 public void testOrderingChildNodes() throws Exception {
48 final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
49 final Module foo = TestUtils.findModule(modules, "foo");
50 AugmentationSchema augment1 = null;
51 for (final AugmentationSchema as : foo.getAugmentations()) {
52 if (as.getChildNodes().size() == 5) {
57 assertNotNull(augment1);
59 final String[] expectedOrder = new String[] { "ds0ChannelNumber",
60 "interface-id", "my-type", "schemas", "odl" };
61 final String[] actualOrder = new String[expectedOrder.length];
64 for (final DataSchemaNode augmentChild : augment1.getChildNodes()) {
65 actualOrder[i] = augmentChild.getQName().getLocalName();
69 assertArrayEquals(expectedOrder, actualOrder);
73 public void testOrderingNestedChildNodes1() throws Exception {
74 final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
75 final Module foo = TestUtils.findModule(modules, "foo");
77 final Collection<DataSchemaNode> childNodes = foo.getChildNodes();
78 final String[] expectedOrder = new String[] { "int32-leaf", "string-leaf",
79 "invalid-pattern-string-leaf",
80 "invalid-direct-string-pattern-def-leaf",
81 "multiple-pattern-string-leaf",
82 "multiple-pattern-direct-string-def-leaf", "length-leaf",
83 "decimal-leaf", "decimal-leaf2", "ext", "union-leaf",
84 "custom-union-leaf", "transfer", "datas", "mycont", "data",
85 "how", "address", "port", "addresses", "peer", "id", "foo-id",
86 "sub-ext", "sub-transfer", "sub-datas" };
87 final String[] actualOrder = new String[childNodes.size()];
90 for (final DataSchemaNode child : childNodes) {
91 actualOrder[i] = child.getQName().getLocalName();
94 assertArrayEquals(expectedOrder, actualOrder);
98 public void testOrderingNestedChildNodes2() throws Exception {
99 final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
100 final Module baz = TestUtils.findModule(modules, "baz");
101 final Set<GroupingDefinition> groupings = baz.getGroupings();
102 assertEquals(1, groupings.size());
103 final GroupingDefinition target = groupings.iterator().next();
105 final Collection<DataSchemaNode> childNodes = target.getChildNodes();
106 final String[] expectedOrder = new String[] { "data", "how", "address",
107 "port", "addresses" };
108 final String[] actualOrder = new String[childNodes.size()];
111 for (final DataSchemaNode child : childNodes) {
112 actualOrder[i] = child.getQName().getLocalName();
115 assertArrayEquals(expectedOrder, actualOrder);
119 public void testOrderingNestedChildNodes3() throws Exception {
120 final Module baz = TestUtils.loadModule(getClass().getResourceAsStream(
121 "/ordering/foo.yang"));
122 final ContainerSchemaNode x = (ContainerSchemaNode) baz
123 .getDataChildByName(QName.create(baz.getQNameModule(), "x"));
124 final Collection<DataSchemaNode> childNodes = x.getChildNodes();
126 final String[] expectedOrder = new String[] { "x15", "x10", "x5", "x1", "a5",
127 "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" };
128 final String[] actualOrder = new String[childNodes.size()];
131 for (final DataSchemaNode child : childNodes) {
132 actualOrder[i] = child.getQName().getLocalName();
135 assertArrayEquals(expectedOrder, actualOrder);