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.net.URISyntaxException;
15 import java.util.Collection;
17 import org.junit.Test;
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;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 public class OrderingTest {
30 public void testOrderingTypedef() throws URISyntaxException,
31 SourceException, ReactorException {
32 Set<Module> modules = TestUtils.loadModules(getClass().getResource(
34 Module bar = TestUtils.findModule(modules, "bar");
35 Set<TypeDefinition<?>> typedefs = bar.getTypeDefinitions();
36 String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2",
37 "string-ext1", "string-ext2", "string-ext3", "string-ext4",
38 "invalid-string-pattern", "multiple-pattern-string",
39 "my-decimal-type", "my-union", "my-union-ext", "nested-union2" };
40 String[] actualOrder = new String[typedefs.size()];
43 for (TypeDefinition<?> type : typedefs) {
44 actualOrder[i] = type.getQName().getLocalName();
47 assertArrayEquals(expectedOrder, actualOrder);
51 public void testOrderingChildNodes() throws URISyntaxException,
52 SourceException, ReactorException {
53 Set<Module> modules = TestUtils.loadModules(getClass().getResource(
55 Module foo = TestUtils.findModule(modules, "foo");
56 AugmentationSchema augment1 = null;
57 for (AugmentationSchema as : foo.getAugmentations()) {
58 if (as.getChildNodes().size() == 5) {
63 assertNotNull(augment1);
65 String[] expectedOrder = new String[] { "ds0ChannelNumber",
66 "interface-id", "my-type", "schemas", "odl" };
67 String[] actualOrder = new String[expectedOrder.length];
70 for (DataSchemaNode augmentChild : augment1.getChildNodes()) {
71 actualOrder[i] = augmentChild.getQName().getLocalName();
75 assertArrayEquals(expectedOrder, actualOrder);
79 public void testOrderingNestedChildNodes1() throws URISyntaxException,
80 SourceException, ReactorException {
81 Set<Module> modules = TestUtils.loadModules(getClass().getResource(
83 Module foo = TestUtils.findModule(modules, "foo");
85 Collection<DataSchemaNode> childNodes = foo.getChildNodes();
86 String[] expectedOrder = new String[] { "int32-leaf", "string-leaf",
87 "invalid-pattern-string-leaf",
88 "invalid-direct-string-pattern-def-leaf",
89 "multiple-pattern-string-leaf",
90 "multiple-pattern-direct-string-def-leaf", "length-leaf",
91 "decimal-leaf", "decimal-leaf2", "ext", "union-leaf",
92 "custom-union-leaf", "transfer", "datas", "mycont", "data",
93 "how", "address", "port", "addresses", "peer", "id", "foo-id",
94 "sub-ext", "sub-transfer", "sub-datas" };
95 String[] actualOrder = new String[childNodes.size()];
98 for (DataSchemaNode child : childNodes) {
99 actualOrder[i] = child.getQName().getLocalName();
102 assertArrayEquals(expectedOrder, actualOrder);
106 public void testOrderingNestedChildNodes2() throws URISyntaxException,
107 SourceException, ReactorException {
108 Set<Module> modules = TestUtils.loadModules(getClass().getResource(
110 Module baz = TestUtils.findModule(modules, "baz");
111 Set<GroupingDefinition> groupings = baz.getGroupings();
112 assertEquals(1, groupings.size());
113 GroupingDefinition target = groupings.iterator().next();
115 Collection<DataSchemaNode> childNodes = target.getChildNodes();
116 String[] expectedOrder = new String[] { "data", "how", "address",
117 "port", "addresses" };
118 String[] actualOrder = new String[childNodes.size()];
121 for (DataSchemaNode child : childNodes) {
122 actualOrder[i] = child.getQName().getLocalName();
125 assertArrayEquals(expectedOrder, actualOrder);
129 public void testOrderingNestedChildNodes3() throws Exception {
130 Module baz = TestUtils.loadModule(getClass().getResourceAsStream(
131 "/ordering/foo.yang"));
132 ContainerSchemaNode x = (ContainerSchemaNode) baz
133 .getDataChildByName("x");
134 Collection<DataSchemaNode> childNodes = x.getChildNodes();
136 String[] expectedOrder = new String[] { "x15", "x10", "x5", "x1", "a5",
137 "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" };
138 String[] actualOrder = new String[childNodes.size()];
141 for (DataSchemaNode child : childNodes) {
142 actualOrder[i] = child.getQName().getLocalName();
145 assertArrayEquals(expectedOrder, actualOrder);