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; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 21 | import org.junit.Rule; |
| 22 | import org.junit.Test; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 23 | import org.junit.rules.ExpectedException; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 25 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangList; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangModule; |
| 29 | import org.onosproject.yangutils.datamodel.YangNode; |
| 30 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangStatusType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 33 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 34 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 35 | import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | import static org.hamcrest.core.Is.is; |
| 37 | |
| 38 | /** |
| 39 | * Test cases for testing leaf-list listener. |
| 40 | */ |
| 41 | public class LeafListListenerTest { |
| 42 | |
| 43 | @Rule |
| 44 | public ExpectedException thrown = ExpectedException.none(); |
| 45 | |
| 46 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 47 | |
| 48 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 49 | * Checks all the values of leaf-list sub-statements are set correctly. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 50 | */ |
| 51 | @Test |
| 52 | public void processLeafListSubStatements() throws IOException, ParserException { |
| 53 | |
| 54 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatements.yang"); |
| 55 | |
| 56 | // Check whether the data model tree returned is of type module. |
| 57 | assertThat((node instanceof YangModule), is(true)); |
| 58 | |
| 59 | // Check whether the node type is set properly to module. |
| 60 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 61 | |
| 62 | // Check whether the module name is set correctly. |
| 63 | YangModule yangNode = (YangModule) node; |
| 64 | assertThat(yangNode.getName(), is("Test")); |
| 65 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 66 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 67 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 68 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 69 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 70 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 71 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 72 | assertThat(leafListInfo.getUnits(), is("\"seconds\"")); |
| 73 | assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 74 | assertThat(leafListInfo.isConfig(), is(true)); |
| 75 | assertThat(leafListInfo.getMaxElelements(), is(3)); |
| 76 | assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 77 | assertThat(leafListInfo.getReference(), is("\"RFC 6020\"")); |
| 78 | } |
| 79 | |
| 80 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 81 | * Checks whether exception is thrown when leaf-list identifier starts with |
| 82 | * digit. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 83 | */ |
| 84 | @Test |
| 85 | public void processLeafListInvalidIdentifier() throws IOException, ParserException { |
| 86 | thrown.expect(ParserException.class); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 87 | thrown.expectMessage("YANG file error : leaf-list name 1invalid-interval is not valid."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 88 | YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidIdentifier.yang"); |
| 89 | } |
| 90 | |
| 91 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 92 | * Checks whether exception is thrown when leaf-list keyword is incorrect. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 93 | */ |
| 94 | @Test |
| 95 | public void processLeafListInvalidStatement() throws IOException, ParserException { |
| 96 | thrown.expect(ParserException.class); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 97 | thrown.expectMessage("mismatched input 'leaflist' expecting {'anyxml', 'augment', 'choice', 'contact', " |
| 98 | + "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import'," |
| 99 | + " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference'," |
Vidyashree Rama | 07021a2 | 2016-03-09 20:41:44 +0530 | [diff] [blame] | 100 | + " 'revision', 'rpc', 'typedef', 'uses', '}'}"); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 101 | YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidStatement.yang"); |
| 102 | } |
| 103 | |
| 104 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 105 | * Checks whether exception is thrown when leaf-list keyword without Left |
| 106 | * brace as per grammar. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 107 | */ |
| 108 | @Test |
| 109 | public void processLeafListWithoutLeftBrace() throws IOException, ParserException { |
| 110 | thrown.expect(ParserException.class); |
| 111 | thrown.expectMessage("missing '{' at 'type'"); |
| 112 | YangNode node = manager.getDataModel("src/test/resources/LeafListWithoutLeftBrace.yang"); |
| 113 | } |
| 114 | |
| 115 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 116 | * Checks whether exception is thrown when config statement cardinality is |
| 117 | * not as per grammar. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 118 | */ |
| 119 | @Test |
| 120 | public void processLeafListConfigInvalidCardinality() throws IOException, ParserException { |
| 121 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 122 | thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf-list " + |
| 123 | "invalid-interval\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 124 | YangNode node = manager.getDataModel("src/test/resources/LeafListConfigInvalidCardinality.yang"); |
| 125 | } |
| 126 | |
| 127 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 128 | * Checks whether exception is thrown when units statement cardinality is |
| 129 | * not as per grammar. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 130 | */ |
| 131 | @Test |
| 132 | public void processLeafListUnitsInvalidCardinality() throws IOException, ParserException { |
| 133 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 134 | thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf-list invalid-interval\""); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 135 | YangNode node = manager.getDataModel("src/test/resources/LeafListUnitsInvalidCardinality.yang"); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Checks leaf-list statement as sub-statement of container. |
| 140 | */ |
| 141 | @Test |
| 142 | public void processContainerSubStatementLeafList() throws IOException, ParserException { |
| 143 | |
| 144 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeafList.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-list properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 161 | ListIterator<YangLeafList> leafListIterator = container.getListOfLeafList().listIterator(); |
| 162 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 163 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 164 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 165 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 166 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 167 | assertThat(leafListInfo.getUnits(), is("\"seconds\"")); |
| 168 | assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 169 | assertThat(leafListInfo.isConfig(), is(true)); |
| 170 | assertThat(leafListInfo.getMinElements(), is(1)); |
| 171 | assertThat(leafListInfo.getMaxElelements(), is(2147483647)); |
| 172 | assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 173 | assertThat(leafListInfo.getReference(), is("\"RFC 6020\"")); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Checks leaf-list statement as sub-statement of list. |
| 178 | */ |
| 179 | @Test |
| 180 | public void processListSubStatementLeafList() throws IOException, ParserException { |
| 181 | |
| 182 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeafList.yang"); |
| 183 | |
| 184 | assertThat((node instanceof YangModule), is(true)); |
| 185 | |
| 186 | // Check whether the node type is set properly to module. |
| 187 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 188 | |
| 189 | // Check whether the module name is set correctly. |
| 190 | YangModule yangNode = (YangModule) node; |
| 191 | assertThat(yangNode.getName(), is("Test")); |
| 192 | |
| 193 | // Check whether the list is child of module |
| 194 | YangList yangList = (YangList) yangNode.getChild(); |
| 195 | assertThat(yangList.getName(), is("valid")); |
| 196 | |
| 197 | // Check whether leaf-list properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 198 | ListIterator<YangLeafList> leafListIterator = yangList.getListOfLeafList().listIterator(); |
| 199 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 200 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 201 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 202 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 203 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 204 | assertThat(leafListInfo.getUnits(), is("\"seconds\"")); |
| 205 | assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 206 | assertThat(leafListInfo.isConfig(), is(true)); |
| 207 | |
| 208 | assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 209 | assertThat(leafListInfo.getReference(), is("\"RFC 6020\"")); |
| 210 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 211 | } |