blob: 7b598940a23127b28af9a82ce0fddfe59557c623 [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;
29import org.onosproject.yangutils.parser.exceptions.ParserException;
30import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
31
32import java.io.IOException;
33import java.util.ListIterator;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053034import java.util.Map;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053035
36/**
37 * Test cases for position listener.
38 */
39public class PositionListenerTest {
40
41 private final YangUtilsParserManager manager = new YangUtilsParserManager();
42
43 /**
44 * Checks explicitly configured value.
45 */
46 @Test
47 public void processPositionStatement() throws IOException, ParserException {
48
49 YangNode node = manager.getDataModel("src/test/resources/PositionStatement.yang");
50
51 // Check whether the data model tree returned is of type module.
52 assertThat((node instanceof YangModule), is(true));
53
54 // Check whether the node type is set properly to module.
55 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
56
57 // Check whether the module name is set correctly.
58 YangModule yangNode = (YangModule) node;
59 assertThat(yangNode.getName(), is("Test"));
60
61 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
62 YangLeaf leafInfo = leafIterator.next();
63
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053064 assertThat(leafInfo.getName(), is("mybits"));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053065 assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
66 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
67 assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
68 is("mybits"));
69
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053070 // Check bit name map
71 Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
72 assertThat(bitNameMap.size(), is(3));
73 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
74 String bitName = element.getKey();
75 YangBit yangBit = element.getValue();
76 if (bitName.equals("disable-nagle")) {
77 assertThat(yangBit.getPosition(), is(0));
78 } else if (bitName.equals("auto-sense-speed")) {
79 assertThat(yangBit.getPosition(), is(1));
80 } else if (bitName.equals("Ten-Mb-only")) {
81 assertThat(yangBit.getPosition(), is(2));
82 } else {
83 throw new IOException("Invalid bit name: " + bitName);
84 }
85 }
86
87 // Check bit position map
88 Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
89 .getBitPositionMap();
90 assertThat(bitPositionMap.size(), is(3));
91 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
92 int position = element.getKey();
93 YangBit yangBit = element.getValue();
94 if (position == 0) {
95 assertThat(yangBit.getBitName(), is("disable-nagle"));
96 } else if (position == 1) {
97 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
98 } else if (position == 2) {
99 assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
100 } else {
101 throw new IOException("Invalid bit position: " + position);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530102 }
103 }
104 }
105
106 /**
Vidyashree Rama468f8282016-03-04 19:08:35 +0530107 * Checks position value with double quotes.
108 */
109 @Test
110 public void processPositionWithDoubleQuotes() throws IOException, ParserException {
111
112 YangNode node = manager.getDataModel("src/test/resources/PositionWithDoubleQuotes.yang");
113
114 // Check whether the data model tree returned is of type module.
115 assertThat((node instanceof YangModule), is(true));
116
117 // Check whether the node type is set properly to module.
118 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
119
120 // Check whether the module name is set correctly.
121 YangModule yangNode = (YangModule) node;
122 assertThat(yangNode.getName(), is("Test"));
123
124 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
125 YangLeaf leafInfo = leafIterator.next();
126
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530127 assertThat(leafInfo.getName(), is("mybits"));
Vidyashree Rama468f8282016-03-04 19:08:35 +0530128 assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
129 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
130 assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
131 is("mybits"));
132
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530133 // Check bit name map
134 Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
135 assertThat(bitNameMap.size(), is(3));
136 for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
137 String bitName = element.getKey();
138 YangBit yangBit = element.getValue();
139 if (bitName.equals("disable-nagle")) {
140 assertThat(yangBit.getPosition(), is(0));
141 } else if (bitName.equals("auto-sense-speed")) {
142 assertThat(yangBit.getPosition(), is(1));
143 } else if (bitName.equals("Ten-Mb-only")) {
144 assertThat(yangBit.getPosition(), is(2));
145 } else {
146 throw new IOException("Invalid bit name: " + bitName);
147 }
148 }
149
150 // Check bit position map
151 Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
152 .getBitPositionMap();
153 assertThat(bitPositionMap.size(), is(3));
154 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
155 int position = element.getKey();
156 YangBit yangBit = element.getValue();
157 if (position == 0) {
158 assertThat(yangBit.getBitName(), is("disable-nagle"));
159 } else if (position == 1) {
160 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
161 } else if (position == 2) {
162 assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
163 } else {
164 throw new IOException("Invalid bit position: " + position);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530165 }
166 }
167 }
168
169 /**
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530170 * Checks explicit value and auto generated value.
171 */
172 @Test
173 public void processPositionImplicitAndExplicit() throws IOException, ParserException {
174
175 YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicit.yang");
176
177 // Check whether the data model tree returned is of type module.
178 assertThat((node instanceof YangModule), is(true));
179
180 // Check whether the node type is set properly to module.
181 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
182
183 // Check whether the module name is set correctly.
184 YangModule yangNode = (YangModule) node;
185 assertThat(yangNode.getName(), is("Test"));
186
187 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
188 YangLeaf leafInfo = leafIterator.next();
189
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530190 assertThat(leafInfo.getName(), is("mybits"));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530191 assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
192 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
193 assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
194 is("mybits"));
195
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530196 // Check bit name map
197 Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().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("Ten-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) leafInfo.getDataType().getDataTypeExtendedInfo())
215 .getBitPositionMap();
216 assertThat(bitPositionMap.size(), is(3));
217 for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
218 int position = element.getKey();
219 YangBit yangBit = element.getValue();
220 if (position == 0) {
221 assertThat(yangBit.getBitName(), is("disable-nagle"));
222 } else if (position == 1) {
223 assertThat(yangBit.getBitName(), is("auto-sense-speed"));
224 } else if (position == 2) {
225 assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
226 } else {
227 throw new IOException("Invalid bit position: " + position);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530228 }
229 }
230 }
231
232 /**
233 * Checks explicit value should not be repeated.
234 */
235 @Test(expected = ParserException.class)
236 public void processPositionDuplication() throws IOException, ParserException {
237
238 YangNode node = manager.getDataModel("src/test/resources/PositionDuplication.yang");
239 }
240
241 /**
242 * Checks explicit or auto generated value should not be repeated.
243 */
244 @Test(expected = ParserException.class)
245 public void processPositionImplicitAndExplicitDuplication() throws IOException, ParserException {
246
247 YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicitDuplication.yang");
248 }
249
250 /**
251 * Checks if negative value of position is not allowed.
252 */
253 @Test(expected = ParserException.class)
254 public void processPositionNegativeValue() throws IOException, ParserException {
255
256 YangNode node = manager.getDataModel("src/test/resources/PositionNegativeValue.yang");
257 }
258}