blob: 7f0d1eafe868048649df1a6de4cc98db4b2cdb5b [file] [log] [blame]
Vidyashree Rama49abe712016-02-13 22:22:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama49abe712016-02-13 22:22:12 +05303 *
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;
Vidyashree Rama49abe712016-02-13 22:22:12 +053021import org.junit.Rule;
22import org.junit.Test;
Vidyashree Rama49abe712016-02-13 22:22:12 +053023import org.junit.rules.ExpectedException;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053024import org.onosproject.yangutils.datamodel.YangContainer;
25import org.onosproject.yangutils.datamodel.YangDataTypes;
Vidyashree Rama49abe712016-02-13 22:22:12 +053026import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053027import org.onosproject.yangutils.datamodel.YangLeafList;
28import org.onosproject.yangutils.datamodel.YangList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053029import org.onosproject.yangutils.datamodel.YangModule;
30import org.onosproject.yangutils.datamodel.YangNode;
31import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053032import org.onosproject.yangutils.datamodel.YangStatusType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053033import org.onosproject.yangutils.parser.exceptions.ParserException;
34import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
35
Vidyashree Rama49abe712016-02-13 22:22:12 +053036import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.core.Is.is;
38
39/**
40 * Test cases for status listener.
41 */
42public class StatusListenerTest {
43
44 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
47 private final YangUtilsParserManager manager = new YangUtilsParserManager();
48
49 /**
50 * Checks valid status statement.
51 */
52 @Test
53 public void processStatusStatementCurrent() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/StatusStatementCurrent.yang");
56
57 // Check whether the data model tree returned is of type module.
58 assertThat((node instanceof YangModule), is(true));
59
60 // Check whether the node type is set properly to module.
61 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
62
63 // Check whether the module name is set correctly.
64 YangModule yangNode = (YangModule) node;
65 assertThat(yangNode.getName(), is("Test"));
66
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053067 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
68 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053069
70 // Check whether the status is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 assertThat(leafInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053072 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
73 }
74
75 /**
76 * Checks valid status statement.
77 */
78 @Test
79 public void processStatusStatementDeprecated() throws IOException, ParserException {
80
81 YangNode node = manager.getDataModel("src/test/resources/StatusStatementDeprecated.yang");
82
83 // Check whether the data model tree returned is of type module.
84 assertThat((node instanceof YangModule), is(true));
85
86 // Check whether the node type is set properly to module.
87 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
88
89 // Check whether the module name is set correctly.
90 YangModule yangNode = (YangModule) node;
91 assertThat(yangNode.getName(), is("Test"));
92
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053093 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
94 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053095
96 // Check whether the status is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053097 assertThat(leafInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053098 assertThat(leafInfo.getStatus(), is(YangStatusType.DEPRECATED));
99 }
100
101 /**
102 * Checks valid status statement.
103 */
104 @Test
105 public void processStatusStatementObsolete() throws IOException, ParserException {
106
107 YangNode node = manager.getDataModel("src/test/resources/StatusStatementObsolete.yang");
108
109 // Check whether the data model tree returned is of type module.
110 assertThat((node instanceof YangModule), is(true));
111
112 // Check whether the node type is set properly to module.
113 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
114
115 // Check whether the module name is set correctly.
116 YangModule yangNode = (YangModule) node;
117 assertThat(yangNode.getName(), is("Test"));
118
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530119 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
120 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530121
122 // Check whether the status is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530123 assertThat(leafInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530124 assertThat(leafInfo.getStatus(), is(YangStatusType.OBSOLETE));
125 }
126
127 /**
128 * Checks whether exception is thrown for invalid status statement.
129 */
130 @Test
131 public void processStatusWithoutStatementEnd() throws IOException, ParserException {
132 thrown.expect(ParserException.class);
133 thrown.expectMessage("missing ';' at '}'");
134 YangNode node = manager.getDataModel("src/test/resources/StatusWithoutStatementEnd.yang");
135 }
136
137 /**
138 * Checks whether exception is thrown for invalid status statement.
139 */
140 @Test
141 public void processStatusInvalidValue() throws IOException, ParserException {
142 thrown.expect(ParserException.class);
Vidyashree Rama74453712016-04-18 12:29:39 +0530143 thrown.expectMessage("YANG file error : status invalid is not valid.");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530144 YangNode node = manager.getDataModel("src/test/resources/StatusInvalidValue.yang");
145 }
146
147 /**
148 * Checks status statement as sub-statement of module.
149 */
150 @Test
151 public void processModuleSubStatementStatus() throws IOException, ParserException {
152 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530153 thrown.expectMessage("mismatched input 'status' expecting {'augment', 'choice', 'contact', 'container', "
154 + "'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include',"
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +0530155 + " 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference', "
156 + "'revision', 'rpc', 'typedef', 'uses', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530157 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementStatus.yang");
158 }
159
160 /**
161 * Checks status statement as sub-statement of container.
162 */
163 @Test
164 public void processContainerSubStatementStatus() throws IOException, ParserException {
165
166 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementStatus.yang");
167
168 // Check whether the data model tree returned is of type module.
169 assertThat((node instanceof YangModule), is(true));
170
171 // Check whether the node type is set properly to module.
172 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
173
174 // Check whether the module name is set correctly.
175 YangModule yangNode = (YangModule) node;
176 assertThat(yangNode.getName(), is("Test"));
177
178 // Check whether status is set correctly.
179 YangContainer container = (YangContainer) yangNode.getChild();
180 assertThat(container.getName(), is("valid"));
181 assertThat(container.isConfig(), is(true));
182 assertThat(container.getStatus(), is(YangStatusType.OBSOLETE));
183
184 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530185 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
186 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530187
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530188 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530189 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530190 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
191 assertThat(leafInfo.getUnits(), is("\"seconds\""));
192 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
193 assertThat(leafInfo.isMandatory(), is(true));
194 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
195 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
196 }
197
198 /**
199 * Checks status statement as sub-statement of list.
200 */
201 @Test
202 public void processListSubStatementStatus() throws IOException, ParserException {
203
204 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementStatus.yang");
205
206 assertThat((node instanceof YangModule), is(true));
207
208 // Check whether the node type is set properly to module.
209 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
210
211 // Check whether the module name is set correctly.
212 YangModule yangNode = (YangModule) node;
213 assertThat(yangNode.getName(), is("Test"));
214
215 // Check whether the list is child of module and status is set.
216 YangList yangList = (YangList) yangNode.getChild();
217 assertThat(yangList.getName(), is("valid"));
218 assertThat(yangList.isConfig(), is(true));
219 assertThat(yangList.getStatus(), is(YangStatusType.CURRENT));
220
221 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530222 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
223 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530224
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530225 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530226 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530227 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
228 assertThat(leafInfo.getUnits(), is("\"seconds\""));
229 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
230 assertThat(leafInfo.isMandatory(), is(true));
231 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
232 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
233 }
234
235 /**
236 * Checks valid status statement as sub-statement of leaf-list.
237 */
238 @Test
239 public void processLeafListSubStatementStatus() throws IOException, ParserException {
240
241 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementStatus.yang");
242
243 // Check whether the data model tree returned is of type module.
244 assertThat((node instanceof YangModule), is(true));
245
246 // Check whether the node type is set properly to module.
247 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
248
249 // Check whether the module name is set correctly.
250 YangModule yangNode = (YangModule) node;
251 assertThat(yangNode.getName(), is("Test"));
252
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530253 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
254 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530255
256 // Check whether status is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530257 assertThat(leafListInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530258 assertThat(leafListInfo.isConfig(), is(true));
259 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
260 }
Vidyashree Ramaa0568d22016-02-24 11:26:42 +0530261
262 /**
263 * Checks default value of status statement.
264 */
265 @Test
266 public void processStatusDefaultValue() throws IOException, ParserException {
267
268 YangNode node = manager.getDataModel("src/test/resources/StatusDefaultValue.yang");
269
270 // Check whether the data model tree returned is of type module.
271 assertThat((node instanceof YangModule), is(true));
272
273 // Check whether the node type is set properly to module.
274 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
275
276 // Check whether the module name is set correctly.
277 YangModule yangNode = (YangModule) node;
278 assertThat(yangNode.getName(), is("Test"));
279
280 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
281 YangLeafList leafListInfo = leafListIterator.next();
282
283 // Check whether status is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530284 assertThat(leafListInfo.getName(), is("invalid-interval"));
Vidyashree Ramaa0568d22016-02-24 11:26:42 +0530285 assertThat(leafListInfo.isConfig(), is(true));
286 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
287 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530288}