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