Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +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 | 480a47d | 2016-02-26 02:47:39 +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.YangBit; |
| 23 | import org.onosproject.yangutils.datamodel.YangBits; |
| 24 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 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; |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangType; |
| 30 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 31 | import org.onosproject.yangutils.datamodel.YangUnion; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 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.List; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 37 | import java.util.ListIterator; |
| 38 | import java.util.Set; |
| 39 | |
| 40 | /** |
| 41 | * Test cases for bit listener. |
| 42 | */ |
| 43 | public class BitListenerTest { |
| 44 | |
| 45 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 46 | |
| 47 | /** |
| 48 | * Checks bit statement without position. |
| 49 | */ |
| 50 | @Test |
| 51 | public void processBitTypeStatement() throws IOException, ParserException { |
| 52 | |
| 53 | YangNode node = manager.getDataModel("src/test/resources/BitTypeStatement.yang"); |
| 54 | |
| 55 | // Check whether the data model tree returned is of type module. |
| 56 | assertThat((node instanceof YangModule), is(true)); |
| 57 | |
| 58 | // Check whether the node type is set properly to module. |
| 59 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 60 | |
| 61 | // Check whether the module name is set correctly. |
| 62 | YangModule yangNode = (YangModule) node; |
| 63 | assertThat(yangNode.getName(), is("Test")); |
| 64 | |
| 65 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 66 | YangLeaf leafInfo = leafIterator.next(); |
| 67 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 68 | assertThat(leafInfo.getName(), is("mybits")); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 69 | assertThat(leafInfo.getDataType().getDataTypeName(), is("bits")); |
| 70 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS)); |
| 71 | assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(), |
| 72 | is("mybits")); |
| 73 | |
| 74 | Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet(); |
| 75 | for (YangBit tmp : bitSet) { |
| 76 | if (tmp.getBitName().equals("disable-nagle")) { |
| 77 | assertThat(tmp.getPosition(), is(0)); |
| 78 | } else if (tmp.getBitName().equals("auto-sense-speed")) { |
| 79 | assertThat(tmp.getPosition(), is(1)); |
| 80 | } else if (tmp.getBitName().equals("Ten-Mb-only")) { |
| 81 | assertThat(tmp.getPosition(), is(2)); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 87 | * Checks bit statement with typedef. |
| 88 | */ |
| 89 | @Test |
| 90 | public void processBitTypedefStatement() throws IOException, ParserException { |
| 91 | |
| 92 | YangNode node = manager.getDataModel("src/test/resources/BitTypedefStatement.yang"); |
| 93 | |
| 94 | // Check whether the data model tree returned is of type module. |
| 95 | assertThat((node instanceof YangModule), is(true)); |
| 96 | |
| 97 | // Check whether the node type is set properly to module. |
| 98 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 99 | |
| 100 | // Check whether the module name is set correctly. |
| 101 | YangModule yangNode = (YangModule) node; |
| 102 | assertThat(yangNode.getName(), is("Test")); |
| 103 | |
| 104 | YangTypeDef typedef = (YangTypeDef) yangNode.getChild(); |
| 105 | assertThat(typedef.getName(), is("type15")); |
| 106 | |
| 107 | YangType type = typedef.getTypeList().iterator().next(); |
| 108 | assertThat(type.getDataType(), is(YangDataTypes.BITS)); |
| 109 | assertThat(type.getDataTypeName(), is("bits")); |
| 110 | Set<YangBit> bitSet = ((YangBits) type.getDataTypeExtendedInfo()).getBitSet(); |
| 111 | for (YangBit tmp : bitSet) { |
| 112 | if (tmp.getBitName().equals("disable-nagle")) { |
| 113 | assertThat(tmp.getPosition(), is(0)); |
| 114 | } else if (tmp.getBitName().equals("auto-sense-speed")) { |
| 115 | assertThat(tmp.getPosition(), is(1)); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Checks bit statement with union. |
| 122 | */ |
| 123 | @Test |
| 124 | public void processBitUnionStatement() throws IOException, ParserException { |
| 125 | |
| 126 | YangNode node = manager.getDataModel("src/test/resources/BitUnionStatement.yang"); |
| 127 | |
| 128 | // Check whether the data model tree returned is of type module. |
| 129 | assertThat((node instanceof YangModule), is(true)); |
| 130 | |
| 131 | // Check whether the node type is set properly to module. |
| 132 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 133 | |
| 134 | // Check whether the module name is set correctly. |
| 135 | YangModule yangNode = (YangModule) node; |
| 136 | assertThat(yangNode.getName(), is("Test")); |
| 137 | |
| 138 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 139 | YangLeaf leafInfo = leafIterator.next(); |
| 140 | |
| 141 | assertThat(leafInfo.getName(), is("type15")); |
| 142 | |
| 143 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION)); |
| 144 | assertThat(leafInfo.getDataType().getDataTypeName(), is("union")); |
| 145 | |
| 146 | YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo(); |
| 147 | |
| 148 | List<YangType<?>> typeList = yangUnion.getTypeList(); |
| 149 | ListIterator<YangType<?>> typeListIterator = typeList.listIterator(); |
| 150 | YangType<?> yangType = typeListIterator.next(); |
| 151 | |
| 152 | assertThat(yangType.getDataType(), is(YangDataTypes.BITS)); |
| 153 | assertThat(yangType.getDataTypeName(), is("bits")); |
| 154 | Set<YangBit> bitSet = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitSet(); |
| 155 | for (YangBit tmp : bitSet) { |
| 156 | if (tmp.getBitName().equals("disable-nagle")) { |
| 157 | assertThat(tmp.getPosition(), is(0)); |
| 158 | } else if (tmp.getBitName().equals("auto-sense-speed")) { |
| 159 | assertThat(tmp.getPosition(), is(1)); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 165 | * Checks if enum with same name is not allowed. |
| 166 | */ |
| 167 | @Test(expected = ParserException.class) |
| 168 | public void processBitWithDuplicateName() throws IOException, ParserException { |
| 169 | |
| 170 | YangNode node = manager.getDataModel("src/test/resources/BitWithDuplicateName.yang"); |
| 171 | } |
| 172 | } |