Migrate Must/WhenStatementSupport to BaseStatementSupport
[yangtools.git] / yang / yang-parser-rfc7950 / 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.assertTrue;
14
15 import java.net.URI;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Optional;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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 static final QNameModule SN = QNameModule.create(URI.create("urn:opendaylight:simple-nodes"),
41         Revision.of("2013-07-30"));
42     private static final QName SN_NODES = QName.create(SN, "nodes");
43     private static final SchemaPath SN_NODES_PATH = SchemaPath.create(true, SN_NODES);
44
45     private SchemaContext context;
46     private Module testModule;
47
48     @Before
49     public void init() throws Exception {
50         context = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
51         testModule = TestUtils.findModule(context, "simple-nodes").get();
52     }
53
54     @Test
55     public void testParseAnyXml() {
56         final AnyxmlSchemaNode data = (AnyxmlSchemaNode) testModule.getDataChildByName(
57             QName.create(testModule.getQNameModule(), "data"));
58         assertNotNull("'anyxml data not found'", data);
59         assertFalse(data.equals(null));
60         assertEquals("RegularAnyxmlEffectiveStatement{qname=(urn:opendaylight:simple-nodes?revision=2013-07-30)data, "
61                 + "path=AbsoluteSchemaPath{path=[(urn:opendaylight:simple-nodes?revision=2013-07-30)data]}}",
62                 data.toString());
63
64         // test SchemaNode args
65         assertEquals(QName.create(SN, "data"), data.getQName());
66         assertEquals(Optional.of("anyxml desc"), data.getDescription());
67         assertEquals(Optional.of("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
74         assertTrue(data.isMandatory());
75         assertEquals("class != 'wheel'", data.getWhenCondition().get().getOriginalString());
76         final Collection<? extends MustDefinition> mustConstraints = data.getMustConstraints();
77         assertEquals(2, mustConstraints.size());
78
79         final String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
80         final String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
81
82         boolean found1 = false;
83         boolean found2 = false;
84         for (final MustDefinition must : mustConstraints) {
85             if (must1.equals(must.getXpath().getOriginalString())) {
86                 found1 = true;
87                 assertEquals(Optional.of("An ethernet MTU must be 1500"), must.getErrorMessage());
88             } else if (must2.equals(must.getXpath().getOriginalString())) {
89                 found2 = true;
90                 assertEquals(Optional.of("An atm MTU must be  64 .. 17966"), must.getErrorMessage());
91                 assertEquals(Optional.of("anyxml data error-app-tag"), must.getErrorAppTag());
92                 assertEquals(Optional.of("an error occured in data"), must.getDescription());
93                 assertEquals(Optional.of("data must ref"), must.getReference());
94             }
95         }
96         assertTrue(found1);
97         assertTrue(found2);
98     }
99
100     @Test
101     public void testParseAnyData() {
102         final AnydataSchemaNode anydata = (AnydataSchemaNode) testModule.findDataChildByName(
103                 QName.create(testModule.getQNameModule(), "data2")).orElse(null);
104
105         assertNotNull("'anydata data not found'", anydata);
106         assertEquals("RegularAnydataEffectiveStatement{qname=(urn:opendaylight:simple-nodes?revision=2013-07-30)data2, "
107                         + "path=AbsoluteSchemaPath{path=[(urn:opendaylight:simple-nodes?revision=2013-07-30)data2]}}",
108                 anydata.toString());
109
110         // test SchemaNode args
111         assertEquals(QName.create(SN, "data2"), anydata.getQName());
112         assertEquals(Optional.of("anydata desc"), anydata.getDescription());
113         assertEquals(Optional.of("data ref"), anydata.getReference());
114         assertEquals(Status.OBSOLETE, anydata.getStatus());
115         assertEquals(0, anydata.getUnknownSchemaNodes().size());
116         // test DataSchemaNode args
117         assertFalse(anydata.isAugmenting());
118         assertFalse(anydata.isConfiguration());
119
120         assertTrue(anydata.isMandatory());
121         assertTrue(anydata.getWhenCondition().isPresent());
122         assertEquals("class != 'wheel'", anydata.getWhenCondition().get().getOriginalString());
123         final Collection<? extends MustDefinition> mustConstraints = anydata.getMustConstraints();
124         assertEquals(2, mustConstraints.size());
125
126         final String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
127         final String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
128
129         boolean found1 = false;
130         boolean found2 = false;
131         for (final MustDefinition must : mustConstraints) {
132             if (must1.equals(must.getXpath().getOriginalString())) {
133                 found1 = true;
134                 assertEquals(Optional.of("An ethernet MTU must be 1500"), must.getErrorMessage());
135             } else if (must2.equals(must.getXpath().getOriginalString())) {
136                 found2 = true;
137                 assertEquals(Optional.of("An atm MTU must be  64 .. 17966"), must.getErrorMessage());
138                 assertEquals(Optional.of("anydata data error-app-tag"), must.getErrorAppTag());
139                 assertEquals(Optional.of("an error occured in data"), must.getDescription());
140                 assertEquals(Optional.of("data must ref"), must.getReference());
141             }
142         }
143         assertTrue(found1);
144         assertTrue(found2);
145     }
146
147     @Test
148     public void testParseContainer() {
149         final ContainerSchemaNode nodes = (ContainerSchemaNode) testModule
150                 .getDataChildByName(QName.create(testModule.getQNameModule(), "nodes"));
151         // test SchemaNode args
152         assertEquals(SN_NODES, nodes.getQName());
153         assertEquals(SN_NODES_PATH, nodes.getPath());
154         assertEquals(Optional.of("nodes collection"), nodes.getDescription());
155         assertEquals(Optional.of("nodes ref"), nodes.getReference());
156         assertEquals(Status.CURRENT, nodes.getStatus());
157         assertEquals(0, nodes.getUnknownSchemaNodes().size());
158         // test DataSchemaNode args
159         assertFalse(nodes.isAugmenting());
160         assertFalse(nodes.isConfiguration());
161
162         // constraints
163         assertEquals("class != 'wheel'", nodes.getWhenCondition().get().getOriginalString());
164         final Collection<? extends MustDefinition> mustConstraints = nodes.getMustConstraints();
165         assertEquals(2, mustConstraints.size());
166
167         final String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
168         final String errMsg1 = "An atm MTU must be  64 .. 17966";
169         final String must2 = "ifId != 0";
170
171         boolean found1 = false;
172         boolean found2 = false;
173         for (final MustDefinition must : mustConstraints) {
174             if (must1.equals(must.getXpath().getOriginalString())) {
175                 found1 = true;
176                 assertEquals(Optional.of(errMsg1), must.getErrorMessage());
177             } else if (must2.equals(must.getXpath().getOriginalString())) {
178                 found2 = true;
179                 assertFalse(must.getErrorMessage().isPresent());
180                 assertFalse(must.getErrorAppTag().isPresent());
181                 assertFalse(must.getDescription().isPresent());
182                 assertFalse(must.getReference().isPresent());
183             }
184         }
185         assertTrue(found1);
186         assertTrue(found2);
187
188         assertTrue(nodes.isPresenceContainer());
189
190         // typedef
191         final Collection<? extends TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
192         assertEquals(1, typedefs.size());
193         final TypeDefinition<?> nodesType = typedefs.iterator().next();
194         final QName typedefQName = QName.create(SN, "nodes-type");
195         assertEquals(typedefQName, nodesType.getQName());
196         assertEquals(SN_NODES_PATH.createChild(QName.create(SN, "nodes-type")), nodesType.getPath());
197         assertFalse(nodesType.getDescription().isPresent());
198         assertFalse(nodesType.getReference().isPresent());
199         assertEquals(Status.CURRENT, nodesType.getStatus());
200         assertEquals(0, nodesType.getUnknownSchemaNodes().size());
201
202         // child nodes
203         // total size = 8: defined 6, inserted by uses 2
204         assertEquals(8, nodes.getChildNodes().size());
205         final LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName(QName.create(
206             testModule.getQNameModule(), "added"));
207         assertEquals(createPath("nodes", "added"), added.getPath());
208         assertEquals(createPath("mytype"), added.getType().getPath());
209
210         final ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName(QName.create(
211             testModule.getQNameModule(), "links"));
212         assertFalse(links.isUserOrdered());
213
214         final Collection<? extends GroupingDefinition> groupings = nodes.getGroupings();
215         assertEquals(1, groupings.size());
216         final GroupingDefinition nodeGroup = groupings.iterator().next();
217         final QName groupQName = QName.create(SN, "node-group");
218         assertEquals(groupQName, nodeGroup.getQName());
219         final SchemaPath nodeGroupPath = SN_NODES_PATH.createChild(groupQName);
220         assertEquals(nodeGroupPath, nodeGroup.getPath());
221
222         final Collection<? extends UsesNode> uses = nodes.getUses();
223         assertEquals(1, uses.size());
224         final UsesNode use = uses.iterator().next();
225         assertEquals(nodeGroup, use.getSourceGrouping());
226     }
227
228
229     private static final URI NS = URI.create("urn:opendaylight:simple-nodes");
230
231     private static SchemaPath createPath(final String... names) {
232         final Revision rev = Revision.of("2013-07-30");
233         final List<QName> path = new ArrayList<>();
234         for (final String name : names) {
235             path.add(QName.create(NS, rev, name));
236         }
237         return SchemaPath.create(path, true);
238     }
239
240 }