blob: 9ebf0dff3fbb25943c52fd89f0bf99cfe531cd92 [file] [log] [blame]
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +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
Vidyashree Rama36f2fab2016-07-15 14:06:56 +053019import java.io.IOException;
20import java.util.ListIterator;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053021import org.junit.Test;
Vidyashree Rama36f2fab2016-07-15 14:06:56 +053022import org.onosproject.yangutils.datamodel.YangAugment;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053023import org.onosproject.yangutils.datamodel.YangCase;
24import org.onosproject.yangutils.datamodel.YangChoice;
25import org.onosproject.yangutils.datamodel.YangContainer;
26import org.onosproject.yangutils.datamodel.YangLeaf;
27import org.onosproject.yangutils.datamodel.YangModule;
28import org.onosproject.yangutils.datamodel.YangNode;
29import org.onosproject.yangutils.datamodel.YangNodeType;
30import org.onosproject.yangutils.parser.exceptions.ParserException;
31import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
32
Vidyashree Rama36f2fab2016-07-15 14:06:56 +053033import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.core.Is.is;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053035
36/**
37 * Test cases for case listener.
38 */
39public class CaseListenerTest {
40
41 private final YangUtilsParserManager manager = new YangUtilsParserManager();
42
43 /**
44 * Checks multiple case statement.
45 */
46 @Test
47 public void processCaseStatement() throws IOException, ParserException {
48
49 YangNode node = manager.getDataModel("src/test/resources/CaseStatement.yang");
50
51 // Check whether the data model tree returned is of type module.
52 assertThat((node instanceof YangModule), is(true));
53
54 // Check whether the node type is set properly to module.
55 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
56
57 // Check whether the module name is set correctly.
58 YangModule yangNode = (YangModule) node;
59 assertThat(yangNode.getName(), is("Test"));
60
61 YangContainer yangContainer = (YangContainer) yangNode.getChild();
62 assertThat(yangContainer.getName(), is("food"));
63
64 YangChoice yangChoice = (YangChoice) yangContainer.getChild();
65 assertThat(yangChoice.getName(), is("snack"));
66
67 YangCase yangCase1 = (YangCase) yangChoice.getChild();
68 assertThat(yangCase1.getName(), is("sports-arena"));
69
70 // Check whether leaf properties as set correctly.
71 ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
72 YangLeaf leafInfo1 = leafIterator1.next();
73
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053074 assertThat(leafInfo1.getName(), is("pretzel"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053075
76 YangCase yangCase2 = (YangCase) yangCase1.getNextSibling();
77 assertThat(yangCase2.getName(), is("late-night"));
78
79 // Check whether leaf properties as set correctly.
80 ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator();
81 YangLeaf leafInfo2 = leafIterator2.next();
82
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083 assertThat(leafInfo2.getName(), is("chocolate"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053084 }
85
86 /**
87 * Checks duplicate case in choice.
88 */
89 @Test(expected = ParserException.class)
90 public void processDuplicateCaseInChoice() throws IOException, ParserException {
91
92 YangNode node = manager.getDataModel("src/test/resources/DuplicateCaseInChoice.yang");
93 }
94
95 /**
96 * Checks duplicate leaf at different hierarchy.
97 */
98 @Test(expected = ParserException.class)
99 public void processDuplicateLeafInHierarchy() throws IOException, ParserException {
100
101 YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInHierarchy.yang");
102 }
103
104 /**
105 * Checks duplicate leaf in different case within choice.
106 */
107 @Test(expected = ParserException.class)
108 public void processDuplicateLeafInChoice() throws IOException, ParserException {
109
110 YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInChoice.yang");
111 }
112
113 /**
114 * Checks same case within different choice.
115 */
116 @Test
117 public void processCaseStatementSameEntryDifferentChoice() throws IOException, ParserException {
118
119 YangNode node = manager.getDataModel("src/test/resources/CaseStatementSameEntryDifferentChoice.yang");
120
121 // Check whether the data model tree returned is of type module.
122 assertThat((node instanceof YangModule), is(true));
123
124 // Check whether the node type is set properly to module.
125 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
126
127 // Check whether the module name is set correctly.
128 YangModule yangNode = (YangModule) node;
129 assertThat(yangNode.getName(), is("Test"));
130
131 YangContainer yangContainer = (YangContainer) yangNode.getChild();
132 assertThat(yangContainer.getName(), is("food"));
133
134 YangChoice yangChoice = (YangChoice) yangContainer.getChild();
135 assertThat(yangChoice.getName(), is("snack"));
136
137 YangCase yangCase1 = (YangCase) yangChoice.getChild();
138 assertThat(yangCase1.getName(), is("sports-arena"));
139
140 // Check whether leaf properties as set correctly.
141 ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
142 YangLeaf leafInfo1 = leafIterator1.next();
143
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530144 assertThat(leafInfo1.getName(), is("pretzel"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530145
146 YangChoice yangChoice2 = (YangChoice) yangChoice.getNextSibling();
147 assertThat(yangChoice2.getName(), is("lunch"));
148
149 YangCase yangCase2 = (YangCase) yangChoice2.getChild();
150 assertThat(yangCase2.getName(), is("sports-arena"));
151
152 // Check whether leaf properties as set correctly.
153 ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator();
154 YangLeaf leafInfo2 = leafIterator2.next();
155
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530156 assertThat(leafInfo2.getName(), is("chocolate"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530157 }
158
159 /**
160 * Checks case choice hierarchy.
161 */
162 @Test
163 public void processCaseChoiceHierarchy() throws IOException, ParserException {
164
165 YangNode node = manager.getDataModel("src/test/resources/CaseChoiceHierarchy.yang");
166
167 // Check whether the data model tree returned is of type module.
168 assertThat((node instanceof YangModule), is(true));
169
170 // Check whether the node type is set properly to module.
171 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
172
173 // Check whether the module name is set correctly.
174 YangModule yangNode = (YangModule) node;
175 assertThat(yangNode.getName(), is("Test"));
176
177 YangContainer yangContainer = (YangContainer) yangNode.getChild();
178 assertThat(yangContainer.getName(), is("food"));
179
180 YangChoice yangChoice1 = (YangChoice) yangContainer.getChild();
181 assertThat(yangChoice1.getName(), is("snack"));
182
183 YangCase yangCase1 = (YangCase) yangChoice1.getChild();
184 assertThat(yangCase1.getName(), is("sports-arena"));
185
186 // Check whether leaf properties as set correctly.
187 ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
188 YangLeaf leafInfo1 = leafIterator1.next();
189
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530190 assertThat(leafInfo1.getName(), is("pretzel"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530191
192 YangCase yangCase2 = (YangCase) yangCase1.getNextSibling();
193 assertThat(yangCase2.getName(), is("late-night"));
194
195 YangChoice yangChoice2 = (YangChoice) yangCase2.getChild();
196 assertThat(yangChoice2.getName(), is("dinner"));
197
198 YangCase yangCase3 = (YangCase) yangChoice2.getChild();
199 assertThat(yangCase3.getName(), is("late-night"));
200
201 // Check whether leaf properties as set correctly.
202 ListIterator<YangLeaf> leafIterator2 = yangCase3.getListOfLeaf().listIterator();
203 YangLeaf leafInfo2 = leafIterator2.next();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530204 assertThat(leafInfo2.getName(), is("beer"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530205 }
Vidyashree Rama36f2fab2016-07-15 14:06:56 +0530206
207 /**
208 * Checks case substatement of augment.
209 */
210 @Test
211 public void processCaseSubStatementOfAugment() throws IOException, ParserException {
212
213 YangNode node = manager.getDataModel("src/test/resources/CaseSubStatementOfAugment.yang");
214
215 // Check whether the data model tree returned is of type module.
216 assertThat((node instanceof YangModule), is(true));
217
218 // Check whether the node type is set properly to module.
219 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
220
221 // Check whether the module name is set correctly.
222 YangModule yangNode = (YangModule) node;
223 assertThat(yangNode.getName(), is("event"));
224
225 YangAugment augment = ((YangAugment) yangNode.getChild());
226 assertThat(augment.getName(), is("/snmp:snmp/snmp:engine/snmp:listen/snmp:transport"));
227
228 YangCase yangCase = ((YangCase) augment.getChild());
229 assertThat(yangCase.getName(), is("tls"));
230
231 YangCase yangCase1 = ((YangCase) yangCase.getNextSibling());
232 assertThat(yangCase1.getName(), is("dtls"));
233
234 YangContainer container = ((YangContainer) yangCase.getChild());
235 assertThat(container.getName(), is("tls"));
236 assertThat(container.getListOfLeaf().iterator().next().getName(), is("ip"));
237 }
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530238}