Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / YangParserSimpleTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt.retest;
9
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;
15 import java.net.URI;
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;
22 import java.util.Set;
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;
38
39 public class YangParserSimpleTest {
40     private final URI snNS = URI.create("urn:opendaylight:simple-nodes");
41     private Date snRev;
42     private final String snPref = "sn";
43
44     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
45     private Set<Module> modules;
46
47     @Before
48     public void init() throws Exception {
49         snRev = simpleDateFormat.parse("2013-07-30");
50         modules = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
51     }
52
53     @Test
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);
58
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());
75
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";
80
81         boolean found1 = false;
82         boolean found2 = false;
83         for (MustDefinition must : mustConstraints) {
84             if (must1.equals(must.toString())) {
85                 found1 = true;
86                 assertEquals(errMsg1, must.getErrorMessage());
87             } else if (must2.equals(must.toString())) {
88                 found2 = true;
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());
93             }
94         }
95         assertTrue(found1);
96         assertTrue(found2);
97
98         assertTrue(constraints.isMandatory());
99         assertTrue(0 == constraints.getMinElements());
100         assertTrue(Integer.MAX_VALUE == constraints.getMaxElements());
101     }
102
103     @Test
104     public void testParseContainer() {
105         Module test = TestUtils.findModule(modules, "simple-nodes");
106
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());
120
121         // constraints
122         ConstraintDefinition constraints = nodes.getConstraints();
123         assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
124         Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
125         assertEquals(2, constraints.getMustConstraints().size());
126
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";
130
131         boolean found1 = false;
132         boolean found2 = false;
133         for (MustDefinition must : mustConstraints) {
134             if (must1.equals(must.toString())) {
135                 found1 = true;
136                 assertEquals(errMsg1, must.getErrorMessage());
137             } else if (must2.equals(must.toString())) {
138                 found2 = true;
139                 assertNull(must.getErrorMessage());
140                 assertNull(must.getErrorAppTag());
141                 assertNull(must.getDescription());
142                 assertNull(must.getReference());
143             }
144         }
145         assertTrue(found1);
146         assertTrue(found2);
147
148         assertFalse(constraints.isMandatory());
149         assertTrue(0 == constraints.getMinElements());
150         assertTrue(Integer.MAX_VALUE == constraints.getMaxElements());
151         assertTrue(nodes.isPresenceContainer());
152
153         // typedef
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());
165
166         // child nodes
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());
172
173         ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
174         assertFalse(links.isUserOrdered());
175
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());
183
184         Set<UsesNode> uses = nodes.getUses();
185         assertEquals(1, uses.size());
186         UsesNode use = uses.iterator().next();
187         assertEquals(nodeGroupPath, use.getGroupingPath());
188     }
189
190
191     private final URI ns = URI.create("urn:opendaylight:simple-nodes");
192     private Date rev;
193     private final String prefix = "sn";
194
195     private SchemaPath createPath(final String... names) {
196         try {
197             rev = new SimpleDateFormat("yyyy-MM-dd").parse("2013-07-30");
198         } catch (ParseException e) {
199             e.printStackTrace();
200         }
201
202         List<QName> path = new ArrayList<>();
203         for (String name : names) {
204             path.add(QName.create(ns, rev, name));
205         }
206         return SchemaPath.create(path, true);
207     }
208
209 }