blob: 6013261889b89442357044d960635c8a96de0eac [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 container listener.
41 */
42public class ContainerListenerTest {
43
44 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
47 private final YangUtilsParserManager manager = new YangUtilsParserManager();
48
49 /**
50 * Checks container statement as sub-statement of module.
51 */
52 @Test
53 public void processModuleSubStatementContainer() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementContainer.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 container is child of module
67 YangContainer yangContainer = (YangContainer) yangNode.getChild();
68 assertThat(yangContainer.getName(), is("valid"));
69 }
70
71 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053072 * Checks if container identifier in module is duplicate.
73 */
74 @Test(expected = ParserException.class)
75 public void processModuleDuplicateContainer() throws IOException, ParserException {
76
77 YangNode node = manager.getDataModel("src/test/resources/ModuleDuplicateContainer.yang");
78 }
79
80 /**
81 * Checks if container identifier in container is duplicate.
82 */
83 @Test(expected = ParserException.class)
84 public void processContainerDuplicateContainer() throws IOException, ParserException {
85
86 YangNode node = manager.getDataModel("src/test/resources/ContainerDuplicateContainer.yang");
87 }
88
89 /**
90 * Checks if container identifier in list is duplicate.
91 */
92 @Test(expected = ParserException.class)
93 public void processListDuplicateContainer() throws IOException, ParserException {
94
95 YangNode node = manager.getDataModel("src/test/resources/ListDuplicateContainer.yang");
96 }
97
98 /**
99 * Checks if container identifier collides with list at same level.
100 */
101 @Test(expected = ParserException.class)
102 public void processDuplicateContainerAndList() throws IOException, ParserException {
103
104 YangNode node = manager.getDataModel("src/test/resources/DuplicateContainerAndList.yang");
105 }
106
107 /**
Vidyashree Rama49abe712016-02-13 22:22:12 +0530108 * Checks container statement as sub-statement of container.
109 */
110 @Test
111 public void processContainerSubStatementContainer() throws IOException, ParserException {
112
113 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementContainer.yang");
114
115 assertThat((node instanceof YangModule), is(true));
116
117 // Check whether the node type is set properly to module.
118 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
119
120 // Check whether the module name is set correctly.
121 YangModule yangNode = (YangModule) node;
122 assertThat(yangNode.getName(), is("Test"));
123
124 // Check whether the container is child of module
125 YangContainer yangContainer = (YangContainer) yangNode.getChild();
126 assertThat(yangContainer.getName(), is("ospf"));
127
128 // Check whether the container is child of container
129 YangContainer yangContainer1 = (YangContainer) yangContainer.getChild();
130 assertThat(yangContainer1.getName(), is("valid"));
131 }
132
133 /**
134 * Checks container statement as sub-statement of list.
135 */
136 @Test
137 public void processListSubStatementContainer() throws IOException, ParserException {
138
139 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementContainer.yang");
140
141 assertThat((node instanceof YangModule), is(true));
142
143 // Check whether the node type is set properly to module.
144 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
145
146 // Check whether the module name is set correctly.
147 YangModule yangNode = (YangModule) node;
148 assertThat(yangNode.getName(), is("Test"));
149
150 // Check whether the list is child of module
151 YangList yangList1 = (YangList) yangNode.getChild();
152 assertThat(yangList1.getName(), is("ospf"));
153
154 ListIterator<String> keyList = yangList1.getKeyList().listIterator();
155 assertThat(keyList.next(), is("process-id"));
156
157 // Check whether the list is child of list
158 YangContainer yangContainer = (YangContainer) yangList1.getChild();
159 assertThat(yangContainer.getName(), is("interface"));
160 }
161
162 /**
163 * Checks container with all its sub-statements.
164 */
165 @Test
166 public void processContainerSubStatements() throws IOException, ParserException {
167
168 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatements.yang");
169
170 assertThat((node instanceof YangModule), is(true));
171
172 // Check whether the node type is set properly to module.
173 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
174
175 // Check whether the module name is set correctly.
176 YangModule yangNode = (YangModule) node;
177 assertThat(yangNode.getName(), is("Test"));
178
179 // Check whether the container is child of module
180 YangContainer yangContainer = (YangContainer) yangNode.getChild();
181
182 // Check whether container properties as set correctly.
183 assertThat(yangContainer.getName(), is("ospf"));
184
185 assertThat(yangContainer.isConfig(), is(true));
186 assertThat(yangContainer.getPresence(), is("\"ospf logs\""));
187 assertThat(yangContainer.getDescription(), is("\"container description\""));
188 assertThat(yangContainer.getStatus(), is(YangStatusType.CURRENT));
189 assertThat(yangContainer.getReference(), is("\"container reference\""));
190
191 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530192 ListIterator<YangLeaf> leafIterator = yangContainer.getListOfLeaf().listIterator();
193 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530194
195 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530196 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530197 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
198 assertThat(leafInfo.getUnits(), is("\"seconds\""));
199 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
200 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
201 }
202
203 /**
204 * Checks cardinality of sub-statements of container.
205 */
206 @Test
207 public void processContainerSubStatementCardinality() throws IOException, ParserException {
208 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530209 thrown.expectMessage("YANG file error: \"reference\" is defined more than once in \"container valid\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530210 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementCardinality.yang");
211 }
212
213 /**
214 * Checks container as root node.
215 */
216 @Test
217 public void processContainerRootNode() throws IOException, ParserException {
218 thrown.expect(ParserException.class);
219 thrown.expectMessage("no viable alternative at input 'container'");
220 YangNode node = manager.getDataModel("src/test/resources/ContainerRootNode.yang");
221 }
222
223 /**
224 * Checks invalid identifier for container statement.
225 */
226 @Test
227 public void processContainerInvalidIdentifier() throws IOException, ParserException {
228 thrown.expect(ParserException.class);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530229 thrown.expectMessage("YANG file error : container name 1valid is not valid.");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530230 YangNode node = manager.getDataModel("src/test/resources/ContainerInvalidIdentifier.yang");
231 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530232}