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.AnydataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
30 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.opendaylight.yangtools.yang.model.api.Status;
37 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.UsesNode;
40 public class YangParserSimpleTest {
41 private static final QNameModule SN = QNameModule.create(URI.create("urn:opendaylight:simple-nodes"),
42 Revision.of("2013-07-30"));
43 private static final QName SN_NODES = QName.create(SN, "nodes");
44 private static final SchemaPath SN_NODES_PATH = SchemaPath.create(true, SN_NODES);
46 private SchemaContext context;
47 private Module testModule;
50 public void init() throws Exception {
51 context = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
52 testModule = TestUtils.findModule(context, "simple-nodes").get();
56 public void testParseAnyXml() {
57 final AnyxmlSchemaNode data = (AnyxmlSchemaNode) testModule.getDataChildByName(
58 QName.create(testModule.getQNameModule(), "data"));
59 assertNotNull("'anyxml data not found'", data);
60 assertFalse(data.equals(null));
61 assertEquals("AnyxmlEffectiveStatementImpl[qname=(urn:opendaylight:simple-nodes?revision=2013-07-30)data, "
62 + "path=AbsoluteSchemaPath{path=[(urn:opendaylight:simple-nodes?revision=2013-07-30)data]}]",
65 // test SchemaNode args
66 assertEquals(QName.create(SN, "data"), data.getQName());
67 assertEquals(Optional.of("anyxml desc"), data.getDescription());
68 assertEquals(Optional.of("data ref"), data.getReference());
69 assertEquals(Status.OBSOLETE, data.getStatus());
70 assertEquals(0, data.getUnknownSchemaNodes().size());
71 // test DataSchemaNode args
72 assertFalse(data.isAugmenting());
73 assertFalse(data.isConfiguration());
75 assertTrue(data.isMandatory());
76 assertEquals("class != 'wheel'", data.getWhenCondition().get().getOriginalString());
77 final Collection<MustDefinition> mustConstraints = data.getMustConstraints();
78 assertEquals(2, mustConstraints.size());
80 final String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
81 final String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
83 boolean found1 = false;
84 boolean found2 = false;
85 for (final MustDefinition must : mustConstraints) {
86 if (must1.equals(must.toString())) {
88 assertEquals(Optional.of("An ethernet MTU must be 1500"), must.getErrorMessage());
89 } else if (must2.equals(must.toString())) {
91 assertEquals(Optional.of("An atm MTU must be 64 .. 17966"), must.getErrorMessage());
92 assertEquals(Optional.of("anyxml data error-app-tag"), must.getErrorAppTag());
93 assertEquals(Optional.of("an error occured in data"), must.getDescription());
94 assertEquals(Optional.of("data must ref"), must.getReference());
102 public void testParseAnyData() {
103 final AnydataSchemaNode anydata = (AnydataSchemaNode) testModule.findDataChildByName(
104 QName.create(testModule.getQNameModule(), "data2")).orElse(null);
106 assertNotNull("'anydata data not found'", anydata);
107 assertEquals("AnydataEffectiveStatementImpl{qname=(urn:opendaylight:simple-nodes?revision=2013-07-30)data2, "
108 + "path=AbsoluteSchemaPath{path=[(urn:opendaylight:simple-nodes?revision=2013-07-30)data2]}}",
111 // test SchemaNode args
112 assertEquals(QName.create(SN, "data2"), anydata.getQName());
113 assertEquals(Optional.of("anydata desc"), anydata.getDescription());
114 assertEquals(Optional.of("data ref"), anydata.getReference());
115 assertEquals(Status.OBSOLETE, anydata.getStatus());
116 assertEquals(0, anydata.getUnknownSchemaNodes().size());
117 // test DataSchemaNode args
118 assertFalse(anydata.isAugmenting());
119 assertFalse(anydata.isConfiguration());
121 assertTrue(anydata.isMandatory());
122 assertTrue(anydata.getWhenCondition().isPresent());
123 assertEquals("class != 'wheel'", anydata.getWhenCondition().get().getOriginalString());
124 final Collection<MustDefinition> mustConstraints = anydata.getMustConstraints();
125 assertEquals(2, mustConstraints.size());
127 final String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
128 final String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
130 boolean found1 = false;
131 boolean found2 = false;
132 for (final MustDefinition must : mustConstraints) {
133 if (must1.equals(must.toString())) {
135 assertEquals(Optional.of("An ethernet MTU must be 1500"), must.getErrorMessage());
136 } else if (must2.equals(must.toString())) {
138 assertEquals(Optional.of("An atm MTU must be 64 .. 17966"), must.getErrorMessage());
139 assertEquals(Optional.of("anydata data error-app-tag"), must.getErrorAppTag());
140 assertEquals(Optional.of("an error occured in data"), must.getDescription());
141 assertEquals(Optional.of("data must ref"), must.getReference());
149 public void testParseContainer() {
150 final ContainerSchemaNode nodes = (ContainerSchemaNode) testModule
151 .getDataChildByName(QName.create(testModule.getQNameModule(), "nodes"));
152 // test SchemaNode args
153 assertEquals(SN_NODES, nodes.getQName());
154 assertEquals(SN_NODES_PATH, nodes.getPath());
155 assertEquals(Optional.of("nodes collection"), nodes.getDescription());
156 assertEquals(Optional.of("nodes ref"), nodes.getReference());
157 assertEquals(Status.CURRENT, nodes.getStatus());
158 assertEquals(0, nodes.getUnknownSchemaNodes().size());
159 // test DataSchemaNode args
160 assertFalse(nodes.isAugmenting());
161 assertFalse(nodes.isConfiguration());
164 assertEquals("class != 'wheel'", nodes.getWhenCondition().get().getOriginalString());
165 final Collection<MustDefinition> mustConstraints = nodes.getMustConstraints();
166 assertEquals(2, mustConstraints.size());
168 final String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
169 final String errMsg1 = "An atm MTU must be 64 .. 17966";
170 final String must2 = "ifId != 0";
172 boolean found1 = false;
173 boolean found2 = false;
174 for (final MustDefinition must : mustConstraints) {
175 if (must1.equals(must.toString())) {
177 assertEquals(Optional.of(errMsg1), must.getErrorMessage());
178 } else if (must2.equals(must.toString())) {
180 assertFalse(must.getErrorMessage().isPresent());
181 assertFalse(must.getErrorAppTag().isPresent());
182 assertFalse(must.getDescription().isPresent());
183 assertFalse(must.getReference().isPresent());
189 assertTrue(nodes.isPresenceContainer());
192 final Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
193 assertEquals(1, typedefs.size());
194 final TypeDefinition<?> nodesType = typedefs.iterator().next();
195 final QName typedefQName = QName.create(SN, "nodes-type");
196 assertEquals(typedefQName, nodesType.getQName());
197 assertEquals(SN_NODES_PATH.createChild(QName.create(SN, "nodes-type")), nodesType.getPath());
198 assertFalse(nodesType.getDescription().isPresent());
199 assertFalse(nodesType.getReference().isPresent());
200 assertEquals(Status.CURRENT, nodesType.getStatus());
201 assertEquals(0, nodesType.getUnknownSchemaNodes().size());
204 // total size = 8: defined 6, inserted by uses 2
205 assertEquals(8, nodes.getChildNodes().size());
206 final LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName(QName.create(
207 testModule.getQNameModule(), "added"));
208 assertEquals(createPath("nodes", "added"), added.getPath());
209 assertEquals(createPath("mytype"), added.getType().getPath());
211 final ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName(QName.create(
212 testModule.getQNameModule(), "links"));
213 assertFalse(links.isUserOrdered());
215 final Set<GroupingDefinition> groupings = nodes.getGroupings();
216 assertEquals(1, groupings.size());
217 final GroupingDefinition nodeGroup = groupings.iterator().next();
218 final QName groupQName = QName.create(SN, "node-group");
219 assertEquals(groupQName, nodeGroup.getQName());
220 final SchemaPath nodeGroupPath = SN_NODES_PATH.createChild(groupQName);
221 assertEquals(nodeGroupPath, nodeGroup.getPath());
223 final Set<UsesNode> uses = nodes.getUses();
224 assertEquals(1, uses.size());
225 final UsesNode use = uses.iterator().next();
226 assertEquals(nodeGroupPath, use.getGroupingPath());
230 private static final URI NS = URI.create("urn:opendaylight:simple-nodes");
232 private static SchemaPath createPath(final String... names) {
233 final Revision rev = Revision.of("2013-07-30");
234 final List<QName> path = new ArrayList<>();
235 for (final String name : names) {
236 path.add(QName.create(NS, rev, name));
238 return SchemaPath.create(path, true);