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