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.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;
30 public class OrderingTest {
32 private SchemaContext context;
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();
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", "multiple-pattern-string", "my-decimal-type", "my-union", "my-union-ext", "nested-union2"
51 final String[] actualOrder = new String[typedefs.size()];
54 for (final TypeDefinition<?> type : typedefs) {
55 actualOrder[offset] = type.getQName().getLocalName();
58 assertArrayEquals(expectedOrder, actualOrder);
62 public void testOrderingChildNodes() throws Exception {
63 AugmentationSchemaNode augment1 = null;
64 for (final AugmentationSchemaNode as : foo.getAugmentations()) {
65 if (as.getChildNodes().size() == 5) {
70 assertNotNull(augment1);
72 final String[] expectedOrder = { "ds0ChannelNumber", "interface-id", "my-type", "schemas", "odl" };
73 final String[] actualOrder = new String[expectedOrder.length];
76 for (final DataSchemaNode augmentChild : augment1.getChildNodes()) {
77 actualOrder[offset] = augmentChild.getQName().getLocalName();
81 assertArrayEquals(expectedOrder, actualOrder);
85 public void testOrderingNestedChildNodes1() throws Exception {
86 final Collection<? extends DataSchemaNode> childNodes = foo.getChildNodes();
87 final String[] expectedOrder = { "int32-leaf", "string-leaf", "multiple-pattern-string-leaf",
88 "multiple-pattern-direct-string-def-leaf", "length-leaf", "decimal-leaf", "decimal-leaf2", "ext",
89 "union-leaf", "custom-union-leaf", "transfer", "datas", "mycont", "data", "how", "address", "port",
90 "addresses", "peer", "id", "foo-id", "sub-ext", "sub-transfer", "sub-datas"
92 final String[] actualOrder = new String[childNodes.size()];
95 for (final DataSchemaNode child : childNodes) {
96 actualOrder[offset] = child.getQName().getLocalName();
99 assertArrayEquals(expectedOrder, actualOrder);
103 public void testOrderingNestedChildNodes2() throws Exception {
104 final Collection<? extends GroupingDefinition> groupings = baz.getGroupings();
105 assertEquals(1, groupings.size());
106 final GroupingDefinition target = groupings.iterator().next();
108 final Collection<? extends DataSchemaNode> childNodes = target.getChildNodes();
109 final String[] expectedOrder = { "data", "how", "address", "port", "addresses" };
110 final String[] actualOrder = new String[childNodes.size()];
113 for (final DataSchemaNode child : childNodes) {
114 actualOrder[offset] = child.getQName().getLocalName();
117 assertArrayEquals(expectedOrder, actualOrder);
121 public void testOrderingNestedChildNodes3() throws Exception {
122 final Module justFoo = StmtTestUtils.parseYangSource("/ordering/foo.yang").getModules().iterator().next();
123 final ContainerSchemaNode x = (ContainerSchemaNode) justFoo
124 .getDataChildByName(QName.create(justFoo.getQNameModule(), "x"));
125 final Collection<? extends DataSchemaNode> childNodes = x.getChildNodes();
127 final String[] expectedOrder = { "x15", "x10", "x5", "x1", "a5", "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" };
128 final String[] actualOrder = new String[childNodes.size()];
131 for (final DataSchemaNode child : childNodes) {
132 actualOrder[offset] = child.getQName().getLocalName();
135 assertArrayEquals(expectedOrder, actualOrder);