Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +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 | f6ccc97 | 2016-03-25 11:25:36 +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 java.io.IOException; |
| 20 | import java.util.List; |
| 21 | import java.util.ListIterator; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 24 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 25 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 26 | import org.onosproject.yangutils.datamodel.YangList; |
| 27 | import org.onosproject.yangutils.datamodel.YangModule; |
| 28 | import org.onosproject.yangutils.datamodel.YangNode; |
| 29 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 30 | import org.onosproject.yangutils.datamodel.YangType; |
| 31 | import org.onosproject.yangutils.datamodel.YangUnion; |
| 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 33 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 34 | |
| 35 | import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | import static org.hamcrest.core.Is.is; |
| 37 | |
| 38 | /** |
| 39 | * Test cases for testing union listener. |
| 40 | */ |
| 41 | public class UnionListenerTest { |
| 42 | |
| 43 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 44 | |
| 45 | /** |
| 46 | * Checks union when type is in leaf. |
| 47 | */ |
| 48 | @Test |
| 49 | public void processUnionWhenTypeInLeaf() throws IOException, ParserException { |
| 50 | |
| 51 | YangNode node = manager.getDataModel("src/test/resources/UnionWhenTypeInLeaf.yang"); |
| 52 | |
| 53 | // Check whether the data model tree returned is of type module. |
| 54 | assertThat((node instanceof YangModule), is(true)); |
| 55 | |
| 56 | // Check whether the node type is set properly to module. |
| 57 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 58 | |
| 59 | // Check whether the module name is set correctly. |
| 60 | YangModule yangNode = (YangModule) node; |
| 61 | assertThat(yangNode.getName(), is("Test")); |
| 62 | |
| 63 | YangList yangList = (YangList) yangNode.getChild(); |
| 64 | assertThat(yangList.getName(), is("valid")); |
| 65 | |
| 66 | ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator(); |
| 67 | YangLeaf leafInfo = leafIterator.next(); |
| 68 | |
| 69 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 70 | |
| 71 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION)); |
| 72 | assertThat(leafInfo.getDataType().getDataTypeName(), is("union")); |
| 73 | |
| 74 | YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo(); |
| 75 | |
| 76 | List<YangType<?>> typeList = yangUnion.getTypeList(); |
| 77 | ListIterator<YangType<?>> typeListIterator = typeList.listIterator(); |
| 78 | YangType<?> yangType = typeListIterator.next(); |
| 79 | |
| 80 | assertThat(yangType.getDataTypeName(), is("int32")); |
| 81 | assertThat(yangType.getDataType(), is(YangDataTypes.INT32)); |
| 82 | |
| 83 | YangType<?> yangTypeEnum = typeListIterator.next(); |
| 84 | |
| 85 | assertThat(yangTypeEnum.getDataTypeName(), is("enumeration")); |
| 86 | assertThat(yangTypeEnum.getDataType(), is(YangDataTypes.ENUMERATION)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Checks union when type is in leaflist. |
| 91 | */ |
| 92 | @Test |
| 93 | public void processUnionWhenTypeInLeafList() throws IOException, ParserException { |
| 94 | |
| 95 | YangNode node = manager.getDataModel("src/test/resources/UnionWhenTypeInLeafList.yang"); |
| 96 | |
| 97 | // Check whether the data model tree returned is of type module. |
| 98 | assertThat((node instanceof YangModule), is(true)); |
| 99 | |
| 100 | // Check whether the node type is set properly to module. |
| 101 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 102 | |
| 103 | // Check whether the module name is set correctly. |
| 104 | YangModule yangNode = (YangModule) node; |
| 105 | assertThat(yangNode.getName(), is("Test")); |
| 106 | |
| 107 | YangList yangList = (YangList) yangNode.getChild(); |
| 108 | assertThat(yangList.getName(), is("valid")); |
| 109 | |
| 110 | ListIterator<YangLeafList> leafListIterator = yangList.getListOfLeafList().listIterator(); |
| 111 | YangLeafList leafListInfo = leafListIterator.next(); |
| 112 | |
| 113 | assertThat(leafListInfo.getLeafName(), is("invalid-interval")); |
| 114 | |
| 115 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UNION)); |
| 116 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("union")); |
| 117 | |
| 118 | YangUnion yangUnion = (YangUnion) leafListInfo.getDataType().getDataTypeExtendedInfo(); |
| 119 | |
| 120 | List<YangType<?>> typeList = yangUnion.getTypeList(); |
| 121 | ListIterator<YangType<?>> typeListIterator = typeList.listIterator(); |
| 122 | YangType<?> yangType = typeListIterator.next(); |
| 123 | |
| 124 | assertThat(yangType.getDataTypeName(), is("int32")); |
| 125 | assertThat(yangType.getDataType(), is(YangDataTypes.INT32)); |
| 126 | |
| 127 | YangType<?> yangTypeEnum = typeListIterator.next(); |
| 128 | |
| 129 | assertThat(yangTypeEnum.getDataTypeName(), is("enumeration")); |
| 130 | assertThat(yangTypeEnum.getDataType(), is(YangDataTypes.ENUMERATION)); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Checks union with empty type. |
| 135 | */ |
| 136 | @Test (expected = ParserException.class) |
| 137 | public void processUnionWithEmptyType() throws IOException, ParserException { |
| 138 | |
| 139 | YangNode node = manager.getDataModel("src/test/resources/UnionWithEmptyType.yang"); |
| 140 | } |
| 141 | } |