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.parser.exceptions.ParserException; |
| 32 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.util.ListIterator; |
| 36 | |
| 37 | import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | import static org.hamcrest.core.Is.is; |
| 39 | |
| 40 | /** |
| 41 | * Test cases for testing leaf listener. |
| 42 | */ |
| 43 | public class LeafListenerTest { |
| 44 | |
| 45 | @Rule |
| 46 | public ExpectedException thrown = ExpectedException.none(); |
| 47 | |
| 48 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 49 | |
| 50 | /** |
| 51 | * Checks all the values of leaf sub-statements are set |
| 52 | * correctly. |
| 53 | */ |
| 54 | @Test |
| 55 | public void processLeafSubStatements() throws IOException, ParserException { |
| 56 | |
| 57 | YangNode node = manager.getDataModel("src/test/resources/LeafSubStatements.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 | |
| 69 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 70 | YangLeaf leafInfo = leafIterator.next(); |
| 71 | |
| 72 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 73 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 74 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 75 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 76 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 77 | assertThat(leafInfo.isConfig(), is(true)); |
| 78 | assertThat(leafInfo.isMandatory(), is(true)); |
| 79 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 80 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Checks whether exception is thrown when leaf identifier |
| 85 | * starts with digit. |
| 86 | */ |
| 87 | @Test |
| 88 | public void processLeafInvalidIdentifier() throws IOException, ParserException { |
| 89 | thrown.expect(ParserException.class); |
| 90 | thrown.expectMessage("mismatched input '1invalid-interval' expecting IDENTIFIER"); |
| 91 | YangNode node = manager.getDataModel("src/test/resources/LeafInvalidIdentifier.yang"); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Checks whether exception is thrown when leaf keyword |
| 96 | * is incorrect. |
| 97 | */ |
| 98 | @Test |
| 99 | public void processLeafInvalidStatement() throws IOException, ParserException { |
| 100 | thrown.expect(ParserException.class); |
| 101 | thrown.expectMessage("mismatched input 'leafs' expecting {'augment', 'choice', 'contact', 'container'," + |
| 102 | " 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include'," + |
| 103 | " 'leaf', 'leaf-list', 'list', 'namespace', 'notification', 'organization', 'prefix', 'reference'," + |
| 104 | " 'revision', 'rpc', 'typedef', 'uses', 'yang-version', '}'}"); |
| 105 | YangNode node = manager.getDataModel("src/test/resources/LeafInvalidStatement.yang"); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Checks whether exception is thrown when leaf keyword |
| 110 | * without Left brace as per grammar. |
| 111 | */ |
| 112 | @Test |
| 113 | public void processLeafWithoutLeftBrace() throws IOException, ParserException { |
| 114 | thrown.expect(ParserException.class); |
| 115 | thrown.expectMessage("missing '{' at 'type'"); |
| 116 | YangNode node = manager.getDataModel("src/test/resources/LeafWithoutLeftBrace.yang"); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Checks whether exception is thrown when config statement |
| 121 | * cardinality is not as per grammar. |
| 122 | */ |
| 123 | @Test |
| 124 | public void processLeafConfigInvalidCardinality() throws IOException, ParserException { |
| 125 | thrown.expect(ParserException.class); |
| 126 | thrown.expectMessage("Internal parser error detected: Invalid cardinality in config before processing."); |
| 127 | YangNode node = manager.getDataModel("src/test/resources/LeafConfigInvalidCardinality.yang"); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Checks whether exception is thrown when mandatory statement |
| 132 | * cardinality is not as per grammar. |
| 133 | */ |
| 134 | @Test |
| 135 | public void processLeafMandatoryInvalidCardinality() throws IOException, ParserException { |
| 136 | thrown.expect(ParserException.class); |
| 137 | thrown.expectMessage("Internal parser error detected: Invalid cardinality in mandatory before processing."); |
| 138 | YangNode node = manager.getDataModel("src/test/resources/LeafMandatoryInvalidCardinality.yang"); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Checks leaf statement as sub-statement of container. |
| 143 | */ |
| 144 | @Test |
| 145 | public void processContainerSubStatementLeaf() throws IOException, ParserException { |
| 146 | |
| 147 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeaf.yang"); |
| 148 | |
| 149 | // Check whether the data model tree returned is of type module. |
| 150 | assertThat((node instanceof YangModule), is(true)); |
| 151 | |
| 152 | // Check whether the node type is set properly to module. |
| 153 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 154 | |
| 155 | // Check whether the module name is set correctly. |
| 156 | YangModule yangNode = (YangModule) node; |
| 157 | assertThat(yangNode.getName(), is("Test")); |
| 158 | |
| 159 | //Check whether the container is child of module. |
| 160 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 161 | assertThat(container.getName(), is("valid")); |
| 162 | |
| 163 | // Check whether leaf properties as set correctly. |
| 164 | ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator(); |
| 165 | YangLeaf leafInfo = leafIterator.next(); |
| 166 | |
| 167 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 168 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 169 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 170 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 171 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 172 | assertThat(leafInfo.isConfig(), is(true)); |
| 173 | assertThat(leafInfo.isMandatory(), is(true)); |
| 174 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 175 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Checks leaf statement as sub-statement of list. |
| 180 | */ |
| 181 | @Test |
| 182 | public void processListSubStatementLeaf() throws IOException, ParserException { |
| 183 | |
| 184 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeaf.yang"); |
| 185 | |
| 186 | assertThat((node instanceof YangModule), is(true)); |
| 187 | |
| 188 | // Check whether the node type is set properly to module. |
| 189 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 190 | |
| 191 | // Check whether the module name is set correctly. |
| 192 | YangModule yangNode = (YangModule) node; |
| 193 | assertThat(yangNode.getName(), is("Test")); |
| 194 | |
| 195 | // Check whether the list is child of module |
| 196 | YangList yangList = (YangList) yangNode.getChild(); |
| 197 | assertThat(yangList.getName(), is("valid")); |
| 198 | |
| 199 | // Check whether leaf properties as set correctly. |
| 200 | ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator(); |
| 201 | YangLeaf leafInfo = leafIterator.next(); |
| 202 | |
| 203 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 204 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 205 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 206 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 207 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 208 | assertThat(leafInfo.isConfig(), is(true)); |
| 209 | assertThat(leafInfo.isMandatory(), is(true)); |
| 210 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 211 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 212 | } |
| 213 | } |