blob: 516848c52e4e967b31d87fc3c02aca6e3da04a53 [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;
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;
24import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025import org.onosproject.yangutils.datamodel.YangLeafList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053026import org.onosproject.yangutils.datamodel.YangModule;
27import org.onosproject.yangutils.datamodel.YangNode;
28import org.onosproject.yangutils.datamodel.YangNodeType;
29import org.onosproject.yangutils.datamodel.YangStatusType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053030import org.onosproject.yangutils.parser.exceptions.ParserException;
31import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
32
Vidyashree Rama49abe712016-02-13 22:22:12 +053033import static org.hamcrest.MatcherAssert.assertThat;
Vidyashree Rama49abe712016-02-13 22:22:12 +053034import static org.hamcrest.Matchers.nullValue;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053035import static org.hamcrest.core.Is.is;
Vidyashree Rama49abe712016-02-13 22:22:12 +053036
37/**
38 * Test cases for units listener.
39 */
40public class UnitsListenerTest {
41
42 @Rule
43 public ExpectedException thrown = ExpectedException.none();
44
45 private final YangUtilsParserManager manager = new YangUtilsParserManager();
46
47 /**
48 * Checks valid units statement.
49 */
50 @Test
51 public void processUnitsStatement() throws IOException, ParserException {
52
53 YangNode node = manager.getDataModel("src/test/resources/UnitsStatement.yang");
54
55 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +053056 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +053057
58 // Check whether the node type is set properly to module.
59 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
60
61 // Check whether the module name is set correctly.
62 YangModule yangNode = (YangModule) node;
63 assertThat(yangNode.getName(), is("Test"));
64
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053065 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
66 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053067
68 // Check whether units value is set correctly.
69 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
70 assertThat(leafInfo.getUnits(), is("\"seconds\""));
71 }
72
73 /**
74 * Checks invalid units statement as sub-statement of module.
75 */
76 @Test
77 public void processModuleSubStatementUnits() throws IOException, ParserException {
78 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 thrown.expectMessage("mismatched input 'type' expecting {'augment', 'choice', 'contact', 'container', "
80 + "'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', "
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +053081 + "'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', "
82 + "'reference', 'revision', 'rpc', 'typedef', 'uses', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +053083 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementUnits.yang");
84 }
85
86 /**
87 * Checks invalid units statement(without statement end).
88 */
89 @Test
90 public void processUnitsWithoutStatementEnd() throws IOException, ParserException {
91 thrown.expect(ParserException.class);
92 thrown.expectMessage("mismatched input '}' expecting {';', '+'}");
93 YangNode node = manager.getDataModel("src/test/resources/UnitsWithoutStatementEnd.yang");
94 }
95
96 /**
97 * Checks order of units statement in leaf.
98 */
99 @Test
100 public void processUnitsStatementOrder() throws IOException, ParserException {
101
102 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementOrder.yang");
103
104 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530105 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530106
107 // Check whether the node type is set properly to module.
108 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
109
110 // Check whether the module name is set correctly.
111 YangModule yangNode = (YangModule) node;
112 assertThat(yangNode.getName(), is("Test"));
113
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530114 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
115 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530116
117 // Check whether leaf properties is set correctly.
118 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530119 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530120 assertThat(leafInfo.getUnits(), is("\"seconds\""));
121 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
122 assertThat(leafInfo.isConfig(), is(true));
123 assertThat(leafInfo.isMandatory(), is(true));
124 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
125 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
126 }
127
128 /**
129 * Checks the default value of unit statement.
130 */
131 @Test
132 public void processUnitsDefaultValue() throws IOException, ParserException {
133
134 YangNode node = manager.getDataModel("src/test/resources/UnitsDefaultValue.yang");
135
136 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530137 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530138
139 // Check whether the node type is set properly to module.
140 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
141
142 // Check whether the module name is set correctly.
143 YangModule yangNode = (YangModule) node;
144 assertThat(yangNode.getName(), is("Test"));
145
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530146 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
147 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530148
149 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
150 assertThat(leafInfo.getUnits(), is(nullValue()));
151 }
152
153 /**
154 * Checks invalid occurance of units statement as sub-statement of leaf.
155 */
156 @Test
157 public void processUnitsStatementCardinality() throws IOException, ParserException {
158 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530159 thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf invalid-interval\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530160 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementCardinality.yang");
161 }
162
163 /**
164 * Checks valid units statement as sub-statement of leaf-list.
165 */
166 @Test
167 public void processLeafListSubStatementUnits() throws IOException, ParserException {
168
169 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementUnits.yang");
170
171 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530172 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530173
174 // Check whether the node type is set properly to module.
175 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
176
177 // Check whether the module name is set correctly.
178 YangModule yangNode = (YangModule) node;
179 assertThat(yangNode.getName(), is("Test"));
180
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530181 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
182 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530183
184 // Check whether units value is set correctly.
185 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
186 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
187 }
188}