Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
| 19 | import org.junit.Rule; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import org.junit.rules.ExpectedException; |
| 23 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 24 | import org.onosproject.yangutils.datamodel.YangModule; |
| 25 | import org.onosproject.yangutils.datamodel.YangNode; |
| 26 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 27 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 28 | import org.onosproject.yangutils.datamodel.YangStatusType; |
| 29 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 30 | import org.onosproject.yangutils.datamodel.YangList; |
| 31 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 33 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.util.ListIterator; |
| 37 | |
| 38 | import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | import static org.hamcrest.core.Is.is; |
| 40 | |
| 41 | /** |
| 42 | * Test case for reference listener. |
| 43 | */ |
| 44 | public class ReferenceListenerTest { |
| 45 | |
| 46 | @Rule |
| 47 | public ExpectedException thrown = ExpectedException.none(); |
| 48 | |
| 49 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 50 | |
| 51 | /** |
| 52 | * Checks valid reference statement. |
| 53 | */ |
| 54 | @Test |
| 55 | public void processReferenceStatement() throws IOException, ParserException { |
| 56 | |
| 57 | YangNode node = manager.getDataModel("src/test/resources/ReferenceStatement.yang"); |
| 58 | |
| 59 | // Check whether the data model tree returned is of type module. |
| 60 | assertThat((node instanceof YangModule), is(true)); |
| 61 | |
| 62 | // Check whether the node type is set properly to module. |
| 63 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 64 | |
| 65 | // Check whether the module name is set correctly. |
| 66 | YangModule yangNode = (YangModule) node; |
| 67 | assertThat(yangNode.getName(), is("Test")); |
| 68 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 69 | ListIterator<YangLeaf<?>> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 70 | YangLeaf<?> leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 71 | |
| 72 | // Check whether the reference is set correctly. |
| 73 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 74 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Checks whether exception is thrown for invalid reference statement. |
| 79 | */ |
| 80 | @Test |
| 81 | public void processReferenceWithoutStatementEnd() throws IOException, ParserException { |
| 82 | thrown.expect(ParserException.class); |
| 83 | thrown.expectMessage("mismatched input '}' expecting {';', '+'}"); |
| 84 | YangNode node = manager.getDataModel("src/test/resources/ReferenceWithoutStatementEnd.yang"); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Checks valid reference statement under module. |
| 89 | */ |
| 90 | @Test |
| 91 | public void processModuleSubStatementReference() throws IOException, ParserException { |
| 92 | |
| 93 | YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementReference.yang"); |
| 94 | |
| 95 | // Check whether the data model tree returned is of type module. |
| 96 | assertThat((node instanceof YangModule), is(true)); |
| 97 | |
| 98 | // Check whether the node type is set properly to module. |
| 99 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 100 | |
| 101 | // Check whether the module name is set correctly. |
| 102 | YangModule yangNode = (YangModule) node; |
| 103 | assertThat(yangNode.getName(), is("Test")); |
| 104 | |
| 105 | // Check whether the reference is set correctly. |
| 106 | assertThat(yangNode.getReference(), is("\"RFC 6020\"")); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Checks valid reference statement under module. |
| 111 | */ |
| 112 | @Test |
| 113 | public void processReferenceEmptyStatement() throws IOException, ParserException { |
| 114 | |
| 115 | YangNode node = manager.getDataModel("src/test/resources/ReferenceEmptyStatement.yang"); |
| 116 | |
| 117 | // Check whether the data model tree returned is of type module. |
| 118 | assertThat((node instanceof YangModule), is(true)); |
| 119 | |
| 120 | // Check whether the node type is set properly to module. |
| 121 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 122 | |
| 123 | // Check whether the module name is set correctly. |
| 124 | YangModule yangNode = (YangModule) node; |
| 125 | assertThat(yangNode.getName(), is("Test")); |
| 126 | |
| 127 | // Check whether the reference is set correctly. |
| 128 | assertThat(yangNode.getReference(), is("\"\"")); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Checks valid reference statement as sub-statement of revision. |
| 133 | */ |
| 134 | @Test |
| 135 | public void processRevisionSubStatementReference() throws IOException, ParserException { |
| 136 | |
| 137 | YangNode node = manager.getDataModel("src/test/resources/RevisionSubStatementReference.yang"); |
| 138 | |
| 139 | // Check whether the data model tree returned is of type module. |
| 140 | assertThat((node instanceof YangModule), is(true)); |
| 141 | |
| 142 | // Check whether the node type is set properly to module. |
| 143 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 144 | |
| 145 | // Check whether the module name is set correctly. |
| 146 | YangModule yangNode = (YangModule) node; |
| 147 | assertThat(yangNode.getName(), is("Test")); |
| 148 | |
| 149 | // Check whether the reference is set correctly. |
| 150 | assertThat(yangNode.getRevision().getReference(), is("\"revision reference\"")); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Checks reference statement as sub-statement of container. |
| 155 | */ |
| 156 | @Test |
| 157 | public void processContainerSubStatementReference() throws IOException, ParserException { |
| 158 | |
| 159 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementReference.yang"); |
| 160 | |
| 161 | // Check whether the data model tree returned is of type module. |
| 162 | assertThat((node instanceof YangModule), is(true)); |
| 163 | |
| 164 | // Check whether the node type is set properly to module. |
| 165 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 166 | |
| 167 | // Check whether the module name is set correctly. |
| 168 | YangModule yangNode = (YangModule) node; |
| 169 | assertThat(yangNode.getName(), is("Test")); |
| 170 | |
| 171 | // Check whether the reference value is set correctly. |
| 172 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 173 | assertThat(container.getName(), is("valid")); |
| 174 | assertThat(container.getReference(), is("\"container reference\"")); |
| 175 | |
| 176 | // Check whether leaf properties as set correctly. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 177 | ListIterator<YangLeaf<?>> leafIterator = container.getListOfLeaf().listIterator(); |
| 178 | YangLeaf<?> leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 179 | |
| 180 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 181 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 182 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 183 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 184 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 185 | assertThat(leafInfo.isMandatory(), is(true)); |
| 186 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 187 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Checks reference statement as sub-statement of list. |
| 192 | */ |
| 193 | @Test |
| 194 | public void processListSubStatementReference() throws IOException, ParserException { |
| 195 | |
| 196 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementReference.yang"); |
| 197 | |
| 198 | assertThat((node instanceof YangModule), is(true)); |
| 199 | |
| 200 | // Check whether the node type is set properly to module. |
| 201 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 202 | |
| 203 | // Check whether the module name is set correctly. |
| 204 | YangModule yangNode = (YangModule) node; |
| 205 | assertThat(yangNode.getName(), is("Test")); |
| 206 | |
| 207 | // Check whether the list is child of module and description value is set correctly. |
| 208 | YangList yangList = (YangList) yangNode.getChild(); |
| 209 | assertThat(yangList.getName(), is("valid")); |
| 210 | assertThat(yangList.isConfig(), is(true)); |
| 211 | assertThat(yangList.getReference(), is("\"list reference\"")); |
| 212 | |
| 213 | // Check whether leaf properties as set correctly. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 214 | ListIterator<YangLeaf<?>> leafIterator = yangList.getListOfLeaf().listIterator(); |
| 215 | YangLeaf<?> leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 216 | |
| 217 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 218 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 219 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 220 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 221 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 222 | assertThat(leafInfo.isMandatory(), is(true)); |
| 223 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 224 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Checks valid reference statement as sub-statement of leaf-list. |
| 229 | */ |
| 230 | @Test |
| 231 | public void processLeafListSubStatementReference() throws IOException, ParserException { |
| 232 | |
| 233 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementReference.yang"); |
| 234 | |
| 235 | // Check whether the data model tree returned is of type module. |
| 236 | assertThat((node instanceof YangModule), is(true)); |
| 237 | |
| 238 | // Check whether the node type is set properly to module. |
| 239 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 240 | |
| 241 | // Check whether the module name is set correctly. |
| 242 | YangModule yangNode = (YangModule) node; |
| 243 | assertThat(yangNode.getName(), is("Test")); |
| 244 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 245 | ListIterator<YangLeafList<?>> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 246 | YangLeafList<?> leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 247 | |
| 248 | // Check whether description value is set correctly. |
| 249 | assertThat(leafListInfo.getLeafName(), is("invalid-interval")); |
| 250 | assertThat(leafListInfo.getReference(), is("\"RFC 6020\"")); |
| 251 | } |
| 252 | } |