blob: 9655b2514dd48e2e9a18129f99e6420f4aa4308d [file] [log] [blame]
Vidyashree Rama49abe712016-02-13 22:22:12 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import org.junit.Rule;
20import org.junit.Test;
21
22import org.junit.rules.ExpectedException;
23import org.onosproject.yangutils.datamodel.YangLeaf;
24import org.onosproject.yangutils.datamodel.YangModule;
25import org.onosproject.yangutils.datamodel.YangNode;
26import org.onosproject.yangutils.datamodel.YangNodeType;
27import org.onosproject.yangutils.datamodel.YangDataTypes;
28import org.onosproject.yangutils.datamodel.YangStatusType;
29import org.onosproject.yangutils.datamodel.YangContainer;
30import org.onosproject.yangutils.datamodel.YangList;
31import org.onosproject.yangutils.datamodel.YangLeafList;
32import org.onosproject.yangutils.parser.exceptions.ParserException;
33import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34
35import java.io.IOException;
36import java.util.ListIterator;
37
38import static org.hamcrest.MatcherAssert.assertThat;
39import static org.hamcrest.core.Is.is;
40
41/**
42 * Test cases for description listener.
43 */
44public class DescriptionListenerTest {
45
46 @Rule
47 public ExpectedException thrown = ExpectedException.none();
48
49 private final YangUtilsParserManager manager = new YangUtilsParserManager();
50
51 /**
52 * Checks valid description statement.
53 */
54 @Test
55 public void processDescriptionValidStatement() throws IOException, ParserException {
56
57 YangNode node = manager.getDataModel("src/test/resources/DescriptionValidStatement.yang");
58
59 // Check whether the data model tree returned is of type module.
60 assertThat((node instanceof YangModule), is(true));
61
62 // Check whether the node type is set properly to module.
63 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
64
65 // Check whether the module name is set correctly.
66 YangModule yangNode = (YangModule) node;
67 assertThat(yangNode.getName(), is("Test"));
68
69 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
70 YangLeaf leafInfo = leafIterator.next();
71
72 // Check whether the description is set correctly.
73 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
74 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
75 }
76
77 /**
78 * Checks whether exception is thrown for invalid description statement.
79 */
80 @Test
81 public void processDescriptionWithoutStatementEnd() throws IOException, ParserException {
82 thrown.expect(ParserException.class);
83 thrown.expectMessage("extraneous input '}' expecting {';', '+'}");
84 YangNode node = manager.getDataModel("src/test/resources/DescriptionWithoutStatementEnd.yang");
85 }
86
87 /**
88 * Checks valid description statement as sub-statement of module.
89 */
90 @Test
91 public void processModuleSubStatementDescription() throws IOException, ParserException {
92
93 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementDescription.yang");
94
95 // Check whether the data model tree returned is of type module.
96 assertThat((node instanceof YangModule), is(true));
97
98 // Check whether the node type is set properly to module.
99 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
100
101 // Check whether the module name is set correctly.
102 YangModule yangNode = (YangModule) node;
103 assertThat(yangNode.getName(), is("Test"));
104
105 // Check whether the description is set correctly.
106 assertThat(yangNode.getDescription(), is("\"Interval before a route is declared invalid\""));
107 }
108
109 /**
110 * Checks valid description statement as sub-statement of module.
111 */
112 @Test
113 public void processDescriptionEmptyStatement() throws IOException, ParserException {
114
115 YangNode node = manager.getDataModel("src/test/resources/DescriptionEmptyStatement.yang");
116
117 // Check whether the data model tree returned is of type module.
118 assertThat((node instanceof YangModule), is(true));
119
120 // Check whether the node type is set properly to module.
121 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
122
123 // Check whether the module name is set correctly.
124 YangModule yangNode = (YangModule) node;
125 assertThat(yangNode.getName(), is("Test"));
126
127 // Check whether the description is set correctly.
128 assertThat(yangNode.getDescription(), is("\"\""));
129 }
130
131 /**
132 * Checks valid description statement as sub-statement of revision.
133 */
134 @Test
135 public void processRevisionSubStatementRevision() throws IOException, ParserException {
136
137 YangNode node = manager.getDataModel("src/test/resources/RevisionSubStatementRevision.yang");
138
139 // Check whether the data model tree returned is of type module.
140 assertThat((node instanceof YangModule), is(true));
141
142 // Check whether the node type is set properly to module.
143 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
144
145 // Check whether the module name is set correctly.
146 YangModule yangNode = (YangModule) node;
147 assertThat(yangNode.getName(), is("Test"));
148
149 // Check whether the description is set correctly.
150 assertThat(yangNode.getDescription(), is("\"module description\""));
151 assertThat(yangNode.getRevision().getDescription(), is("\"revision description\""));
152 }
153
154 /**
155 * Checks description statement as sub-statement of container.
156 */
157 @Test
158 public void processContainerSubStatementDescription() throws IOException, ParserException {
159
160 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementDescription.yang");
161
162 // Check whether the data model tree returned is of type module.
163 assertThat((node instanceof YangModule), is(true));
164
165 // Check whether the node type is set properly to module.
166 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
167
168 // Check whether the module name is set correctly.
169 YangModule yangNode = (YangModule) node;
170 assertThat(yangNode.getName(), is("Test"));
171
172 // Check whether the description value is set correctly.
173 YangContainer container = (YangContainer) yangNode.getChild();
174 assertThat(container.getName(), is("valid"));
175 assertThat(container.getDescription(), is("\"container description\""));
176
177 // Check whether leaf properties as set correctly.
178 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
179 YangLeaf leafInfo = leafIterator.next();
180
181 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
182 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
183 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
184 assertThat(leafInfo.getUnits(), is("\"seconds\""));
185 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
186 assertThat(leafInfo.isMandatory(), is(true));
187 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
188 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
189 }
190
191 /**
192 * Checks description statement as sub-statement of list.
193 */
194 @Test
195 public void processListSubStatementDescription() throws IOException, ParserException {
196
197 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementDescription.yang");
198
199 assertThat((node instanceof YangModule), is(true));
200
201 // Check whether the node type is set properly to module.
202 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
203
204 // Check whether the module name is set correctly.
205 YangModule yangNode = (YangModule) node;
206 assertThat(yangNode.getName(), is("Test"));
207
208 // Check whether the list is child of module and description value is set correctly.
209 YangList yangList = (YangList) yangNode.getChild();
210 assertThat(yangList.getName(), is("valid"));
211 assertThat(yangList.isConfig(), is(true));
212 assertThat(yangList.getDescription(), is("\"list description\""));
213
214 // Check whether leaf properties as set correctly.
215 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
216 YangLeaf leafInfo = leafIterator.next();
217
218 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
219 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
220 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
221 assertThat(leafInfo.getUnits(), is("\"seconds\""));
222 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
223 assertThat(leafInfo.isMandatory(), is(true));
224 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
225 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
226 }
227
228 /**
229 * Checks valid description statement as sub-statement of leaf-list.
230 */
231 @Test
232 public void processLeafListSubStatementDescription() throws IOException, ParserException {
233
234 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementDescription.yang");
235
236 // Check whether the data model tree returned is of type module.
237 assertThat((node instanceof YangModule), is(true));
238
239 // Check whether the node type is set properly to module.
240 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
241
242 // Check whether the module name is set correctly.
243 YangModule yangNode = (YangModule) node;
244 assertThat(yangNode.getName(), is("Test"));
245
246 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
247 YangLeafList leafListInfo = leafListIterator.next();
248
249 // Check whether description value is set correctly.
250 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
251 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
252 }
253}