blob: 675a9e99a5f8543c0bad64db44ad67b62789ac95 [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
19import static org.hamcrest.MatcherAssert.assertThat;
20import static org.hamcrest.core.Is.is;
21import org.junit.Test;
22import org.onosproject.yangutils.datamodel.YangCase;
23import org.onosproject.yangutils.datamodel.YangChoice;
24import org.onosproject.yangutils.datamodel.YangContainer;
25import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangModule;
27import org.onosproject.yangutils.datamodel.YangNode;
28import org.onosproject.yangutils.datamodel.YangNodeType;
29import org.onosproject.yangutils.parser.exceptions.ParserException;
30import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
31
32import java.io.IOException;
33import java.util.ListIterator;
34
35/**
36 * Test cases for case listener.
37 */
38public class CaseListenerTest {
39
40 private final YangUtilsParserManager manager = new YangUtilsParserManager();
41
42 /**
43 * Checks multiple case statement.
44 */
45 @Test
46 public void processCaseStatement() throws IOException, ParserException {
47
48 YangNode node = manager.getDataModel("src/test/resources/CaseStatement.yang");
49
50 // Check whether the data model tree returned is of type module.
51 assertThat((node instanceof YangModule), is(true));
52
53 // Check whether the node type is set properly to module.
54 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
55
56 // Check whether the module name is set correctly.
57 YangModule yangNode = (YangModule) node;
58 assertThat(yangNode.getName(), is("Test"));
59
60 YangContainer yangContainer = (YangContainer) yangNode.getChild();
61 assertThat(yangContainer.getName(), is("food"));
62
63 YangChoice yangChoice = (YangChoice) yangContainer.getChild();
64 assertThat(yangChoice.getName(), is("snack"));
65
66 YangCase yangCase1 = (YangCase) yangChoice.getChild();
67 assertThat(yangCase1.getName(), is("sports-arena"));
68
69 // Check whether leaf properties as set correctly.
70 ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
71 YangLeaf leafInfo1 = leafIterator1.next();
72
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053073 assertThat(leafInfo1.getName(), is("pretzel"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053074
75 YangCase yangCase2 = (YangCase) yangCase1.getNextSibling();
76 assertThat(yangCase2.getName(), is("late-night"));
77
78 // Check whether leaf properties as set correctly.
79 ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator();
80 YangLeaf leafInfo2 = leafIterator2.next();
81
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053082 assertThat(leafInfo2.getName(), is("chocolate"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053083 }
84
85 /**
86 * Checks duplicate case in choice.
87 */
88 @Test(expected = ParserException.class)
89 public void processDuplicateCaseInChoice() throws IOException, ParserException {
90
91 YangNode node = manager.getDataModel("src/test/resources/DuplicateCaseInChoice.yang");
92 }
93
94 /**
95 * Checks duplicate leaf at different hierarchy.
96 */
97 @Test(expected = ParserException.class)
98 public void processDuplicateLeafInHierarchy() throws IOException, ParserException {
99
100 YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInHierarchy.yang");
101 }
102
103 /**
104 * Checks duplicate leaf in different case within choice.
105 */
106 @Test(expected = ParserException.class)
107 public void processDuplicateLeafInChoice() throws IOException, ParserException {
108
109 YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInChoice.yang");
110 }
111
112 /**
113 * Checks same case within different choice.
114 */
115 @Test
116 public void processCaseStatementSameEntryDifferentChoice() throws IOException, ParserException {
117
118 YangNode node = manager.getDataModel("src/test/resources/CaseStatementSameEntryDifferentChoice.yang");
119
120 // Check whether the data model tree returned is of type module.
121 assertThat((node instanceof YangModule), is(true));
122
123 // Check whether the node type is set properly to module.
124 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
125
126 // Check whether the module name is set correctly.
127 YangModule yangNode = (YangModule) node;
128 assertThat(yangNode.getName(), is("Test"));
129
130 YangContainer yangContainer = (YangContainer) yangNode.getChild();
131 assertThat(yangContainer.getName(), is("food"));
132
133 YangChoice yangChoice = (YangChoice) yangContainer.getChild();
134 assertThat(yangChoice.getName(), is("snack"));
135
136 YangCase yangCase1 = (YangCase) yangChoice.getChild();
137 assertThat(yangCase1.getName(), is("sports-arena"));
138
139 // Check whether leaf properties as set correctly.
140 ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
141 YangLeaf leafInfo1 = leafIterator1.next();
142
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530143 assertThat(leafInfo1.getName(), is("pretzel"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530144
145 YangChoice yangChoice2 = (YangChoice) yangChoice.getNextSibling();
146 assertThat(yangChoice2.getName(), is("lunch"));
147
148 YangCase yangCase2 = (YangCase) yangChoice2.getChild();
149 assertThat(yangCase2.getName(), is("sports-arena"));
150
151 // Check whether leaf properties as set correctly.
152 ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator();
153 YangLeaf leafInfo2 = leafIterator2.next();
154
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530155 assertThat(leafInfo2.getName(), is("chocolate"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530156 }
157
158 /**
159 * Checks case choice hierarchy.
160 */
161 @Test
162 public void processCaseChoiceHierarchy() throws IOException, ParserException {
163
164 YangNode node = manager.getDataModel("src/test/resources/CaseChoiceHierarchy.yang");
165
166 // Check whether the data model tree returned is of type module.
167 assertThat((node instanceof YangModule), is(true));
168
169 // Check whether the node type is set properly to module.
170 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
171
172 // Check whether the module name is set correctly.
173 YangModule yangNode = (YangModule) node;
174 assertThat(yangNode.getName(), is("Test"));
175
176 YangContainer yangContainer = (YangContainer) yangNode.getChild();
177 assertThat(yangContainer.getName(), is("food"));
178
179 YangChoice yangChoice1 = (YangChoice) yangContainer.getChild();
180 assertThat(yangChoice1.getName(), is("snack"));
181
182 YangCase yangCase1 = (YangCase) yangChoice1.getChild();
183 assertThat(yangCase1.getName(), is("sports-arena"));
184
185 // Check whether leaf properties as set correctly.
186 ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
187 YangLeaf leafInfo1 = leafIterator1.next();
188
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530189 assertThat(leafInfo1.getName(), is("pretzel"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530190
191 YangCase yangCase2 = (YangCase) yangCase1.getNextSibling();
192 assertThat(yangCase2.getName(), is("late-night"));
193
194 YangChoice yangChoice2 = (YangChoice) yangCase2.getChild();
195 assertThat(yangChoice2.getName(), is("dinner"));
196
197 YangCase yangCase3 = (YangCase) yangChoice2.getChild();
198 assertThat(yangCase3.getName(), is("late-night"));
199
200 // Check whether leaf properties as set correctly.
201 ListIterator<YangLeaf> leafIterator2 = yangCase3.getListOfLeaf().listIterator();
202 YangLeaf leafInfo2 = leafIterator2.next();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530203 assertThat(leafInfo2.getName(), is("beer"));
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530204 }
205}