Bug 3670 (part 2/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / 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.parser.impl;
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
16 import org.junit.Before;
17
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.model.api.UsesNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
26 import org.opendaylight.yangtools.yang.model.api.Status;
27 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
28 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
29 import java.net.URI;
30 import java.text.DateFormat;
31 import java.text.ParseException;
32 import java.text.SimpleDateFormat;
33 import java.util.ArrayList;
34 import java.util.Date;
35 import java.util.List;
36 import java.util.Set;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40
41 public class YangParserSimpleTest {
42     private final URI snNS = URI.create("urn:opendaylight:simple-nodes");
43     private Date snRev;
44     private final String snPref = "sn";
45
46     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
47     private Set<Module> modules;
48
49     @Before
50     public void init() throws Exception {
51         snRev = simpleDateFormat.parse("2013-07-30");
52         modules = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
53     }
54
55     @Test
56     public void testParseAnyXml() {
57         Module testModule = TestUtils.findModule(modules, "simple-nodes");
58         AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
59         assertNotNull("'anyxml data not found'", data);
60
61         // test SchemaNode args
62         QName qname = data.getQName();
63         assertEquals("data", qname.getLocalName());
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());
77
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";
82
83         boolean found1 = false;
84         boolean found2 = false;
85         for (MustDefinition must : mustConstraints) {
86             if (must1.equals(must.toString())) {
87                 found1 = true;
88                 assertEquals(errMsg1, must.getErrorMessage());
89             } else if (must2.equals(must.toString())) {
90                 found2 = true;
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());
95             }
96         }
97         assertTrue(found1);
98         assertTrue(found2);
99
100         assertTrue(constraints.isMandatory());
101         assertNull(constraints.getMinElements());
102         assertNull(constraints.getMaxElements());
103     }
104
105     @Test
106     public void testParseContainer() {
107         Module test = TestUtils.findModule(modules, "simple-nodes");
108
109         ContainerSchemaNode nodes = (ContainerSchemaNode) test.getDataChildByName("nodes");
110         // test SchemaNode args
111         QName expectedQName = QName.create(snNS, snRev, "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());
122
123         // constraints
124         ConstraintDefinition constraints = nodes.getConstraints();
125         assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
126         Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
127         assertEquals(2, constraints.getMustConstraints().size());
128
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";
132
133         boolean found1 = false;
134         boolean found2 = false;
135         for (MustDefinition must : mustConstraints) {
136             if (must1.equals(must.toString())) {
137                 found1 = true;
138                 assertEquals(errMsg1, must.getErrorMessage());
139             } else if (must2.equals(must.toString())) {
140                 found2 = true;
141                 assertNull(must.getErrorMessage());
142                 assertNull(must.getErrorAppTag());
143                 assertNull(must.getDescription());
144                 assertNull(must.getReference());
145             }
146         }
147         assertTrue(found1);
148         assertTrue(found2);
149
150         assertFalse(constraints.isMandatory());
151         assertNull(constraints.getMinElements());
152         assertNull(constraints.getMaxElements());
153         assertTrue(nodes.isPresenceContainer());
154
155         // typedef
156         Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
157         assertEquals(1, typedefs.size());
158         TypeDefinition<?> nodesType = typedefs.iterator().next();
159         QName typedefQName = QName.create(snNS, snRev, "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         assertTrue(nodesType.getDescription().isEmpty());
164         assertTrue(nodesType.getReference().isEmpty());
165         assertEquals(Status.CURRENT, nodesType.getStatus());
166         assertEquals(0, nodesType.getUnknownSchemaNodes().size());
167
168         // child nodes
169         // total size = 8: defined 6, inserted by uses 2
170         assertEquals(8, nodes.getChildNodes().size());
171         LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added");
172         assertEquals(createPath("nodes", "added"), added.getPath());
173         assertEquals(createPath("mytype"), added.getType().getPath());
174
175         ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
176         assertFalse(links.isUserOrdered());
177
178         Set<GroupingDefinition> groupings = nodes.getGroupings();
179         assertEquals(1, groupings.size());
180         GroupingDefinition nodeGroup = groupings.iterator().next();
181         QName groupQName = QName.create(snNS, snRev, "node-group");
182         assertEquals(groupQName, nodeGroup.getQName());
183         SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
184         assertEquals(nodeGroupPath, nodeGroup.getPath());
185
186         Set<UsesNode> uses = nodes.getUses();
187         assertEquals(1, uses.size());
188         UsesNode use = uses.iterator().next();
189         assertEquals(nodeGroupPath, use.getGroupingPath());
190     }
191
192
193     private final URI ns = URI.create("urn:opendaylight:simple-nodes");
194     private Date rev;
195     private final String prefix = "sn";
196
197     private SchemaPath createPath(final String... names) {
198         try {
199             rev = new SimpleDateFormat("yyyy-MM-dd").parse("2013-07-30");
200         } catch (ParseException e) {
201             e.printStackTrace();
202         }
203
204         List<QName> path = new ArrayList<>();
205         for (String name : names) {
206             path.add(QName.create(ns, rev, name));
207         }
208         return SchemaPath.create(path, true);
209     }
210
211 }