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.*;
12 import java.io.FileNotFoundException;
14 import java.text.DateFormat;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
25 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
28 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
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.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.api.Status;
35 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.UsesNode;
38 public class YangParserSimpleTest {
39 private final URI snNS = URI.create("urn:opendaylight:simple-nodes");
41 private final String snPref = "sn";
43 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
44 private Set<Module> modules;
47 public void init() throws FileNotFoundException, ParseException {
48 snRev = simpleDateFormat.parse("2013-07-30");
49 modules = TestUtils.loadModules(getClass().getResource("/simple-test").getPath());
53 public void testParseAnyXml() {
54 Module testModule = TestUtils.findModule(modules, "simple-nodes");
55 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
56 assertNotNull("'anyxml data not found'", data);
58 // test SchemaNode args
59 QName qname = data.getQName();
60 assertEquals("data", qname.getLocalName());
61 assertEquals(snPref, qname.getPrefix());
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 assertNull(constraints.getMinElements());
100 assertNull(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 = new QName(snNS, snRev, snPref, "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 assertNull(constraints.getMinElements());
150 assertNull(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 = new QName(snNS, snRev, snPref, "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 AnyXmlSchemaNode text = (AnyXmlSchemaNode)nodes.getDataChildByName("text");
171 ChoiceNode level = (ChoiceNode)nodes.getDataChildByName("level");
172 assertNotNull(level);
173 ContainerSchemaNode node = (ContainerSchemaNode)nodes.getDataChildByName("node");
175 LeafSchemaNode nodesId = (LeafSchemaNode)nodes.getDataChildByName("nodes-id");
176 assertNotNull(nodesId);
177 LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added");
178 assertNotNull(added);
179 ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
180 assertNotNull(links);
181 assertFalse(links.isUserOrdered());
182 LeafSchemaNode source = (LeafSchemaNode)nodes.getDataChildByName("source");
183 assertNotNull(source);
184 LeafSchemaNode target = (LeafSchemaNode)nodes.getDataChildByName("target");
185 assertNotNull(target);
187 Set<GroupingDefinition> groupings = nodes.getGroupings();
188 assertEquals(1, groupings.size());
189 GroupingDefinition nodeGroup = groupings.iterator().next();
190 QName groupQName = new QName(snNS, snRev, snPref, "node-group");
191 assertEquals(groupQName, nodeGroup.getQName());
192 SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
193 assertEquals(nodeGroupPath, nodeGroup.getPath());
195 Set<UsesNode> uses = nodes.getUses();
196 assertEquals(1, uses.size());
197 UsesNode use = uses.iterator().next();
198 assertEquals(nodeGroupPath, use.getGroupingPath());