Do not include path in anydata/anyxml toString()
[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                 data.toString());
62
63         // test SchemaNode args
64         assertEquals(QName.create(SN, "data"), data.getQName());
65         assertEquals(Optional.of("anyxml desc"), data.getDescription());
66         assertEquals(Optional.of("data ref"), data.getReference());
67         assertEquals(Status.OBSOLETE, data.getStatus());
68         assertEquals(0, data.getUnknownSchemaNodes().size());
69         // test DataSchemaNode args
70         assertFalse(data.isAugmenting());
71         assertEquals(Optional.of(Boolean.FALSE), data.effectiveConfig());
72
73         assertTrue(data.isMandatory());
74         assertEquals("class != 'wheel'", data.getWhenCondition().orElseThrow().toString());
75         final Collection<? extends MustDefinition> mustConstraints = data.getMustConstraints();
76         assertEquals(2, mustConstraints.size());
77
78         final String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
79         final String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
80
81         boolean found1 = false;
82         boolean found2 = false;
83         for (final MustDefinition must : mustConstraints) {
84             if (must1.equals(must.getXpath().toString())) {
85                 found1 = true;
86                 assertEquals(Optional.of("An ethernet MTU must be 1500"), must.getErrorMessage());
87             } else if (must2.equals(must.getXpath().toString())) {
88                 found2 = true;
89                 assertEquals(Optional.of("An atm MTU must be  64 .. 17966"), must.getErrorMessage());
90                 assertEquals(Optional.of("anyxml data error-app-tag"), must.getErrorAppTag());
91                 assertEquals(Optional.of("an error occured in data"), must.getDescription());
92                 assertEquals(Optional.of("data must ref"), must.getReference());
93             }
94         }
95         assertTrue(found1);
96         assertTrue(found2);
97     }
98
99     @Test
100     public void testParseAnyData() {
101         final AnydataSchemaNode anydata = (AnydataSchemaNode) testModule.findDataChildByName(
102                 QName.create(testModule.getQNameModule(), "data2")).orElse(null);
103
104         assertNotNull("'anydata data not found'", anydata);
105         assertEquals("RegularAnydataEffectiveStatement{qname=(urn:opendaylight:simple-nodes?revision=2013-07-30)data2}",
106                 anydata.toString());
107
108         // test SchemaNode args
109         assertEquals(QName.create(SN, "data2"), anydata.getQName());
110         assertEquals(Optional.of("anydata desc"), anydata.getDescription());
111         assertEquals(Optional.of("data ref"), anydata.getReference());
112         assertEquals(Status.OBSOLETE, anydata.getStatus());
113         assertEquals(0, anydata.getUnknownSchemaNodes().size());
114         // test DataSchemaNode args
115         assertFalse(anydata.isAugmenting());
116         assertEquals(Optional.of(Boolean.FALSE), anydata.effectiveConfig());
117
118         assertTrue(anydata.isMandatory());
119         assertTrue(anydata.getWhenCondition().isPresent());
120         assertEquals("class != 'wheel'", anydata.getWhenCondition().orElseThrow().toString());
121         final Collection<? extends MustDefinition> mustConstraints = anydata.getMustConstraints();
122         assertEquals(2, mustConstraints.size());
123
124         final String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
125         final String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
126
127         boolean found1 = false;
128         boolean found2 = false;
129         for (final MustDefinition must : mustConstraints) {
130             if (must1.equals(must.getXpath().toString())) {
131                 found1 = true;
132                 assertEquals(Optional.of("An ethernet MTU must be 1500"), must.getErrorMessage());
133             } else if (must2.equals(must.getXpath().toString())) {
134                 found2 = true;
135                 assertEquals(Optional.of("An atm MTU must be  64 .. 17966"), must.getErrorMessage());
136                 assertEquals(Optional.of("anydata data error-app-tag"), must.getErrorAppTag());
137                 assertEquals(Optional.of("an error occured in data"), must.getDescription());
138                 assertEquals(Optional.of("data must ref"), must.getReference());
139             }
140         }
141         assertTrue(found1);
142         assertTrue(found2);
143     }
144
145     @Test
146     public void testParseContainer() {
147         final ContainerSchemaNode nodes = (ContainerSchemaNode) testModule
148                 .getDataChildByName(QName.create(testModule.getQNameModule(), "nodes"));
149         // test SchemaNode args
150         assertEquals(SN_NODES, nodes.getQName());
151         assertEquals(SN_NODES_PATH, nodes.getPath());
152         assertEquals(Optional.of("nodes collection"), nodes.getDescription());
153         assertEquals(Optional.of("nodes ref"), nodes.getReference());
154         assertEquals(Status.CURRENT, nodes.getStatus());
155         assertEquals(0, nodes.getUnknownSchemaNodes().size());
156         // test DataSchemaNode args
157         assertFalse(nodes.isAugmenting());
158         assertEquals(Optional.of(Boolean.FALSE), nodes.effectiveConfig());
159
160         // constraints
161         assertEquals("class != 'wheel'", nodes.getWhenCondition().orElseThrow().toString());
162         final Collection<? extends MustDefinition> mustConstraints = nodes.getMustConstraints();
163         assertEquals(2, mustConstraints.size());
164
165         final String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
166         final String errMsg1 = "An atm MTU must be  64 .. 17966";
167         final String must2 = "ifId != 0";
168
169         boolean found1 = false;
170         boolean found2 = false;
171         for (final MustDefinition must : mustConstraints) {
172             if (must1.equals(must.getXpath().toString())) {
173                 found1 = true;
174                 assertEquals(Optional.of(errMsg1), must.getErrorMessage());
175             } else if (must2.equals(must.getXpath().toString())) {
176                 found2 = true;
177                 assertFalse(must.getErrorMessage().isPresent());
178                 assertFalse(must.getErrorAppTag().isPresent());
179                 assertFalse(must.getDescription().isPresent());
180                 assertFalse(must.getReference().isPresent());
181             }
182         }
183         assertTrue(found1);
184         assertTrue(found2);
185
186         assertTrue(nodes.isPresenceContainer());
187
188         // typedef
189         final Collection<? extends TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
190         assertEquals(1, typedefs.size());
191         final TypeDefinition<?> nodesType = typedefs.iterator().next();
192         final QName typedefQName = QName.create(SN, "nodes-type");
193         assertEquals(typedefQName, nodesType.getQName());
194         assertFalse(nodesType.getDescription().isPresent());
195         assertFalse(nodesType.getReference().isPresent());
196         assertEquals(Status.CURRENT, nodesType.getStatus());
197         assertEquals(0, nodesType.getUnknownSchemaNodes().size());
198
199         // child nodes
200         // total size = 8: defined 6, inserted by uses 2
201         assertEquals(8, nodes.getChildNodes().size());
202         final LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName(QName.create(
203             testModule.getQNameModule(), "added"));
204         assertEquals(createPath("nodes", "added"), added.getPath());
205         assertEquals(createPath("mytype").getLastComponent(), added.getType().getQName());
206
207         final ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName(QName.create(
208             testModule.getQNameModule(), "links"));
209         assertFalse(links.isUserOrdered());
210
211         final Collection<? extends GroupingDefinition> groupings = nodes.getGroupings();
212         assertEquals(1, groupings.size());
213         final GroupingDefinition nodeGroup = groupings.iterator().next();
214         assertEquals(QName.create(SN, "node-group"), nodeGroup.getQName());
215
216         final Collection<? extends UsesNode> uses = nodes.getUses();
217         assertEquals(1, uses.size());
218         final UsesNode use = uses.iterator().next();
219         assertEquals(nodeGroup, use.getSourceGrouping());
220     }
221
222
223     private static final URI NS = URI.create("urn:opendaylight:simple-nodes");
224
225     private static SchemaPath createPath(final String... names) {
226         final Revision rev = Revision.of("2013-07-30");
227         final List<QName> path = new ArrayList<>();
228         for (final String name : names) {
229             path.add(QName.create(NS, rev, name));
230         }
231         return SchemaPath.create(path, true);
232     }
233
234 }