blob: a4410a705e1681f88830f64f45cef016b4744ccc [file] [log] [blame]
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +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
19import static org.hamcrest.MatcherAssert.assertThat;
20import static org.hamcrest.core.Is.is;
21import org.junit.Test;
22import org.onosproject.yangutils.datamodel.YangBit;
23import org.onosproject.yangutils.datamodel.YangBits;
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +053024import org.onosproject.yangutils.datamodel.YangDerivedInfo;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053026import org.onosproject.yangutils.datamodel.YangLeaf;
27import org.onosproject.yangutils.datamodel.YangModule;
28import org.onosproject.yangutils.datamodel.YangNode;
29import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053030import org.onosproject.yangutils.datamodel.YangType;
31import org.onosproject.yangutils.datamodel.YangTypeDef;
32import org.onosproject.yangutils.datamodel.YangUnion;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053033import org.onosproject.yangutils.parser.exceptions.ParserException;
34import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
35
36import java.io.IOException;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053037import java.util.List;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053038import java.util.ListIterator;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053039import java.util.Map;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053040
41/**
42 * Test cases for bit listener.
43 */
44public class BitListenerTest {
45
46 private final YangUtilsParserManager manager = new YangUtilsParserManager();
47
48 /**
49 * Checks bit statement without position.
50 */
51 @Test
52 public void processBitTypeStatement() throws IOException, ParserException {
53
54 YangNode node = manager.getDataModel("src/test/resources/BitTypeStatement.yang");
55
56 // Check whether the data model tree returned is of type module.
57 assertThat((node instanceof YangModule), is(true));
58
59 // Check whether the node type is set properly to module.
60 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
61
62 // Check whether the module name is set correctly.
63 YangModule yangNode = (YangModule) node;
64 assertThat(yangNode.getName(), is("Test"));
65
66 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
67 YangLeaf leafInfo = leafIterator.next();
68
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053069 assertThat(leafInfo.getName(), is("mybits"));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053070 assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
71 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
72 assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
73 is("mybits"));
74
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053075 // Check bit name map
76 Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
77 assertThat(bitNameMap.size(), is(3));
78 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
79 String bitName = element.getKey();
80 YangBit yangBit = element.getValue();
81 if (bitName.equals("disable-nagle")) {
82 assertThat(yangBit.getPosition(), is(0));
83 } else if (bitName.equals("auto-sense-speed")) {
84 assertThat(yangBit.getPosition(), is(1));
85 } else if (bitName.equals("Ten-Mb-only")) {
86 assertThat(yangBit.getPosition(), is(2));
87 } else {
88 throw new IOException("Invalid bit name: " + bitName);
89 }
90 }
91
92 // Check bit position map
93 Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
94 .getBitPositionMap();
95 assertThat(bitPositionMap.size(), is(3));
96 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
97 int position = element.getKey();
98 YangBit yangBit = element.getValue();
99 if (position == 0) {
100 assertThat(yangBit.getBitName(), is("disable-nagle"));
101 } else if (position == 1) {
102 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
103 } else if (position == 2) {
104 assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
105 } else {
106 throw new IOException("Invalid bit position: " + position);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530107 }
108 }
109 }
110
111 /**
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530112 * Checks bit statement with typedef.
113 */
114 @Test
115 public void processBitTypedefStatement() throws IOException, ParserException {
116
117 YangNode node = manager.getDataModel("src/test/resources/BitTypedefStatement.yang");
118
119 // Check whether the data model tree returned is of type module.
120 assertThat((node instanceof YangModule), is(true));
121
122 // Check whether the node type is set properly to module.
123 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
124
125 // Check whether the module name is set correctly.
126 YangModule yangNode = (YangModule) node;
127 assertThat(yangNode.getName(), is("Test"));
128
129 YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
130 assertThat(typedef.getName(), is("type15"));
131
132 YangType type = typedef.getTypeList().iterator().next();
133 assertThat(type.getDataType(), is(YangDataTypes.BITS));
134 assertThat(type.getDataTypeName(), is("bits"));
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530135
136 // Check bit name map
137 Map<String, YangBit> bitNameMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitNameMap();
138 assertThat(bitNameMap.size(), is(3));
139 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
140 String bitName = element.getKey();
141 YangBit yangBit = element.getValue();
142 if (bitName.equals("disable-nagle")) {
143 assertThat(yangBit.getPosition(), is(0));
144 } else if (bitName.equals("auto-sense-speed")) {
145 assertThat(yangBit.getPosition(), is(1));
146 } else if (bitName.equals("Mb-only")) {
147 assertThat(yangBit.getPosition(), is(2));
148 } else {
149 throw new IOException("Invalid bit name: " + bitName);
150 }
151 }
152
153 // Check bit position map
154 Map<Integer, YangBit> bitPositionMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitPositionMap();
155 assertThat(bitPositionMap.size(), is(3));
156 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
157 int position = element.getKey();
158 YangBit yangBit = element.getValue();
159 if (position == 0) {
160 assertThat(yangBit.getBitName(), is("disable-nagle"));
161 } else if (position == 1) {
162 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
163 } else if (position == 2) {
164 assertThat(yangBit.getBitName(), is("Mb-only"));
165 } else {
166 throw new IOException("Invalid bit position: " + position);
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530167 }
168 }
169 }
170
171 /**
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530172 * Checks bit statement with typedef with referred leaf.
173 */
174 @Test
175 public void processBitTypedefReferredLeafStatement() throws IOException, ParserException {
176
177 YangNode node = manager.getDataModel("src/test/resources/BitTypedefReferredLeafStatement.yang");
178
179 // Check whether the data model tree returned is of type module.
180 assertThat((node instanceof YangModule), is(true));
181
182 // Check whether the node type is set properly to module.
183 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
184
185 // Check whether the module name is set correctly.
186 YangModule yangNode = (YangModule) node;
187 assertThat(yangNode.getName(), is("Test"));
188
189 YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
190 assertThat(typedef.getName(), is("topBits"));
191
192 YangType type = typedef.getTypeList().iterator().next();
193 assertThat(type.getDataType(), is(YangDataTypes.BITS));
194 assertThat(type.getDataTypeName(), is("bits"));
195
196 // Check bit name map
197 Map<String, YangBit> bitNameMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitNameMap();
198 assertThat(bitNameMap.size(), is(3));
199 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
200 String bitName = element.getKey();
201 YangBit yangBit = element.getValue();
202 if (bitName.equals("disable-nagle")) {
203 assertThat(yangBit.getPosition(), is(0));
204 } else if (bitName.equals("auto-sense-speed")) {
205 assertThat(yangBit.getPosition(), is(1));
206 } else if (bitName.equals("Mb-only")) {
207 assertThat(yangBit.getPosition(), is(2));
208 } else {
209 throw new IOException("Invalid bit name: " + bitName);
210 }
211 }
212
213 // Check bit position map
214 Map<Integer, YangBit> bitPositionMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitPositionMap();
215 assertThat(bitPositionMap.size(), is(3));
216 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
217 int position = element.getKey();
218 YangBit yangBit = element.getValue();
219 if (position == 0) {
220 assertThat(yangBit.getBitName(), is("disable-nagle"));
221 } else if (position == 1) {
222 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
223 } else if (position == 2) {
224 assertThat(yangBit.getBitName(), is("Mb-only"));
225 } else {
226 throw new IOException("Invalid bit position: " + position);
227 }
228 }
229
230 // Check leaf reffered typedef
231 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
232 YangLeaf leafInfo = leafIterator.next();
233
234 assertThat(leafInfo.getName(), is("myBits"));
235 assertThat(leafInfo.getDataType().getDataTypeName(), is("topBits"));
236 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
237 YangType<YangDerivedInfo> typeDerived = (YangType<YangDerivedInfo>) leafInfo.getDataType();
238 YangDerivedInfo derivedInfo = (YangDerivedInfo) typeDerived.getDataTypeExtendedInfo();
239 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
240 assertThat(prevTypedef.getName(), is("topBits"));
241 YangType topType = prevTypedef.getTypeList().iterator().next();
242 assertThat(topType.getDataType(), is(YangDataTypes.BITS));
243 assertThat(topType.getDataTypeName(), is("bits"));
244 YangType<YangBits> typeBits = (YangType<YangBits>) topType;
245 YangBits bits = typeBits.getDataTypeExtendedInfo();
246
247 // Check bit name map
248 bitNameMap = bits.getBitNameMap();
249 assertThat(bitNameMap.size(), is(3));
250 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
251 String bitName = element.getKey();
252 YangBit yangBit = element.getValue();
253 if (bitName.equals("disable-nagle")) {
254 assertThat(yangBit.getPosition(), is(0));
255 } else if (bitName.equals("auto-sense-speed")) {
256 assertThat(yangBit.getPosition(), is(1));
257 } else if (bitName.equals("Mb-only")) {
258 assertThat(yangBit.getPosition(), is(2));
259 } else {
260 throw new IOException("Invalid bit name: " + bitName);
261 }
262 }
263
264 // Check bit position map
265 bitPositionMap = bits.getBitPositionMap();
266 assertThat(bitPositionMap.size(), is(3));
267 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
268 int position = element.getKey();
269 YangBit yangBit = element.getValue();
270 if (position == 0) {
271 assertThat(yangBit.getBitName(), is("disable-nagle"));
272 } else if (position == 1) {
273 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
274 } else if (position == 2) {
275 assertThat(yangBit.getBitName(), is("Mb-only"));
276 } else {
277 throw new IOException("Invalid bit position: " + position);
278 }
279 }
280 }
281
282 /**
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530283 * Checks bit statement with union.
284 */
285 @Test
286 public void processBitUnionStatement() throws IOException, ParserException {
287
288 YangNode node = manager.getDataModel("src/test/resources/BitUnionStatement.yang");
289
290 // Check whether the data model tree returned is of type module.
291 assertThat((node instanceof YangModule), is(true));
292
293 // Check whether the node type is set properly to module.
294 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
295
296 // Check whether the module name is set correctly.
297 YangModule yangNode = (YangModule) node;
298 assertThat(yangNode.getName(), is("Test"));
299
300 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
301 YangLeaf leafInfo = leafIterator.next();
302
303 assertThat(leafInfo.getName(), is("type15"));
304
305 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION));
306 assertThat(leafInfo.getDataType().getDataTypeName(), is("union"));
307
308 YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo();
309
310 List<YangType<?>> typeList = yangUnion.getTypeList();
311 ListIterator<YangType<?>> typeListIterator = typeList.listIterator();
312 YangType<?> yangType = typeListIterator.next();
313
314 assertThat(yangType.getDataType(), is(YangDataTypes.BITS));
315 assertThat(yangType.getDataTypeName(), is("bits"));
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530316
317 // Check bit name map
318 Map<String, YangBit> bitNameMap = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitNameMap();
319 assertThat(bitNameMap.size(), is(3));
320 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
321 String bitName = element.getKey();
322 YangBit yangBit = element.getValue();
323 if (bitName.equals("disable-nagle")) {
324 assertThat(yangBit.getPosition(), is(0));
325 } else if (bitName.equals("auto-sense-speed")) {
326 assertThat(yangBit.getPosition(), is(1));
327 } else if (bitName.equals("Mb-only")) {
328 assertThat(yangBit.getPosition(), is(2));
329 } else {
330 throw new IOException("Invalid bit name: " + bitName);
331 }
332 }
333
334 // Check bit position map
335 Map<Integer, YangBit> bitPositionMap = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitPositionMap();
336 assertThat(bitPositionMap.size(), is(3));
337 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
338 int position = element.getKey();
339 YangBit yangBit = element.getValue();
340 if (position == 0) {
341 assertThat(yangBit.getBitName(), is("disable-nagle"));
342 } else if (position == 1) {
343 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
344 } else if (position == 2) {
345 assertThat(yangBit.getBitName(), is("Mb-only"));
346 } else {
347 throw new IOException("Invalid bit position: " + position);
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530348 }
349 }
350 }
351
352 /**
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530353 * Checks if enum with same name is not allowed.
354 */
355 @Test(expected = ParserException.class)
356 public void processBitWithDuplicateName() throws IOException, ParserException {
357
358 YangNode node = manager.getDataModel("src/test/resources/BitWithDuplicateName.yang");
359 }
360}