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