blob: 1f6aa5fbd402d336d68c36cc470936615a90d188 [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
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053019import java.io.IOException;
20import java.util.ListIterator;
21
Vidyashree Rama49abe712016-02-13 22:22:12 +053022import org.junit.Rule;
23import org.junit.Test;
Vidyashree Rama49abe712016-02-13 22:22:12 +053024import org.junit.rules.ExpectedException;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025import org.onosproject.yangutils.datamodel.YangContainer;
26import org.onosproject.yangutils.datamodel.YangDataTypes;
Vidyashree Rama49abe712016-02-13 22:22:12 +053027import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053028import org.onosproject.yangutils.datamodel.YangLeafList;
29import org.onosproject.yangutils.datamodel.YangList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053030import org.onosproject.yangutils.datamodel.YangModule;
31import org.onosproject.yangutils.datamodel.YangNode;
32import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053033import org.onosproject.yangutils.datamodel.YangStatusType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053034import org.onosproject.yangutils.parser.exceptions.ParserException;
35import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
36
Vidyashree Rama49abe712016-02-13 22:22:12 +053037import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.core.Is.is;
39
40/**
41 * Test cases for status listener.
42 */
43public class StatusListenerTest {
44
45 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
48 private final YangUtilsParserManager manager = new YangUtilsParserManager();
49
50 /**
51 * Checks valid status statement.
52 */
53 @Test
54 public void processStatusStatementCurrent() throws IOException, ParserException {
55
56 YangNode node = manager.getDataModel("src/test/resources/StatusStatementCurrent.yang");
57
58 // Check whether the data model tree returned is of type module.
59 assertThat((node instanceof YangModule), is(true));
60
61 // Check whether the node type is set properly to module.
62 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
63
64 // Check whether the module name is set correctly.
65 YangModule yangNode = (YangModule) node;
66 assertThat(yangNode.getName(), is("Test"));
67
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053068 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
69 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053070
71 // Check whether the status is set correctly.
72 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
73 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
74 }
75
76 /**
77 * Checks valid status statement.
78 */
79 @Test
80 public void processStatusStatementDeprecated() throws IOException, ParserException {
81
82 YangNode node = manager.getDataModel("src/test/resources/StatusStatementDeprecated.yang");
83
84 // Check whether the data model tree returned is of type module.
85 assertThat((node instanceof YangModule), is(true));
86
87 // Check whether the node type is set properly to module.
88 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
89
90 // Check whether the module name is set correctly.
91 YangModule yangNode = (YangModule) node;
92 assertThat(yangNode.getName(), is("Test"));
93
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053094 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
95 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053096
97 // Check whether the status is set correctly.
98 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
99 assertThat(leafInfo.getStatus(), is(YangStatusType.DEPRECATED));
100 }
101
102 /**
103 * Checks valid status statement.
104 */
105 @Test
106 public void processStatusStatementObsolete() throws IOException, ParserException {
107
108 YangNode node = manager.getDataModel("src/test/resources/StatusStatementObsolete.yang");
109
110 // Check whether the data model tree returned is of type module.
111 assertThat((node instanceof YangModule), is(true));
112
113 // Check whether the node type is set properly to module.
114 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
115
116 // Check whether the module name is set correctly.
117 YangModule yangNode = (YangModule) node;
118 assertThat(yangNode.getName(), is("Test"));
119
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530120 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
121 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530122
123 // Check whether the status is set correctly.
124 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
125 assertThat(leafInfo.getStatus(), is(YangStatusType.OBSOLETE));
126 }
127
128 /**
129 * Checks whether exception is thrown for invalid status statement.
130 */
131 @Test
132 public void processStatusWithoutStatementEnd() throws IOException, ParserException {
133 thrown.expect(ParserException.class);
134 thrown.expectMessage("missing ';' at '}'");
135 YangNode node = manager.getDataModel("src/test/resources/StatusWithoutStatementEnd.yang");
136 }
137
138 /**
139 * Checks whether exception is thrown for invalid status statement.
140 */
141 @Test
142 public void processStatusInvalidValue() throws IOException, ParserException {
143 thrown.expect(ParserException.class);
144 thrown.expectMessage("mismatched input 'invalid' expecting {'current', 'deprecated', 'obsolete'}");
145 YangNode node = manager.getDataModel("src/test/resources/StatusInvalidValue.yang");
146 }
147
148 /**
149 * Checks status statement as sub-statement of module.
150 */
151 @Test
152 public void processModuleSubStatementStatus() throws IOException, ParserException {
153 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530154 thrown.expectMessage("mismatched input 'status' expecting {'augment', 'choice', 'contact', 'container', "
155 + "'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include',"
156 + " 'leaf', 'leaf-list', 'list', 'namespace', 'notification', 'organization', 'prefix', 'reference', "
157 + "'revision', 'rpc', 'typedef', 'uses', 'yang-version', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530158 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementStatus.yang");
159 }
160
161 /**
162 * Checks status statement as sub-statement of container.
163 */
164 @Test
165 public void processContainerSubStatementStatus() throws IOException, ParserException {
166
167 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementStatus.yang");
168
169 // Check whether the data model tree returned is of type module.
170 assertThat((node instanceof YangModule), is(true));
171
172 // Check whether the node type is set properly to module.
173 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
174
175 // Check whether the module name is set correctly.
176 YangModule yangNode = (YangModule) node;
177 assertThat(yangNode.getName(), is("Test"));
178
179 // Check whether status is set correctly.
180 YangContainer container = (YangContainer) yangNode.getChild();
181 assertThat(container.getName(), is("valid"));
182 assertThat(container.isConfig(), is(true));
183 assertThat(container.getStatus(), is(YangStatusType.OBSOLETE));
184
185 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530186 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
187 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530188
189 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
190 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
191 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
192 assertThat(leafInfo.getUnits(), is("\"seconds\""));
193 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
194 assertThat(leafInfo.isMandatory(), is(true));
195 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
196 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
197 }
198
199 /**
200 * Checks status statement as sub-statement of list.
201 */
202 @Test
203 public void processListSubStatementStatus() throws IOException, ParserException {
204
205 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementStatus.yang");
206
207 assertThat((node instanceof YangModule), is(true));
208
209 // Check whether the node type is set properly to module.
210 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
211
212 // Check whether the module name is set correctly.
213 YangModule yangNode = (YangModule) node;
214 assertThat(yangNode.getName(), is("Test"));
215
216 // Check whether the list is child of module and status is set.
217 YangList yangList = (YangList) yangNode.getChild();
218 assertThat(yangList.getName(), is("valid"));
219 assertThat(yangList.isConfig(), is(true));
220 assertThat(yangList.getStatus(), is(YangStatusType.CURRENT));
221
222 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530223 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
224 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530225
226 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
227 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
228 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
229 assertThat(leafInfo.getUnits(), is("\"seconds\""));
230 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
231 assertThat(leafInfo.isMandatory(), is(true));
232 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
233 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
234 }
235
236 /**
237 * Checks valid status statement as sub-statement of leaf-list.
238 */
239 @Test
240 public void processLeafListSubStatementStatus() throws IOException, ParserException {
241
242 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementStatus.yang");
243
244 // Check whether the data model tree returned is of type module.
245 assertThat((node instanceof YangModule), is(true));
246
247 // Check whether the node type is set properly to module.
248 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
249
250 // Check whether the module name is set correctly.
251 YangModule yangNode = (YangModule) node;
252 assertThat(yangNode.getName(), is("Test"));
253
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530254 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
255 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530256
257 // Check whether status is set correctly.
258 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
259 assertThat(leafListInfo.isConfig(), is(true));
260 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
261 }
Vidyashree Ramaa0568d22016-02-24 11:26:42 +0530262
263 /**
264 * Checks default value of status statement.
265 */
266 @Test
267 public void processStatusDefaultValue() throws IOException, ParserException {
268
269 YangNode node = manager.getDataModel("src/test/resources/StatusDefaultValue.yang");
270
271 // Check whether the data model tree returned is of type module.
272 assertThat((node instanceof YangModule), is(true));
273
274 // Check whether the node type is set properly to module.
275 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
276
277 // Check whether the module name is set correctly.
278 YangModule yangNode = (YangModule) node;
279 assertThat(yangNode.getName(), is("Test"));
280
281 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
282 YangLeafList leafListInfo = leafListIterator.next();
283
284 // Check whether status is set correctly.
285 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
286 assertThat(leafListInfo.isConfig(), is(true));
287 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
288 }
Vidyashree Rama49abe712016-02-13 22:22:12 +0530289}