blob: c398569df96c3715725cc4aaa820070397121b45 [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 case for reference listener.
42 */
43public class ReferenceListenerTest {
44
45 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
48 private final YangUtilsParserManager manager = new YangUtilsParserManager();
49
50 /**
51 * Checks valid reference statement.
52 */
53 @Test
54 public void processReferenceStatement() throws IOException, ParserException {
55
56 YangNode node = manager.getDataModel("src/test/resources/ReferenceStatement.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 reference is set correctly.
72 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
73 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
74 }
75
76 /**
77 * Checks whether exception is thrown for invalid reference statement.
78 */
79 @Test
80 public void processReferenceWithoutStatementEnd() throws IOException, ParserException {
81 thrown.expect(ParserException.class);
82 thrown.expectMessage("mismatched input '}' expecting {';', '+'}");
83 YangNode node = manager.getDataModel("src/test/resources/ReferenceWithoutStatementEnd.yang");
84 }
85
86 /**
87 * Checks valid reference statement under module.
88 */
89 @Test
90 public void processModuleSubStatementReference() throws IOException, ParserException {
91
92 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementReference.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 reference is set correctly.
105 assertThat(yangNode.getReference(), is("\"RFC 6020\""));
106 }
107
108 /**
109 * Checks valid reference statement under module.
110 */
111 @Test
112 public void processReferenceEmptyStatement() throws IOException, ParserException {
113
114 YangNode node = manager.getDataModel("src/test/resources/ReferenceEmptyStatement.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 reference is set correctly.
127 assertThat(yangNode.getReference(), is("\"\""));
128 }
129
130 /**
131 * Checks valid reference statement as sub-statement of revision.
132 */
133 @Test
134 public void processRevisionSubStatementReference() throws IOException, ParserException {
135
136 YangNode node = manager.getDataModel("src/test/resources/RevisionSubStatementReference.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 reference is set correctly.
149 assertThat(yangNode.getRevision().getReference(), is("\"revision reference\""));
150 }
151
152 /**
153 * Checks reference statement as sub-statement of container.
154 */
155 @Test
156 public void processContainerSubStatementReference() throws IOException, ParserException {
157
158 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementReference.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 reference value is set correctly.
171 YangContainer container = (YangContainer) yangNode.getChild();
172 assertThat(container.getName(), is("valid"));
173 assertThat(container.getReference(), is("\"container reference\""));
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
179 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
180 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
181 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 reference statement as sub-statement of list.
191 */
192 @Test
193 public void processListSubStatementReference() throws IOException, ParserException {
194
195 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementReference.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.getReference(), is("\"list reference\""));
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
216 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
217 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
218 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 reference statement as sub-statement of leaf-list.
228 */
229 @Test
230 public void processLeafListSubStatementReference() throws IOException, ParserException {
231
232 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementReference.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.
248 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
249 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
250 }
251}