blob: 6d97bb84cea12d487e17ead6495e1ae6edf4a556 [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;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.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 /**
Vidyashree Rama49abe712016-02-13 22:22:12 +0530148 * Checks status statement as sub-statement of container.
149 */
150 @Test
151 public void processContainerSubStatementStatus() throws IOException, ParserException {
152
153 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementStatus.yang");
154
155 // Check whether the data model tree returned is of type module.
156 assertThat((node instanceof YangModule), is(true));
157
158 // Check whether the node type is set properly to module.
159 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
160
161 // Check whether the module name is set correctly.
162 YangModule yangNode = (YangModule) node;
163 assertThat(yangNode.getName(), is("Test"));
164
165 // Check whether status is set correctly.
166 YangContainer container = (YangContainer) yangNode.getChild();
167 assertThat(container.getName(), is("valid"));
168 assertThat(container.isConfig(), is(true));
169 assertThat(container.getStatus(), is(YangStatusType.OBSOLETE));
170
171 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530172 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
173 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530174
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530175 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530176 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530177 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
178 assertThat(leafInfo.getUnits(), is("\"seconds\""));
179 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
180 assertThat(leafInfo.isMandatory(), is(true));
181 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
182 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
183 }
184
185 /**
186 * Checks status statement as sub-statement of list.
187 */
188 @Test
189 public void processListSubStatementStatus() throws IOException, ParserException {
190
191 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementStatus.yang");
192
193 assertThat((node instanceof YangModule), is(true));
194
195 // Check whether the node type is set properly to module.
196 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
197
198 // Check whether the module name is set correctly.
199 YangModule yangNode = (YangModule) node;
200 assertThat(yangNode.getName(), is("Test"));
201
202 // Check whether the list is child of module and status is set.
203 YangList yangList = (YangList) yangNode.getChild();
204 assertThat(yangList.getName(), is("valid"));
205 assertThat(yangList.isConfig(), is(true));
206 assertThat(yangList.getStatus(), is(YangStatusType.CURRENT));
207
208 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530209 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
210 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530211
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530212 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530213 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530214 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
215 assertThat(leafInfo.getUnits(), is("\"seconds\""));
216 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
217 assertThat(leafInfo.isMandatory(), is(true));
218 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
219 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
220 }
221
222 /**
223 * Checks valid status statement as sub-statement of leaf-list.
224 */
225 @Test
226 public void processLeafListSubStatementStatus() throws IOException, ParserException {
227
228 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementStatus.yang");
229
230 // Check whether the data model tree returned is of type module.
231 assertThat((node instanceof YangModule), is(true));
232
233 // Check whether the node type is set properly to module.
234 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
235
236 // Check whether the module name is set correctly.
237 YangModule yangNode = (YangModule) node;
238 assertThat(yangNode.getName(), is("Test"));
239
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530240 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
241 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530242
243 // Check whether status is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530244 assertThat(leafListInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530245 assertThat(leafListInfo.isConfig(), is(true));
246 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
247 }
Vidyashree Ramaa0568d22016-02-24 11:26:42 +0530248
249 /**
250 * Checks default value of status statement.
251 */
252 @Test
253 public void processStatusDefaultValue() throws IOException, ParserException {
254
255 YangNode node = manager.getDataModel("src/test/resources/StatusDefaultValue.yang");
256
257 // Check whether the data model tree returned is of type module.
258 assertThat((node instanceof YangModule), is(true));
259
260 // Check whether the node type is set properly to module.
261 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
262
263 // Check whether the module name is set correctly.
264 YangModule yangNode = (YangModule) node;
265 assertThat(yangNode.getName(), is("Test"));
266
267 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
268 YangLeafList leafListInfo = leafListIterator.next();
269
270 // Check whether status is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530271 assertThat(leafListInfo.getName(), is("invalid-interval"));
Vidyashree Ramaa0568d22016-02-24 11:26:42 +0530272 assertThat(leafListInfo.isConfig(), is(true));
273 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
274 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530275}