Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.util.ListIterator; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 21 | import org.junit.Rule; |
| 22 | import org.junit.Test; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 23 | import org.junit.rules.ExpectedException; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 25 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangLeaf; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 28 | import org.onosproject.yangutils.datamodel.YangList; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangModule; |
| 30 | import org.onosproject.yangutils.datamodel.YangNode; |
| 31 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.YangStatusType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 34 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 35 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 36 | import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | import static org.hamcrest.core.Is.is; |
| 38 | |
| 39 | /** |
| 40 | * Test cases for status listener. |
| 41 | */ |
| 42 | public class StatusListenerTest { |
| 43 | |
| 44 | @Rule |
| 45 | public ExpectedException thrown = ExpectedException.none(); |
| 46 | |
| 47 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 48 | |
| 49 | /** |
| 50 | * Checks valid status statement. |
| 51 | */ |
| 52 | @Test |
| 53 | public void processStatusStatementCurrent() throws IOException, ParserException { |
| 54 | |
| 55 | YangNode node = manager.getDataModel("src/test/resources/StatusStatementCurrent.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 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 67 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 68 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 69 | |
| 70 | // Check whether the status is set correctly. |
| 71 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 72 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Checks valid status statement. |
| 77 | */ |
| 78 | @Test |
| 79 | public void processStatusStatementDeprecated() throws IOException, ParserException { |
| 80 | |
| 81 | YangNode node = manager.getDataModel("src/test/resources/StatusStatementDeprecated.yang"); |
| 82 | |
| 83 | // Check whether the data model tree returned is of type module. |
| 84 | assertThat((node instanceof YangModule), is(true)); |
| 85 | |
| 86 | // Check whether the node type is set properly to module. |
| 87 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 88 | |
| 89 | // Check whether the module name is set correctly. |
| 90 | YangModule yangNode = (YangModule) node; |
| 91 | assertThat(yangNode.getName(), is("Test")); |
| 92 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 93 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 94 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 95 | |
| 96 | // Check whether the status is set correctly. |
| 97 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 98 | assertThat(leafInfo.getStatus(), is(YangStatusType.DEPRECATED)); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Checks valid status statement. |
| 103 | */ |
| 104 | @Test |
| 105 | public void processStatusStatementObsolete() throws IOException, ParserException { |
| 106 | |
| 107 | YangNode node = manager.getDataModel("src/test/resources/StatusStatementObsolete.yang"); |
| 108 | |
| 109 | // Check whether the data model tree returned is of type module. |
| 110 | assertThat((node instanceof YangModule), is(true)); |
| 111 | |
| 112 | // Check whether the node type is set properly to module. |
| 113 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 114 | |
| 115 | // Check whether the module name is set correctly. |
| 116 | YangModule yangNode = (YangModule) node; |
| 117 | assertThat(yangNode.getName(), is("Test")); |
| 118 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 119 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 120 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 121 | |
| 122 | // Check whether the status is set correctly. |
| 123 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 124 | assertThat(leafInfo.getStatus(), is(YangStatusType.OBSOLETE)); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Checks whether exception is thrown for invalid status statement. |
| 129 | */ |
| 130 | @Test |
| 131 | public void processStatusWithoutStatementEnd() throws IOException, ParserException { |
| 132 | thrown.expect(ParserException.class); |
| 133 | thrown.expectMessage("missing ';' at '}'"); |
| 134 | YangNode node = manager.getDataModel("src/test/resources/StatusWithoutStatementEnd.yang"); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Checks whether exception is thrown for invalid status statement. |
| 139 | */ |
| 140 | @Test |
| 141 | public void processStatusInvalidValue() throws IOException, ParserException { |
| 142 | thrown.expect(ParserException.class); |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 143 | thrown.expectMessage("Invalid content in status \"invalid\" before processing."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 144 | YangNode node = manager.getDataModel("src/test/resources/StatusInvalidValue.yang"); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Checks status statement as sub-statement of module. |
| 149 | */ |
| 150 | @Test |
| 151 | public void processModuleSubStatementStatus() throws IOException, ParserException { |
| 152 | thrown.expect(ParserException.class); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 153 | thrown.expectMessage("mismatched input 'status' expecting {'augment', 'choice', 'contact', 'container', " |
| 154 | + "'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include'," |
Vidyashree Rama | 07021a2 | 2016-03-09 20:41:44 +0530 | [diff] [blame] | 155 | + " 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference', " |
| 156 | + "'revision', 'rpc', 'typedef', 'uses', '}'}"); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 157 | YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementStatus.yang"); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Checks status statement as sub-statement of container. |
| 162 | */ |
| 163 | @Test |
| 164 | public void processContainerSubStatementStatus() throws IOException, ParserException { |
| 165 | |
| 166 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementStatus.yang"); |
| 167 | |
| 168 | // Check whether the data model tree returned is of type module. |
| 169 | assertThat((node instanceof YangModule), is(true)); |
| 170 | |
| 171 | // Check whether the node type is set properly to module. |
| 172 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 173 | |
| 174 | // Check whether the module name is set correctly. |
| 175 | YangModule yangNode = (YangModule) node; |
| 176 | assertThat(yangNode.getName(), is("Test")); |
| 177 | |
| 178 | // Check whether status is set correctly. |
| 179 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 180 | assertThat(container.getName(), is("valid")); |
| 181 | assertThat(container.isConfig(), is(true)); |
| 182 | assertThat(container.getStatus(), is(YangStatusType.OBSOLETE)); |
| 183 | |
| 184 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 185 | ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator(); |
| 186 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 187 | |
| 188 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 189 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 190 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 191 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 192 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 193 | assertThat(leafInfo.isMandatory(), is(true)); |
| 194 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 195 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Checks status statement as sub-statement of list. |
| 200 | */ |
| 201 | @Test |
| 202 | public void processListSubStatementStatus() throws IOException, ParserException { |
| 203 | |
| 204 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementStatus.yang"); |
| 205 | |
| 206 | assertThat((node instanceof YangModule), is(true)); |
| 207 | |
| 208 | // Check whether the node type is set properly to module. |
| 209 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 210 | |
| 211 | // Check whether the module name is set correctly. |
| 212 | YangModule yangNode = (YangModule) node; |
| 213 | assertThat(yangNode.getName(), is("Test")); |
| 214 | |
| 215 | // Check whether the list is child of module and status is set. |
| 216 | YangList yangList = (YangList) yangNode.getChild(); |
| 217 | assertThat(yangList.getName(), is("valid")); |
| 218 | assertThat(yangList.isConfig(), is(true)); |
| 219 | assertThat(yangList.getStatus(), is(YangStatusType.CURRENT)); |
| 220 | |
| 221 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 222 | ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator(); |
| 223 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 224 | |
| 225 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 226 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 227 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 228 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 229 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 230 | assertThat(leafInfo.isMandatory(), is(true)); |
| 231 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 232 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Checks valid status statement as sub-statement of leaf-list. |
| 237 | */ |
| 238 | @Test |
| 239 | public void processLeafListSubStatementStatus() throws IOException, ParserException { |
| 240 | |
| 241 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementStatus.yang"); |
| 242 | |
| 243 | // Check whether the data model tree returned is of type module. |
| 244 | assertThat((node instanceof YangModule), is(true)); |
| 245 | |
| 246 | // Check whether the node type is set properly to module. |
| 247 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 248 | |
| 249 | // Check whether the module name is set correctly. |
| 250 | YangModule yangNode = (YangModule) node; |
| 251 | assertThat(yangNode.getName(), is("Test")); |
| 252 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 253 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 254 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 255 | |
| 256 | // Check whether status is set correctly. |
| 257 | assertThat(leafListInfo.getLeafName(), is("invalid-interval")); |
| 258 | assertThat(leafListInfo.isConfig(), is(true)); |
| 259 | assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 260 | } |
Vidyashree Rama | ec578d0 | 2016-02-24 11:26:42 +0530 | [diff] [blame] | 261 | |
| 262 | /** |
| 263 | * Checks default value of status statement. |
| 264 | */ |
| 265 | @Test |
| 266 | public void processStatusDefaultValue() throws IOException, ParserException { |
| 267 | |
| 268 | YangNode node = manager.getDataModel("src/test/resources/StatusDefaultValue.yang"); |
| 269 | |
| 270 | // Check whether the data model tree returned is of type module. |
| 271 | assertThat((node instanceof YangModule), is(true)); |
| 272 | |
| 273 | // Check whether the node type is set properly to module. |
| 274 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 275 | |
| 276 | // Check whether the module name is set correctly. |
| 277 | YangModule yangNode = (YangModule) node; |
| 278 | assertThat(yangNode.getName(), is("Test")); |
| 279 | |
| 280 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 281 | YangLeafList leafListInfo = leafListIterator.next(); |
| 282 | |
| 283 | // Check whether status is set correctly. |
| 284 | assertThat(leafListInfo.getLeafName(), is("invalid-interval")); |
| 285 | assertThat(leafListInfo.isConfig(), is(true)); |
| 286 | assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 287 | } |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 288 | } |