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.stmt.retest;
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;
16 import java.text.DateFormat;
17 import java.text.ParseException;
18 import java.text.SimpleDateFormat;
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
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.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 final URI snNS = URI.create("urn:opendaylight:simple-nodes");
42 private final String snPref = "sn";
44 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
45 private Set<Module> modules;
48 public void init() throws Exception {
49 snRev = simpleDateFormat.parse("2013-07-30");
50 modules = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
54 public void testParseAnyXml() {
55 Module testModule = TestUtils.findModule(modules, "simple-nodes");
56 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
57 assertNotNull("'anyxml data not found'", data);
59 // test SchemaNode args
60 QName qname = data.getQName();
61 assertEquals("data", qname.getLocalName());
62 assertEquals(snNS, qname.getNamespace());
63 assertEquals(snRev, qname.getRevision());
64 assertEquals("anyxml desc", data.getDescription());
65 assertEquals("data ref", data.getReference());
66 assertEquals(Status.OBSOLETE, data.getStatus());
67 assertEquals(0, data.getUnknownSchemaNodes().size());
68 // test DataSchemaNode args
69 assertFalse(data.isAugmenting());
70 assertFalse(data.isConfiguration());
71 ConstraintDefinition constraints = data.getConstraints();
72 assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
73 Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
74 assertEquals(2, constraints.getMustConstraints().size());
76 String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
77 String errMsg1 = "An ethernet MTU must be 1500";
78 String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
79 String errMsg2 = "An atm MTU must be 64 .. 17966";
81 boolean found1 = false;
82 boolean found2 = false;
83 for (MustDefinition must : mustConstraints) {
84 if (must1.equals(must.toString())) {
86 assertEquals(errMsg1, must.getErrorMessage());
87 } else if (must2.equals(must.toString())) {
89 assertEquals(errMsg2, must.getErrorMessage());
90 assertEquals("anyxml data error-app-tag", must.getErrorAppTag());
91 assertEquals("an error occured in data", must.getDescription());
92 assertEquals("data must ref", must.getReference());
98 assertTrue(constraints.isMandatory());
99 assertTrue(0 == constraints.getMinElements());
100 assertTrue(Integer.MAX_VALUE == constraints.getMaxElements());
104 public void testParseContainer() {
105 Module test = TestUtils.findModule(modules, "simple-nodes");
107 ContainerSchemaNode nodes = (ContainerSchemaNode) test.getDataChildByName("nodes");
108 // test SchemaNode args
109 QName expectedQName = QName.create(snNS, snRev, "nodes");
110 assertEquals(expectedQName, nodes.getQName());
111 SchemaPath expectedPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes");
112 assertEquals(expectedPath, nodes.getPath());
113 assertEquals("nodes collection", nodes.getDescription());
114 assertEquals("nodes ref", nodes.getReference());
115 assertEquals(Status.CURRENT, nodes.getStatus());
116 assertEquals(0, nodes.getUnknownSchemaNodes().size());
117 // test DataSchemaNode args
118 assertFalse(nodes.isAugmenting());
119 assertFalse(nodes.isConfiguration());
122 ConstraintDefinition constraints = nodes.getConstraints();
123 assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
124 Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
125 assertEquals(2, constraints.getMustConstraints().size());
127 String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
128 String errMsg1 = "An atm MTU must be 64 .. 17966";
129 String must2 = "ifId != 0";
131 boolean found1 = false;
132 boolean found2 = false;
133 for (MustDefinition must : mustConstraints) {
134 if (must1.equals(must.toString())) {
136 assertEquals(errMsg1, must.getErrorMessage());
137 } else if (must2.equals(must.toString())) {
139 assertNull(must.getErrorMessage());
140 assertNull(must.getErrorAppTag());
141 assertNull(must.getDescription());
142 assertNull(must.getReference());
148 assertFalse(constraints.isMandatory());
149 assertTrue(0 == constraints.getMinElements());
150 assertTrue(Integer.MAX_VALUE == constraints.getMaxElements());
151 assertTrue(nodes.isPresenceContainer());
154 Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
155 assertEquals(1, typedefs.size());
156 TypeDefinition<?> nodesType = typedefs.iterator().next();
157 QName typedefQName = QName.create(snNS, snRev, "nodes-type");
158 assertEquals(typedefQName, nodesType.getQName());
159 SchemaPath nodesTypePath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "nodes-type");
160 assertEquals(nodesTypePath, nodesType.getPath());
161 assertNull(nodesType.getDescription());
162 assertNull(nodesType.getReference());
163 assertEquals(Status.CURRENT, nodesType.getStatus());
164 assertEquals(0, nodesType.getUnknownSchemaNodes().size());
167 // total size = 8: defined 6, inserted by uses 2
168 assertEquals(8, nodes.getChildNodes().size());
169 LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added");
170 assertEquals(createPath("nodes", "added"), added.getPath());
171 assertEquals(createPath("mytype"), added.getType().getPath());
173 ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
174 assertFalse(links.isUserOrdered());
176 Set<GroupingDefinition> groupings = nodes.getGroupings();
177 assertEquals(1, groupings.size());
178 GroupingDefinition nodeGroup = groupings.iterator().next();
179 QName groupQName = QName.create(snNS, snRev, "node-group");
180 assertEquals(groupQName, nodeGroup.getQName());
181 SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
182 assertEquals(nodeGroupPath, nodeGroup.getPath());
184 Set<UsesNode> uses = nodes.getUses();
185 assertEquals(1, uses.size());
186 UsesNode use = uses.iterator().next();
187 assertEquals(nodeGroupPath, use.getGroupingPath());
191 private final URI ns = URI.create("urn:opendaylight:simple-nodes");
193 private final String prefix = "sn";
195 private SchemaPath createPath(final String... names) {
197 rev = new SimpleDateFormat("yyyy-MM-dd").parse("2013-07-30");
198 } catch (ParseException e) {
202 List<QName> path = new ArrayList<>();
203 for (String name : names) {
204 path.add(QName.create(ns, rev, name));
206 return SchemaPath.create(path, true);