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; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 21 | |
| 22 | import org.junit.Rule; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 23 | import org.junit.Test; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 24 | import org.junit.rules.ExpectedException; |
Gaurav Agrawal | a8391d0 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangEnum; |
| 27 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
| 28 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 29 | import org.onosproject.yangutils.datamodel.YangModule; |
| 30 | import org.onosproject.yangutils.datamodel.YangNode; |
| 31 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 33 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 34 | |
| 35 | import java.io.IOException; |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 36 | import java.util.Iterator; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 37 | import java.util.ListIterator; |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 38 | import java.util.SortedSet; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Test cases for enum listener. |
| 42 | */ |
| 43 | public class EnumListenerTest { |
| 44 | |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 45 | @Rule |
| 46 | public ExpectedException thrown = ExpectedException.none(); |
| 47 | |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 48 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 49 | |
| 50 | /** |
| 51 | * Checks enum statement without value. |
| 52 | */ |
| 53 | @Test |
| 54 | public void processEnumTypeStatement() throws IOException, ParserException { |
| 55 | |
| 56 | YangNode node = manager.getDataModel("src/test/resources/EnumTypeStatement.yang"); |
| 57 | |
| 58 | // Check whether the data model tree returned is of type module. |
| 59 | assertThat((node instanceof YangModule), is(true)); |
| 60 | |
| 61 | // Check whether the node type is set properly to module. |
| 62 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 63 | |
| 64 | // Check whether the module name is set correctly. |
| 65 | YangModule yangNode = (YangModule) node; |
| 66 | assertThat(yangNode.getName(), is("Test")); |
| 67 | |
| 68 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 69 | YangLeaf leafInfo = leafIterator.next(); |
| 70 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 71 | assertThat(leafInfo.getName(), is("speed")); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 72 | assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration")); |
| 73 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 74 | assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(), |
| 75 | is("speed_enum")); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 76 | |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 77 | SortedSet<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet(); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 78 | for (YangEnum tmp : enumSet) { |
| 79 | if (tmp.getNamedValue().equals("10m")) { |
| 80 | assertThat(tmp.getValue(), is(0)); |
| 81 | } else if (tmp.getNamedValue().equals("100m")) { |
| 82 | assertThat(tmp.getValue(), is(1)); |
| 83 | } else if (tmp.getNamedValue().equals("auto")) { |
| 84 | assertThat(tmp.getValue(), is(2)); |
| 85 | } |
| 86 | } |
| 87 | } |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Checks if enum with same name is not allowed. |
| 91 | */ |
| 92 | @Test(expected = ParserException.class) |
| 93 | public void processEnumWithDuplicateName() throws IOException, ParserException { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 94 | YangNode node = manager.getDataModel("src/test/resources/EnumWithDuplicateName.yang"); |
| 95 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Checks enum boundary value. |
| 99 | */ |
| 100 | @Test |
| 101 | public void processEnumBoundaryValue() throws IOException, ParserException { |
| 102 | thrown.expect(ParserException.class); |
| 103 | thrown.expectMessage("YANG file error : value value 21474836472147483647 is not valid."); |
| 104 | YangNode node = manager.getDataModel("src/test/resources/EnumBoundaryValue.yang"); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Checks whether exception is thrown if value is not specified following max enum value. |
| 109 | */ |
| 110 | @Test |
| 111 | public void processEnumMaxNextValue() throws IOException, ParserException { |
| 112 | thrown.expect(ParserException.class); |
| 113 | thrown.expectMessage("YANG file error : " |
| 114 | + "An enum value MUST be specified for enum substatements following the one" |
| 115 | + "with the current highest value"); |
| 116 | YangNode node = manager.getDataModel("src/test/resources/EnumMaxNextValue.yang"); |
| 117 | } |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Checks enum values stored are sorted. |
| 121 | */ |
| 122 | @Test |
| 123 | public void processEnumSorted() throws IOException, ParserException { |
| 124 | YangNode node = manager.getDataModel("src/test/resources/EnumSorted.yang"); |
| 125 | // Check whether the data model tree returned is of type module. |
| 126 | assertThat((node instanceof YangModule), is(true)); |
| 127 | |
| 128 | // Check whether the node type is set properly to module. |
| 129 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 130 | |
| 131 | // Check whether the module name is set correctly. |
| 132 | YangModule yangNode = (YangModule) node; |
| 133 | assertThat(yangNode.getName(), is("Test")); |
| 134 | |
| 135 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 136 | YangLeaf leafInfo = leafIterator.next(); |
| 137 | |
| 138 | assertThat(leafInfo.getName(), is("ifType")); |
| 139 | assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration")); |
| 140 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION)); |
| 141 | assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(), |
| 142 | is("ifType_enum")); |
| 143 | |
| 144 | SortedSet<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet(); |
| 145 | Iterator<YangEnum> enumIterator = enumSet.iterator(); |
| 146 | assertThat(enumIterator.next().getNamedValue(), is("five")); |
| 147 | } |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 148 | } |