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; |
| 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; |
Gaurav Agrawal | a8391d0 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.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 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 70 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 71 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 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 without Left brace |
| 94 | * as per grammar. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 95 | */ |
| 96 | @Test |
| 97 | public void processLeafWithoutLeftBrace() throws IOException, ParserException { |
| 98 | thrown.expect(ParserException.class); |
| 99 | thrown.expectMessage("missing '{' at 'type'"); |
| 100 | YangNode node = manager.getDataModel("src/test/resources/LeafWithoutLeftBrace.yang"); |
| 101 | } |
| 102 | |
| 103 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 104 | * Checks whether exception is thrown when config statement cardinality is |
| 105 | * not as per grammar. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 106 | */ |
| 107 | @Test |
| 108 | public void processLeafConfigInvalidCardinality() throws IOException, ParserException { |
| 109 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 110 | thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf invalid-interval\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 111 | YangNode node = manager.getDataModel("src/test/resources/LeafConfigInvalidCardinality.yang"); |
| 112 | } |
| 113 | |
| 114 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 115 | * Checks whether exception is thrown when mandatory statement cardinality |
| 116 | * is not as per grammar. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 117 | */ |
| 118 | @Test |
| 119 | public void processLeafMandatoryInvalidCardinality() throws IOException, ParserException { |
| 120 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 121 | thrown.expectMessage("YANG file error: \"mandatory\" is defined more than once in \"leaf invalid-interval\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 122 | YangNode node = manager.getDataModel("src/test/resources/LeafMandatoryInvalidCardinality.yang"); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Checks leaf statement as sub-statement of container. |
| 127 | */ |
| 128 | @Test |
| 129 | public void processContainerSubStatementLeaf() throws IOException, ParserException { |
| 130 | |
| 131 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeaf.yang"); |
| 132 | |
| 133 | // Check whether the data model tree returned is of type module. |
| 134 | assertThat((node instanceof YangModule), is(true)); |
| 135 | |
| 136 | // Check whether the node type is set properly to module. |
| 137 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 138 | |
| 139 | // Check whether the module name is set correctly. |
| 140 | YangModule yangNode = (YangModule) node; |
| 141 | assertThat(yangNode.getName(), is("Test")); |
| 142 | |
| 143 | //Check whether the container is child of module. |
| 144 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 145 | assertThat(container.getName(), is("valid")); |
| 146 | |
| 147 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 148 | ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator(); |
| 149 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 150 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 151 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 152 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 153 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 154 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 155 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 156 | assertThat(leafInfo.isConfig(), is(true)); |
| 157 | assertThat(leafInfo.isMandatory(), is(true)); |
| 158 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 159 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 160 | } |
| 161 | |
| 162 | /** |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 163 | * Checks duplicate leaf statement as sub-statement of module. |
| 164 | */ |
| 165 | @Test(expected = ParserException.class) |
| 166 | public void processModuleWithDuplicateLeaf() throws IOException, ParserException { |
| 167 | |
| 168 | YangNode node = manager.getDataModel("src/test/resources/ModuleWithDuplicateLeaf.yang"); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Checks duplicate leaf statement as sub-statement of container. |
| 173 | */ |
| 174 | @Test(expected = ParserException.class) |
| 175 | public void processContainerWithDuplicateLeaf() throws IOException, ParserException { |
| 176 | |
| 177 | YangNode node = manager.getDataModel("src/test/resources/ContainerWithDuplicateLeaf.yang"); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Checks duplicate leaf statement as sub-statement of list. |
| 182 | */ |
| 183 | @Test(expected = ParserException.class) |
| 184 | public void processListWithDuplicateLeaf() throws IOException, ParserException { |
| 185 | |
| 186 | YangNode node = manager.getDataModel("src/test/resources/ListWithDuplicateLeaf.yang"); |
| 187 | } |
| 188 | |
| 189 | /** |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 190 | * Checks leaf statement as sub-statement of list. |
| 191 | */ |
| 192 | @Test |
| 193 | public void processListSubStatementLeaf() throws IOException, ParserException { |
| 194 | |
| 195 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeaf.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 |
| 207 | YangList yangList = (YangList) yangNode.getChild(); |
| 208 | assertThat(yangList.getName(), is("valid")); |
| 209 | |
| 210 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 211 | ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator(); |
| 212 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 213 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 214 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 215 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 216 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 217 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 218 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 219 | assertThat(leafInfo.isConfig(), is(true)); |
| 220 | assertThat(leafInfo.isMandatory(), is(true)); |
| 221 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 222 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 223 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 224 | } |