Bug 2366 - Effective statments impl merge, retest & bugfix
[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     //TODO: uncomment and run this test once commits are merged
50     @Before
51     public void init() throws Exception {
52         snRev = simpleDateFormat.parse("2013-07-30");
53         modules = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
54     }
55
56     @Test
57     public void testParseAnyXml() {
58         Module testModule = TestUtils.findModule(modules, "simple-nodes");
59         AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
60         assertNotNull("'anyxml data not found'", data);
61
62         // test SchemaNode args
63         QName qname = data.getQName();
64         assertEquals("data", qname.getLocalName());
65         assertEquals(snNS, qname.getNamespace());
66         assertEquals(snRev, qname.getRevision());
67         assertEquals("anyxml desc", data.getDescription());
68         assertEquals("data ref", data.getReference());
69         assertEquals(Status.OBSOLETE, data.getStatus());
70         assertEquals(0, data.getUnknownSchemaNodes().size());
71         // test DataSchemaNode args
72         assertFalse(data.isAugmenting());
73         assertFalse(data.isConfiguration());
74         ConstraintDefinition constraints = data.getConstraints();
75         assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
76         Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
77         assertEquals(2, constraints.getMustConstraints().size());
78
79         String must1 = "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"";
80         String errMsg1 = "An ethernet MTU must be 1500";
81         String must2 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
82         String errMsg2 = "An atm MTU must be  64 .. 17966";
83
84         boolean found1 = false;
85         boolean found2 = false;
86         for (MustDefinition must : mustConstraints) {
87             if (must1.equals(must.toString())) {
88                 found1 = true;
89                 assertEquals(errMsg1, must.getErrorMessage());
90             } else if (must2.equals(must.toString())) {
91                 found2 = true;
92                 assertEquals(errMsg2, must.getErrorMessage());
93                 assertEquals("anyxml data error-app-tag", must.getErrorAppTag());
94                 assertEquals("an error occured in data", must.getDescription());
95                 assertEquals("data must ref", must.getReference());
96             }
97         }
98         assertTrue(found1);
99         assertTrue(found2);
100
101         assertTrue(constraints.isMandatory());
102         assertNull(constraints.getMinElements());
103         assertNull(constraints.getMaxElements());
104     }
105
106     @Test
107     public void testParseContainer() {
108         Module test = TestUtils.findModule(modules, "simple-nodes");
109
110         ContainerSchemaNode nodes = (ContainerSchemaNode) test.getDataChildByName("nodes");
111         // test SchemaNode args
112         QName expectedQName = QName.create(snNS, snRev, "nodes");
113         assertEquals(expectedQName, nodes.getQName());
114         SchemaPath expectedPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes");
115         assertEquals(expectedPath, nodes.getPath());
116         assertEquals("nodes collection", nodes.getDescription());
117         assertEquals("nodes ref", nodes.getReference());
118         assertEquals(Status.CURRENT, nodes.getStatus());
119         assertEquals(0, nodes.getUnknownSchemaNodes().size());
120         // test DataSchemaNode args
121         assertFalse(nodes.isAugmenting());
122         assertFalse(nodes.isConfiguration());
123
124         // constraints
125         ConstraintDefinition constraints = nodes.getConstraints();
126         assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
127         Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
128         assertEquals(2, constraints.getMustConstraints().size());
129
130         String must1 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
131         String errMsg1 = "An atm MTU must be  64 .. 17966";
132         String must2 = "ifId != 0";
133
134         boolean found1 = false;
135         boolean found2 = false;
136         for (MustDefinition must : mustConstraints) {
137             if (must1.equals(must.toString())) {
138                 found1 = true;
139                 assertEquals(errMsg1, must.getErrorMessage());
140             } else if (must2.equals(must.toString())) {
141                 found2 = true;
142                 assertNull(must.getErrorMessage());
143                 assertNull(must.getErrorAppTag());
144                 assertNull(must.getDescription());
145                 assertNull(must.getReference());
146             }
147         }
148         assertTrue(found1);
149         assertTrue(found2);
150
151         assertFalse(constraints.isMandatory());
152         assertNull(constraints.getMinElements());
153         assertNull(constraints.getMaxElements());
154         assertTrue(nodes.isPresenceContainer());
155
156         // typedef
157         Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
158         assertEquals(1, typedefs.size());
159         TypeDefinition<?> nodesType = typedefs.iterator().next();
160         QName typedefQName = QName.create(snNS, snRev, "nodes-type");
161         assertEquals(typedefQName, nodesType.getQName());
162         SchemaPath nodesTypePath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "nodes-type");
163         assertEquals(nodesTypePath, nodesType.getPath());
164         assertTrue(nodesType.getDescription().isEmpty());
165         assertTrue(nodesType.getReference().isEmpty());
166         assertEquals(Status.CURRENT, nodesType.getStatus());
167         assertEquals(0, nodesType.getUnknownSchemaNodes().size());
168
169         // child nodes
170         // total size = 8: defined 6, inserted by uses 2
171         assertEquals(8, nodes.getChildNodes().size());
172         LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added");
173         assertEquals(createPath("nodes", "added"), added.getPath());
174         assertEquals(createPath("mytype"), added.getType().getPath());
175
176         ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
177         assertFalse(links.isUserOrdered());
178
179         Set<GroupingDefinition> groupings = nodes.getGroupings();
180         assertEquals(1, groupings.size());
181         GroupingDefinition nodeGroup = groupings.iterator().next();
182         QName groupQName = QName.create(snNS, snRev, "node-group");
183         assertEquals(groupQName, nodeGroup.getQName());
184         SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
185         assertEquals(nodeGroupPath, nodeGroup.getPath());
186
187         Set<UsesNode> uses = nodes.getUses();
188         assertEquals(1, uses.size());
189         UsesNode use = uses.iterator().next();
190         assertEquals(nodeGroupPath, use.getGroupingPath());
191     }
192
193
194     private final URI ns = URI.create("urn:opendaylight:simple-nodes");
195     private Date rev;
196     private final String prefix = "sn";
197
198     private SchemaPath createPath(final String... names) {
199         try {
200             rev = new SimpleDateFormat("yyyy-MM-dd").parse("2013-07-30");
201         } catch (ParseException e) {
202             e.printStackTrace();
203         }
204
205         List<QName> path = new ArrayList<>();
206         for (String name : names) {
207             path.add(QName.create(ns, rev, name));
208         }
209         return SchemaPath.create(path, true);
210     }
211
212 }