Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 49abe71 | 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 | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.util.ListIterator; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 21 | import org.junit.Rule; |
| 22 | import org.junit.Test; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 23 | import org.junit.rules.ExpectedException; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangContainer; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangList; |
Vidyashree Rama | 49abe71 | 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 | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangStatusType; |
Vidyashree Rama | 49abe71 | 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 | 49abe71 | 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 | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 49 | * Checks all the values of leaf-list sub-statements are set correctly. |
Vidyashree Rama | 49abe71 | 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 | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 66 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 67 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 68 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 69 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 70 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 49abe71 | 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)); |
rama-huawei | 6c728a9 | 2016-07-11 14:48:12 +0530 | [diff] [blame^] | 75 | assertThat(leafListInfo.getMaxElements().getMaxElement(), is(3)); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 76 | assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 77 | assertThat(leafListInfo.getReference(), is("\"RFC 6020\"")); |
| 78 | } |
| 79 | |
| 80 | /** |
Vinod Kumar S | 0c330cd | 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 | 49abe71 | 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 | 468f828 | 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 | 49abe71 | 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 | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 92 | * Checks whether exception is thrown when leaf-list keyword is incorrect. |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 93 | */ |
Vidyashree Rama | 528ef30 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 94 | @Test(expected = ParserException.class) |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 95 | public void processLeafListInvalidStatement() throws IOException, ParserException { |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 96 | YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidStatement.yang"); |
| 97 | } |
| 98 | |
| 99 | /** |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 100 | * Checks whether exception is thrown when leaf-list keyword without Left |
| 101 | * brace as per grammar. |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 102 | */ |
| 103 | @Test |
| 104 | public void processLeafListWithoutLeftBrace() throws IOException, ParserException { |
| 105 | thrown.expect(ParserException.class); |
| 106 | thrown.expectMessage("missing '{' at 'type'"); |
| 107 | YangNode node = manager.getDataModel("src/test/resources/LeafListWithoutLeftBrace.yang"); |
| 108 | } |
| 109 | |
| 110 | /** |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 111 | * Checks whether exception is thrown when config statement cardinality is |
| 112 | * not as per grammar. |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 113 | */ |
| 114 | @Test |
| 115 | public void processLeafListConfigInvalidCardinality() throws IOException, ParserException { |
| 116 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 117 | thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf-list " + |
| 118 | "invalid-interval\"."); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 119 | YangNode node = manager.getDataModel("src/test/resources/LeafListConfigInvalidCardinality.yang"); |
| 120 | } |
| 121 | |
| 122 | /** |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 123 | * Checks whether exception is thrown when units statement cardinality is |
| 124 | * not as per grammar. |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 125 | */ |
| 126 | @Test |
| 127 | public void processLeafListUnitsInvalidCardinality() throws IOException, ParserException { |
| 128 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 129 | thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf-list invalid-interval\""); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 130 | YangNode node = manager.getDataModel("src/test/resources/LeafListUnitsInvalidCardinality.yang"); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Checks leaf-list statement as sub-statement of container. |
| 135 | */ |
| 136 | @Test |
| 137 | public void processContainerSubStatementLeafList() throws IOException, ParserException { |
| 138 | |
| 139 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeafList.yang"); |
| 140 | |
| 141 | // Check whether the data model tree returned is of type module. |
| 142 | assertThat((node instanceof YangModule), is(true)); |
| 143 | |
| 144 | // Check whether the node type is set properly to module. |
| 145 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 146 | |
| 147 | // Check whether the module name is set correctly. |
| 148 | YangModule yangNode = (YangModule) node; |
| 149 | assertThat(yangNode.getName(), is("Test")); |
| 150 | |
| 151 | //Check whether the container is child of module. |
| 152 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 153 | assertThat(container.getName(), is("valid")); |
| 154 | |
| 155 | // Check whether leaf-list properties as set correctly. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 156 | ListIterator<YangLeafList> leafListIterator = container.getListOfLeafList().listIterator(); |
| 157 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 158 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 159 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 160 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 161 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 162 | assertThat(leafListInfo.getUnits(), is("\"seconds\"")); |
| 163 | assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 164 | assertThat(leafListInfo.isConfig(), is(true)); |
rama-huawei | 6c728a9 | 2016-07-11 14:48:12 +0530 | [diff] [blame^] | 165 | assertThat(leafListInfo.getMinElements().getMinElement(), is(1)); |
| 166 | assertThat(leafListInfo.getMaxElements().getMaxElement(), is(2147483647)); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 167 | assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 168 | assertThat(leafListInfo.getReference(), is("\"RFC 6020\"")); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Checks leaf-list statement as sub-statement of list. |
| 173 | */ |
| 174 | @Test |
| 175 | public void processListSubStatementLeafList() throws IOException, ParserException { |
| 176 | |
| 177 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeafList.yang"); |
| 178 | |
| 179 | assertThat((node instanceof YangModule), is(true)); |
| 180 | |
| 181 | // Check whether the node type is set properly to module. |
| 182 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 183 | |
| 184 | // Check whether the module name is set correctly. |
| 185 | YangModule yangNode = (YangModule) node; |
| 186 | assertThat(yangNode.getName(), is("Test")); |
| 187 | |
| 188 | // Check whether the list is child of module |
| 189 | YangList yangList = (YangList) yangNode.getChild(); |
| 190 | assertThat(yangList.getName(), is("valid")); |
| 191 | |
| 192 | // Check whether leaf-list properties as set correctly. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 193 | ListIterator<YangLeafList> leafListIterator = yangList.getListOfLeafList().listIterator(); |
| 194 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 195 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 196 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 197 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 198 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 199 | assertThat(leafListInfo.getUnits(), is("\"seconds\"")); |
| 200 | assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 201 | assertThat(leafListInfo.isConfig(), is(true)); |
| 202 | |
| 203 | assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 204 | assertThat(leafListInfo.getReference(), is("\"RFC 6020\"")); |
| 205 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 206 | } |