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 | |
| 19 | import org.junit.Rule; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import org.junit.rules.ExpectedException; |
| 23 | import org.onosproject.yangutils.datamodel.YangModule; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 26 | import org.onosproject.yangutils.datamodel.YangList; |
| 27 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 28 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.util.List; |
| 32 | import java.util.ListIterator; |
| 33 | |
| 34 | import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | import static org.hamcrest.core.Is.is; |
| 36 | |
| 37 | /** |
| 38 | * Test cases for key listener. |
| 39 | */ |
| 40 | public class KeyListenerTest { |
| 41 | |
| 42 | @Rule |
| 43 | public ExpectedException thrown = ExpectedException.none(); |
| 44 | |
| 45 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 46 | |
| 47 | /** |
| 48 | * Checks key statement as sub-statement of list. |
| 49 | */ |
| 50 | @Test |
| 51 | public void processListSubStatementKey() throws IOException, ParserException { |
| 52 | |
| 53 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementKey.yang"); |
| 54 | |
| 55 | assertThat((node instanceof YangModule), is(true)); |
| 56 | |
| 57 | // Check whether the node type is set properly to module. |
| 58 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 59 | |
| 60 | // Check whether the module name is set correctly. |
| 61 | YangModule yangNode = (YangModule) node; |
| 62 | assertThat(yangNode.getName(), is("Test")); |
| 63 | |
| 64 | // Check whether the list is child of module |
| 65 | YangList yangList = (YangList) yangNode.getChild(); |
| 66 | assertThat(yangList.getName(), is("valid")); |
| 67 | |
| 68 | ListIterator<String> keyList = yangList.getKeyList().listIterator(); |
| 69 | assertThat(keyList.next(), is("invalid-interval")); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Check multiple key values. |
| 74 | */ |
| 75 | @Test |
| 76 | public void processMultipleKeyValues() throws IOException, ParserException { |
| 77 | |
| 78 | YangNode node = manager.getDataModel("src/test/resources/MultipleKeyValues.yang"); |
| 79 | |
| 80 | assertThat((node instanceof YangModule), is(true)); |
| 81 | |
| 82 | // Check whether the node type is set properly to module. |
| 83 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 84 | |
| 85 | // Check whether the module name is set correctly. |
| 86 | YangModule yangNode = (YangModule) node; |
| 87 | assertThat(yangNode.getName(), is("Test")); |
| 88 | |
| 89 | // Check whether the list is child of module |
| 90 | YangList yangList = (YangList) yangNode.getChild(); |
| 91 | assertThat(yangList.getName(), is("valid")); |
| 92 | |
| 93 | List<String> keyList = yangList.getKeyList(); |
| 94 | assertThat(keyList.contains("ospf"), is(true)); |
| 95 | assertThat(keyList.contains("isis"), is(true)); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Checks key statement without statement end. |
| 100 | */ |
| 101 | @Test |
| 102 | public void processKeyWithoutStatementEnd() throws IOException, ParserException { |
| 103 | thrown.expect(ParserException.class); |
| 104 | thrown.expectMessage("mismatched input 'leaf' expecting {';', '+'}"); |
| 105 | YangNode node = manager.getDataModel("src/test/resources/KeyWithoutStatementEnd.yang"); |
| 106 | } |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Checks key values are set correctly. |
| 110 | */ |
| 111 | @Test |
| 112 | public void processConfigFalseNoKey() throws IOException, ParserException { |
| 113 | YangNode node = manager.getDataModel("src/test/resources/ConfigFalseNoKey.yang"); |
| 114 | |
| 115 | assertThat((node instanceof YangModule), is(true)); |
| 116 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 117 | YangModule yangNode = (YangModule) node; |
| 118 | assertThat(yangNode.getName(), is("Test")); |
| 119 | |
| 120 | // Check whether the list is child of module |
| 121 | YangList yangList = (YangList) yangNode.getChild(); |
| 122 | assertThat(yangList.getName(), is("valid")); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Checks key values are set correctly. |
| 127 | */ |
| 128 | @Test |
| 129 | public void processConfigFalseValidKeyValidLeaf() throws IOException, ParserException { |
| 130 | YangNode node = manager.getDataModel("src/test/resources/ConfigFalseValidKeyValidLeaf.yang"); |
| 131 | |
| 132 | assertThat((node instanceof YangModule), is(true)); |
| 133 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 134 | YangModule yangNode = (YangModule) node; |
| 135 | assertThat(yangNode.getName(), is("Test")); |
| 136 | |
| 137 | // Check whether the list is child of module |
| 138 | YangList yangList = (YangList) yangNode.getChild(); |
| 139 | assertThat(yangList.getName(), is("valid")); |
| 140 | |
| 141 | ListIterator<String> keyList = yangList.getKeyList().listIterator(); |
| 142 | assertThat(keyList.next(), is("invalid-interval")); |
| 143 | } |
| 144 | |
| 145 | /** |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 146 | * Checks whether exception is thrown when list's config is set to true and there is no key. |
| 147 | */ |
| 148 | @Test |
| 149 | public void processConfigTrueNoKey() throws IOException, ParserException { |
| 150 | thrown.expect(ParserException.class); |
| 151 | thrown.expectMessage("A list must have atleast one key leaf if config is true"); |
| 152 | YangNode node = manager.getDataModel("src/test/resources/ConfigTrueNoKey.yang"); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Checks whether exception is thrown when list's config is set to true and there is no leaf. |
| 157 | */ |
| 158 | @Test |
| 159 | public void processConfigTrueNoleafNoLeafList() throws IOException, ParserException { |
| 160 | thrown.expect(ParserException.class); |
| 161 | thrown.expectMessage("A list must have atleast one key leaf if config is true"); |
| 162 | YangNode node = manager.getDataModel("src/test/resources/ConfigTrueNoleafNoLeafList.yang"); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Checks key values are set correctly. |
| 167 | */ |
| 168 | @Test |
| 169 | public void processConfigTrueValidKeyValidLeaf() throws IOException, ParserException { |
| 170 | YangNode node = manager.getDataModel("src/test/resources/ConfigTrueValidKeyValidLeaf.yang"); |
| 171 | |
| 172 | assertThat((node instanceof YangModule), is(true)); |
| 173 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 174 | YangModule yangNode = (YangModule) node; |
| 175 | assertThat(yangNode.getName(), is("Test")); |
| 176 | |
| 177 | // Check whether the list is child of module |
| 178 | YangList yangList = (YangList) yangNode.getChild(); |
| 179 | assertThat(yangList.getName(), is("valid")); |
| 180 | |
| 181 | ListIterator<String> keyList = yangList.getKeyList().listIterator(); |
| 182 | assertThat(keyList.next(), is("invalid-interval")); |
| 183 | } |
| 184 | |
| 185 | /** |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 186 | * Checks key values are set correctly. |
| 187 | */ |
| 188 | @Test |
| 189 | public void processKeyWithUsesInList() throws IOException, ParserException { |
| 190 | YangNode node = manager.getDataModel("src/test/resources/KeyWithUsesInList.yang"); |
| 191 | |
| 192 | assertThat((node instanceof YangModule), is(true)); |
| 193 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 194 | YangModule yangNode = (YangModule) node; |
| 195 | assertThat(yangNode.getName(), is("Test")); |
| 196 | |
| 197 | // Check whether the list is child of module |
| 198 | YangList yangList = (YangList) yangNode.getChild().getNextSibling(); |
| 199 | assertThat(yangList.getName(), is("valid")); |
| 200 | |
| 201 | ListIterator<String> keyList = yangList.getKeyList().listIterator(); |
| 202 | assertThat(keyList.next(), is("invalid-interval")); |
| 203 | } |
| 204 | |
| 205 | /** |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 206 | * Checks whether exception is thrown when key leaf identifier is not found in list. |
| 207 | */ |
| 208 | @Test |
| 209 | public void processInvalidLeafIdentifier() throws IOException, ParserException { |
| 210 | thrown.expect(ParserException.class); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 211 | thrown.expectMessage("An identifier, in key, must refer to a child leaf of the list"); |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 212 | YangNode node = manager.getDataModel("src/test/resources/InvalidLeafIdentifier.yang"); |
| 213 | } |
| 214 | |
| 215 | /** |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 216 | * Checks whether exception is thrown when key leaf is of type empty. |
| 217 | */ |
| 218 | @Test |
| 219 | public void processKeyLeafTypeEmpty() throws IOException, ParserException { |
| 220 | thrown.expect(ParserException.class); |
| 221 | thrown.expectMessage("A leaf that is part of the key must not be the built-in type \"empty\"."); |
| 222 | YangNode node = manager.getDataModel("src/test/resources/KeyLeafTypeEmpty.yang"); |
| 223 | } |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 224 | } |