blob: c234feeb2558f449daf27250f7dfd2e2238fe8ff [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
19import org.junit.Rule;
20import org.junit.Test;
21
22import org.junit.rules.ExpectedException;
23import org.onosproject.yangutils.datamodel.YangLeaf;
24import org.onosproject.yangutils.datamodel.YangModule;
25import org.onosproject.yangutils.datamodel.YangNode;
26import org.onosproject.yangutils.datamodel.YangNodeType;
27import org.onosproject.yangutils.datamodel.YangDataTypes;
28import org.onosproject.yangutils.datamodel.YangStatusType;
29import org.onosproject.yangutils.datamodel.YangContainer;
30import org.onosproject.yangutils.datamodel.YangList;
31import org.onosproject.yangutils.datamodel.YangLeafList;
32import org.onosproject.yangutils.parser.exceptions.ParserException;
33import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34
35import java.io.IOException;
36import java.util.ListIterator;
37
38import static org.hamcrest.MatcherAssert.assertThat;
39import static org.hamcrest.core.Is.is;
40
41/**
42 * Test cases for config listener.
43 */
44public class ConfigListenerTest {
45
46 @Rule
47 public ExpectedException thrown = ExpectedException.none();
48
49 private final YangUtilsParserManager manager = new YangUtilsParserManager();
50
51 /**
52 * Checks valid config statement.
53 */
54 @Test
55 public void processConfigTrue() throws IOException, ParserException {
56
57 YangNode node = manager.getDataModel("src/test/resources/ConfigTrue.yang");
58
59 // Check whether the data model tree returned is of type module.
60 assertThat((node instanceof YangModule), is(true));
61
62 // Check whether the node type is set properly to module.
63 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
64
65 // Check whether the module name is set correctly.
66 YangModule yangNode = (YangModule) node;
67 assertThat(yangNode.getName(), is("Test"));
68
69 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
70 YangLeaf leafInfo = leafIterator.next();
71
72 // Check whether the Config value is set correctly.
73 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
74 assertThat(leafInfo.isConfig(), is(true));
75 }
76
77 /**
78 * Checks valid config statement.
79 */
80 @Test
81 public void processConfigFalse() throws IOException, ParserException {
82
83 YangNode node = manager.getDataModel("src/test/resources/ConfigFalse.yang");
84
85 // Check whether the data model tree returned is of type module.
86 assertThat((node instanceof YangModule), is(true));
87
88 // Check whether the node type is set properly to module.
89 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
90
91 // Check whether the module name is set correctly.
92 YangModule yangNode = (YangModule) node;
93 assertThat(yangNode.getName(), is("Test"));
94
95 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
96 YangLeaf leafInfo = leafIterator.next();
97
98 // Check whether the Config value is set correctly.
99 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
100 assertThat(leafInfo.isConfig(), is(false));
101 }
102
103 /**
104 * Checks invalid config statement and expects parser exception.
105 */
106 @Test
107 public void processConfigWithoutStatementEnd() throws IOException, ParserException {
108 thrown.expect(ParserException.class);
109 thrown.expectMessage("missing ';' at '}'");
110 YangNode node = manager.getDataModel("src/test/resources/ConfigWithoutStatementEnd.yang");
111 }
112
113 /**
114 * Checks invalid config statement and expects parser exception.
115 */
116 @Test
117 public void processConfigInvalidValue() throws IOException, ParserException {
118 thrown.expect(ParserException.class);
119 thrown.expectMessage("mismatched input 'invalid' expecting {'false', 'true'}");
120 YangNode node = manager.getDataModel("src/test/resources/ConfigInvalidValue.yang");
121 }
122
123 /**
124 * Checks invalid config statement and expects parser exception.
125 */
126 @Test
127 public void processConfigEmptyValue() throws IOException, ParserException {
128 thrown.expect(ParserException.class);
129 thrown.expectMessage("missing {'false', 'true'} at ';'");
130 YangNode node = manager.getDataModel("src/test/resources/ConfigEmptyValue.yang");
131 }
132
133 /**
134 * Checks config statement as sub-statement of module.
135 */
136 @Test
137 public void processModuleSubStatementConfig() throws IOException, ParserException {
138 thrown.expect(ParserException.class);
139 thrown.expectMessage("mismatched input 'config' expecting {'augment', 'choice', 'contact', 'container'," +
140 " 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include', " +
141 "'leaf', 'leaf-list', 'list', 'namespace', 'notification', 'organization', 'prefix', 'reference'," +
142 " 'revision', 'rpc', 'typedef', 'uses', 'yang-version', '}'}");
143 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementConfig.yang");
144 }
145
146 /**
147 * Checks config statement as sub-statement of container.
148 */
149 @Test
150 public void processContainerSubStatementConfig() throws IOException, ParserException {
151
152 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementConfig.yang");
153
154 // Check whether the data model tree returned is of type module.
155 assertThat((node instanceof YangModule), is(true));
156
157 // Check whether the node type is set properly to module.
158 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
159
160 // Check whether the module name is set correctly.
161 YangModule yangNode = (YangModule) node;
162 assertThat(yangNode.getName(), is("Test"));
163
164 // Check whether the config value is set correctly.
165 YangContainer container = (YangContainer) yangNode.getChild();
166 assertThat(container.getName(), is("valid"));
167 assertThat(container.isConfig(), is(true));
168
169 // Check whether leaf properties as set correctly.
170 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
171 YangLeaf leafInfo = leafIterator.next();
172
173 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
174 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
175 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
176 assertThat(leafInfo.getUnits(), is("\"seconds\""));
177 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
178 assertThat(leafInfo.isMandatory(), is(true));
179 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
180 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
181 }
182
183 /**
184 * Checks config statement as sub-statement of list.
185 */
186 @Test
187 public void processListSubStatementConfig() throws IOException, ParserException {
188
189 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementConfig.yang");
190
191 assertThat((node instanceof YangModule), is(true));
192
193 // Check whether the node type is set properly to module.
194 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
195
196 // Check whether the module name is set correctly.
197 YangModule yangNode = (YangModule) node;
198 assertThat(yangNode.getName(), is("Test"));
199
200 // Check whether the list is child of module and config value is set.
201 YangList yangList = (YangList) yangNode.getChild();
202 assertThat(yangList.getName(), is("valid"));
203 assertThat(yangList.isConfig(), is(true));
204
205 // Check whether leaf properties as set correctly.
206 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
207 YangLeaf leafInfo = leafIterator.next();
208
209 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
210 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
211 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
212 assertThat(leafInfo.getUnits(), is("\"seconds\""));
213 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
214 assertThat(leafInfo.isMandatory(), is(true));
215 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
216 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
217 }
218
219 /**
220 * Checks valid config statement as sub-statement of leaf-list.
221 */
222 @Test
223 public void processLeafListSubStatementConfig() throws IOException, ParserException {
224
225 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementConfig.yang");
226
227 // Check whether the data model tree returned is of type module.
228 assertThat((node instanceof YangModule), is(true));
229
230 // Check whether the node type is set properly to module.
231 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
232
233 // Check whether the module name is set correctly.
234 YangModule yangNode = (YangModule) node;
235 assertThat(yangNode.getName(), is("Test"));
236
237 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
238 YangLeafList leafListInfo = leafListIterator.next();
239
240 // Check whether config value is set correctly.
241 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
242 assertThat(leafListInfo.isConfig(), is(true));
243 }
244}