blob: 9bbd0f5e78191244971e583f35ec424cb639767c [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;
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;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053026import org.onosproject.yangutils.datamodel.utils.builtindatatype.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.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 listener.
41 */
42public class LeafListenerTest {
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 sub-statements are set correctly.
Vidyashree Rama49abe712016-02-13 22:22:12 +053051 */
52 @Test
53 public void processLeafSubStatements() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/LeafSubStatements.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
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053070 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053071 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053072 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
73 assertThat(leafInfo.getUnits(), is("\"seconds\""));
74 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
75 assertThat(leafInfo.isConfig(), is(true));
76 assertThat(leafInfo.isMandatory(), is(true));
77 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
78 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
79 }
80
81 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053082 * Checks whether exception is thrown when leaf identifier starts with
83 * digit.
Vidyashree Rama49abe712016-02-13 22:22:12 +053084 */
85 @Test
86 public void processLeafInvalidIdentifier() throws IOException, ParserException {
87 thrown.expect(ParserException.class);
Vidyashree Rama468f8282016-03-04 19:08:35 +053088 thrown.expectMessage("YANG file error : leaf name 1invalid-interval is not valid.");
Vidyashree Rama49abe712016-02-13 22:22:12 +053089 YangNode node = manager.getDataModel("src/test/resources/LeafInvalidIdentifier.yang");
90 }
91
92 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053093 * Checks whether exception is thrown when leaf keyword without Left brace
94 * as per grammar.
Vidyashree Rama49abe712016-02-13 22:22:12 +053095 */
96 @Test
97 public void processLeafWithoutLeftBrace() throws IOException, ParserException {
98 thrown.expect(ParserException.class);
99 thrown.expectMessage("missing '{' at 'type'");
100 YangNode node = manager.getDataModel("src/test/resources/LeafWithoutLeftBrace.yang");
101 }
102
103 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530104 * Checks whether exception is thrown when config statement cardinality is
105 * not as per grammar.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530106 */
107 @Test
108 public void processLeafConfigInvalidCardinality() throws IOException, ParserException {
109 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530110 thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf invalid-interval\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530111 YangNode node = manager.getDataModel("src/test/resources/LeafConfigInvalidCardinality.yang");
112 }
113
114 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530115 * Checks whether exception is thrown when mandatory statement cardinality
116 * is not as per grammar.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530117 */
118 @Test
119 public void processLeafMandatoryInvalidCardinality() throws IOException, ParserException {
120 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530121 thrown.expectMessage("YANG file error: \"mandatory\" is defined more than once in \"leaf invalid-interval\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530122 YangNode node = manager.getDataModel("src/test/resources/LeafMandatoryInvalidCardinality.yang");
123 }
124
125 /**
126 * Checks leaf statement as sub-statement of container.
127 */
128 @Test
129 public void processContainerSubStatementLeaf() throws IOException, ParserException {
130
131 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeaf.yang");
132
133 // Check whether the data model tree returned is of type module.
134 assertThat((node instanceof YangModule), is(true));
135
136 // Check whether the node type is set properly to module.
137 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
138
139 // Check whether the module name is set correctly.
140 YangModule yangNode = (YangModule) node;
141 assertThat(yangNode.getName(), is("Test"));
142
143 //Check whether the container is child of module.
144 YangContainer container = (YangContainer) yangNode.getChild();
145 assertThat(container.getName(), is("valid"));
146
147 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530148 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
149 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530150
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530151 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530152 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530153 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
154 assertThat(leafInfo.getUnits(), is("\"seconds\""));
155 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
156 assertThat(leafInfo.isConfig(), is(true));
157 assertThat(leafInfo.isMandatory(), is(true));
158 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
159 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
160 }
161
162 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530163 * Checks duplicate leaf statement as sub-statement of module.
164 */
165 @Test(expected = ParserException.class)
166 public void processModuleWithDuplicateLeaf() throws IOException, ParserException {
167
168 YangNode node = manager.getDataModel("src/test/resources/ModuleWithDuplicateLeaf.yang");
169 }
170
171 /**
172 * Checks duplicate leaf statement as sub-statement of container.
173 */
174 @Test(expected = ParserException.class)
175 public void processContainerWithDuplicateLeaf() throws IOException, ParserException {
176
177 YangNode node = manager.getDataModel("src/test/resources/ContainerWithDuplicateLeaf.yang");
178 }
179
180 /**
181 * Checks duplicate leaf statement as sub-statement of list.
182 */
183 @Test(expected = ParserException.class)
184 public void processListWithDuplicateLeaf() throws IOException, ParserException {
185
186 YangNode node = manager.getDataModel("src/test/resources/ListWithDuplicateLeaf.yang");
187 }
188
189 /**
Vidyashree Rama49abe712016-02-13 22:22:12 +0530190 * Checks leaf statement as sub-statement of list.
191 */
192 @Test
193 public void processListSubStatementLeaf() throws IOException, ParserException {
194
195 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeaf.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
207 YangList yangList = (YangList) yangNode.getChild();
208 assertThat(yangList.getName(), is("valid"));
209
210 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530211 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
212 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530213
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530214 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530215 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530216 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
217 assertThat(leafInfo.getUnits(), is("\"seconds\""));
218 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
219 assertThat(leafInfo.isConfig(), is(true));
220 assertThat(leafInfo.isMandatory(), is(true));
221 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
222 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
223 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530224}