blob: acd0c8aa5a6c679b9c70a7456535647261a9e0c2 [file] [log] [blame]
Gaurav Agrawalbd804472016-03-25 11:25:36 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawalbd804472016-03-25 11:25:36 +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 java.io.IOException;
20import java.util.ListIterator;
21import org.junit.Test;
22import org.onosproject.yangutils.datamodel.YangContainer;
23import org.onosproject.yangutils.datamodel.YangGrouping;
24import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangList;
26import org.onosproject.yangutils.datamodel.YangModule;
27import org.onosproject.yangutils.datamodel.YangNode;
28import org.onosproject.yangutils.datamodel.YangNodeType;
29import org.onosproject.yangutils.datamodel.YangStatusType;
30import org.onosproject.yangutils.parser.exceptions.ParserException;
31import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
32
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.core.Is.is;
35
36/**
37 * Test cases for testing grouping listener.
38 */
39public class GroupingListenerTest {
40
41 private final YangUtilsParserManager manager = new YangUtilsParserManager();
42
43 /**
44 * Checks grouping statement inside module.
45 */
46 @Test
47 public void processGroupingInModule() throws IOException, ParserException {
48
49 YangNode node = manager.getDataModel("src/test/resources/GroupingInModule.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 YangGrouping yangGrouping = (YangGrouping) yangNode.getChild();
62 assertThat(yangGrouping.getName(), is("endpoint"));
63
64 ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator();
65 YangLeaf leafInfo = leafIterator.next();
66
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067 assertThat(leafInfo.getName(), is("address"));
Gaurav Agrawalbd804472016-03-25 11:25:36 +053068 }
69
70 /**
71 * Checks grouping statement inside container.
72 */
73 @Test
74 public void processGroupingInContainer() throws IOException, ParserException {
75
76 YangNode node = manager.getDataModel("src/test/resources/GroupingInContainer.yang");
77
78 // Check whether the data model tree returned is of type module.
79 assertThat((node instanceof YangModule), is(true));
80
81 // Check whether the node type is set properly to module.
82 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
83
84 // Check whether the module name is set correctly.
85 YangModule yangNode = (YangModule) node;
86 assertThat(yangNode.getName(), is("Test"));
87
88 YangContainer yangContainer = (YangContainer) yangNode.getChild();
89 assertThat(yangContainer.getName(), is("valid"));
90
91 YangGrouping yangGrouping = (YangGrouping) yangContainer.getChild();
92 assertThat(yangGrouping.getName(), is("endpoint"));
93
94 ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator();
95 YangLeaf leafInfo = leafIterator.next();
96
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053097 assertThat(leafInfo.getName(), is("address"));
Gaurav Agrawalbd804472016-03-25 11:25:36 +053098 }
99
100 /**
101 * Checks grouping statement inside list.
102 */
103 @Test
104 public void processGroupingInList() throws IOException, ParserException {
105
106 YangNode node = manager.getDataModel("src/test/resources/GroupingInList.yang");
107
108 // Check whether the data model tree returned is of type module.
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 YangList yangList = (YangList) yangNode.getChild();
119 assertThat(yangList.getName(), is("valid"));
120
121 YangGrouping yangGrouping = (YangGrouping) yangList.getChild();
122 assertThat(yangGrouping.getName(), is("endpoint"));
123
124 ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator();
125 YangLeaf leafInfo = leafIterator.next();
126
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530127 assertThat(leafInfo.getName(), is("address"));
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530128 }
129
130 /**
131 * Checks grouping with attributes.
132 */
133 @Test
134 public void processGroupingAttributes() throws IOException, ParserException {
135
136 YangNode node = manager.getDataModel("src/test/resources/GroupingAttributes.yang");
137
138 // Check whether the data model tree returned is of type module.
139 assertThat((node instanceof YangModule), is(true));
140
141 // Check whether the node type is set properly to module.
142 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
143
144 // Check whether the module name is set correctly.
145 YangModule yangNode = (YangModule) node;
146 assertThat(yangNode.getName(), is("Test"));
147
148 YangList yangList = (YangList) yangNode.getChild();
149 assertThat(yangList.getName(), is("valid"));
150
151 YangGrouping yangGrouping = (YangGrouping) yangList.getChild();
152 assertThat(yangGrouping.getName(), is("endpoint"));
153 assertThat(yangGrouping.getStatus(), is(YangStatusType.CURRENT));
154 assertThat(yangGrouping.getReference(), is("\"RFC 6020\""));
155 assertThat(yangGrouping.getDescription(), is("\"grouping under test\""));
156
157 ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator();
158 YangLeaf leafInfo = leafIterator.next();
159
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530160 assertThat(leafInfo.getName(), is("address"));
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530161 }
162
163 /**
164 * Checks duplicate grouping in list.
165 */
166 @Test(expected = ParserException.class)
167 public void processDuplicateGroupingInList() throws IOException, ParserException {
168
169 YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInList.yang");
170 }
171
172 /**
173 * Checks duplicate grouping in container.
174 */
175 @Test (expected = ParserException.class)
176 public void processDuplicateGroupingInContainer() throws IOException, ParserException {
177
178 YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInContainer.yang");
179 }
180
181 /**
182 * Checks duplicate grouping in module.
183 */
184 @Test (expected = ParserException.class)
185 public void processDuplicateGroupingInModule() throws IOException, ParserException {
186
187 YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInModule.yang");
188 }
189}