Make ConstraintMetaDefition attributes Optional
[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.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.AnyXmlSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
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("anyxml desc", data.getDescription());
68         assertEquals("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         final ConstraintDefinition constraints = data.getConstraints();
75         assertEquals("class != 'wheel'", constraints.getWhenCondition().get().toString());
76         final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
77         assertEquals(2, constraints.getMustConstraints().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.toString())) {
86                 found1 = true;
87                 assertEquals(Optional.of("An ethernet MTU must be 1500"), must.getErrorMessage());
88             } else if (must2.equals(must.toString())) {
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("an error occured in data", must.getDescription());
93                 assertEquals("data must ref", must.getReference());
94             }
95         }
96         assertTrue(found1);
97         assertTrue(found2);
98
99         assertTrue(constraints.isMandatory());
100         assertNull(constraints.getMinElements());
101         assertNull(constraints.getMaxElements());
102     }
103
104     @Test
105     public void testParseContainer() {
106         final ContainerSchemaNode nodes = (ContainerSchemaNode) testModule
107                 .getDataChildByName(QName.create(testModule.getQNameModule(), "nodes"));
108         // test SchemaNode args
109         assertEquals(SN_NODES, nodes.getQName());
110         assertEquals(SN_NODES_PATH, nodes.getPath());
111         assertEquals("nodes collection", nodes.getDescription());
112         assertEquals("nodes ref", nodes.getReference());
113         assertEquals(Status.CURRENT, nodes.getStatus());
114         assertEquals(0, nodes.getUnknownSchemaNodes().size());
115         // test DataSchemaNode args
116         assertFalse(nodes.isAugmenting());
117         assertFalse(nodes.isConfiguration());
118
119         // constraints
120         final ConstraintDefinition constraints = nodes.getConstraints();
121         assertEquals("class != 'wheel'", constraints.getWhenCondition().get().toString());
122         final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
123         assertEquals(2, constraints.getMustConstraints().size());
124
125         final String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
126         final String errMsg1 = "An atm MTU must be  64 .. 17966";
127         final String must2 = "ifId != 0";
128
129         boolean found1 = false;
130         boolean found2 = false;
131         for (final MustDefinition must : mustConstraints) {
132             if (must1.equals(must.toString())) {
133                 found1 = true;
134                 assertEquals(Optional.of(errMsg1), must.getErrorMessage());
135             } else if (must2.equals(must.toString())) {
136                 found2 = true;
137                 assertFalse(must.getErrorMessage().isPresent());
138                 assertFalse(must.getErrorAppTag().isPresent());
139                 assertNull(must.getDescription());
140                 assertNull(must.getReference());
141             }
142         }
143         assertTrue(found1);
144         assertTrue(found2);
145
146         assertFalse(constraints.isMandatory());
147         assertNull(constraints.getMinElements());
148         assertNull(constraints.getMaxElements());
149         assertTrue(nodes.isPresenceContainer());
150
151         // typedef
152         final Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
153         assertEquals(1, typedefs.size());
154         final TypeDefinition<?> nodesType = typedefs.iterator().next();
155         final QName typedefQName = QName.create(SN, "nodes-type");
156         assertEquals(typedefQName, nodesType.getQName());
157         assertEquals(SN_NODES_PATH.createChild(QName.create(SN, "nodes-type")), nodesType.getPath());
158         assertNull(nodesType.getDescription());
159         assertNull(nodesType.getReference());
160         assertEquals(Status.CURRENT, nodesType.getStatus());
161         assertEquals(0, nodesType.getUnknownSchemaNodes().size());
162
163         // child nodes
164         // total size = 8: defined 6, inserted by uses 2
165         assertEquals(8, nodes.getChildNodes().size());
166         final LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName(QName.create(
167             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(
172             testModule.getQNameModule(), "links"));
173         assertFalse(links.isUserOrdered());
174
175         final Set<GroupingDefinition> groupings = nodes.getGroupings();
176         assertEquals(1, groupings.size());
177         final GroupingDefinition nodeGroup = groupings.iterator().next();
178         final QName groupQName = QName.create(SN, "node-group");
179         assertEquals(groupQName, nodeGroup.getQName());
180         final SchemaPath nodeGroupPath = SN_NODES_PATH.createChild(groupQName);
181         assertEquals(nodeGroupPath, nodeGroup.getPath());
182
183         final Set<UsesNode> uses = nodes.getUses();
184         assertEquals(1, uses.size());
185         final UsesNode use = uses.iterator().next();
186         assertEquals(nodeGroupPath, use.getGroupingPath());
187     }
188
189
190     private static final URI NS = URI.create("urn:opendaylight:simple-nodes");
191
192     private static SchemaPath createPath(final String... names) {
193         final Revision rev = Revision.of("2013-07-30");
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 }