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