Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +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 | 4ce3acf | 2016-02-25 04:37:05 +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; |
Gaurav Agrawal | a8391d0 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangEnum; |
| 24 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
| 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 | import java.util.Set; |
| 35 | |
| 36 | /** |
| 37 | * Test cases for value listener. |
| 38 | */ |
| 39 | public class ValueListenerTest { |
| 40 | |
| 41 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 42 | |
| 43 | /** |
| 44 | * Checks explicitly configured value. |
| 45 | */ |
| 46 | @Test |
| 47 | public void processValueStatement() throws IOException, ParserException { |
| 48 | |
| 49 | YangNode node = manager.getDataModel("src/test/resources/ValueStatement.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 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 62 | YangLeaf leafInfo = leafIterator.next(); |
| 63 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 64 | assertThat(leafInfo.getName(), is("speed")); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 65 | assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration")); |
| 66 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 67 | assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(), |
| 68 | is("speed_enum")); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 69 | |
| 70 | Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet(); |
| 71 | for (YangEnum tmp : enumSet) { |
| 72 | if (tmp.getNamedValue().equals("10m")) { |
| 73 | assertThat(tmp.getValue(), is(10)); |
| 74 | } else if (tmp.getNamedValue().equals("100m")) { |
| 75 | assertThat(tmp.getValue(), is(100)); |
| 76 | } else if (tmp.getNamedValue().equals("auto")) { |
| 77 | assertThat(tmp.getValue(), is(1000)); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 83 | * Checks explicitly configured negative value. |
| 84 | */ |
| 85 | @Test |
| 86 | public void processValueStatementWithNegativeValue() throws IOException, ParserException { |
| 87 | |
| 88 | YangNode node = manager.getDataModel("src/test/resources/ValueStatementWithNegativeValue.yang"); |
| 89 | |
| 90 | // Check whether the data model tree returned is of type module. |
| 91 | assertThat((node instanceof YangModule), is(true)); |
| 92 | |
| 93 | // Check whether the node type is set properly to module. |
| 94 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 95 | |
| 96 | // Check whether the module name is set correctly. |
| 97 | YangModule yangNode = (YangModule) node; |
| 98 | assertThat(yangNode.getName(), is("Test")); |
| 99 | |
| 100 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 101 | YangLeaf leafInfo = leafIterator.next(); |
| 102 | |
| 103 | assertThat(leafInfo.getName(), is("speed")); |
| 104 | assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration")); |
| 105 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION)); |
| 106 | assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(), |
| 107 | is("speed_enum")); |
| 108 | |
| 109 | Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet(); |
| 110 | for (YangEnum tmp : enumSet) { |
| 111 | if (tmp.getNamedValue().equals("10m")) { |
| 112 | assertThat(tmp.getValue(), is(-2)); |
| 113 | } else if (tmp.getNamedValue().equals("100m")) { |
| 114 | assertThat(tmp.getValue(), is(-1)); |
| 115 | } else if (tmp.getNamedValue().equals("auto")) { |
| 116 | assertThat(tmp.getValue(), is(0)); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Checks explicitly configured value with double quotes. |
| 123 | */ |
| 124 | @Test |
| 125 | public void processValueStatementWithQuotes() throws IOException, ParserException { |
| 126 | |
| 127 | YangNode node = manager.getDataModel("src/test/resources/ValueStatementWithQuotes.yang"); |
| 128 | |
| 129 | // Check whether the data model tree returned is of type module. |
| 130 | assertThat((node instanceof YangModule), is(true)); |
| 131 | |
| 132 | // Check whether the node type is set properly to module. |
| 133 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 134 | |
| 135 | // Check whether the module name is set correctly. |
| 136 | YangModule yangNode = (YangModule) node; |
| 137 | assertThat(yangNode.getName(), is("Test")); |
| 138 | |
| 139 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 140 | YangLeaf leafInfo = leafIterator.next(); |
| 141 | |
| 142 | assertThat(leafInfo.getName(), is("speed")); |
| 143 | assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration")); |
| 144 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION)); |
| 145 | assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(), |
| 146 | is("speed_enum")); |
| 147 | |
| 148 | Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet(); |
| 149 | for (YangEnum tmp : enumSet) { |
| 150 | if (tmp.getNamedValue().equals("10m")) { |
| 151 | assertThat(tmp.getValue(), is(10)); |
| 152 | } else if (tmp.getNamedValue().equals("100m")) { |
| 153 | assertThat(tmp.getValue(), is(100)); |
| 154 | } else if (tmp.getNamedValue().equals("auto")) { |
| 155 | assertThat(tmp.getValue(), is(1000)); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 161 | * Checks explicit value and auto generated value. |
| 162 | */ |
| 163 | @Test |
| 164 | public void processValueAndAutoStatement() throws IOException, ParserException { |
| 165 | |
| 166 | YangNode node = manager.getDataModel("src/test/resources/ValueAndAutoStatement.yang"); |
| 167 | |
| 168 | // Check whether the data model tree returned is of type module. |
| 169 | assertThat((node instanceof YangModule), is(true)); |
| 170 | |
| 171 | // Check whether the node type is set properly to module. |
| 172 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 173 | |
| 174 | // Check whether the module name is set correctly. |
| 175 | YangModule yangNode = (YangModule) node; |
| 176 | assertThat(yangNode.getName(), is("Test")); |
| 177 | |
| 178 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 179 | YangLeaf leafInfo = leafIterator.next(); |
| 180 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 181 | assertThat(leafInfo.getName(), is("speed")); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 182 | assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration")); |
| 183 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 184 | assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(), |
| 185 | is("speed_enum")); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 186 | |
| 187 | Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet(); |
| 188 | for (YangEnum tmp : enumSet) { |
| 189 | if (tmp.getNamedValue().equals("10m")) { |
| 190 | assertThat(tmp.getValue(), is(10)); |
| 191 | } else if (tmp.getNamedValue().equals("100m")) { |
| 192 | assertThat(tmp.getValue(), is(11)); |
| 193 | } else if (tmp.getNamedValue().equals("auto")) { |
| 194 | assertThat(tmp.getValue(), is(1000)); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Checks explicit value should not be repeated. |
| 201 | */ |
| 202 | @Test(expected = ParserException.class) |
| 203 | public void processValueDuplication() throws IOException, ParserException { |
| 204 | |
| 205 | YangNode node = manager.getDataModel("src/test/resources/ValueDuplication.yang"); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Checks explicit or auto generated value should not be repeated. |
| 210 | */ |
| 211 | @Test(expected = ParserException.class) |
| 212 | public void processValueExplicitAndAutoDuplication() throws IOException, ParserException { |
| 213 | |
| 214 | YangNode node = manager.getDataModel("src/test/resources/ValueExplicitAndAutoDuplication.yang"); |
| 215 | } |
| 216 | } |