Merge "Bug 1372 - toString methods in generated classes"
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / YangParserSimpleTest.java
1 /*
2  * Copyright (c) 2013 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.parser.impl;
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.DateFormat;
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.Set;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
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.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 final URI snNS = URI.create("urn:opendaylight:simple-nodes");
42     private Date snRev;
43     private final String snPref = "sn";
44
45     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
46     private Set<Module> modules;
47
48     @Before
49     public void init() throws Exception {
50         snRev = simpleDateFormat.parse("2013-07-30");
51         modules = TestUtils.loadModules(getClass().getResource("/simple-test").toURI());
52     }
53
54     @Test
55     public void testParseAnyXml() {
56         Module testModule = TestUtils.findModule(modules, "simple-nodes");
57         AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
58         assertNotNull("'anyxml data not found'", data);
59
60         // test SchemaNode args
61         QName qname = data.getQName();
62         assertEquals("data", qname.getLocalName());
63         assertEquals(snNS, qname.getNamespace());
64         assertEquals(snRev, qname.getRevision());
65         assertEquals("anyxml desc", data.getDescription());
66         assertEquals("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         assertFalse(data.isConfiguration());
72         ConstraintDefinition constraints = data.getConstraints();
73         assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
74         Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
75         assertEquals(2, constraints.getMustConstraints().size());
76
77         String must1 = "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"";
78         String errMsg1 = "An ethernet MTU must be 1500";
79         String must2 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
80         String errMsg2 = "An atm MTU must be  64 .. 17966";
81
82         boolean found1 = false;
83         boolean found2 = false;
84         for (MustDefinition must : mustConstraints) {
85             if (must1.equals(must.toString())) {
86                 found1 = true;
87                 assertEquals(errMsg1, must.getErrorMessage());
88             } else if (must2.equals(must.toString())) {
89                 found2 = true;
90                 assertEquals(errMsg2, must.getErrorMessage());
91                 assertEquals("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         Module test = TestUtils.findModule(modules, "simple-nodes");
107
108         ContainerSchemaNode nodes = (ContainerSchemaNode) test.getDataChildByName("nodes");
109         // test SchemaNode args
110         QName expectedQName = new QName(snNS, snRev, snPref, "nodes");
111         assertEquals(expectedQName, nodes.getQName());
112         SchemaPath expectedPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes");
113         assertEquals(expectedPath, nodes.getPath());
114         assertEquals("nodes collection", nodes.getDescription());
115         assertEquals("nodes ref", nodes.getReference());
116         assertEquals(Status.CURRENT, nodes.getStatus());
117         assertEquals(0, nodes.getUnknownSchemaNodes().size());
118         // test DataSchemaNode args
119         assertFalse(nodes.isAugmenting());
120         assertFalse(nodes.isConfiguration());
121
122         // constraints
123         ConstraintDefinition constraints = nodes.getConstraints();
124         assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
125         Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
126         assertEquals(2, constraints.getMustConstraints().size());
127
128         String must1 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
129         String errMsg1 = "An atm MTU must be  64 .. 17966";
130         String must2 = "ifId != 0";
131
132         boolean found1 = false;
133         boolean found2 = false;
134         for (MustDefinition must : mustConstraints) {
135             if (must1.equals(must.toString())) {
136                 found1 = true;
137                 assertEquals(errMsg1, must.getErrorMessage());
138             } else if (must2.equals(must.toString())) {
139                 found2 = true;
140                 assertNull(must.getErrorMessage());
141                 assertNull(must.getErrorAppTag());
142                 assertNull(must.getDescription());
143                 assertNull(must.getReference());
144             }
145         }
146         assertTrue(found1);
147         assertTrue(found2);
148
149         assertFalse(constraints.isMandatory());
150         assertNull(constraints.getMinElements());
151         assertNull(constraints.getMaxElements());
152         assertTrue(nodes.isPresenceContainer());
153
154         // typedef
155         Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
156         assertEquals(1, typedefs.size());
157         TypeDefinition<?> nodesType = typedefs.iterator().next();
158         QName typedefQName = new QName(snNS, snRev, snPref, "nodes-type");
159         assertEquals(typedefQName, nodesType.getQName());
160         SchemaPath nodesTypePath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "nodes-type");
161         assertEquals(nodesTypePath, nodesType.getPath());
162         assertTrue(nodesType.getDescription().isEmpty());
163         assertTrue(nodesType.getReference().isEmpty());
164         assertEquals(Status.CURRENT, nodesType.getStatus());
165         assertEquals(0, nodesType.getUnknownSchemaNodes().size());
166
167         // child nodes
168         // total size = 8: defined 6, inserted by uses 2
169         assertEquals(8, nodes.getChildNodes().size());
170         LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added");
171         assertEquals(createPath("nodes", "added"), added.getPath());
172         assertEquals(createPath("mytype"), added.getType().getPath());
173
174         ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
175         assertFalse(links.isUserOrdered());
176
177         Set<GroupingDefinition> groupings = nodes.getGroupings();
178         assertEquals(1, groupings.size());
179         GroupingDefinition nodeGroup = groupings.iterator().next();
180         QName groupQName = new QName(snNS, snRev, snPref, "node-group");
181         assertEquals(groupQName, nodeGroup.getQName());
182         SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
183         assertEquals(nodeGroupPath, nodeGroup.getPath());
184
185         Set<UsesNode> uses = nodes.getUses();
186         assertEquals(1, uses.size());
187         UsesNode use = uses.iterator().next();
188         assertEquals(nodeGroupPath, use.getGroupingPath());
189     }
190
191
192     private final URI ns = URI.create("urn:opendaylight:simple-nodes");
193     private Date rev;
194     private final String prefix = "sn";
195
196     private SchemaPath createPath(final String... names) {
197         try {
198             rev = new SimpleDateFormat("yyyy-MM-dd").parse("2013-07-30");
199         } catch (ParseException e) {
200             e.printStackTrace();
201         }
202
203         List<QName> path = new ArrayList<>();
204         for (String name : names) {
205             path.add(new QName(ns, rev, prefix, name));
206         }
207         return SchemaPath.create(path, true);
208     }
209
210 }