blob: e3f581b6ebbf593869e36e928a9fa862e7a12c23 [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
19import org.junit.Rule;
20import org.junit.Test;
21
22import org.junit.rules.ExpectedException;
23import org.onosproject.yangutils.datamodel.YangLeafList;
24import org.onosproject.yangutils.datamodel.YangModule;
25import org.onosproject.yangutils.datamodel.YangNode;
26import org.onosproject.yangutils.datamodel.YangNodeType;
27import org.onosproject.yangutils.datamodel.YangDataTypes;
28import org.onosproject.yangutils.datamodel.YangStatusType;
29import org.onosproject.yangutils.datamodel.YangContainer;
30import org.onosproject.yangutils.datamodel.YangList;
31import org.onosproject.yangutils.parser.exceptions.ParserException;
32import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
33
34import java.io.IOException;
35import java.util.ListIterator;
36
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.core.Is.is;
39
40/**
41 * Test cases for testing leaf-list listener.
42 */
43public class LeafListListenerTest {
44
45 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
48 private final YangUtilsParserManager manager = new YangUtilsParserManager();
49
50 /**
51 * Checks all the values of leaf-list sub-statements are set
52 * correctly.
53 */
54 @Test
55 public void processLeafListSubStatements() throws IOException, ParserException {
56
57 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatements.yang");
58
59 // Check whether the data model tree returned is of type module.
60 assertThat((node instanceof YangModule), is(true));
61
62 // Check whether the node type is set properly to module.
63 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
64
65 // Check whether the module name is set correctly.
66 YangModule yangNode = (YangModule) node;
67 assertThat(yangNode.getName(), is("Test"));
68
Bharat saraswal870c56f2016-02-20 21:57:16 +053069 ListIterator<YangLeafList<?>> leafListIterator = yangNode.getListOfLeafList().listIterator();
70 YangLeafList<?> leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053071
72 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
73 assertThat(leafListInfo.getDataType().getDataTypeName(), is("\"uint16\""));
74 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
75 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
76 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
77 assertThat(leafListInfo.isConfig(), is(true));
78 assertThat(leafListInfo.getMaxElelements(), is(3));
79 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
80 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
81 }
82
83 /**
84 * Checks whether exception is thrown when leaf-list identifier
85 * starts with digit.
86 */
87 @Test
88 public void processLeafListInvalidIdentifier() throws IOException, ParserException {
89 thrown.expect(ParserException.class);
90 thrown.expectMessage("mismatched input '1invalid-interval' expecting IDENTIFIER");
91 YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidIdentifier.yang");
92 }
93
94 /**
95 * Checks whether exception is thrown when leaf-list keyword
96 * is incorrect.
97 */
98 @Test
99 public void processLeafListInvalidStatement() throws IOException, ParserException {
100 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530101 thrown.expectMessage("mismatched input 'leaflist' expecting {'augment', 'choice', 'contact', 'container',"
102 + " 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include',"
103 + " 'leaf', 'leaf-list', 'list', 'namespace', 'notification', 'organization', 'prefix', 'reference',"
104 + " 'revision', 'rpc', 'typedef', 'uses', 'yang-version', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530105 YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidStatement.yang");
106 }
107
108 /**
109 * Checks whether exception is thrown when leaf-list keyword
110 * without Left brace as per grammar.
111 */
112 @Test
113 public void processLeafListWithoutLeftBrace() throws IOException, ParserException {
114 thrown.expect(ParserException.class);
115 thrown.expectMessage("missing '{' at 'type'");
116 YangNode node = manager.getDataModel("src/test/resources/LeafListWithoutLeftBrace.yang");
117 }
118
119 /**
120 * Checks whether exception is thrown when config statement
121 * cardinality is not as per grammar.
122 */
123 @Test
124 public void processLeafListConfigInvalidCardinality() throws IOException, ParserException {
125 thrown.expect(ParserException.class);
126 thrown.expectMessage("Internal parser error detected: Invalid cardinality in config before processing.");
127 YangNode node = manager.getDataModel("src/test/resources/LeafListConfigInvalidCardinality.yang");
128 }
129
130 /**
131 * Checks whether exception is thrown when units statement
132 * cardinality is not as per grammar.
133 */
134 @Test
135 public void processLeafListUnitsInvalidCardinality() throws IOException, ParserException {
136 thrown.expect(ParserException.class);
137 thrown.expectMessage("Internal parser error detected: Invalid cardinality in units before processing.");
138 YangNode node = manager.getDataModel("src/test/resources/LeafListUnitsInvalidCardinality.yang");
139 }
140
141 /**
142 * Checks leaf-list statement as sub-statement of container.
143 */
144 @Test
145 public void processContainerSubStatementLeafList() throws IOException, ParserException {
146
147 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeafList.yang");
148
149 // Check whether the data model tree returned is of type module.
150 assertThat((node instanceof YangModule), is(true));
151
152 // Check whether the node type is set properly to module.
153 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
154
155 // Check whether the module name is set correctly.
156 YangModule yangNode = (YangModule) node;
157 assertThat(yangNode.getName(), is("Test"));
158
159 //Check whether the container is child of module.
160 YangContainer container = (YangContainer) yangNode.getChild();
161 assertThat(container.getName(), is("valid"));
162
163 // Check whether leaf-list properties as set correctly.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530164 ListIterator<YangLeafList<?>> leafListIterator = container.getListOfLeafList().listIterator();
165 YangLeafList<?> leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530166
167 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
168 assertThat(leafListInfo.getDataType().getDataTypeName(), is("\"uint16\""));
169 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
170 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
171 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
172 assertThat(leafListInfo.isConfig(), is(true));
173 assertThat(leafListInfo.getMinElements(), is(1));
174 assertThat(leafListInfo.getMaxElelements(), is(2147483647));
175 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
176 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
177 }
178
179 /**
180 * Checks leaf-list statement as sub-statement of list.
181 */
182 @Test
183 public void processListSubStatementLeafList() throws IOException, ParserException {
184
185 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeafList.yang");
186
187 assertThat((node instanceof YangModule), is(true));
188
189 // Check whether the node type is set properly to module.
190 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
191
192 // Check whether the module name is set correctly.
193 YangModule yangNode = (YangModule) node;
194 assertThat(yangNode.getName(), is("Test"));
195
196 // Check whether the list is child of module
197 YangList yangList = (YangList) yangNode.getChild();
198 assertThat(yangList.getName(), is("valid"));
199
200 // Check whether leaf-list properties as set correctly.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530201 ListIterator<YangLeafList<?>> leafListIterator = yangList.getListOfLeafList().listIterator();
202 YangLeafList<?> leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530203
204 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
205 assertThat(leafListInfo.getDataType().getDataTypeName(), is("\"uint16\""));
206 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
207 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
208 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
209 assertThat(leafListInfo.isConfig(), is(true));
210
211 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
212 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
213 }
214}