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 description listener. |
| 41 | */ |
| 42 | public class DescriptionListenerTest { |
| 43 | |
| 44 | @Rule |
| 45 | public ExpectedException thrown = ExpectedException.none(); |
| 46 | |
| 47 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 48 | |
| 49 | /** |
| 50 | * Checks valid description statement. |
| 51 | */ |
| 52 | @Test |
| 53 | public void processDescriptionValidStatement() throws IOException, ParserException { |
| 54 | |
| 55 | YangNode node = manager.getDataModel("src/test/resources/DescriptionValidStatement.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 description is set correctly. |
| 71 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 72 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Checks whether exception is thrown for invalid description statement. |
| 77 | */ |
| 78 | @Test |
| 79 | public void processDescriptionWithoutStatementEnd() throws IOException, ParserException { |
| 80 | thrown.expect(ParserException.class); |
| 81 | thrown.expectMessage("extraneous input '}' expecting {';', '+'}"); |
| 82 | YangNode node = manager.getDataModel("src/test/resources/DescriptionWithoutStatementEnd.yang"); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Checks valid description statement as sub-statement of module. |
| 87 | */ |
| 88 | @Test |
| 89 | public void processModuleSubStatementDescription() throws IOException, ParserException { |
| 90 | |
| 91 | YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementDescription.yang"); |
| 92 | |
| 93 | // Check whether the data model tree returned is of type module. |
| 94 | assertThat((node instanceof YangModule), is(true)); |
| 95 | |
| 96 | // Check whether the node type is set properly to module. |
| 97 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 98 | |
| 99 | // Check whether the module name is set correctly. |
| 100 | YangModule yangNode = (YangModule) node; |
| 101 | assertThat(yangNode.getName(), is("Test")); |
| 102 | |
| 103 | // Check whether the description is set correctly. |
| 104 | assertThat(yangNode.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Checks valid description statement as sub-statement of module. |
| 109 | */ |
| 110 | @Test |
| 111 | public void processDescriptionEmptyStatement() throws IOException, ParserException { |
| 112 | |
| 113 | YangNode node = manager.getDataModel("src/test/resources/DescriptionEmptyStatement.yang"); |
| 114 | |
| 115 | // Check whether the data model tree returned is of type module. |
| 116 | assertThat((node instanceof YangModule), is(true)); |
| 117 | |
| 118 | // Check whether the node type is set properly to module. |
| 119 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 120 | |
| 121 | // Check whether the module name is set correctly. |
| 122 | YangModule yangNode = (YangModule) node; |
| 123 | assertThat(yangNode.getName(), is("Test")); |
| 124 | |
| 125 | // Check whether the description is set correctly. |
| 126 | assertThat(yangNode.getDescription(), is("\"\"")); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Checks valid description statement as sub-statement of revision. |
| 131 | */ |
| 132 | @Test |
| 133 | public void processRevisionSubStatementRevision() throws IOException, ParserException { |
| 134 | |
| 135 | YangNode node = manager.getDataModel("src/test/resources/RevisionSubStatementRevision.yang"); |
| 136 | |
| 137 | // Check whether the data model tree returned is of type module. |
| 138 | assertThat((node instanceof YangModule), is(true)); |
| 139 | |
| 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 | |
| 147 | // Check whether the description is set correctly. |
| 148 | assertThat(yangNode.getDescription(), is("\"module description\"")); |
| 149 | assertThat(yangNode.getRevision().getDescription(), is("\"revision description\"")); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Checks description statement as sub-statement of container. |
| 154 | */ |
| 155 | @Test |
| 156 | public void processContainerSubStatementDescription() throws IOException, ParserException { |
| 157 | |
| 158 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementDescription.yang"); |
| 159 | |
| 160 | // Check whether the data model tree returned is of type module. |
| 161 | assertThat((node instanceof YangModule), is(true)); |
| 162 | |
| 163 | // Check whether the node type is set properly to module. |
| 164 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 165 | |
| 166 | // Check whether the module name is set correctly. |
| 167 | YangModule yangNode = (YangModule) node; |
| 168 | assertThat(yangNode.getName(), is("Test")); |
| 169 | |
| 170 | // Check whether the description value is set correctly. |
| 171 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 172 | assertThat(container.getName(), is("valid")); |
| 173 | assertThat(container.getDescription(), is("\"container description\"")); |
| 174 | |
| 175 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 176 | ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator(); |
| 177 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 178 | |
| 179 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 180 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 181 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 182 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 183 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 184 | assertThat(leafInfo.isMandatory(), is(true)); |
| 185 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 186 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Checks description statement as sub-statement of list. |
| 191 | */ |
| 192 | @Test |
| 193 | public void processListSubStatementDescription() throws IOException, ParserException { |
| 194 | |
| 195 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementDescription.yang"); |
| 196 | |
| 197 | assertThat((node instanceof YangModule), is(true)); |
| 198 | |
| 199 | // Check whether the node type is set properly to module. |
| 200 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 201 | |
| 202 | // Check whether the module name is set correctly. |
| 203 | YangModule yangNode = (YangModule) node; |
| 204 | assertThat(yangNode.getName(), is("Test")); |
| 205 | |
| 206 | // Check whether the list is child of module and description value is set correctly. |
| 207 | YangList yangList = (YangList) yangNode.getChild(); |
| 208 | assertThat(yangList.getName(), is("valid")); |
| 209 | assertThat(yangList.isConfig(), is(true)); |
| 210 | assertThat(yangList.getDescription(), is("\"list description\"")); |
| 211 | |
| 212 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 213 | ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator(); |
| 214 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 215 | |
| 216 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 217 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 218 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 219 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 220 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 221 | assertThat(leafInfo.isMandatory(), is(true)); |
| 222 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 223 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Checks valid description statement as sub-statement of leaf-list. |
| 228 | */ |
| 229 | @Test |
| 230 | public void processLeafListSubStatementDescription() throws IOException, ParserException { |
| 231 | |
| 232 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementDescription.yang"); |
| 233 | |
| 234 | // Check whether the data model tree returned is of type module. |
| 235 | assertThat((node instanceof YangModule), is(true)); |
| 236 | |
| 237 | // Check whether the node type is set properly to module. |
| 238 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 239 | |
| 240 | // Check whether the module name is set correctly. |
| 241 | YangModule yangNode = (YangModule) node; |
| 242 | assertThat(yangNode.getName(), is("Test")); |
| 243 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 244 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 245 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 246 | |
| 247 | // Check whether description value is set correctly. |
| 248 | assertThat(leafListInfo.getLeafName(), is("invalid-interval")); |
| 249 | assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 250 | } |
| 251 | } |