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; |
| 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 bit listener. |
| 38 | */ |
| 39 | public class BitListenerTest { |
| 40 | |
| 41 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 42 | |
| 43 | /** |
| 44 | * Checks bit statement without position. |
| 45 | */ |
| 46 | @Test |
| 47 | public void processBitTypeStatement() throws IOException, ParserException { |
| 48 | |
| 49 | YangNode node = manager.getDataModel("src/test/resources/BitTypeStatement.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 | |
| 64 | assertThat(leafInfo.getLeafName(), is("mybits")); |
| 65 | assertThat(leafInfo.getDataType().getDataTypeName(), is("bits")); |
| 66 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS)); |
| 67 | assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(), |
| 68 | is("mybits")); |
| 69 | |
| 70 | Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet(); |
| 71 | for (YangBit tmp : bitSet) { |
| 72 | if (tmp.getBitName().equals("disable-nagle")) { |
| 73 | assertThat(tmp.getPosition(), is(0)); |
| 74 | } else if (tmp.getBitName().equals("auto-sense-speed")) { |
| 75 | assertThat(tmp.getPosition(), is(1)); |
| 76 | } else if (tmp.getBitName().equals("Ten-Mb-only")) { |
| 77 | assertThat(tmp.getPosition(), is(2)); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Checks if enum with same name is not allowed. |
| 84 | */ |
| 85 | @Test(expected = ParserException.class) |
| 86 | public void processBitWithDuplicateName() throws IOException, ParserException { |
| 87 | |
| 88 | YangNode node = manager.getDataModel("src/test/resources/BitWithDuplicateName.yang"); |
| 89 | } |
| 90 | } |