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", "invalid-string-pattern", "multiple-pattern-string", "my-decimal-type", "my-union",
50 "my-union-ext", "nested-union2"
52 final String[] actualOrder = new String[typedefs.size()];
55 for (final TypeDefinition<?> type : typedefs) {
56 actualOrder[offset] = type.getQName().getLocalName();
59 assertArrayEquals(expectedOrder, actualOrder);
63 public void testOrderingChildNodes() throws Exception {
64 AugmentationSchemaNode augment1 = null;
65 for (final AugmentationSchemaNode as : foo.getAugmentations()) {
66 if (as.getChildNodes().size() == 5) {
71 assertNotNull(augment1);
73 final String[] expectedOrder = { "ds0ChannelNumber", "interface-id", "my-type", "schemas", "odl" };
74 final String[] actualOrder = new String[expectedOrder.length];
77 for (final DataSchemaNode augmentChild : augment1.getChildNodes()) {
78 actualOrder[offset] = augmentChild.getQName().getLocalName();
82 assertArrayEquals(expectedOrder, actualOrder);
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"
94 final String[] actualOrder = new String[childNodes.size()];
97 for (final DataSchemaNode child : childNodes) {
98 actualOrder[offset] = child.getQName().getLocalName();
101 assertArrayEquals(expectedOrder, actualOrder);
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();
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()];
115 for (final DataSchemaNode child : childNodes) {
116 actualOrder[offset] = child.getQName().getLocalName();
119 assertArrayEquals(expectedOrder, actualOrder);
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();
130 final String[] expectedOrder = { "x15", "x10", "x5", "x1", "a5", "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" };
131 final String[] actualOrder = new String[childNodes.size()];
134 for (final DataSchemaNode child : childNodes) {
135 actualOrder[offset] = child.getQName().getLocalName();
138 assertArrayEquals(expectedOrder, actualOrder);