Brian O'Connor | 75218c2 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | */ |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 16 | package org.onosproject.yangutils.parser.impl.listeners; |
| 17 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | import java.util.ListIterator; |
janani b | b08850e | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 20 | import org.junit.Rule; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 21 | import org.junit.Test; |
janani b | b08850e | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 22 | import org.junit.rules.ExpectedException; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangContainer; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
| 25 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Gaurav Agrawal | a8391d0 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangLeaf; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangModule; |
| 30 | import org.onosproject.yangutils.datamodel.YangNode; |
| 31 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 33 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 34 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 35 | import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | import static org.hamcrest.core.Is.is; |
| 37 | |
| 38 | /** |
| 39 | * Test case for type listener. |
| 40 | */ |
| 41 | public class TypeListenerTest { |
| 42 | |
janani b | b08850e | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 43 | @Rule |
| 44 | public ExpectedException thrown = ExpectedException.none(); |
| 45 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 46 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 47 | |
| 48 | /** |
| 49 | * Checks derived statement without contraints. |
| 50 | */ |
| 51 | @Test |
| 52 | public void processDerivedTypeStatement() throws IOException, ParserException { |
| 53 | |
| 54 | YangNode node = manager.getDataModel("src/test/resources/DerivedTypeStatement.yang"); |
| 55 | |
| 56 | // Check whether the data model tree returned is of type module. |
| 57 | assertThat((node instanceof YangModule), is(true)); |
| 58 | |
| 59 | // Check whether the node type is set properly to module. |
| 60 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 61 | |
| 62 | // Check whether the module name is set correctly. |
| 63 | YangModule yangNode = (YangModule) node; |
| 64 | assertThat(yangNode.getName(), is("Test")); |
| 65 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 66 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 67 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 68 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 69 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 70 | assertThat(leafInfo.getDataType().getDataTypeName(), is("hello")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 71 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED)); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Checks valid yang data type. |
| 76 | */ |
| 77 | @Test |
| 78 | public void processIntegerTypeStatement() throws IOException, ParserException { |
| 79 | |
| 80 | YangNode node = manager.getDataModel("src/test/resources/IntegerTypeStatement.yang"); |
| 81 | |
| 82 | // Check whether the data model tree returned is of type module. |
| 83 | assertThat((node instanceof YangModule), is(true)); |
| 84 | |
| 85 | // Check whether the node type is set properly to module. |
| 86 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 87 | |
| 88 | // Check whether the module name is set correctly. |
| 89 | YangModule yangNode = (YangModule) node; |
| 90 | assertThat(yangNode.getName(), is("Test")); |
| 91 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 92 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 93 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 94 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 95 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 96 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 97 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Checks type for leaf-list. |
| 102 | */ |
| 103 | @Test |
| 104 | public void processLeafListSubStatementType() throws IOException, ParserException { |
| 105 | |
| 106 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementType.yang"); |
| 107 | |
| 108 | // Check whether the data model tree returned is of type module. |
| 109 | assertThat((node instanceof YangModule), is(true)); |
| 110 | |
| 111 | // Check whether the node type is set properly to module. |
| 112 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 113 | |
| 114 | // Check whether the module name is set correctly. |
| 115 | YangModule yangNode = (YangModule) node; |
| 116 | assertThat(yangNode.getName(), is("Test")); |
| 117 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 118 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 119 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 120 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 121 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 122 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 123 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 124 | } |
janani b | b08850e | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 125 | |
| 126 | /** |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 127 | * Checks for type instance-identifier. |
janani b | b08850e | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 128 | */ |
| 129 | @Test |
| 130 | public void processInstanceIdentifierType() throws IOException, ParserException { |
| 131 | |
janani b | b08850e | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 132 | YangNode node = manager |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 133 | .getDataModel("src/test/resources/InstanceIdentifierListener.yang"); |
| 134 | |
| 135 | // Check whether the data model tree returned is of type module. |
| 136 | assertThat((node instanceof YangModule), is(true)); |
| 137 | |
| 138 | // Check whether the node type is set properly to module. |
| 139 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 140 | |
| 141 | // Check whether the module name is set correctly. |
| 142 | YangModule yangNode = (YangModule) node; |
| 143 | assertThat(yangNode.getName(), is("Test")); |
| 144 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 145 | ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator(); |
| 146 | YangLeaf leafInfo = leafIterator.next(); |
| 147 | |
| 148 | assertThat(leafInfo.getName(), is("invalid-interval")); |
| 149 | assertThat(leafInfo.getDataType().getDataTypeName(), is("instance-identifier")); |
| 150 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INSTANCE_IDENTIFIER)); |
janani b | b08850e | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 151 | } |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Checks for leaf ref path concatenation. |
| 155 | */ |
| 156 | @Test |
| 157 | public void processLeafRefPathConcatenation() throws IOException, ParserException { |
| 158 | |
| 159 | YangNode node = manager |
| 160 | .getDataModel("src/test/resources/leafRefPathConcatenation.yang"); |
| 161 | |
| 162 | assertThat((node instanceof YangModule), is(true)); |
| 163 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 164 | YangModule yangNode = (YangModule) node; |
| 165 | assertThat(yangNode.getName(), is("test")); |
| 166 | |
| 167 | YangTypeDef typeDef = (YangTypeDef) yangNode.getChild(); |
| 168 | assertThat(typeDef.getName(), is("isis-instance-state-ref")); |
| 169 | assertThat(typeDef.getTypeDefBaseType().getDataType(), is(YangDataTypes.LEAFREF)); |
| 170 | YangLeafRef leafRef = ((YangLeafRef) typeDef.getTypeDefBaseType().getDataTypeExtendedInfo()); |
| 171 | assertThat(leafRef.getPath(), is("/isis-prefix-ipv4-std/default-metric")); |
| 172 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 173 | } |