blob: 083bf81e7da5dfdaf3d30c16a0bd368f1a76c2f1 [file] [log] [blame]
Vidyashree Rama49abe712016-02-13 22:22:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama49abe712016-02-13 22:22:12 +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
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053019import java.io.IOException;
20import java.util.ListIterator;
Vidyashree Rama49abe712016-02-13 22:22:12 +053021import org.junit.Rule;
22import org.junit.Test;
Vidyashree Rama49abe712016-02-13 22:22:12 +053023import org.junit.rules.ExpectedException;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053024import org.onosproject.yangutils.datamodel.YangContainer;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vidyashree Rama49abe712016-02-13 22:22:12 +053026import org.onosproject.yangutils.datamodel.YangLeafList;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053027import org.onosproject.yangutils.datamodel.YangList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053028import org.onosproject.yangutils.datamodel.YangModule;
29import org.onosproject.yangutils.datamodel.YangNode;
30import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053031import org.onosproject.yangutils.datamodel.YangStatusType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053032import org.onosproject.yangutils.parser.exceptions.ParserException;
33import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34
Vidyashree Rama49abe712016-02-13 22:22:12 +053035import static org.hamcrest.MatcherAssert.assertThat;
36import static org.hamcrest.core.Is.is;
37
38/**
39 * Test cases for testing leaf-list listener.
40 */
41public class LeafListListenerTest {
42
43 @Rule
44 public ExpectedException thrown = ExpectedException.none();
45
46 private final YangUtilsParserManager manager = new YangUtilsParserManager();
47
48 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053049 * Checks all the values of leaf-list sub-statements are set correctly.
Vidyashree Rama49abe712016-02-13 22:22:12 +053050 */
51 @Test
52 public void processLeafListSubStatements() throws IOException, ParserException {
53
54 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatements.yang");
55
56 // Check whether the data model tree returned is of type module.
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
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053066 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
67 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053068
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053069 assertThat(leafListInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053070 assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053071 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
72 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
73 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
74 assertThat(leafListInfo.isConfig(), is(true));
rama-huawei6c728a92016-07-11 14:48:12 +053075 assertThat(leafListInfo.getMaxElements().getMaxElement(), is(3));
Vidyashree Rama49abe712016-02-13 22:22:12 +053076 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
77 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
78 }
79
80 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053081 * Checks whether exception is thrown when leaf-list identifier starts with
82 * digit.
Vidyashree Rama49abe712016-02-13 22:22:12 +053083 */
84 @Test
85 public void processLeafListInvalidIdentifier() throws IOException, ParserException {
86 thrown.expect(ParserException.class);
Vidyashree Rama468f8282016-03-04 19:08:35 +053087 thrown.expectMessage("YANG file error : leaf-list name 1invalid-interval is not valid.");
Vidyashree Rama49abe712016-02-13 22:22:12 +053088 YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidIdentifier.yang");
89 }
90
91 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053092 * Checks whether exception is thrown when leaf-list keyword is incorrect.
Vidyashree Rama49abe712016-02-13 22:22:12 +053093 */
Vidyashree Rama528ef302016-06-30 14:31:18 +053094 @Test(expected = ParserException.class)
Vidyashree Rama49abe712016-02-13 22:22:12 +053095 public void processLeafListInvalidStatement() throws IOException, ParserException {
Vidyashree Rama49abe712016-02-13 22:22:12 +053096 YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidStatement.yang");
97 }
98
99 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530100 * Checks whether exception is thrown when leaf-list keyword without Left
101 * brace as per grammar.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530102 */
103 @Test
104 public void processLeafListWithoutLeftBrace() throws IOException, ParserException {
105 thrown.expect(ParserException.class);
106 thrown.expectMessage("missing '{' at 'type'");
107 YangNode node = manager.getDataModel("src/test/resources/LeafListWithoutLeftBrace.yang");
108 }
109
110 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530111 * Checks whether exception is thrown when config statement cardinality is
112 * not as per grammar.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530113 */
114 @Test
115 public void processLeafListConfigInvalidCardinality() throws IOException, ParserException {
116 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530117 thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf-list " +
118 "invalid-interval\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530119 YangNode node = manager.getDataModel("src/test/resources/LeafListConfigInvalidCardinality.yang");
120 }
121
122 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530123 * Checks whether exception is thrown when units statement cardinality is
124 * not as per grammar.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530125 */
126 @Test
127 public void processLeafListUnitsInvalidCardinality() throws IOException, ParserException {
128 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530129 thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf-list invalid-interval\"");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530130 YangNode node = manager.getDataModel("src/test/resources/LeafListUnitsInvalidCardinality.yang");
131 }
132
133 /**
134 * Checks leaf-list statement as sub-statement of container.
135 */
136 @Test
137 public void processContainerSubStatementLeafList() throws IOException, ParserException {
138
139 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeafList.yang");
140
141 // Check whether the data model tree returned is of type module.
142 assertThat((node instanceof YangModule), is(true));
143
144 // Check whether the node type is set properly to module.
145 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
146
147 // Check whether the module name is set correctly.
148 YangModule yangNode = (YangModule) node;
149 assertThat(yangNode.getName(), is("Test"));
150
151 //Check whether the container is child of module.
152 YangContainer container = (YangContainer) yangNode.getChild();
153 assertThat(container.getName(), is("valid"));
154
155 // Check whether leaf-list properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530156 ListIterator<YangLeafList> leafListIterator = container.getListOfLeafList().listIterator();
157 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530158
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530159 assertThat(leafListInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530160 assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530161 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
162 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
163 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
164 assertThat(leafListInfo.isConfig(), is(true));
rama-huawei6c728a92016-07-11 14:48:12 +0530165 assertThat(leafListInfo.getMinElements().getMinElement(), is(1));
166 assertThat(leafListInfo.getMaxElements().getMaxElement(), is(2147483647));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530167 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
168 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
169 }
170
171 /**
172 * Checks leaf-list statement as sub-statement of list.
173 */
174 @Test
175 public void processListSubStatementLeafList() throws IOException, ParserException {
176
177 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeafList.yang");
178
179 assertThat((node instanceof YangModule), is(true));
180
181 // Check whether the node type is set properly to module.
182 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
183
184 // Check whether the module name is set correctly.
185 YangModule yangNode = (YangModule) node;
186 assertThat(yangNode.getName(), is("Test"));
187
188 // Check whether the list is child of module
189 YangList yangList = (YangList) yangNode.getChild();
190 assertThat(yangList.getName(), is("valid"));
191
192 // Check whether leaf-list properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530193 ListIterator<YangLeafList> leafListIterator = yangList.getListOfLeafList().listIterator();
194 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530195
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530196 assertThat(leafListInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530197 assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530198 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
199 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
200 assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
201 assertThat(leafListInfo.isConfig(), is(true));
202
203 assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
204 assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
205 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530206}