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.ArrayList;
18 import java.util.Date;
19 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
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.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
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 final URI snNS = URI.create("urn:opendaylight:simple-nodes");
43 private final String snPref = "sn";
45 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
46 private Set<Module> modules;
49 public void init() throws FileNotFoundException, ParseException {
50 snRev = simpleDateFormat.parse("2013-07-30");
51 modules = TestUtils.loadModules(getClass().getResource("/simple-test").getPath());
55 public void testParseAnyXml() {
56 Module testModule = TestUtils.findModule(modules, "simple-nodes");
57 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
58 assertNotNull("'anyxml data not found'", data);
60 // test SchemaNode args
61 QName qname = data.getQName();
62 assertEquals("data", qname.getLocalName());
63 assertEquals(snPref, qname.getPrefix());
64 assertEquals(snNS, qname.getNamespace());
65 assertEquals(snRev, qname.getRevision());
66 assertEquals("anyxml desc", data.getDescription());
67 assertEquals("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());
73 ConstraintDefinition constraints = data.getConstraints();
74 assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
75 Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
76 assertEquals(2, constraints.getMustConstraints().size());
78 String must1 = "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"";
79 String errMsg1 = "An ethernet MTU must be 1500";
80 String must2 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
81 String errMsg2 = "An atm MTU must be 64 .. 17966";
83 boolean found1 = false;
84 boolean found2 = false;
85 for (MustDefinition must : mustConstraints) {
86 if (must1.equals(must.toString())) {
88 assertEquals(errMsg1, must.getErrorMessage());
89 } else if (must2.equals(must.toString())) {
91 assertEquals(errMsg2, must.getErrorMessage());
92 assertEquals("anyxml data error-app-tag", must.getErrorAppTag());
93 assertEquals("an error occured in data", must.getDescription());
94 assertEquals("data must ref", must.getReference());
100 assertTrue(constraints.isMandatory());
101 assertNull(constraints.getMinElements());
102 assertNull(constraints.getMaxElements());
106 public void testParseContainer() {
107 Module test = TestUtils.findModule(modules, "simple-nodes");
109 ContainerSchemaNode nodes = (ContainerSchemaNode) test.getDataChildByName("nodes");
110 // test SchemaNode args
111 QName expectedQName = new QName(snNS, snRev, snPref, "nodes");
112 assertEquals(expectedQName, nodes.getQName());
113 SchemaPath expectedPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes");
114 assertEquals(expectedPath, nodes.getPath());
115 assertEquals("nodes collection", nodes.getDescription());
116 assertEquals("nodes ref", nodes.getReference());
117 assertEquals(Status.CURRENT, nodes.getStatus());
118 assertEquals(0, nodes.getUnknownSchemaNodes().size());
119 // test DataSchemaNode args
120 assertFalse(nodes.isAugmenting());
121 assertFalse(nodes.isConfiguration());
124 ConstraintDefinition constraints = nodes.getConstraints();
125 assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
126 Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
127 assertEquals(2, constraints.getMustConstraints().size());
129 String must1 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
130 String errMsg1 = "An atm MTU must be 64 .. 17966";
131 String must2 = "ifId != 0";
133 boolean found1 = false;
134 boolean found2 = false;
135 for (MustDefinition must : mustConstraints) {
136 if (must1.equals(must.toString())) {
138 assertEquals(errMsg1, must.getErrorMessage());
139 } else if (must2.equals(must.toString())) {
141 assertNull(must.getErrorMessage());
142 assertNull(must.getErrorAppTag());
143 assertNull(must.getDescription());
144 assertNull(must.getReference());
150 assertFalse(constraints.isMandatory());
151 assertNull(constraints.getMinElements());
152 assertNull(constraints.getMaxElements());
153 assertTrue(nodes.isPresenceContainer());
156 Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
157 assertEquals(1, typedefs.size());
158 TypeDefinition<?> nodesType = typedefs.iterator().next();
159 QName typedefQName = new QName(snNS, snRev, snPref, "nodes-type");
160 assertEquals(typedefQName, nodesType.getQName());
161 SchemaPath nodesTypePath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "nodes-type");
162 assertEquals(nodesTypePath, nodesType.getPath());
163 assertNull(nodesType.getDescription());
164 assertNull(nodesType.getReference());
165 assertEquals(Status.CURRENT, nodesType.getStatus());
166 assertEquals(0, nodesType.getUnknownSchemaNodes().size());
169 // total size = 8: defined 6, inserted by uses 2
170 assertEquals(8, nodes.getChildNodes().size());
171 AnyXmlSchemaNode text = (AnyXmlSchemaNode)nodes.getDataChildByName("text");
173 ChoiceNode level = (ChoiceNode)nodes.getDataChildByName("level");
174 assertNotNull(level);
175 ContainerSchemaNode node = (ContainerSchemaNode)nodes.getDataChildByName("node");
177 LeafSchemaNode nodesId = (LeafSchemaNode)nodes.getDataChildByName("nodes-id");
178 assertNotNull(nodesId);
179 LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added");
180 assertNotNull(added);
181 assertEquals(createPath("nodes", "added"), added.getPath());
182 assertEquals(createPath("nodes", "added", "mytype"), added.getType().getPath());
184 ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
185 assertNotNull(links);
186 assertFalse(links.isUserOrdered());
187 LeafSchemaNode source = (LeafSchemaNode)nodes.getDataChildByName("source");
188 assertNotNull(source);
189 LeafSchemaNode target = (LeafSchemaNode)nodes.getDataChildByName("target");
190 assertNotNull(target);
192 Set<GroupingDefinition> groupings = nodes.getGroupings();
193 assertEquals(1, groupings.size());
194 GroupingDefinition nodeGroup = groupings.iterator().next();
195 QName groupQName = new QName(snNS, snRev, snPref, "node-group");
196 assertEquals(groupQName, nodeGroup.getQName());
197 SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
198 assertEquals(nodeGroupPath, nodeGroup.getPath());
200 Set<UsesNode> uses = nodes.getUses();
201 assertEquals(1, uses.size());
202 UsesNode use = uses.iterator().next();
203 assertEquals(nodeGroupPath, use.getGroupingPath());
207 private final URI ns = URI.create("urn:opendaylight:simple-nodes");
209 private final String prefix = "sn";
211 private SchemaPath createPath(String... names) {
213 rev = TestUtils.simpleDateFormat.parse("2013-07-30");
214 } catch (ParseException e) {
218 List<QName> path = new ArrayList<>();
219 for (String name : names) {
220 path.add(new QName(ns, rev, prefix, name));
222 return new SchemaPath(path, true);