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.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 testing leaf listener. |
| 41 | */ |
| 42 | public class LeafListenerTest { |
| 43 | |
| 44 | @Rule |
| 45 | public ExpectedException thrown = ExpectedException.none(); |
| 46 | |
| 47 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 48 | |
| 49 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 50 | * Checks all the values of leaf sub-statements are set correctly. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 51 | */ |
| 52 | @Test |
| 53 | public void processLeafSubStatements() throws IOException, ParserException { |
| 54 | |
| 55 | YangNode node = manager.getDataModel("src/test/resources/LeafSubStatements.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 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 71 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 72 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 73 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 74 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 75 | assertThat(leafInfo.isConfig(), is(true)); |
| 76 | assertThat(leafInfo.isMandatory(), is(true)); |
| 77 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 78 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 79 | } |
| 80 | |
| 81 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 82 | * Checks whether exception is thrown when leaf identifier starts with |
| 83 | * digit. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 84 | */ |
| 85 | @Test |
| 86 | public void processLeafInvalidIdentifier() throws IOException, ParserException { |
| 87 | thrown.expect(ParserException.class); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 88 | thrown.expectMessage("YANG file error : leaf name 1invalid-interval is not valid."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 89 | YangNode node = manager.getDataModel("src/test/resources/LeafInvalidIdentifier.yang"); |
| 90 | } |
| 91 | |
| 92 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 93 | * Checks whether exception is thrown when leaf keyword is incorrect. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 94 | */ |
| 95 | @Test |
| 96 | public void processLeafInvalidStatement() throws IOException, ParserException { |
| 97 | thrown.expect(ParserException.class); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 98 | thrown.expectMessage("mismatched input 'leafs' expecting {'augment', 'choice', 'contact', 'container'," |
| 99 | + " 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include'," |
Vidyashree Rama | 07021a2 | 2016-03-09 20:41:44 +0530 | [diff] [blame^] | 100 | + " 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference'," |
| 101 | + " 'revision', 'rpc', 'typedef', 'uses', '}'}"); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 102 | YangNode node = manager.getDataModel("src/test/resources/LeafInvalidStatement.yang"); |
| 103 | } |
| 104 | |
| 105 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 106 | * Checks whether exception is thrown when leaf keyword without Left brace |
| 107 | * as per grammar. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 108 | */ |
| 109 | @Test |
| 110 | public void processLeafWithoutLeftBrace() throws IOException, ParserException { |
| 111 | thrown.expect(ParserException.class); |
| 112 | thrown.expectMessage("missing '{' at 'type'"); |
| 113 | YangNode node = manager.getDataModel("src/test/resources/LeafWithoutLeftBrace.yang"); |
| 114 | } |
| 115 | |
| 116 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 117 | * Checks whether exception is thrown when config statement cardinality is |
| 118 | * not as per grammar. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 119 | */ |
| 120 | @Test |
| 121 | public void processLeafConfigInvalidCardinality() throws IOException, ParserException { |
| 122 | thrown.expect(ParserException.class); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 123 | thrown.expectMessage("YANG file error: Invalid cardinality of config in leaf \"invalid-interval\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 124 | YangNode node = manager.getDataModel("src/test/resources/LeafConfigInvalidCardinality.yang"); |
| 125 | } |
| 126 | |
| 127 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 128 | * Checks whether exception is thrown when mandatory statement cardinality |
| 129 | * is not as per grammar. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 130 | */ |
| 131 | @Test |
| 132 | public void processLeafMandatoryInvalidCardinality() throws IOException, ParserException { |
| 133 | thrown.expect(ParserException.class); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 134 | thrown.expectMessage("YANG file error: Invalid cardinality of mandatory in leaf \"invalid-interval\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 135 | YangNode node = manager.getDataModel("src/test/resources/LeafMandatoryInvalidCardinality.yang"); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Checks leaf statement as sub-statement of container. |
| 140 | */ |
| 141 | @Test |
| 142 | public void processContainerSubStatementLeaf() throws IOException, ParserException { |
| 143 | |
| 144 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeaf.yang"); |
| 145 | |
| 146 | // Check whether the data model tree returned is of type module. |
| 147 | assertThat((node instanceof YangModule), is(true)); |
| 148 | |
| 149 | // Check whether the node type is set properly to module. |
| 150 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 151 | |
| 152 | // Check whether the module name is set correctly. |
| 153 | YangModule yangNode = (YangModule) node; |
| 154 | assertThat(yangNode.getName(), is("Test")); |
| 155 | |
| 156 | //Check whether the container is child of module. |
| 157 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 158 | assertThat(container.getName(), is("valid")); |
| 159 | |
| 160 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 161 | ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator(); |
| 162 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 163 | |
| 164 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 165 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 166 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 167 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 168 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 169 | assertThat(leafInfo.isConfig(), is(true)); |
| 170 | assertThat(leafInfo.isMandatory(), is(true)); |
| 171 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 172 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 173 | } |
| 174 | |
| 175 | /** |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 176 | * Checks duplicate leaf statement as sub-statement of module. |
| 177 | */ |
| 178 | @Test(expected = ParserException.class) |
| 179 | public void processModuleWithDuplicateLeaf() throws IOException, ParserException { |
| 180 | |
| 181 | YangNode node = manager.getDataModel("src/test/resources/ModuleWithDuplicateLeaf.yang"); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Checks duplicate leaf statement as sub-statement of container. |
| 186 | */ |
| 187 | @Test(expected = ParserException.class) |
| 188 | public void processContainerWithDuplicateLeaf() throws IOException, ParserException { |
| 189 | |
| 190 | YangNode node = manager.getDataModel("src/test/resources/ContainerWithDuplicateLeaf.yang"); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Checks duplicate leaf statement as sub-statement of list. |
| 195 | */ |
| 196 | @Test(expected = ParserException.class) |
| 197 | public void processListWithDuplicateLeaf() throws IOException, ParserException { |
| 198 | |
| 199 | YangNode node = manager.getDataModel("src/test/resources/ListWithDuplicateLeaf.yang"); |
| 200 | } |
| 201 | |
| 202 | /** |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 203 | * Checks leaf statement as sub-statement of list. |
| 204 | */ |
| 205 | @Test |
| 206 | public void processListSubStatementLeaf() throws IOException, ParserException { |
| 207 | |
| 208 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeaf.yang"); |
| 209 | |
| 210 | assertThat((node instanceof YangModule), is(true)); |
| 211 | |
| 212 | // Check whether the node type is set properly to module. |
| 213 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 214 | |
| 215 | // Check whether the module name is set correctly. |
| 216 | YangModule yangNode = (YangModule) node; |
| 217 | assertThat(yangNode.getName(), is("Test")); |
| 218 | |
| 219 | // Check whether the list is child of module |
| 220 | YangList yangList = (YangList) yangNode.getChild(); |
| 221 | assertThat(yangList.getName(), is("valid")); |
| 222 | |
| 223 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 224 | ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator(); |
| 225 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 226 | |
| 227 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 228 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 229 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 230 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 231 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 232 | assertThat(leafInfo.isConfig(), is(true)); |
| 233 | assertThat(leafInfo.isMandatory(), is(true)); |
| 234 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 235 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 236 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 237 | } |