blob: 8a8d02abaabf4c226bb851b6bd0a70dd40dd1699 [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;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.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 case for reference listener.
41 */
42public class ReferenceListenerTest {
43
44 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
47 private final YangUtilsParserManager manager = new YangUtilsParserManager();
48
49 /**
50 * Checks valid reference statement.
51 */
52 @Test
53 public void processReferenceStatement() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/ReferenceStatement.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 reference 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.getReference(), is("\"RFC 6020\""));
73 }
74
75 /**
76 * Checks whether exception is thrown for invalid reference statement.
77 */
78 @Test
79 public void processReferenceWithoutStatementEnd() throws IOException, ParserException {
80 thrown.expect(ParserException.class);
81 thrown.expectMessage("mismatched input '}' expecting {';', '+'}");
82 YangNode node = manager.getDataModel("src/test/resources/ReferenceWithoutStatementEnd.yang");
83 }
84
85 /**
86 * Checks valid reference statement under module.
87 */
88 @Test
89 public void processModuleSubStatementReference() throws IOException, ParserException {
90
91 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementReference.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 reference is set correctly.
104 assertThat(yangNode.getReference(), is("\"RFC 6020\""));
105 }
106
107 /**
108 * Checks valid reference statement under module.
109 */
110 @Test
111 public void processReferenceEmptyStatement() throws IOException, ParserException {
112
113 YangNode node = manager.getDataModel("src/test/resources/ReferenceEmptyStatement.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 reference is set correctly.
126 assertThat(yangNode.getReference(), is("\"\""));
127 }
128
129 /**
130 * Checks valid reference statement as sub-statement of revision.
131 */
132 @Test
133 public void processRevisionSubStatementReference() throws IOException, ParserException {
134
135 YangNode node = manager.getDataModel("src/test/resources/RevisionSubStatementReference.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 reference is set correctly.
148 assertThat(yangNode.getRevision().getReference(), is("\"revision reference\""));
149 }
150
151 /**
152 * Checks reference statement as sub-statement of container.
153 */
154 @Test
155 public void processContainerSubStatementReference() throws IOException, ParserException {
156
157 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementReference.yang");
158
159 // Check whether the data model tree returned is of type module.
160 assertThat((node instanceof YangModule), is(true));
161
162 // Check whether the node type is set properly to module.
163 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
164
165 // Check whether the module name is set correctly.
166 YangModule yangNode = (YangModule) node;
167 assertThat(yangNode.getName(), is("Test"));
168
169 // Check whether the reference value is set correctly.
170 YangContainer container = (YangContainer) yangNode.getChild();
171 assertThat(container.getName(), is("valid"));
172 assertThat(container.getReference(), is("\"container reference\""));
173
174 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530175 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
176 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530177
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530178 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530179 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530180 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
181 assertThat(leafInfo.getUnits(), is("\"seconds\""));
182 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
183 assertThat(leafInfo.isMandatory(), is(true));
184 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
185 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
186 }
187
188 /**
189 * Checks reference statement as sub-statement of list.
190 */
191 @Test
192 public void processListSubStatementReference() throws IOException, ParserException {
193
194 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementReference.yang");
195
196 assertThat((node instanceof YangModule), is(true));
197
198 // Check whether the node type is set properly to module.
199 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
200
201 // Check whether the module name is set correctly.
202 YangModule yangNode = (YangModule) node;
203 assertThat(yangNode.getName(), is("Test"));
204
205 // Check whether the list is child of module and description value is set correctly.
206 YangList yangList = (YangList) yangNode.getChild();
207 assertThat(yangList.getName(), is("valid"));
208 assertThat(yangList.isConfig(), is(true));
209 assertThat(yangList.getReference(), is("\"list reference\""));
210
211 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530212 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
213 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530214
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530215 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530216 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530217 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
218 assertThat(leafInfo.getUnits(), is("\"seconds\""));
219 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
220 assertThat(leafInfo.isMandatory(), is(true));
221 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
222 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
223 }
224
225 /**
226 * Checks valid reference statement as sub-statement of leaf-list.
227 */
228 @Test
229 public void processLeafListSubStatementReference() throws IOException, ParserException {
230
231 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementReference.yang");
232
233 // Check whether the data model tree returned is of type module.
234 assertThat((node instanceof YangModule), is(true));
235
236 // Check whether the node type is set properly to module.
237 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
238
239 // Check whether the module name is set correctly.
240 YangModule yangNode = (YangModule) node;
241 assertThat(yangNode.getName(), is("Test"));
242
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530243 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
244 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530245
246 // Check whether description value is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530247 assertThat(leafListInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530248 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
249 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530250}