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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Optional;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.api.Status;
36 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.UsesNode;
39 public class YangParserSimpleTest {
40 private static final QNameModule SN = QNameModule.create(URI.create("urn:opendaylight:simple-nodes"),
41 Revision.of("2013-07-30"));
42 private static final QName SN_NODES = QName.create(SN, "nodes");
43 private static final SchemaPath SN_NODES_PATH = SchemaPath.create(true, SN_NODES);
45 private SchemaContext context;
46 private Module testModule;
49 public void init() throws Exception {
50 context = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
51 testModule = TestUtils.findModule(context, "simple-nodes").get();
55 public void testParseAnyXml() {
56 final AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName(
57 QName.create(testModule.getQNameModule(), "data"));
58 assertNotNull("'anyxml data not found'", data);
59 assertFalse(data.equals(null));
60 assertEquals("AnyxmlEffectiveStatementImpl[qname=(urn:opendaylight:simple-nodes?revision=2013-07-30)data, "
61 + "path=AbsoluteSchemaPath{path=[(urn:opendaylight:simple-nodes?revision=2013-07-30)data]}]",
64 // test SchemaNode args
65 assertEquals(QName.create(SN, "data"), data.getQName());
66 assertEquals(Optional.of("anyxml desc"), data.getDescription());
67 assertEquals(Optional.of("data ref"), data.getReference());
68 assertEquals(Status.OBSOLETE, data.getStatus());
69 assertEquals(0, data.getUnknownSchemaNodes().size());
70 // test DataSchemaNode args
71 assertFalse(data.isAugmenting());
72 assertFalse(data.isConfiguration());
74 assertTrue(data.isMandatory());
75 assertEquals("class != 'wheel'", data.getWhenCondition().get().toString());
76 final Collection<MustDefinition> mustConstraints = data.getMustConstraints();
77 assertEquals(2, mustConstraints.size());
79 final String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
80 final String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
82 boolean found1 = false;
83 boolean found2 = false;
84 for (final MustDefinition must : mustConstraints) {
85 if (must1.equals(must.toString())) {
87 assertEquals(Optional.of("An ethernet MTU must be 1500"), must.getErrorMessage());
88 } else if (must2.equals(must.toString())) {
90 assertEquals(Optional.of("An atm MTU must be 64 .. 17966"), must.getErrorMessage());
91 assertEquals(Optional.of("anyxml data error-app-tag"), must.getErrorAppTag());
92 assertEquals(Optional.of("an error occured in data"), must.getDescription());
93 assertEquals(Optional.of("data must ref"), must.getReference());
101 public void testParseContainer() {
102 final ContainerSchemaNode nodes = (ContainerSchemaNode) testModule
103 .getDataChildByName(QName.create(testModule.getQNameModule(), "nodes"));
104 // test SchemaNode args
105 assertEquals(SN_NODES, nodes.getQName());
106 assertEquals(SN_NODES_PATH, nodes.getPath());
107 assertEquals(Optional.of("nodes collection"), nodes.getDescription());
108 assertEquals(Optional.of("nodes ref"), nodes.getReference());
109 assertEquals(Status.CURRENT, nodes.getStatus());
110 assertEquals(0, nodes.getUnknownSchemaNodes().size());
111 // test DataSchemaNode args
112 assertFalse(nodes.isAugmenting());
113 assertFalse(nodes.isConfiguration());
116 assertEquals("class != 'wheel'", nodes.getWhenCondition().get().toString());
117 final Collection<MustDefinition> mustConstraints = nodes.getMustConstraints();
118 assertEquals(2, mustConstraints.size());
120 final String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
121 final String errMsg1 = "An atm MTU must be 64 .. 17966";
122 final String must2 = "ifId != 0";
124 boolean found1 = false;
125 boolean found2 = false;
126 for (final MustDefinition must : mustConstraints) {
127 if (must1.equals(must.toString())) {
129 assertEquals(Optional.of(errMsg1), must.getErrorMessage());
130 } else if (must2.equals(must.toString())) {
132 assertFalse(must.getErrorMessage().isPresent());
133 assertFalse(must.getErrorAppTag().isPresent());
134 assertFalse(must.getDescription().isPresent());
135 assertFalse(must.getReference().isPresent());
141 assertTrue(nodes.isPresenceContainer());
144 final Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
145 assertEquals(1, typedefs.size());
146 final TypeDefinition<?> nodesType = typedefs.iterator().next();
147 final QName typedefQName = QName.create(SN, "nodes-type");
148 assertEquals(typedefQName, nodesType.getQName());
149 assertEquals(SN_NODES_PATH.createChild(QName.create(SN, "nodes-type")), nodesType.getPath());
150 assertFalse(nodesType.getDescription().isPresent());
151 assertFalse(nodesType.getReference().isPresent());
152 assertEquals(Status.CURRENT, nodesType.getStatus());
153 assertEquals(0, nodesType.getUnknownSchemaNodes().size());
156 // total size = 8: defined 6, inserted by uses 2
157 assertEquals(8, nodes.getChildNodes().size());
158 final LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName(QName.create(
159 testModule.getQNameModule(), "added"));
160 assertEquals(createPath("nodes", "added"), added.getPath());
161 assertEquals(createPath("mytype"), added.getType().getPath());
163 final ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName(QName.create(
164 testModule.getQNameModule(), "links"));
165 assertFalse(links.isUserOrdered());
167 final Set<GroupingDefinition> groupings = nodes.getGroupings();
168 assertEquals(1, groupings.size());
169 final GroupingDefinition nodeGroup = groupings.iterator().next();
170 final QName groupQName = QName.create(SN, "node-group");
171 assertEquals(groupQName, nodeGroup.getQName());
172 final SchemaPath nodeGroupPath = SN_NODES_PATH.createChild(groupQName);
173 assertEquals(nodeGroupPath, nodeGroup.getPath());
175 final Set<UsesNode> uses = nodes.getUses();
176 assertEquals(1, uses.size());
177 final UsesNode use = uses.iterator().next();
178 assertEquals(nodeGroupPath, use.getGroupingPath());
182 private static final URI NS = URI.create("urn:opendaylight:simple-nodes");
184 private static SchemaPath createPath(final String... names) {
185 final Revision rev = Revision.of("2013-07-30");
186 final List<QName> path = new ArrayList<>();
187 for (final String name : names) {
188 path.add(QName.create(NS, rev, name));
190 return SchemaPath.create(path, true);