blob: ef25c97d0e22e963ec4db5db6d6117603b4ebf27 [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;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053024import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangModule;
27import org.onosproject.yangutils.datamodel.YangNode;
28import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053029import org.onosproject.yangutils.datamodel.YangType;
30import org.onosproject.yangutils.datamodel.YangTypeDef;
31import org.onosproject.yangutils.datamodel.YangUnion;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053032import org.onosproject.yangutils.parser.exceptions.ParserException;
33import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34
35import java.io.IOException;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053036import java.util.List;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053037import java.util.ListIterator;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053038import java.util.Map;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053039
40/**
41 * Test cases for bit listener.
42 */
43public class BitListenerTest {
44
45 private final YangUtilsParserManager manager = new YangUtilsParserManager();
46
47 /**
48 * Checks bit statement without position.
49 */
50 @Test
51 public void processBitTypeStatement() throws IOException, ParserException {
52
53 YangNode node = manager.getDataModel("src/test/resources/BitTypeStatement.yang");
54
55 // Check whether the data model tree returned is of type module.
56 assertThat((node instanceof YangModule), is(true));
57
58 // Check whether the node type is set properly to module.
59 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
60
61 // Check whether the module name is set correctly.
62 YangModule yangNode = (YangModule) node;
63 assertThat(yangNode.getName(), is("Test"));
64
65 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
66 YangLeaf leafInfo = leafIterator.next();
67
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053068 assertThat(leafInfo.getName(), is("mybits"));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053069 assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
70 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
71 assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
72 is("mybits"));
73
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053074 // Check bit name map
75 Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
76 assertThat(bitNameMap.size(), is(3));
77 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
78 String bitName = element.getKey();
79 YangBit yangBit = element.getValue();
80 if (bitName.equals("disable-nagle")) {
81 assertThat(yangBit.getPosition(), is(0));
82 } else if (bitName.equals("auto-sense-speed")) {
83 assertThat(yangBit.getPosition(), is(1));
84 } else if (bitName.equals("Ten-Mb-only")) {
85 assertThat(yangBit.getPosition(), is(2));
86 } else {
87 throw new IOException("Invalid bit name: " + bitName);
88 }
89 }
90
91 // Check bit position map
92 Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
93 .getBitPositionMap();
94 assertThat(bitPositionMap.size(), is(3));
95 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
96 int position = element.getKey();
97 YangBit yangBit = element.getValue();
98 if (position == 0) {
99 assertThat(yangBit.getBitName(), is("disable-nagle"));
100 } else if (position == 1) {
101 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
102 } else if (position == 2) {
103 assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
104 } else {
105 throw new IOException("Invalid bit position: " + position);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530106 }
107 }
108 }
109
110 /**
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530111 * Checks bit statement with typedef.
112 */
113 @Test
114 public void processBitTypedefStatement() throws IOException, ParserException {
115
116 YangNode node = manager.getDataModel("src/test/resources/BitTypedefStatement.yang");
117
118 // Check whether the data model tree returned is of type module.
119 assertThat((node instanceof YangModule), is(true));
120
121 // Check whether the node type is set properly to module.
122 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
123
124 // Check whether the module name is set correctly.
125 YangModule yangNode = (YangModule) node;
126 assertThat(yangNode.getName(), is("Test"));
127
128 YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
129 assertThat(typedef.getName(), is("type15"));
130
131 YangType type = typedef.getTypeList().iterator().next();
132 assertThat(type.getDataType(), is(YangDataTypes.BITS));
133 assertThat(type.getDataTypeName(), is("bits"));
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530134
135 // Check bit name map
136 Map<String, YangBit> bitNameMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitNameMap();
137 assertThat(bitNameMap.size(), is(3));
138 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
139 String bitName = element.getKey();
140 YangBit yangBit = element.getValue();
141 if (bitName.equals("disable-nagle")) {
142 assertThat(yangBit.getPosition(), is(0));
143 } else if (bitName.equals("auto-sense-speed")) {
144 assertThat(yangBit.getPosition(), is(1));
145 } else if (bitName.equals("Mb-only")) {
146 assertThat(yangBit.getPosition(), is(2));
147 } else {
148 throw new IOException("Invalid bit name: " + bitName);
149 }
150 }
151
152 // Check bit position map
153 Map<Integer, YangBit> bitPositionMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitPositionMap();
154 assertThat(bitPositionMap.size(), is(3));
155 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
156 int position = element.getKey();
157 YangBit yangBit = element.getValue();
158 if (position == 0) {
159 assertThat(yangBit.getBitName(), is("disable-nagle"));
160 } else if (position == 1) {
161 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
162 } else if (position == 2) {
163 assertThat(yangBit.getBitName(), is("Mb-only"));
164 } else {
165 throw new IOException("Invalid bit position: " + position);
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530166 }
167 }
168 }
169
170 /**
171 * Checks bit statement with union.
172 */
173 @Test
174 public void processBitUnionStatement() throws IOException, ParserException {
175
176 YangNode node = manager.getDataModel("src/test/resources/BitUnionStatement.yang");
177
178 // Check whether the data model tree returned is of type module.
179 assertThat((node instanceof YangModule), is(true));
180
181 // Check whether the node type is set properly to module.
182 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
183
184 // Check whether the module name is set correctly.
185 YangModule yangNode = (YangModule) node;
186 assertThat(yangNode.getName(), is("Test"));
187
188 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
189 YangLeaf leafInfo = leafIterator.next();
190
191 assertThat(leafInfo.getName(), is("type15"));
192
193 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION));
194 assertThat(leafInfo.getDataType().getDataTypeName(), is("union"));
195
196 YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo();
197
198 List<YangType<?>> typeList = yangUnion.getTypeList();
199 ListIterator<YangType<?>> typeListIterator = typeList.listIterator();
200 YangType<?> yangType = typeListIterator.next();
201
202 assertThat(yangType.getDataType(), is(YangDataTypes.BITS));
203 assertThat(yangType.getDataTypeName(), is("bits"));
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530204
205 // Check bit name map
206 Map<String, YangBit> bitNameMap = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitNameMap();
207 assertThat(bitNameMap.size(), is(3));
208 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
209 String bitName = element.getKey();
210 YangBit yangBit = element.getValue();
211 if (bitName.equals("disable-nagle")) {
212 assertThat(yangBit.getPosition(), is(0));
213 } else if (bitName.equals("auto-sense-speed")) {
214 assertThat(yangBit.getPosition(), is(1));
215 } else if (bitName.equals("Mb-only")) {
216 assertThat(yangBit.getPosition(), is(2));
217 } else {
218 throw new IOException("Invalid bit name: " + bitName);
219 }
220 }
221
222 // Check bit position map
223 Map<Integer, YangBit> bitPositionMap = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitPositionMap();
224 assertThat(bitPositionMap.size(), is(3));
225 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
226 int position = element.getKey();
227 YangBit yangBit = element.getValue();
228 if (position == 0) {
229 assertThat(yangBit.getBitName(), is("disable-nagle"));
230 } else if (position == 1) {
231 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
232 } else if (position == 2) {
233 assertThat(yangBit.getBitName(), is("Mb-only"));
234 } else {
235 throw new IOException("Invalid bit position: " + position);
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530236 }
237 }
238 }
239
240 /**
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530241 * Checks if enum with same name is not allowed.
242 */
243 @Test(expected = ParserException.class)
244 public void processBitWithDuplicateName() throws IOException, ParserException {
245
246 YangNode node = manager.getDataModel("src/test/resources/BitWithDuplicateName.yang");
247 }
248}