Remove deprecated Yin/YangStatementSourceImpl
[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.QNameModule;
26 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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 =
43             QNameModule.create(URI.create("urn:opendaylight:simple-nodes"), QName.parseRevision("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(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]}]", data.toString());
63
64         // test SchemaNode args
65         assertEquals(QName.create(SN, "data"), data.getQName());
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 ContainerSchemaNode nodes = (ContainerSchemaNode) testModule
108                 .getDataChildByName(QName.create(testModule.getQNameModule(), "nodes"));
109         // test SchemaNode args
110         assertEquals(SN_NODES, nodes.getQName());
111         assertEquals(SN_NODES_PATH, nodes.getPath());
112         assertEquals("nodes collection", nodes.getDescription());
113         assertEquals("nodes ref", nodes.getReference());
114         assertEquals(Status.CURRENT, nodes.getStatus());
115         assertEquals(0, nodes.getUnknownSchemaNodes().size());
116         // test DataSchemaNode args
117         assertFalse(nodes.isAugmenting());
118         assertFalse(nodes.isConfiguration());
119
120         // constraints
121         final ConstraintDefinition constraints = nodes.getConstraints();
122         assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
123         final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
124         assertEquals(2, constraints.getMustConstraints().size());
125
126         final String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
127         final String errMsg1 = "An atm MTU must be  64 .. 17966";
128         final String must2 = "ifId != 0";
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(errMsg1, must.getErrorMessage());
136             } else if (must2.equals(must.toString())) {
137                 found2 = true;
138                 assertNull(must.getErrorMessage());
139                 assertNull(must.getErrorAppTag());
140                 assertNull(must.getDescription());
141                 assertNull(must.getReference());
142             }
143         }
144         assertTrue(found1);
145         assertTrue(found2);
146
147         assertFalse(constraints.isMandatory());
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         assertNull(nodesType.getDescription());
160         assertNull(nodesType.getReference());
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(testModule.getQNameModule(), "added"));
168         assertEquals(createPath("nodes", "added"), added.getPath());
169         assertEquals(createPath("mytype"), added.getType().getPath());
170
171         final ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName(QName.create(testModule.getQNameModule(), "links"));
172         assertFalse(links.isUserOrdered());
173
174         final Set<GroupingDefinition> groupings = nodes.getGroupings();
175         assertEquals(1, groupings.size());
176         final GroupingDefinition nodeGroup = groupings.iterator().next();
177         final QName groupQName = QName.create(SN, "node-group");
178         assertEquals(groupQName, nodeGroup.getQName());
179         final SchemaPath nodeGroupPath = SN_NODES_PATH.createChild(groupQName);
180         assertEquals(nodeGroupPath, nodeGroup.getPath());
181
182         final Set<UsesNode> uses = nodes.getUses();
183         assertEquals(1, uses.size());
184         final UsesNode use = uses.iterator().next();
185         assertEquals(nodeGroupPath, use.getGroupingPath());
186     }
187
188
189     private static final URI NS = URI.create("urn:opendaylight:simple-nodes");
190
191     private static SchemaPath createPath(final String... names) throws ParseException {
192         final Date rev = SimpleDateFormatUtil.getRevisionFormat().parse("2013-07-30");
193
194         final List<QName> path = new ArrayList<>();
195         for (final String name : names) {
196             path.add(QName.create(NS, rev, name));
197         }
198         return SchemaPath.create(path, true);
199     }
200
201 }