blob: f3042bbcde724f9af183e01d84bd5b92a7bc1701 [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
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053019import java.io.IOException;
20import java.util.ListIterator;
21
Vidyashree Rama49abe712016-02-13 22:22:12 +053022import org.junit.Rule;
23import org.junit.Test;
Vidyashree Rama49abe712016-02-13 22:22:12 +053024import org.junit.rules.ExpectedException;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025import org.onosproject.yangutils.datamodel.YangContainer;
26import org.onosproject.yangutils.datamodel.YangDataTypes;
Vidyashree Rama49abe712016-02-13 22:22:12 +053027import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053028import org.onosproject.yangutils.datamodel.YangLeafList;
29import org.onosproject.yangutils.datamodel.YangList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053030import org.onosproject.yangutils.datamodel.YangModule;
31import org.onosproject.yangutils.datamodel.YangNode;
32import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053033import org.onosproject.yangutils.datamodel.YangStatusType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053034import org.onosproject.yangutils.parser.exceptions.ParserException;
35import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
36
Vidyashree Rama49abe712016-02-13 22:22:12 +053037import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.core.Is.is;
39
40/**
41 * Test cases for description listener.
42 */
43public class DescriptionListenerTest {
44
45 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
48 private final YangUtilsParserManager manager = new YangUtilsParserManager();
49
50 /**
51 * Checks valid description statement.
52 */
53 @Test
54 public void processDescriptionValidStatement() throws IOException, ParserException {
55
56 YangNode node = manager.getDataModel("src/test/resources/DescriptionValidStatement.yang");
57
58 // Check whether the data model tree returned is of type module.
59 assertThat((node instanceof YangModule), is(true));
60
61 // Check whether the node type is set properly to module.
62 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
63
64 // Check whether the module name is set correctly.
65 YangModule yangNode = (YangModule) node;
66 assertThat(yangNode.getName(), is("Test"));
67
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053068 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
69 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053070
71 // Check whether the description is set correctly.
72 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
73 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
74 }
75
76 /**
77 * Checks whether exception is thrown for invalid description statement.
78 */
79 @Test
80 public void processDescriptionWithoutStatementEnd() throws IOException, ParserException {
81 thrown.expect(ParserException.class);
82 thrown.expectMessage("extraneous input '}' expecting {';', '+'}");
83 YangNode node = manager.getDataModel("src/test/resources/DescriptionWithoutStatementEnd.yang");
84 }
85
86 /**
87 * Checks valid description statement as sub-statement of module.
88 */
89 @Test
90 public void processModuleSubStatementDescription() throws IOException, ParserException {
91
92 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementDescription.yang");
93
94 // Check whether the data model tree returned is of type module.
95 assertThat((node instanceof YangModule), is(true));
96
97 // Check whether the node type is set properly to module.
98 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
99
100 // Check whether the module name is set correctly.
101 YangModule yangNode = (YangModule) node;
102 assertThat(yangNode.getName(), is("Test"));
103
104 // Check whether the description is set correctly.
105 assertThat(yangNode.getDescription(), is("\"Interval before a route is declared invalid\""));
106 }
107
108 /**
109 * Checks valid description statement as sub-statement of module.
110 */
111 @Test
112 public void processDescriptionEmptyStatement() throws IOException, ParserException {
113
114 YangNode node = manager.getDataModel("src/test/resources/DescriptionEmptyStatement.yang");
115
116 // Check whether the data model tree returned is of type module.
117 assertThat((node instanceof YangModule), is(true));
118
119 // Check whether the node type is set properly to module.
120 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
121
122 // Check whether the module name is set correctly.
123 YangModule yangNode = (YangModule) node;
124 assertThat(yangNode.getName(), is("Test"));
125
126 // Check whether the description is set correctly.
127 assertThat(yangNode.getDescription(), is("\"\""));
128 }
129
130 /**
131 * Checks valid description statement as sub-statement of revision.
132 */
133 @Test
134 public void processRevisionSubStatementRevision() throws IOException, ParserException {
135
136 YangNode node = manager.getDataModel("src/test/resources/RevisionSubStatementRevision.yang");
137
138 // Check whether the data model tree returned is of type module.
139 assertThat((node instanceof YangModule), is(true));
140
141 // Check whether the node type is set properly to module.
142 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
143
144 // Check whether the module name is set correctly.
145 YangModule yangNode = (YangModule) node;
146 assertThat(yangNode.getName(), is("Test"));
147
148 // Check whether the description is set correctly.
149 assertThat(yangNode.getDescription(), is("\"module description\""));
150 assertThat(yangNode.getRevision().getDescription(), is("\"revision description\""));
151 }
152
153 /**
154 * Checks description statement as sub-statement of container.
155 */
156 @Test
157 public void processContainerSubStatementDescription() throws IOException, ParserException {
158
159 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementDescription.yang");
160
161 // Check whether the data model tree returned is of type module.
162 assertThat((node instanceof YangModule), is(true));
163
164 // Check whether the node type is set properly to module.
165 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
166
167 // Check whether the module name is set correctly.
168 YangModule yangNode = (YangModule) node;
169 assertThat(yangNode.getName(), is("Test"));
170
171 // Check whether the description value is set correctly.
172 YangContainer container = (YangContainer) yangNode.getChild();
173 assertThat(container.getName(), is("valid"));
174 assertThat(container.getDescription(), is("\"container description\""));
175
176 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530177 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
178 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530179
180 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
181 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
182 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
183 assertThat(leafInfo.getUnits(), is("\"seconds\""));
184 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
185 assertThat(leafInfo.isMandatory(), is(true));
186 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
187 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
188 }
189
190 /**
191 * Checks description statement as sub-statement of list.
192 */
193 @Test
194 public void processListSubStatementDescription() throws IOException, ParserException {
195
196 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementDescription.yang");
197
198 assertThat((node instanceof YangModule), is(true));
199
200 // Check whether the node type is set properly to module.
201 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
202
203 // Check whether the module name is set correctly.
204 YangModule yangNode = (YangModule) node;
205 assertThat(yangNode.getName(), is("Test"));
206
207 // Check whether the list is child of module and description value is set correctly.
208 YangList yangList = (YangList) yangNode.getChild();
209 assertThat(yangList.getName(), is("valid"));
210 assertThat(yangList.isConfig(), is(true));
211 assertThat(yangList.getDescription(), is("\"list description\""));
212
213 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530214 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
215 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530216
217 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
218 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
219 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
220 assertThat(leafInfo.getUnits(), is("\"seconds\""));
221 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
222 assertThat(leafInfo.isMandatory(), is(true));
223 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
224 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
225 }
226
227 /**
228 * Checks valid description statement as sub-statement of leaf-list.
229 */
230 @Test
231 public void processLeafListSubStatementDescription() throws IOException, ParserException {
232
233 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementDescription.yang");
234
235 // Check whether the data model tree returned is of type module.
236 assertThat((node instanceof YangModule), is(true));
237
238 // Check whether the node type is set properly to module.
239 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
240
241 // Check whether the module name is set correctly.
242 YangModule yangNode = (YangModule) node;
243 assertThat(yangNode.getName(), is("Test"));
244
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530245 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
246 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530247
248 // Check whether description value is set correctly.
249 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
250 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
251 }
252}