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