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