Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +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 | |
Vidyashree Rama | 36f2fab | 2016-07-15 14:06:56 +0530 | [diff] [blame^] | 19 | import java.io.IOException; |
| 20 | import java.util.ListIterator; |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 21 | import org.junit.Test; |
Vidyashree Rama | 36f2fab | 2016-07-15 14:06:56 +0530 | [diff] [blame^] | 22 | import org.onosproject.yangutils.datamodel.YangAugment; |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangCase; |
| 24 | import org.onosproject.yangutils.datamodel.YangChoice; |
| 25 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 26 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 27 | import org.onosproject.yangutils.datamodel.YangModule; |
| 28 | import org.onosproject.yangutils.datamodel.YangNode; |
| 29 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 30 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 31 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 32 | |
Vidyashree Rama | 36f2fab | 2016-07-15 14:06:56 +0530 | [diff] [blame^] | 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.core.Is.is; |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Test cases for case listener. |
| 38 | */ |
| 39 | public class CaseListenerTest { |
| 40 | |
| 41 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 42 | |
| 43 | /** |
| 44 | * Checks multiple case statement. |
| 45 | */ |
| 46 | @Test |
| 47 | public void processCaseStatement() throws IOException, ParserException { |
| 48 | |
| 49 | YangNode node = manager.getDataModel("src/test/resources/CaseStatement.yang"); |
| 50 | |
| 51 | // Check whether the data model tree returned is of type module. |
| 52 | assertThat((node instanceof YangModule), is(true)); |
| 53 | |
| 54 | // Check whether the node type is set properly to module. |
| 55 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 56 | |
| 57 | // Check whether the module name is set correctly. |
| 58 | YangModule yangNode = (YangModule) node; |
| 59 | assertThat(yangNode.getName(), is("Test")); |
| 60 | |
| 61 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 62 | assertThat(yangContainer.getName(), is("food")); |
| 63 | |
| 64 | YangChoice yangChoice = (YangChoice) yangContainer.getChild(); |
| 65 | assertThat(yangChoice.getName(), is("snack")); |
| 66 | |
| 67 | YangCase yangCase1 = (YangCase) yangChoice.getChild(); |
| 68 | assertThat(yangCase1.getName(), is("sports-arena")); |
| 69 | |
| 70 | // Check whether leaf properties as set correctly. |
| 71 | ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator(); |
| 72 | YangLeaf leafInfo1 = leafIterator1.next(); |
| 73 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 74 | assertThat(leafInfo1.getName(), is("pretzel")); |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 75 | |
| 76 | YangCase yangCase2 = (YangCase) yangCase1.getNextSibling(); |
| 77 | assertThat(yangCase2.getName(), is("late-night")); |
| 78 | |
| 79 | // Check whether leaf properties as set correctly. |
| 80 | ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator(); |
| 81 | YangLeaf leafInfo2 = leafIterator2.next(); |
| 82 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 83 | assertThat(leafInfo2.getName(), is("chocolate")); |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Checks duplicate case in choice. |
| 88 | */ |
| 89 | @Test(expected = ParserException.class) |
| 90 | public void processDuplicateCaseInChoice() throws IOException, ParserException { |
| 91 | |
| 92 | YangNode node = manager.getDataModel("src/test/resources/DuplicateCaseInChoice.yang"); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Checks duplicate leaf at different hierarchy. |
| 97 | */ |
| 98 | @Test(expected = ParserException.class) |
| 99 | public void processDuplicateLeafInHierarchy() throws IOException, ParserException { |
| 100 | |
| 101 | YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInHierarchy.yang"); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Checks duplicate leaf in different case within choice. |
| 106 | */ |
| 107 | @Test(expected = ParserException.class) |
| 108 | public void processDuplicateLeafInChoice() throws IOException, ParserException { |
| 109 | |
| 110 | YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInChoice.yang"); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Checks same case within different choice. |
| 115 | */ |
| 116 | @Test |
| 117 | public void processCaseStatementSameEntryDifferentChoice() throws IOException, ParserException { |
| 118 | |
| 119 | YangNode node = manager.getDataModel("src/test/resources/CaseStatementSameEntryDifferentChoice.yang"); |
| 120 | |
| 121 | // Check whether the data model tree returned is of type module. |
| 122 | assertThat((node instanceof YangModule), is(true)); |
| 123 | |
| 124 | // Check whether the node type is set properly to module. |
| 125 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 126 | |
| 127 | // Check whether the module name is set correctly. |
| 128 | YangModule yangNode = (YangModule) node; |
| 129 | assertThat(yangNode.getName(), is("Test")); |
| 130 | |
| 131 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 132 | assertThat(yangContainer.getName(), is("food")); |
| 133 | |
| 134 | YangChoice yangChoice = (YangChoice) yangContainer.getChild(); |
| 135 | assertThat(yangChoice.getName(), is("snack")); |
| 136 | |
| 137 | YangCase yangCase1 = (YangCase) yangChoice.getChild(); |
| 138 | assertThat(yangCase1.getName(), is("sports-arena")); |
| 139 | |
| 140 | // Check whether leaf properties as set correctly. |
| 141 | ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator(); |
| 142 | YangLeaf leafInfo1 = leafIterator1.next(); |
| 143 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 144 | assertThat(leafInfo1.getName(), is("pretzel")); |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 145 | |
| 146 | YangChoice yangChoice2 = (YangChoice) yangChoice.getNextSibling(); |
| 147 | assertThat(yangChoice2.getName(), is("lunch")); |
| 148 | |
| 149 | YangCase yangCase2 = (YangCase) yangChoice2.getChild(); |
| 150 | assertThat(yangCase2.getName(), is("sports-arena")); |
| 151 | |
| 152 | // Check whether leaf properties as set correctly. |
| 153 | ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator(); |
| 154 | YangLeaf leafInfo2 = leafIterator2.next(); |
| 155 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 156 | assertThat(leafInfo2.getName(), is("chocolate")); |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Checks case choice hierarchy. |
| 161 | */ |
| 162 | @Test |
| 163 | public void processCaseChoiceHierarchy() throws IOException, ParserException { |
| 164 | |
| 165 | YangNode node = manager.getDataModel("src/test/resources/CaseChoiceHierarchy.yang"); |
| 166 | |
| 167 | // Check whether the data model tree returned is of type module. |
| 168 | assertThat((node instanceof YangModule), is(true)); |
| 169 | |
| 170 | // Check whether the node type is set properly to module. |
| 171 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 172 | |
| 173 | // Check whether the module name is set correctly. |
| 174 | YangModule yangNode = (YangModule) node; |
| 175 | assertThat(yangNode.getName(), is("Test")); |
| 176 | |
| 177 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 178 | assertThat(yangContainer.getName(), is("food")); |
| 179 | |
| 180 | YangChoice yangChoice1 = (YangChoice) yangContainer.getChild(); |
| 181 | assertThat(yangChoice1.getName(), is("snack")); |
| 182 | |
| 183 | YangCase yangCase1 = (YangCase) yangChoice1.getChild(); |
| 184 | assertThat(yangCase1.getName(), is("sports-arena")); |
| 185 | |
| 186 | // Check whether leaf properties as set correctly. |
| 187 | ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator(); |
| 188 | YangLeaf leafInfo1 = leafIterator1.next(); |
| 189 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 190 | assertThat(leafInfo1.getName(), is("pretzel")); |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 191 | |
| 192 | YangCase yangCase2 = (YangCase) yangCase1.getNextSibling(); |
| 193 | assertThat(yangCase2.getName(), is("late-night")); |
| 194 | |
| 195 | YangChoice yangChoice2 = (YangChoice) yangCase2.getChild(); |
| 196 | assertThat(yangChoice2.getName(), is("dinner")); |
| 197 | |
| 198 | YangCase yangCase3 = (YangCase) yangChoice2.getChild(); |
| 199 | assertThat(yangCase3.getName(), is("late-night")); |
| 200 | |
| 201 | // Check whether leaf properties as set correctly. |
| 202 | ListIterator<YangLeaf> leafIterator2 = yangCase3.getListOfLeaf().listIterator(); |
| 203 | YangLeaf leafInfo2 = leafIterator2.next(); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 204 | assertThat(leafInfo2.getName(), is("beer")); |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 205 | } |
Vidyashree Rama | 36f2fab | 2016-07-15 14:06:56 +0530 | [diff] [blame^] | 206 | |
| 207 | /** |
| 208 | * Checks case substatement of augment. |
| 209 | */ |
| 210 | @Test |
| 211 | public void processCaseSubStatementOfAugment() throws IOException, ParserException { |
| 212 | |
| 213 | YangNode node = manager.getDataModel("src/test/resources/CaseSubStatementOfAugment.yang"); |
| 214 | |
| 215 | // Check whether the data model tree returned is of type module. |
| 216 | assertThat((node instanceof YangModule), is(true)); |
| 217 | |
| 218 | // Check whether the node type is set properly to module. |
| 219 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 220 | |
| 221 | // Check whether the module name is set correctly. |
| 222 | YangModule yangNode = (YangModule) node; |
| 223 | assertThat(yangNode.getName(), is("event")); |
| 224 | |
| 225 | YangAugment augment = ((YangAugment) yangNode.getChild()); |
| 226 | assertThat(augment.getName(), is("/snmp:snmp/snmp:engine/snmp:listen/snmp:transport")); |
| 227 | |
| 228 | YangCase yangCase = ((YangCase) augment.getChild()); |
| 229 | assertThat(yangCase.getName(), is("tls")); |
| 230 | |
| 231 | YangCase yangCase1 = ((YangCase) yangCase.getNextSibling()); |
| 232 | assertThat(yangCase1.getName(), is("dtls")); |
| 233 | |
| 234 | YangContainer container = ((YangContainer) yangCase.getChild()); |
| 235 | assertThat(container.getName(), is("tls")); |
| 236 | assertThat(container.getListOfLeaf().iterator().next().getName(), is("ip")); |
| 237 | } |
Gaurav Agrawal | e3ed0d9 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 238 | } |