blob: 71a3042da6b2fa50c75b7ba558b15038b83d5b54 [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;
21
Vidyashree Rama49abe712016-02-13 22:22:12 +053022import org.junit.Rule;
23import org.junit.Test;
Vidyashree Rama49abe712016-02-13 22:22:12 +053024import org.junit.rules.ExpectedException;
25import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053026import org.onosproject.yangutils.datamodel.YangLeafList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053027import org.onosproject.yangutils.datamodel.YangModule;
28import org.onosproject.yangutils.datamodel.YangNode;
29import org.onosproject.yangutils.datamodel.YangNodeType;
30import org.onosproject.yangutils.datamodel.YangStatusType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053031import org.onosproject.yangutils.parser.exceptions.ParserException;
32import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
33
Vidyashree Rama49abe712016-02-13 22:22:12 +053034import static org.hamcrest.MatcherAssert.assertThat;
Vidyashree Rama49abe712016-02-13 22:22:12 +053035import static org.hamcrest.Matchers.nullValue;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053036import static org.hamcrest.core.Is.is;
Vidyashree Rama49abe712016-02-13 22:22:12 +053037
38/**
39 * Test cases for units listener.
40 */
41public class UnitsListenerTest {
42
43 @Rule
44 public ExpectedException thrown = ExpectedException.none();
45
46 private final YangUtilsParserManager manager = new YangUtilsParserManager();
47
48 /**
49 * Checks valid units statement.
50 */
51 @Test
52 public void processUnitsStatement() throws IOException, ParserException {
53
54 YangNode node = manager.getDataModel("src/test/resources/UnitsStatement.yang");
55
56 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +053057 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +053058
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<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
67 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053068
69 // Check whether units value is set correctly.
70 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
71 assertThat(leafInfo.getUnits(), is("\"seconds\""));
72 }
73
74 /**
75 * Checks invalid units statement as sub-statement of module.
76 */
77 @Test
78 public void processModuleSubStatementUnits() throws IOException, ParserException {
79 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +053080 thrown.expectMessage("mismatched input 'type' expecting {'augment', 'choice', 'contact', 'container', "
81 + "'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', "
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +053082 + "'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', "
83 + "'reference', 'revision', 'rpc', 'typedef', 'uses', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +053084 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementUnits.yang");
85 }
86
87 /**
88 * Checks invalid units statement(without statement end).
89 */
90 @Test
91 public void processUnitsWithoutStatementEnd() throws IOException, ParserException {
92 thrown.expect(ParserException.class);
93 thrown.expectMessage("mismatched input '}' expecting {';', '+'}");
94 YangNode node = manager.getDataModel("src/test/resources/UnitsWithoutStatementEnd.yang");
95 }
96
97 /**
98 * Checks order of units statement in leaf.
99 */
100 @Test
101 public void processUnitsStatementOrder() throws IOException, ParserException {
102
103 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementOrder.yang");
104
105 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530106 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530107
108 // Check whether the node type is set properly to module.
109 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
110
111 // Check whether the module name is set correctly.
112 YangModule yangNode = (YangModule) node;
113 assertThat(yangNode.getName(), is("Test"));
114
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530115 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
116 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530117
118 // Check whether leaf properties is set correctly.
119 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
120 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
121 assertThat(leafInfo.getUnits(), is("\"seconds\""));
122 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
123 assertThat(leafInfo.isConfig(), is(true));
124 assertThat(leafInfo.isMandatory(), is(true));
125 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
126 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
127 }
128
129 /**
130 * Checks the default value of unit statement.
131 */
132 @Test
133 public void processUnitsDefaultValue() throws IOException, ParserException {
134
135 YangNode node = manager.getDataModel("src/test/resources/UnitsDefaultValue.yang");
136
137 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530138 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530139
140 // Check whether the node type is set properly to module.
141 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
142
143 // Check whether the module name is set correctly.
144 YangModule yangNode = (YangModule) node;
145 assertThat(yangNode.getName(), is("Test"));
146
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530147 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
148 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530149
150 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
151 assertThat(leafInfo.getUnits(), is(nullValue()));
152 }
153
154 /**
155 * Checks invalid occurance of units statement as sub-statement of leaf.
156 */
157 @Test
158 public void processUnitsStatementCardinality() throws IOException, ParserException {
159 thrown.expect(ParserException.class);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 thrown.expectMessage("YANG file error: Invalid cardinality of units in leaf \"invalid-interval\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530161 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementCardinality.yang");
162 }
163
164 /**
165 * Checks valid units statement as sub-statement of leaf-list.
166 */
167 @Test
168 public void processLeafListSubStatementUnits() throws IOException, ParserException {
169
170 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementUnits.yang");
171
172 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530173 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530174
175 // Check whether the node type is set properly to module.
176 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
177
178 // Check whether the module name is set correctly.
179 YangModule yangNode = (YangModule) node;
180 assertThat(yangNode.getName(), is("Test"));
181
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530182 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
183 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530184
185 // Check whether units value is set correctly.
186 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
187 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
188 }
189}