blob: b150f81f73396743899654accd883442f62c337c [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.YangLeafList;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053028import 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 testing leaf-list listener.
41 */
42public class LeafListListenerTest {
43
44 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
47 private final YangUtilsParserManager manager = new YangUtilsParserManager();
48
49 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053050 * Checks all the values of leaf-list sub-statements are set correctly.
Vidyashree Rama49abe712016-02-13 22:22:12 +053051 */
52 @Test
53 public void processLeafListSubStatements() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatements.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<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
68 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053069
70 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
71 assertThat(leafListInfo.getDataType().getDataTypeName(), is("\"uint16\""));
72 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
73 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
74 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
75 assertThat(leafListInfo.isConfig(), is(true));
76 assertThat(leafListInfo.getMaxElelements(), is(3));
77 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
78 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
79 }
80
81 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053082 * Checks whether exception is thrown when leaf-list identifier starts with
83 * digit.
Vidyashree Rama49abe712016-02-13 22:22:12 +053084 */
85 @Test
86 public void processLeafListInvalidIdentifier() throws IOException, ParserException {
87 thrown.expect(ParserException.class);
Vidyashree Rama468f8282016-03-04 19:08:35 +053088 thrown.expectMessage("YANG file error : leaf-list name 1invalid-interval is not valid.");
Vidyashree Rama49abe712016-02-13 22:22:12 +053089 YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidIdentifier.yang");
90 }
91
92 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053093 * Checks whether exception is thrown when leaf-list keyword is incorrect.
Vidyashree Rama49abe712016-02-13 22:22:12 +053094 */
95 @Test
96 public void processLeafListInvalidStatement() throws IOException, ParserException {
97 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +053098 thrown.expectMessage("mismatched input 'leaflist' expecting {'augment', 'choice', 'contact', 'container',"
99 + " 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include',"
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +0530100 + " 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
101 + " 'revision', 'rpc', 'typedef', 'uses', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530102 YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidStatement.yang");
103 }
104
105 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530106 * Checks whether exception is thrown when leaf-list keyword without Left
107 * brace as per grammar.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530108 */
109 @Test
110 public void processLeafListWithoutLeftBrace() throws IOException, ParserException {
111 thrown.expect(ParserException.class);
112 thrown.expectMessage("missing '{' at 'type'");
113 YangNode node = manager.getDataModel("src/test/resources/LeafListWithoutLeftBrace.yang");
114 }
115
116 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530117 * Checks whether exception is thrown when config statement cardinality is
118 * not as per grammar.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530119 */
120 @Test
121 public void processLeafListConfigInvalidCardinality() throws IOException, ParserException {
122 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530123 thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf-list " +
124 "invalid-interval\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530125 YangNode node = manager.getDataModel("src/test/resources/LeafListConfigInvalidCardinality.yang");
126 }
127
128 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530129 * Checks whether exception is thrown when units statement cardinality is
130 * not as per grammar.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530131 */
132 @Test
133 public void processLeafListUnitsInvalidCardinality() throws IOException, ParserException {
134 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530135 thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf-list invalid-interval\"");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530136 YangNode node = manager.getDataModel("src/test/resources/LeafListUnitsInvalidCardinality.yang");
137 }
138
139 /**
140 * Checks leaf-list statement as sub-statement of container.
141 */
142 @Test
143 public void processContainerSubStatementLeafList() throws IOException, ParserException {
144
145 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeafList.yang");
146
147 // Check whether the data model tree returned is of type module.
148 assertThat((node instanceof YangModule), is(true));
149
150 // Check whether the node type is set properly to module.
151 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
152
153 // Check whether the module name is set correctly.
154 YangModule yangNode = (YangModule) node;
155 assertThat(yangNode.getName(), is("Test"));
156
157 //Check whether the container is child of module.
158 YangContainer container = (YangContainer) yangNode.getChild();
159 assertThat(container.getName(), is("valid"));
160
161 // Check whether leaf-list properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530162 ListIterator<YangLeafList> leafListIterator = container.getListOfLeafList().listIterator();
163 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530164
165 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
166 assertThat(leafListInfo.getDataType().getDataTypeName(), is("\"uint16\""));
167 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
168 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
169 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
170 assertThat(leafListInfo.isConfig(), is(true));
171 assertThat(leafListInfo.getMinElements(), is(1));
172 assertThat(leafListInfo.getMaxElelements(), is(2147483647));
173 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
174 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
175 }
176
177 /**
178 * Checks leaf-list statement as sub-statement of list.
179 */
180 @Test
181 public void processListSubStatementLeafList() throws IOException, ParserException {
182
183 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeafList.yang");
184
185 assertThat((node instanceof YangModule), is(true));
186
187 // Check whether the node type is set properly to module.
188 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
189
190 // Check whether the module name is set correctly.
191 YangModule yangNode = (YangModule) node;
192 assertThat(yangNode.getName(), is("Test"));
193
194 // Check whether the list is child of module
195 YangList yangList = (YangList) yangNode.getChild();
196 assertThat(yangList.getName(), is("valid"));
197
198 // Check whether leaf-list properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530199 ListIterator<YangLeafList> leafListIterator = yangList.getListOfLeafList().listIterator();
200 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530201
202 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
203 assertThat(leafListInfo.getDataType().getDataTypeName(), is("\"uint16\""));
204 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
205 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
206 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
207 assertThat(leafListInfo.isConfig(), is(true));
208
209 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
210 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
211 }
212}