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.*;
13 import java.text.DateFormat;
14 import java.text.ParseException;
15 import java.text.SimpleDateFormat;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.List;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.*;
26 public class YangParserSimpleTest {
27 private final URI snNS = URI.create("urn:opendaylight:simple-nodes");
29 private final String snPref = "sn";
31 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
32 private Set<Module> modules;
35 public void init() throws Exception {
36 snRev = simpleDateFormat.parse("2013-07-30");
37 modules = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
41 public void testParseAnyXml() {
42 Module testModule = TestUtils.findModule(modules, "simple-nodes");
43 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
44 assertNotNull("'anyxml data not found'", data);
46 // test SchemaNode args
47 QName qname = data.getQName();
48 assertEquals("data", qname.getLocalName());
49 assertEquals(snPref, qname.getPrefix());
50 assertEquals(snNS, qname.getNamespace());
51 assertEquals(snRev, qname.getRevision());
52 assertEquals("anyxml desc", data.getDescription());
53 assertEquals("data ref", data.getReference());
54 assertEquals(Status.OBSOLETE, data.getStatus());
55 assertEquals(0, data.getUnknownSchemaNodes().size());
56 // test DataSchemaNode args
57 assertFalse(data.isAugmenting());
58 assertFalse(data.isConfiguration());
59 ConstraintDefinition constraints = data.getConstraints();
60 assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
61 Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
62 assertEquals(2, constraints.getMustConstraints().size());
64 String must1 = "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"";
65 String errMsg1 = "An ethernet MTU must be 1500";
66 String must2 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
67 String errMsg2 = "An atm MTU must be 64 .. 17966";
69 boolean found1 = false;
70 boolean found2 = false;
71 for (MustDefinition must : mustConstraints) {
72 if (must1.equals(must.toString())) {
74 assertEquals(errMsg1, must.getErrorMessage());
75 } else if (must2.equals(must.toString())) {
77 assertEquals(errMsg2, must.getErrorMessage());
78 assertEquals("anyxml data error-app-tag", must.getErrorAppTag());
79 assertEquals("an error occured in data", must.getDescription());
80 assertEquals("data must ref", must.getReference());
86 assertTrue(constraints.isMandatory());
87 assertNull(constraints.getMinElements());
88 assertNull(constraints.getMaxElements());
92 public void testParseContainer() {
93 Module test = TestUtils.findModule(modules, "simple-nodes");
95 ContainerSchemaNode nodes = (ContainerSchemaNode) test.getDataChildByName("nodes");
96 // test SchemaNode args
97 QName expectedQName = new QName(snNS, snRev, snPref, "nodes");
98 assertEquals(expectedQName, nodes.getQName());
99 SchemaPath expectedPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes");
100 assertEquals(expectedPath, nodes.getPath());
101 assertEquals("nodes collection", nodes.getDescription());
102 assertEquals("nodes ref", nodes.getReference());
103 assertEquals(Status.CURRENT, nodes.getStatus());
104 assertEquals(0, nodes.getUnknownSchemaNodes().size());
105 // test DataSchemaNode args
106 assertFalse(nodes.isAugmenting());
107 assertFalse(nodes.isConfiguration());
110 ConstraintDefinition constraints = nodes.getConstraints();
111 assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
112 Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
113 assertEquals(2, constraints.getMustConstraints().size());
115 String must1 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
116 String errMsg1 = "An atm MTU must be 64 .. 17966";
117 String must2 = "ifId != 0";
119 boolean found1 = false;
120 boolean found2 = false;
121 for (MustDefinition must : mustConstraints) {
122 if (must1.equals(must.toString())) {
124 assertEquals(errMsg1, must.getErrorMessage());
125 } else if (must2.equals(must.toString())) {
127 assertNull(must.getErrorMessage());
128 assertNull(must.getErrorAppTag());
129 assertNull(must.getDescription());
130 assertNull(must.getReference());
136 assertFalse(constraints.isMandatory());
137 assertNull(constraints.getMinElements());
138 assertNull(constraints.getMaxElements());
139 assertTrue(nodes.isPresenceContainer());
142 Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
143 assertEquals(1, typedefs.size());
144 TypeDefinition<?> nodesType = typedefs.iterator().next();
145 QName typedefQName = new QName(snNS, snRev, snPref, "nodes-type");
146 assertEquals(typedefQName, nodesType.getQName());
147 SchemaPath nodesTypePath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "nodes-type");
148 assertEquals(nodesTypePath, nodesType.getPath());
149 assertNull(nodesType.getDescription());
150 assertNull(nodesType.getReference());
151 assertEquals(Status.CURRENT, nodesType.getStatus());
152 assertEquals(0, nodesType.getUnknownSchemaNodes().size());
155 // total size = 8: defined 6, inserted by uses 2
156 assertEquals(8, nodes.getChildNodes().size());
157 LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added");
158 assertEquals(createPath("nodes", "added"), added.getPath());
159 assertEquals(createPath("mytype"), added.getType().getPath());
161 ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
162 assertFalse(links.isUserOrdered());
164 Set<GroupingDefinition> groupings = nodes.getGroupings();
165 assertEquals(1, groupings.size());
166 GroupingDefinition nodeGroup = groupings.iterator().next();
167 QName groupQName = new QName(snNS, snRev, snPref, "node-group");
168 assertEquals(groupQName, nodeGroup.getQName());
169 SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
170 assertEquals(nodeGroupPath, nodeGroup.getPath());
172 Set<UsesNode> uses = nodes.getUses();
173 assertEquals(1, uses.size());
174 UsesNode use = uses.iterator().next();
175 assertEquals(nodeGroupPath, use.getGroupingPath());
179 private final URI ns = URI.create("urn:opendaylight:simple-nodes");
181 private final String prefix = "sn";
183 private SchemaPath createPath(String... names) {
185 rev = new SimpleDateFormat("yyyy-MM-dd").parse("2013-07-30");
186 } catch (ParseException e) {
190 List<QName> path = new ArrayList<>();
191 for (String name : names) {
192 path.add(new QName(ns, rev, prefix, name));
194 return new SchemaPath(path, true);