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;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 public class OrderingTest {
33 private SchemaContext context;
39 public void setup() throws ReactorException, IOException, YangSyntaxErrorException, URISyntaxException {
40 context = TestUtils.loadModules(getClass().getResource("/model").toURI());
41 foo = TestUtils.findModule(context, "foo").get();
42 bar = TestUtils.findModule(context, "bar").get();
43 baz = TestUtils.findModule(context, "baz").get();
47 public void testOrderingTypedef() throws Exception {
48 final Set<TypeDefinition<?>> typedefs = bar.getTypeDefinitions();
49 final String[] expectedOrder = { "int32-ext1", "int32-ext2", "string-ext1", "string-ext2", "string-ext3",
50 "string-ext4", "invalid-string-pattern", "multiple-pattern-string", "my-decimal-type", "my-union",
51 "my-union-ext", "nested-union2"
53 final String[] actualOrder = new String[typedefs.size()];
56 for (final TypeDefinition<?> type : typedefs) {
57 actualOrder[offset] = type.getQName().getLocalName();
60 assertArrayEquals(expectedOrder, actualOrder);
64 public void testOrderingChildNodes() throws Exception {
65 AugmentationSchema augment1 = null;
66 for (final AugmentationSchema as : foo.getAugmentations()) {
67 if (as.getChildNodes().size() == 5) {
72 assertNotNull(augment1);
74 final String[] expectedOrder = { "ds0ChannelNumber", "interface-id", "my-type", "schemas", "odl" };
75 final String[] actualOrder = new String[expectedOrder.length];
78 for (final DataSchemaNode augmentChild : augment1.getChildNodes()) {
79 actualOrder[offset] = augmentChild.getQName().getLocalName();
83 assertArrayEquals(expectedOrder, actualOrder);
87 public void testOrderingNestedChildNodes1() throws Exception {
88 final Collection<DataSchemaNode> childNodes = foo.getChildNodes();
89 final String[] expectedOrder = { "int32-leaf", "string-leaf", "invalid-pattern-string-leaf",
90 "invalid-direct-string-pattern-def-leaf", "multiple-pattern-string-leaf",
91 "multiple-pattern-direct-string-def-leaf", "length-leaf", "decimal-leaf", "decimal-leaf2", "ext",
92 "union-leaf", "custom-union-leaf", "transfer", "datas", "mycont", "data", "how", "address", "port",
93 "addresses", "peer", "id", "foo-id", "sub-ext", "sub-transfer", "sub-datas"
95 final String[] actualOrder = new String[childNodes.size()];
98 for (final DataSchemaNode child : childNodes) {
99 actualOrder[offset] = child.getQName().getLocalName();
102 assertArrayEquals(expectedOrder, actualOrder);
106 public void testOrderingNestedChildNodes2() throws Exception {
107 final Set<GroupingDefinition> groupings = baz.getGroupings();
108 assertEquals(1, groupings.size());
109 final GroupingDefinition target = groupings.iterator().next();
111 final Collection<DataSchemaNode> childNodes = target.getChildNodes();
112 final String[] expectedOrder = { "data", "how", "address", "port", "addresses" };
113 final String[] actualOrder = new String[childNodes.size()];
116 for (final DataSchemaNode child : childNodes) {
117 actualOrder[offset] = child.getQName().getLocalName();
120 assertArrayEquals(expectedOrder, actualOrder);
124 public void testOrderingNestedChildNodes3() throws Exception {
125 final Module baz = TestUtils.loadModuleResources(getClass(), "/ordering/foo.yang")
126 .getModules().iterator().next();
127 final ContainerSchemaNode x = (ContainerSchemaNode) baz
128 .getDataChildByName(QName.create(baz.getQNameModule(), "x"));
129 final Collection<DataSchemaNode> childNodes = x.getChildNodes();
131 final String[] expectedOrder = { "x15", "x10", "x5", "x1", "a5", "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" };
132 final String[] actualOrder = new String[childNodes.size()];
135 for (final DataSchemaNode child : childNodes) {
136 actualOrder[offset] = child.getQName().getLocalName();
139 assertArrayEquals(expectedOrder, actualOrder);