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 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.util.ListIterator; |
| 21 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 22 | import org.junit.Rule; |
| 23 | import org.junit.Test; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 24 | import org.junit.rules.ExpectedException; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 26 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangLeaf; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 29 | import org.onosproject.yangutils.datamodel.YangList; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangModule; |
| 31 | import org.onosproject.yangutils.datamodel.YangNode; |
| 32 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.datamodel.YangStatusType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 35 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 36 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 37 | import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | import static org.hamcrest.core.Is.is; |
| 39 | |
| 40 | /** |
| 41 | * Test case for reference listener. |
| 42 | */ |
| 43 | public class ReferenceListenerTest { |
| 44 | |
| 45 | @Rule |
| 46 | public ExpectedException thrown = ExpectedException.none(); |
| 47 | |
| 48 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 49 | |
| 50 | /** |
| 51 | * Checks valid reference statement. |
| 52 | */ |
| 53 | @Test |
| 54 | public void processReferenceStatement() throws IOException, ParserException { |
| 55 | |
| 56 | YangNode node = manager.getDataModel("src/test/resources/ReferenceStatement.yang"); |
| 57 | |
| 58 | // Check whether the data model tree returned is of type module. |
| 59 | assertThat((node instanceof YangModule), is(true)); |
| 60 | |
| 61 | // Check whether the node type is set properly to module. |
| 62 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 63 | |
| 64 | // Check whether the module name is set correctly. |
| 65 | YangModule yangNode = (YangModule) node; |
| 66 | assertThat(yangNode.getName(), is("Test")); |
| 67 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 68 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 69 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 70 | |
| 71 | // Check whether the reference is set correctly. |
| 72 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 73 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Checks whether exception is thrown for invalid reference statement. |
| 78 | */ |
| 79 | @Test |
| 80 | public void processReferenceWithoutStatementEnd() throws IOException, ParserException { |
| 81 | thrown.expect(ParserException.class); |
| 82 | thrown.expectMessage("mismatched input '}' expecting {';', '+'}"); |
| 83 | YangNode node = manager.getDataModel("src/test/resources/ReferenceWithoutStatementEnd.yang"); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Checks valid reference statement under module. |
| 88 | */ |
| 89 | @Test |
| 90 | public void processModuleSubStatementReference() throws IOException, ParserException { |
| 91 | |
| 92 | YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementReference.yang"); |
| 93 | |
| 94 | // Check whether the data model tree returned is of type module. |
| 95 | assertThat((node instanceof YangModule), is(true)); |
| 96 | |
| 97 | // Check whether the node type is set properly to module. |
| 98 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 99 | |
| 100 | // Check whether the module name is set correctly. |
| 101 | YangModule yangNode = (YangModule) node; |
| 102 | assertThat(yangNode.getName(), is("Test")); |
| 103 | |
| 104 | // Check whether the reference is set correctly. |
| 105 | assertThat(yangNode.getReference(), is("\"RFC 6020\"")); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Checks valid reference statement under module. |
| 110 | */ |
| 111 | @Test |
| 112 | public void processReferenceEmptyStatement() throws IOException, ParserException { |
| 113 | |
| 114 | YangNode node = manager.getDataModel("src/test/resources/ReferenceEmptyStatement.yang"); |
| 115 | |
| 116 | // Check whether the data model tree returned is of type module. |
| 117 | assertThat((node instanceof YangModule), is(true)); |
| 118 | |
| 119 | // Check whether the node type is set properly to module. |
| 120 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 121 | |
| 122 | // Check whether the module name is set correctly. |
| 123 | YangModule yangNode = (YangModule) node; |
| 124 | assertThat(yangNode.getName(), is("Test")); |
| 125 | |
| 126 | // Check whether the reference is set correctly. |
| 127 | assertThat(yangNode.getReference(), is("\"\"")); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Checks valid reference statement as sub-statement of revision. |
| 132 | */ |
| 133 | @Test |
| 134 | public void processRevisionSubStatementReference() throws IOException, ParserException { |
| 135 | |
| 136 | YangNode node = manager.getDataModel("src/test/resources/RevisionSubStatementReference.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 | |
| 148 | // Check whether the reference is set correctly. |
| 149 | assertThat(yangNode.getRevision().getReference(), is("\"revision reference\"")); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Checks reference statement as sub-statement of container. |
| 154 | */ |
| 155 | @Test |
| 156 | public void processContainerSubStatementReference() throws IOException, ParserException { |
| 157 | |
| 158 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementReference.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 reference value is set correctly. |
| 171 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 172 | assertThat(container.getName(), is("valid")); |
| 173 | assertThat(container.getReference(), is("\"container reference\"")); |
| 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")); |
| 180 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 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 reference statement as sub-statement of list. |
| 191 | */ |
| 192 | @Test |
| 193 | public void processListSubStatementReference() throws IOException, ParserException { |
| 194 | |
| 195 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementReference.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.getReference(), is("\"list reference\"")); |
| 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")); |
| 217 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 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 reference statement as sub-statement of leaf-list. |
| 228 | */ |
| 229 | @Test |
| 230 | public void processLeafListSubStatementReference() throws IOException, ParserException { |
| 231 | |
| 232 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementReference.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.getReference(), is("\"RFC 6020\"")); |
| 250 | } |
| 251 | } |