blob: 9cfacf9251d48475afee98c072b7589a86155ef3 [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 status listener.
43 */
44public class StatusListenerTest {
45
46 @Rule
47 public ExpectedException thrown = ExpectedException.none();
48
49 private final YangUtilsParserManager manager = new YangUtilsParserManager();
50
51 /**
52 * Checks valid status statement.
53 */
54 @Test
55 public void processStatusStatementCurrent() throws IOException, ParserException {
56
57 YangNode node = manager.getDataModel("src/test/resources/StatusStatementCurrent.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 status is set correctly.
73 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
74 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
75 }
76
77 /**
78 * Checks valid status statement.
79 */
80 @Test
81 public void processStatusStatementDeprecated() throws IOException, ParserException {
82
83 YangNode node = manager.getDataModel("src/test/resources/StatusStatementDeprecated.yang");
84
85 // Check whether the data model tree returned is of type module.
86 assertThat((node instanceof YangModule), is(true));
87
88 // Check whether the node type is set properly to module.
89 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
90
91 // Check whether the module name is set correctly.
92 YangModule yangNode = (YangModule) node;
93 assertThat(yangNode.getName(), is("Test"));
94
95 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
96 YangLeaf leafInfo = leafIterator.next();
97
98 // Check whether the status is set correctly.
99 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
100 assertThat(leafInfo.getStatus(), is(YangStatusType.DEPRECATED));
101 }
102
103 /**
104 * Checks valid status statement.
105 */
106 @Test
107 public void processStatusStatementObsolete() throws IOException, ParserException {
108
109 YangNode node = manager.getDataModel("src/test/resources/StatusStatementObsolete.yang");
110
111 // Check whether the data model tree returned is of type module.
112 assertThat((node instanceof YangModule), is(true));
113
114 // Check whether the node type is set properly to module.
115 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
116
117 // Check whether the module name is set correctly.
118 YangModule yangNode = (YangModule) node;
119 assertThat(yangNode.getName(), is("Test"));
120
121 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
122 YangLeaf leafInfo = leafIterator.next();
123
124 // Check whether the status is set correctly.
125 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
126 assertThat(leafInfo.getStatus(), is(YangStatusType.OBSOLETE));
127 }
128
129 /**
130 * Checks whether exception is thrown for invalid status statement.
131 */
132 @Test
133 public void processStatusWithoutStatementEnd() throws IOException, ParserException {
134 thrown.expect(ParserException.class);
135 thrown.expectMessage("missing ';' at '}'");
136 YangNode node = manager.getDataModel("src/test/resources/StatusWithoutStatementEnd.yang");
137 }
138
139 /**
140 * Checks whether exception is thrown for invalid status statement.
141 */
142 @Test
143 public void processStatusInvalidValue() throws IOException, ParserException {
144 thrown.expect(ParserException.class);
145 thrown.expectMessage("mismatched input 'invalid' expecting {'current', 'deprecated', 'obsolete'}");
146 YangNode node = manager.getDataModel("src/test/resources/StatusInvalidValue.yang");
147 }
148
149 /**
150 * Checks status statement as sub-statement of module.
151 */
152 @Test
153 public void processModuleSubStatementStatus() throws IOException, ParserException {
154 thrown.expect(ParserException.class);
155 thrown.expectMessage("mismatched input 'status' expecting {'augment', 'choice', 'contact', 'container', " +
156 "'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include'," +
157 " 'leaf', 'leaf-list', 'list', 'namespace', 'notification', 'organization', 'prefix', 'reference', " +
158 "'revision', 'rpc', 'typedef', 'uses', 'yang-version', '}'}");
159 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementStatus.yang");
160 }
161
162 /**
163 * Checks status statement as sub-statement of container.
164 */
165 @Test
166 public void processContainerSubStatementStatus() throws IOException, ParserException {
167
168 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementStatus.yang");
169
170 // Check whether the data model tree returned is of type module.
171 assertThat((node instanceof YangModule), is(true));
172
173 // Check whether the node type is set properly to module.
174 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
175
176 // Check whether the module name is set correctly.
177 YangModule yangNode = (YangModule) node;
178 assertThat(yangNode.getName(), is("Test"));
179
180 // Check whether status is set correctly.
181 YangContainer container = (YangContainer) yangNode.getChild();
182 assertThat(container.getName(), is("valid"));
183 assertThat(container.isConfig(), is(true));
184 assertThat(container.getStatus(), is(YangStatusType.OBSOLETE));
185
186 // Check whether leaf properties as set correctly.
187 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
188 YangLeaf leafInfo = leafIterator.next();
189
190 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
191 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
192 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
193 assertThat(leafInfo.getUnits(), is("\"seconds\""));
194 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
195 assertThat(leafInfo.isMandatory(), is(true));
196 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
197 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
198 }
199
200 /**
201 * Checks status statement as sub-statement of list.
202 */
203 @Test
204 public void processListSubStatementStatus() throws IOException, ParserException {
205
206 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementStatus.yang");
207
208 assertThat((node instanceof YangModule), is(true));
209
210 // Check whether the node type is set properly to module.
211 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
212
213 // Check whether the module name is set correctly.
214 YangModule yangNode = (YangModule) node;
215 assertThat(yangNode.getName(), is("Test"));
216
217 // Check whether the list is child of module and status is set.
218 YangList yangList = (YangList) yangNode.getChild();
219 assertThat(yangList.getName(), is("valid"));
220 assertThat(yangList.isConfig(), is(true));
221 assertThat(yangList.getStatus(), is(YangStatusType.CURRENT));
222
223 // Check whether leaf properties as set correctly.
224 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
225 YangLeaf leafInfo = leafIterator.next();
226
227 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
228 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
229 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
230 assertThat(leafInfo.getUnits(), is("\"seconds\""));
231 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
232 assertThat(leafInfo.isMandatory(), is(true));
233 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
234 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
235 }
236
237 /**
238 * Checks valid status statement as sub-statement of leaf-list.
239 */
240 @Test
241 public void processLeafListSubStatementStatus() throws IOException, ParserException {
242
243 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementStatus.yang");
244
245 // Check whether the data model tree returned is of type module.
246 assertThat((node instanceof YangModule), is(true));
247
248 // Check whether the node type is set properly to module.
249 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
250
251 // Check whether the module name is set correctly.
252 YangModule yangNode = (YangModule) node;
253 assertThat(yangNode.getName(), is("Test"));
254
255 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
256 YangLeafList leafListInfo = leafListIterator.next();
257
258 // Check whether status is set correctly.
259 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
260 assertThat(leafListInfo.isConfig(), is(true));
261 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
262 }
263}