b3f301591d0bc473e062557ac4959f5c04b94eb6
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangParserSimpleTest.java
1 /*
2  * Copyright (c) 2016 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;
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 java.net.URI;
17 import java.text.ParseException;
18 import java.util.ArrayList;
19 import java.util.Date;
20 import java.util.List;
21 import java.util.Set;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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 Set<Module> modules;
45
46     @Before
47     public void init() throws Exception {
48         snRev = SimpleDateFormatUtil.getRevisionFormat().parse("2013-07-30");
49         modules = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
50     }
51
52     @Test
53     public void testParseAnyXml() {
54         final Module testModule = TestUtils.findModule(modules, "simple-nodes");
55         final AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName(QName.create(testModule.getQNameModule(), "data"));
56         assertNotNull("'anyxml data not found'", data);
57         assertFalse(data.equals(null));
58         assertEquals("AnyXmlEffectiveStatementImpl[qname=(urn:opendaylight:simple-nodes?revision=2013-07-30)data, " +
59                 "path=AbsoluteSchemaPath{path=[(urn:opendaylight:simple-nodes?revision=2013-07-30)data]}]", data.toString());
60
61         // test SchemaNode args
62         final 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         final ConstraintDefinition constraints = data.getConstraints();
74         assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
75         final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
76         assertEquals(2, constraints.getMustConstraints().size());
77
78         final String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
79         final String errMsg1 = "An ethernet MTU must be 1500";
80         final String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
81         final String errMsg2 = "An atm MTU must be  64 .. 17966";
82
83         boolean found1 = false;
84         boolean found2 = false;
85         for (final 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() throws ParseException {
107         final Module test = TestUtils.findModule(modules, "simple-nodes");
108
109         final ContainerSchemaNode nodes = (ContainerSchemaNode) test.getDataChildByName(QName.create(test.getQNameModule(), "nodes"));
110         // test SchemaNode args
111         final QName expectedQName = QName.create(snNS, snRev, "nodes");
112         assertEquals(expectedQName, nodes.getQName());
113         final 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         final ConstraintDefinition constraints = nodes.getConstraints();
125         assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
126         final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
127         assertEquals(2, constraints.getMustConstraints().size());
128
129         final String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
130         final String errMsg1 = "An atm MTU must be  64 .. 17966";
131         final String must2 = "ifId != 0";
132
133         boolean found1 = false;
134         boolean found2 = false;
135         for (final 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         final Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
157         assertEquals(1, typedefs.size());
158         final TypeDefinition<?> nodesType = typedefs.iterator().next();
159         final QName typedefQName = QName.create(snNS, snRev, "nodes-type");
160         assertEquals(typedefQName, nodesType.getQName());
161         final 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());
167
168         // child nodes
169         // total size = 8: defined 6, inserted by uses 2
170         assertEquals(8, nodes.getChildNodes().size());
171         final LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName(QName.create(test.getQNameModule(), "added"));
172         assertEquals(createPath("nodes", "added"), added.getPath());
173         assertEquals(createPath("mytype"), added.getType().getPath());
174
175         final ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName(QName.create(test.getQNameModule(), "links"));
176         assertFalse(links.isUserOrdered());
177
178         final Set<GroupingDefinition> groupings = nodes.getGroupings();
179         assertEquals(1, groupings.size());
180         final GroupingDefinition nodeGroup = groupings.iterator().next();
181         final QName groupQName = QName.create(snNS, snRev, "node-group");
182         assertEquals(groupQName, nodeGroup.getQName());
183         final SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
184         assertEquals(nodeGroupPath, nodeGroup.getPath());
185
186         final Set<UsesNode> uses = nodes.getUses();
187         assertEquals(1, uses.size());
188         final UsesNode use = uses.iterator().next();
189         assertEquals(nodeGroupPath, use.getGroupingPath());
190     }
191
192
193     private static final URI NS = URI.create("urn:opendaylight:simple-nodes");
194
195     private static SchemaPath createPath(final String... names) throws ParseException {
196         final Date rev = SimpleDateFormatUtil.getRevisionFormat().parse("2013-07-30");
197
198         final List<QName> path = new ArrayList<>();
199         for (final String name : names) {
200             path.add(QName.create(NS, rev, name));
201         }
202         return SchemaPath.create(path, true);
203     }
204
205 }