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