blob: c89138b99638f62cd8bb60b69dab9f6fa1896796 [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
19import org.junit.Rule;
20import org.junit.Test;
21
22import org.junit.rules.ExpectedException;
23import org.onosproject.yangutils.datamodel.YangLeaf;
24import org.onosproject.yangutils.datamodel.YangModule;
25import org.onosproject.yangutils.datamodel.YangNode;
26import org.onosproject.yangutils.datamodel.YangNodeType;
27import org.onosproject.yangutils.datamodel.YangStatusType;
28import org.onosproject.yangutils.datamodel.YangLeafList;
29import org.onosproject.yangutils.parser.exceptions.ParserException;
30import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
31
32import java.io.IOException;
33import java.util.ListIterator;
34
35import static org.hamcrest.MatcherAssert.assertThat;
36import static org.hamcrest.core.Is.is;
37import static org.hamcrest.Matchers.nullValue;
38
39/**
40 * Test cases for units listener.
41 */
42public class UnitsListenerTest {
43
44 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
47 private final YangUtilsParserManager manager = new YangUtilsParserManager();
48
49 /**
50 * Checks valid units statement.
51 */
52 @Test
53 public void processUnitsStatement() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/UnitsStatement.yang");
56
57 // Check whether the data model tree returned is of type module.
58 assertThat((node instanceof YangModule), is(true));
59
60 // Check whether the node type is set properly to module.
61 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
62
63 // Check whether the module name is set correctly.
64 YangModule yangNode = (YangModule) node;
65 assertThat(yangNode.getName(), is("Test"));
66
Bharat saraswal870c56f2016-02-20 21:57:16 +053067 ListIterator<YangLeaf<?>> leafIterator = yangNode.getListOfLeaf().listIterator();
68 YangLeaf<?> leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053069
70 // Check whether units value is set correctly.
71 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
72 assertThat(leafInfo.getUnits(), is("\"seconds\""));
73 }
74
75 /**
76 * Checks invalid units statement as sub-statement of module.
77 */
78 @Test
79 public void processModuleSubStatementUnits() throws IOException, ParserException {
80 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +053081 thrown.expectMessage("mismatched input 'type' expecting {'augment', 'choice', 'contact', 'container', "
82 + "'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', "
83 + "'include', 'leaf', 'leaf-list', 'list', 'namespace', 'notification', 'organization', "
84 + "'prefix', 'reference', 'revision', 'rpc', 'typedef', 'uses', 'yang-version', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +053085 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementUnits.yang");
86 }
87
88 /**
89 * Checks invalid units statement(without statement end).
90 */
91 @Test
92 public void processUnitsWithoutStatementEnd() throws IOException, ParserException {
93 thrown.expect(ParserException.class);
94 thrown.expectMessage("mismatched input '}' expecting {';', '+'}");
95 YangNode node = manager.getDataModel("src/test/resources/UnitsWithoutStatementEnd.yang");
96 }
97
98 /**
99 * Checks order of units statement in leaf.
100 */
101 @Test
102 public void processUnitsStatementOrder() throws IOException, ParserException {
103
104 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementOrder.yang");
105
106 // Check whether the data model tree returned is of type module.
107 assertThat((node instanceof YangModule), is(true));
108
109 // Check whether the node type is set properly to module.
110 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
111
112 // Check whether the module name is set correctly.
113 YangModule yangNode = (YangModule) node;
114 assertThat(yangNode.getName(), is("Test"));
115
Bharat saraswal870c56f2016-02-20 21:57:16 +0530116 ListIterator<YangLeaf<?>> leafIterator = yangNode.getListOfLeaf().listIterator();
117 YangLeaf<?> leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530118
119 // Check whether leaf properties is set correctly.
120 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
121 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
122 assertThat(leafInfo.getUnits(), is("\"seconds\""));
123 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
124 assertThat(leafInfo.isConfig(), is(true));
125 assertThat(leafInfo.isMandatory(), is(true));
126 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
127 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
128 }
129
130 /**
131 * Checks the default value of unit statement.
132 */
133 @Test
134 public void processUnitsDefaultValue() throws IOException, ParserException {
135
136 YangNode node = manager.getDataModel("src/test/resources/UnitsDefaultValue.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
Bharat saraswal870c56f2016-02-20 21:57:16 +0530148 ListIterator<YangLeaf<?>> leafIterator = yangNode.getListOfLeaf().listIterator();
149 YangLeaf<?> leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530150
151 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
152 assertThat(leafInfo.getUnits(), is(nullValue()));
153 }
154
155 /**
156 * Checks invalid occurance of units statement as sub-statement of leaf.
157 */
158 @Test
159 public void processUnitsStatementCardinality() throws IOException, ParserException {
160 thrown.expect(ParserException.class);
161 thrown.expectMessage("Internal parser error detected: Invalid cardinality in units before processing.");
162 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementCardinality.yang");
163 }
164
165 /**
166 * Checks valid units statement as sub-statement of leaf-list.
167 */
168 @Test
169 public void processLeafListSubStatementUnits() throws IOException, ParserException {
170
171 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementUnits.yang");
172
173 // Check whether the data model tree returned is of type module.
174 assertThat((node instanceof YangModule), is(true));
175
176 // Check whether the node type is set properly to module.
177 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
178
179 // Check whether the module name is set correctly.
180 YangModule yangNode = (YangModule) node;
181 assertThat(yangNode.getName(), is("Test"));
182
Bharat saraswal870c56f2016-02-20 21:57:16 +0530183 ListIterator<YangLeafList<?>> leafListIterator = yangNode.getListOfLeafList().listIterator();
184 YangLeafList<?> leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530185
186 // Check whether units value is set correctly.
187 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
188 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
189 }
190}