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 list listener. |
| 41 | */ |
| 42 | public class ListListenerTest { |
| 43 | |
| 44 | @Rule |
| 45 | public ExpectedException thrown = ExpectedException.none(); |
| 46 | |
| 47 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 48 | |
| 49 | /** |
| 50 | * Checks list statement as sub-statement of module. |
| 51 | */ |
| 52 | @Test |
| 53 | public void processModuleSubStatementList() throws IOException, ParserException { |
| 54 | |
| 55 | YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementList.yang"); |
| 56 | |
| 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 | |
| 66 | // Check whether the list is child of module |
| 67 | YangList yangList = (YangList) yangNode.getChild(); |
| 68 | assertThat(yangList.getName(), is("valid")); |
| 69 | |
| 70 | ListIterator<String> keyList = yangList.getKeyList().listIterator(); |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 71 | assertThat(keyList.next(), is("invalid-interval")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Checks list statement as sub-statement of container. |
| 76 | */ |
| 77 | @Test |
| 78 | public void processContainerSubStatementList() throws IOException, ParserException { |
| 79 | |
| 80 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementList.yang"); |
| 81 | |
| 82 | assertThat((node instanceof YangModule), is(true)); |
| 83 | |
| 84 | // Check whether the node type is set properly to module. |
| 85 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 86 | |
| 87 | // Check whether the module name is set correctly. |
| 88 | YangModule yangNode = (YangModule) node; |
| 89 | assertThat(yangNode.getName(), is("Test")); |
| 90 | |
| 91 | // Check whether the container is child of module |
| 92 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 93 | assertThat(yangContainer.getName(), is("ospf")); |
| 94 | |
| 95 | // Check whether the list is child of container |
| 96 | YangList yangList = (YangList) yangContainer.getChild(); |
| 97 | assertThat(yangList.getName(), is("valid")); |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 98 | assertThat(yangList.getKeyList().contains("invalid-interval"), is(true)); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Checks list statement as sub-statement of list. |
| 103 | */ |
| 104 | @Test |
| 105 | public void processListSubStatementList() throws IOException, ParserException { |
| 106 | |
| 107 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementList.yang"); |
| 108 | |
| 109 | assertThat((node instanceof YangModule), is(true)); |
| 110 | |
| 111 | // Check whether the node type is set properly to module. |
| 112 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 113 | |
| 114 | // Check whether the module name is set correctly. |
| 115 | YangModule yangNode = (YangModule) node; |
| 116 | assertThat(yangNode.getName(), is("Test")); |
| 117 | |
| 118 | // Check whether the list is child of module |
| 119 | YangList yangList1 = (YangList) yangNode.getChild(); |
| 120 | assertThat(yangList1.getName(), is("ospf")); |
| 121 | assertThat(yangList1.getKeyList().contains("process-id"), is(true)); |
| 122 | |
| 123 | // Check whether the list is child of list |
| 124 | YangList yangList = (YangList) yangList1.getChild(); |
| 125 | assertThat(yangList.getName(), is("valid")); |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 126 | assertThat(yangList.getKeyList().contains("invalid-interval"), is(true)); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Checks list with all its sub-statements. |
| 131 | */ |
| 132 | @Test |
| 133 | public void processListSubStatements() throws IOException, ParserException { |
| 134 | |
| 135 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatements.yang"); |
| 136 | |
| 137 | assertThat((node instanceof YangModule), is(true)); |
| 138 | |
| 139 | // Check whether the node type is set properly to module. |
| 140 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 141 | |
| 142 | // Check whether the module name is set correctly. |
| 143 | YangModule yangNode = (YangModule) node; |
| 144 | assertThat(yangNode.getName(), is("Test")); |
| 145 | |
| 146 | // Check whether the list is child of module |
| 147 | YangList yangList = (YangList) yangNode.getChild(); |
| 148 | |
| 149 | // Check whether list properties as set correctly. |
| 150 | assertThat(yangList.getName(), is("ospf")); |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 151 | assertThat(yangList.getKeyList().contains("invalid-interval"), is(true)); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 152 | |
| 153 | assertThat(yangList.isConfig(), is(true)); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 154 | assertThat(yangList.getMaxElements(), is(10)); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 155 | assertThat(yangList.getMinElements(), is(3)); |
| 156 | assertThat(yangList.getDescription(), is("\"list description\"")); |
| 157 | assertThat(yangList.getStatus(), is(YangStatusType.CURRENT)); |
| 158 | assertThat(yangList.getReference(), is("\"list reference\"")); |
| 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 = yangList.getListOfLeaf().listIterator(); |
| 162 | YangLeaf leafInfo = leafIterator.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(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 165 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 166 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 167 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 168 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 169 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Checks cardinality of sub-statements of list. |
| 174 | */ |
| 175 | @Test |
| 176 | public void processListSubStatementsCardinality() throws IOException, ParserException { |
| 177 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 178 | thrown.expectMessage("YANG file error: \"reference\" is defined more than once in \"list valid\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 179 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementsCardinality.yang"); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Checks list statement without child. |
| 184 | */ |
| 185 | @Test |
| 186 | public void processListStatementWithoutChild() throws IOException, ParserException { |
| 187 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 188 | thrown.expectMessage("YANG file error: Missing \"data-def-substatements\" in \"list valid\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 189 | YangNode node = manager.getDataModel("src/test/resources/ListStatementWithoutChild.yang"); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Checks list as root node. |
| 194 | */ |
| 195 | @Test |
| 196 | public void processListAsRootNode() throws IOException, ParserException { |
| 197 | thrown.expect(ParserException.class); |
| 198 | thrown.expectMessage("no viable alternative at input 'list'"); |
| 199 | YangNode node = manager.getDataModel("src/test/resources/ListAsRootNode.yang"); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Checks invalid identifier for list statement. |
| 204 | */ |
| 205 | @Test |
| 206 | public void processListInvalidIdentifier() throws IOException, ParserException { |
| 207 | thrown.expect(ParserException.class); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 208 | thrown.expectMessage("YANG file error : list name 1valid is not valid."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 209 | YangNode node = manager.getDataModel("src/test/resources/ListInvalidIdentifier.yang"); |
| 210 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 211 | } |