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; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangLeaf; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangModule; |
| 27 | import org.onosproject.yangutils.datamodel.YangNode; |
| 28 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 30 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 31 | |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 32 | import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | import static org.hamcrest.core.Is.is; |
| 34 | |
| 35 | /** |
| 36 | * Test case for type listener. |
| 37 | */ |
| 38 | public class TypeListenerTest { |
| 39 | |
janani b | 6240d29 | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 40 | @Rule |
| 41 | public ExpectedException thrown = ExpectedException.none(); |
| 42 | |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 43 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 44 | |
| 45 | /** |
| 46 | * Checks derived statement without contraints. |
| 47 | */ |
| 48 | @Test |
| 49 | public void processDerivedTypeStatement() throws IOException, ParserException { |
| 50 | |
| 51 | YangNode node = manager.getDataModel("src/test/resources/DerivedTypeStatement.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 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 63 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 64 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 65 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 66 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 67 | assertThat(leafInfo.getDataType().getDataTypeName(), is("hello")); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 68 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED)); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Checks valid yang data type. |
| 73 | */ |
| 74 | @Test |
| 75 | public void processIntegerTypeStatement() throws IOException, ParserException { |
| 76 | |
| 77 | YangNode node = manager.getDataModel("src/test/resources/IntegerTypeStatement.yang"); |
| 78 | |
| 79 | // Check whether the data model tree returned is of type module. |
| 80 | assertThat((node instanceof YangModule), is(true)); |
| 81 | |
| 82 | // Check whether the node type is set properly to module. |
| 83 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 84 | |
| 85 | // Check whether the module name is set correctly. |
| 86 | YangModule yangNode = (YangModule) node; |
| 87 | assertThat(yangNode.getName(), is("Test")); |
| 88 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 89 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 90 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 91 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 92 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 93 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 94 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Checks type for leaf-list. |
| 99 | */ |
| 100 | @Test |
| 101 | public void processLeafListSubStatementType() throws IOException, ParserException { |
| 102 | |
| 103 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementType.yang"); |
| 104 | |
| 105 | // Check whether the data model tree returned is of type module. |
| 106 | assertThat((node instanceof YangModule), is(true)); |
| 107 | |
| 108 | // Check whether the node type is set properly to module. |
| 109 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 110 | |
| 111 | // Check whether the module name is set correctly. |
| 112 | YangModule yangNode = (YangModule) node; |
| 113 | assertThat(yangNode.getName(), is("Test")); |
| 114 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 115 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 116 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 117 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 118 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 119 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 120 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 121 | } |
janani b | 6240d29 | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Checks for unsupported type leafref. |
| 125 | */ |
| 126 | @Test |
| 127 | public void processLeafrefType() throws IOException, ParserException { |
| 128 | |
| 129 | thrown.expect(ParserException.class); |
| 130 | thrown.expectMessage("YANG file error : \"leafref\" is not supported in current version," |
| 131 | + " please check wiki for YANG utils road map."); |
| 132 | |
| 133 | YangNode node = manager |
| 134 | .getDataModel("src/test/resources/LeafrefInvalidIdentifier.yang"); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Checks for unsupported type identityref. |
| 139 | */ |
| 140 | @Test |
| 141 | public void processIdentityrefType() throws IOException, ParserException { |
| 142 | |
| 143 | thrown.expect(ParserException.class); |
| 144 | thrown.expectMessage("YANG file error : \"identityref\" is not supported in current version," |
| 145 | + " please check wiki for YANG utils road map."); |
| 146 | |
| 147 | YangNode node = manager |
| 148 | .getDataModel("src/test/resources/IdentityrefInvalidIdentifier.yang"); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Checks for unsupported type instance identifier. |
| 153 | */ |
| 154 | @Test |
| 155 | public void processInstanceIdentifierType() throws IOException, ParserException { |
| 156 | |
| 157 | thrown.expect(ParserException.class); |
| 158 | thrown.expectMessage("YANG file error : \"instance-identifier\" is not supported in current version," |
| 159 | + " please check wiki for YANG utils road map."); |
| 160 | |
| 161 | YangNode node = manager |
| 162 | .getDataModel("src/test/resources/InstanceIdentifierInvalidIdentifier.yang"); |
| 163 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 164 | } |