Added more tests. Fixed javadocs.
[yangtools.git] / 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.Date;
18 import java.util.Set;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
25 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
28 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
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.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.api.Status;
35 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.UsesNode;
37
38 public class YangParserSimpleTest {
39
40     private final URI snNS = URI.create("urn:opendaylight:simple-nodes");
41     private Date snRev;
42     private final String snPref = "sn";
43
44     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
45     private Set<Module> modules;
46
47     @Before
48     public void init() throws FileNotFoundException, ParseException {
49         snRev = simpleDateFormat.parse("2013-07-30");
50         modules = TestUtils.loadModules(getClass().getResource("/simple-test").getPath());
51     }
52
53     @Test
54     public void testParseAnyXml() {
55         Module testModule = TestUtils.findModule(modules, "simple-nodes");
56         AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
57         assertNotNull("'anyxml data not found'", data);
58
59         // test SchemaNode args
60         QName qname = data.getQName();
61         assertEquals("data", qname.getLocalName());
62         assertEquals(snPref, qname.getPrefix());
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         assertNull(nodesType.getDescription());
163         assertNull(nodesType.getReference());
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         AnyXmlSchemaNode text = (AnyXmlSchemaNode)nodes.getDataChildByName("text");
171         assertNotNull(text);
172         ChoiceNode level = (ChoiceNode)nodes.getDataChildByName("level");
173         assertNotNull(level);
174         ContainerSchemaNode node = (ContainerSchemaNode)nodes.getDataChildByName("node");
175         assertNotNull(node);
176         LeafSchemaNode nodesId = (LeafSchemaNode)nodes.getDataChildByName("nodes-id");
177         assertNotNull(nodesId);
178         LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added");
179         assertNotNull(added);
180         ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
181         assertNotNull(links);
182         assertFalse(links.isUserOrdered());
183         LeafSchemaNode source = (LeafSchemaNode)nodes.getDataChildByName("source");
184         assertNotNull(source);
185         LeafSchemaNode target = (LeafSchemaNode)nodes.getDataChildByName("target");
186         assertNotNull(target);
187
188         Set<GroupingDefinition> groupings = nodes.getGroupings();
189         assertEquals(1, groupings.size());
190         GroupingDefinition nodeGroup = groupings.iterator().next();
191         QName groupQName = new QName(snNS, snRev, snPref, "node-group");
192         assertEquals(groupQName, nodeGroup.getQName());
193         SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
194         assertEquals(nodeGroupPath, nodeGroup.getPath());
195
196         Set<UsesNode> uses = nodes.getUses();
197         assertEquals(1, uses.size());
198         UsesNode use = uses.iterator().next();
199         assertEquals(nodeGroupPath, use.getGroupingPath());
200     }
201
202 }