2 * Copyright (c) 2013 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.parser.impl;
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.Test;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 public class OrderingTest {
29 public void testOrderingTypedef() throws IOException, URISyntaxException {
30 Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
31 Module bar = TestUtils.findModule(modules, "bar");
32 Set<TypeDefinition<?>> typedefs = bar.getTypeDefinitions();
33 String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2", "invalid-string-pattern",
34 "multiple-pattern-string", "my-decimal-type", "my-union", "my-union-ext", "nested-union2",
35 "string-ext1", "string-ext2", "string-ext3", "string-ext4" };
36 String[] actualOrder = new String[typedefs.size()];
39 for (TypeDefinition<?> type : typedefs) {
40 actualOrder[i] = type.getQName().getLocalName();
43 assertArrayEquals(expectedOrder, actualOrder);
47 public void testOrderingChildNodes() throws IOException, URISyntaxException {
48 Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
49 Module foo = TestUtils.findModule(modules, "foo");
50 AugmentationSchema augment1 = null;
51 for (AugmentationSchema as : foo.getAugmentations()) {
52 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
57 assertNotNull(augment1);
59 String[] expectedOrder = new String[] { "ds0ChannelNumber", "interface-id", "my-type", "schemas", "odl" };
60 String[] actualOrder = new String[expectedOrder.length];
63 for (DataSchemaNode augmentChild : augment1.getChildNodes()) {
64 actualOrder[i] = augmentChild.getQName().getLocalName();
68 assertArrayEquals(expectedOrder, actualOrder);
72 public void testOrderingNestedChildNodes1() throws IOException, URISyntaxException {
73 Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
74 Module foo = TestUtils.findModule(modules, "foo");
76 Collection<DataSchemaNode> childNodes = foo.getChildNodes();
77 String[] expectedOrder = new String[] { "int32-leaf", "string-leaf", "invalid-pattern-string-leaf",
78 "invalid-direct-string-pattern-def-leaf", "multiple-pattern-string-leaf",
79 "multiple-pattern-direct-string-def-leaf", "length-leaf", "decimal-leaf", "decimal-leaf2", "ext",
80 "union-leaf", "custom-union-leaf", "transfer", "datas", "mycont", "data", "how", "address", "port",
81 "addresses", "peer", "id", "foo-id","sub-ext", "sub-transfer", "sub-datas" };
82 String[] actualOrder = new String[childNodes.size()];
85 for (DataSchemaNode child : childNodes) {
86 actualOrder[i] = child.getQName().getLocalName();
89 assertArrayEquals(expectedOrder, actualOrder);
93 public void testOrderingNestedChildNodes2() throws IOException, URISyntaxException {
94 Set<Module> modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
95 Module baz = TestUtils.findModule(modules, "baz");
96 Set<GroupingDefinition> groupings = baz.getGroupings();
97 assertEquals(1, groupings.size());
98 GroupingDefinition target = groupings.iterator().next();
100 Collection<DataSchemaNode> childNodes = target.getChildNodes();
101 String[] expectedOrder = new String[] { "data", "how", "address", "port", "addresses" };
102 String[] actualOrder = new String[childNodes.size()];
105 for (DataSchemaNode child : childNodes) {
106 actualOrder[i] = child.getQName().getLocalName();
109 assertArrayEquals(expectedOrder, actualOrder);
113 public void testOrderingNestedChildNodes3() throws Exception {
114 Module baz = TestUtils.loadModule(getClass().getResourceAsStream("/ordering/foo.yang"));
115 ContainerSchemaNode x = (ContainerSchemaNode) baz.getDataChildByName("x");
116 Collection<DataSchemaNode> childNodes = x.getChildNodes();
118 String[] expectedOrder = new String[] { "x15", "x10", "x5", "x1", "a5", "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" };
119 String[] actualOrder = new String[childNodes.size()];
122 for (DataSchemaNode child : childNodes) {
123 actualOrder[i] = child.getQName().getLocalName();
126 assertArrayEquals(expectedOrder, actualOrder);