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