blob: bee66aa55fdba5f6593ac359a7f68adb785a5e21 [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.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 list listener.
41 */
42public class ListListenerTest {
43
44 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
47 private final YangUtilsParserManager manager = new YangUtilsParserManager();
48
49 /**
50 * Checks list statement as sub-statement of module.
51 */
52 @Test
53 public void processModuleSubStatementList() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementList.yang");
56
57 assertThat((node instanceof YangModule), is(true));
58
59 // Check whether the node type is set properly to module.
60 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
61
62 // Check whether the module name is set correctly.
63 YangModule yangNode = (YangModule) node;
64 assertThat(yangNode.getName(), is("Test"));
65
66 // Check whether the list is child of module
67 YangList yangList = (YangList) yangNode.getChild();
68 assertThat(yangList.getName(), is("valid"));
69
70 ListIterator<String> keyList = yangList.getKeyList().listIterator();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053071 assertThat(keyList.next(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053072 }
73
74 /**
75 * Checks list statement as sub-statement of container.
76 */
77 @Test
78 public void processContainerSubStatementList() throws IOException, ParserException {
79
80 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementList.yang");
81
82 assertThat((node instanceof YangModule), is(true));
83
84 // Check whether the node type is set properly to module.
85 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
86
87 // Check whether the module name is set correctly.
88 YangModule yangNode = (YangModule) node;
89 assertThat(yangNode.getName(), is("Test"));
90
91 // Check whether the container is child of module
92 YangContainer yangContainer = (YangContainer) yangNode.getChild();
93 assertThat(yangContainer.getName(), is("ospf"));
94
95 // Check whether the list is child of container
96 YangList yangList = (YangList) yangContainer.getChild();
97 assertThat(yangList.getName(), is("valid"));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053098 assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +053099 }
100
101 /**
102 * Checks list statement as sub-statement of list.
103 */
104 @Test
105 public void processListSubStatementList() throws IOException, ParserException {
106
107 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementList.yang");
108
109 assertThat((node instanceof YangModule), is(true));
110
111 // Check whether the node type is set properly to module.
112 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
113
114 // Check whether the module name is set correctly.
115 YangModule yangNode = (YangModule) node;
116 assertThat(yangNode.getName(), is("Test"));
117
118 // Check whether the list is child of module
119 YangList yangList1 = (YangList) yangNode.getChild();
120 assertThat(yangList1.getName(), is("ospf"));
121 assertThat(yangList1.getKeyList().contains("process-id"), is(true));
122
123 // Check whether the list is child of list
124 YangList yangList = (YangList) yangList1.getChild();
125 assertThat(yangList.getName(), is("valid"));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530126 assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530127 }
128
129 /**
130 * Checks list with all its sub-statements.
131 */
132 @Test
133 public void processListSubStatements() throws IOException, ParserException {
134
135 YangNode node = manager.getDataModel("src/test/resources/ListSubStatements.yang");
136
137 assertThat((node instanceof YangModule), is(true));
138
139 // Check whether the node type is set properly to module.
140 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
141
142 // Check whether the module name is set correctly.
143 YangModule yangNode = (YangModule) node;
144 assertThat(yangNode.getName(), is("Test"));
145
146 // Check whether the list is child of module
147 YangList yangList = (YangList) yangNode.getChild();
148
149 // Check whether list properties as set correctly.
150 assertThat(yangList.getName(), is("ospf"));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530151 assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530152
153 assertThat(yangList.isConfig(), is(true));
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530154 assertThat(yangList.getMaxElements(), is(10));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530155 assertThat(yangList.getMinElements(), is(3));
156 assertThat(yangList.getDescription(), is("\"list description\""));
157 assertThat(yangList.getStatus(), is(YangStatusType.CURRENT));
158 assertThat(yangList.getReference(), is("\"list reference\""));
159
160 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530161 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
162 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530163
164 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530165 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530166 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
167 assertThat(leafInfo.getUnits(), is("\"seconds\""));
168 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
169 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
170 }
171
172 /**
173 * Checks cardinality of sub-statements of list.
174 */
175 @Test
176 public void processListSubStatementsCardinality() throws IOException, ParserException {
177 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530178 thrown.expectMessage("YANG file error: \"reference\" is defined more than once in \"list valid\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530179 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementsCardinality.yang");
180 }
181
182 /**
183 * Checks list statement without child.
184 */
185 @Test
186 public void processListStatementWithoutChild() throws IOException, ParserException {
187 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530188 thrown.expectMessage("YANG file error: Missing \"data-def-substatements\" in \"list valid\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530189 YangNode node = manager.getDataModel("src/test/resources/ListStatementWithoutChild.yang");
190 }
191
192 /**
193 * Checks list as root node.
194 */
195 @Test
196 public void processListAsRootNode() throws IOException, ParserException {
197 thrown.expect(ParserException.class);
198 thrown.expectMessage("no viable alternative at input 'list'");
199 YangNode node = manager.getDataModel("src/test/resources/ListAsRootNode.yang");
200 }
201
202 /**
203 * Checks invalid identifier for list statement.
204 */
205 @Test
206 public void processListInvalidIdentifier() throws IOException, ParserException {
207 thrown.expect(ParserException.class);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530208 thrown.expectMessage("YANG file error : list name 1valid is not valid.");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530209 YangNode node = manager.getDataModel("src/test/resources/ListInvalidIdentifier.yang");
210 }
211}