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