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.assertNull;
14 import static org.junit.Assert.assertTrue;
17 import java.util.ArrayList;
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.ConstraintDefinition;
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());
74 final ConstraintDefinition constraints = data.getConstraints();
75 assertEquals("class != 'wheel'", constraints.getWhenCondition().get().toString());
76 final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
77 assertEquals(2, constraints.getMustConstraints().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());
99 assertTrue(constraints.isMandatory());
100 assertNull(constraints.getMinElements());
101 assertNull(constraints.getMaxElements());
105 public void testParseContainer() {
106 final ContainerSchemaNode nodes = (ContainerSchemaNode) testModule
107 .getDataChildByName(QName.create(testModule.getQNameModule(), "nodes"));
108 // test SchemaNode args
109 assertEquals(SN_NODES, nodes.getQName());
110 assertEquals(SN_NODES_PATH, nodes.getPath());
111 assertEquals(Optional.of("nodes collection"), nodes.getDescription());
112 assertEquals(Optional.of("nodes ref"), nodes.getReference());
113 assertEquals(Status.CURRENT, nodes.getStatus());
114 assertEquals(0, nodes.getUnknownSchemaNodes().size());
115 // test DataSchemaNode args
116 assertFalse(nodes.isAugmenting());
117 assertFalse(nodes.isConfiguration());
120 final ConstraintDefinition constraints = nodes.getConstraints();
121 assertEquals("class != 'wheel'", constraints.getWhenCondition().get().toString());
122 final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
123 assertEquals(2, constraints.getMustConstraints().size());
125 final String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
126 final String errMsg1 = "An atm MTU must be 64 .. 17966";
127 final String must2 = "ifId != 0";
129 boolean found1 = false;
130 boolean found2 = false;
131 for (final MustDefinition must : mustConstraints) {
132 if (must1.equals(must.toString())) {
134 assertEquals(Optional.of(errMsg1), must.getErrorMessage());
135 } else if (must2.equals(must.toString())) {
137 assertFalse(must.getErrorMessage().isPresent());
138 assertFalse(must.getErrorAppTag().isPresent());
139 assertFalse(must.getDescription().isPresent());
140 assertFalse(must.getReference().isPresent());
146 assertFalse(constraints.isMandatory());
147 assertNull(constraints.getMinElements());
148 assertNull(constraints.getMaxElements());
149 assertTrue(nodes.isPresenceContainer());
152 final Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
153 assertEquals(1, typedefs.size());
154 final TypeDefinition<?> nodesType = typedefs.iterator().next();
155 final QName typedefQName = QName.create(SN, "nodes-type");
156 assertEquals(typedefQName, nodesType.getQName());
157 assertEquals(SN_NODES_PATH.createChild(QName.create(SN, "nodes-type")), nodesType.getPath());
158 assertFalse(nodesType.getDescription().isPresent());
159 assertFalse(nodesType.getReference().isPresent());
160 assertEquals(Status.CURRENT, nodesType.getStatus());
161 assertEquals(0, nodesType.getUnknownSchemaNodes().size());
164 // total size = 8: defined 6, inserted by uses 2
165 assertEquals(8, nodes.getChildNodes().size());
166 final LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName(QName.create(
167 testModule.getQNameModule(), "added"));
168 assertEquals(createPath("nodes", "added"), added.getPath());
169 assertEquals(createPath("mytype"), added.getType().getPath());
171 final ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName(QName.create(
172 testModule.getQNameModule(), "links"));
173 assertFalse(links.isUserOrdered());
175 final Set<GroupingDefinition> groupings = nodes.getGroupings();
176 assertEquals(1, groupings.size());
177 final GroupingDefinition nodeGroup = groupings.iterator().next();
178 final QName groupQName = QName.create(SN, "node-group");
179 assertEquals(groupQName, nodeGroup.getQName());
180 final SchemaPath nodeGroupPath = SN_NODES_PATH.createChild(groupQName);
181 assertEquals(nodeGroupPath, nodeGroup.getPath());
183 final Set<UsesNode> uses = nodes.getUses();
184 assertEquals(1, uses.size());
185 final UsesNode use = uses.iterator().next();
186 assertEquals(nodeGroupPath, use.getGroupingPath());
190 private static final URI NS = URI.create("urn:opendaylight:simple-nodes");
192 private static SchemaPath createPath(final String... names) {
193 final Revision rev = Revision.of("2013-07-30");
194 final List<QName> path = new ArrayList<>();
195 for (final String name : names) {
196 path.add(QName.create(NS, rev, name));
198 return SchemaPath.create(path, true);