Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.util.ListIterator; |
| 21 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 22 | import org.junit.Rule; |
| 23 | import org.junit.Test; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 24 | import org.junit.rules.ExpectedException; |
| 25 | import org.onosproject.yangutils.datamodel.YangLeaf; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Vidyashree Rama | 50b73a0 | 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; |
| 30 | import org.onosproject.yangutils.datamodel.YangStatusType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 32 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 33 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 34 | import static org.hamcrest.MatcherAssert.assertThat; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 35 | import static org.hamcrest.Matchers.nullValue; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 36 | import static org.hamcrest.core.Is.is; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Test cases for units listener. |
| 40 | */ |
| 41 | public class UnitsListenerTest { |
| 42 | |
| 43 | @Rule |
| 44 | public ExpectedException thrown = ExpectedException.none(); |
| 45 | |
| 46 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 47 | |
| 48 | /** |
| 49 | * Checks valid units statement. |
| 50 | */ |
| 51 | @Test |
| 52 | public void processUnitsStatement() throws IOException, ParserException { |
| 53 | |
| 54 | YangNode node = manager.getDataModel("src/test/resources/UnitsStatement.yang"); |
| 55 | |
| 56 | // Check whether the data model tree returned is of type module. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 57 | assertThat(node instanceof YangModule, is(true)); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 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 | |
| 69 | // Check whether units value is set correctly. |
| 70 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 71 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Checks invalid units statement as sub-statement of module. |
| 76 | */ |
| 77 | @Test |
| 78 | public void processModuleSubStatementUnits() throws IOException, ParserException { |
| 79 | thrown.expect(ParserException.class); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 80 | thrown.expectMessage("mismatched input 'type' expecting {'augment', 'choice', 'contact', 'container', " |
| 81 | + "'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', " |
Vidyashree Rama | 07021a2 | 2016-03-09 20:41:44 +0530 | [diff] [blame] | 82 | + "'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', " |
| 83 | + "'reference', 'revision', 'rpc', 'typedef', 'uses', '}'}"); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 84 | YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementUnits.yang"); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Checks invalid units statement(without statement end). |
| 89 | */ |
| 90 | @Test |
| 91 | public void processUnitsWithoutStatementEnd() throws IOException, ParserException { |
| 92 | thrown.expect(ParserException.class); |
| 93 | thrown.expectMessage("mismatched input '}' expecting {';', '+'}"); |
| 94 | YangNode node = manager.getDataModel("src/test/resources/UnitsWithoutStatementEnd.yang"); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Checks order of units statement in leaf. |
| 99 | */ |
| 100 | @Test |
| 101 | public void processUnitsStatementOrder() throws IOException, ParserException { |
| 102 | |
| 103 | YangNode node = manager.getDataModel("src/test/resources/UnitsStatementOrder.yang"); |
| 104 | |
| 105 | // Check whether the data model tree returned is of type module. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 106 | assertThat(node instanceof YangModule, is(true)); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 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 | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 115 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 116 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 117 | |
| 118 | // Check whether leaf properties is set correctly. |
| 119 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 120 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 121 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 122 | assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\"")); |
| 123 | assertThat(leafInfo.isConfig(), is(true)); |
| 124 | assertThat(leafInfo.isMandatory(), is(true)); |
| 125 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 126 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Checks the default value of unit statement. |
| 131 | */ |
| 132 | @Test |
| 133 | public void processUnitsDefaultValue() throws IOException, ParserException { |
| 134 | |
| 135 | YangNode node = manager.getDataModel("src/test/resources/UnitsDefaultValue.yang"); |
| 136 | |
| 137 | // Check whether the data model tree returned is of type module. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 138 | assertThat(node instanceof YangModule, is(true)); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 139 | |
| 140 | // Check whether the node type is set properly to module. |
| 141 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 142 | |
| 143 | // Check whether the module name is set correctly. |
| 144 | YangModule yangNode = (YangModule) node; |
| 145 | assertThat(yangNode.getName(), is("Test")); |
| 146 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 147 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 148 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 149 | |
| 150 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 151 | assertThat(leafInfo.getUnits(), is(nullValue())); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Checks invalid occurance of units statement as sub-statement of leaf. |
| 156 | */ |
| 157 | @Test |
| 158 | public void processUnitsStatementCardinality() throws IOException, ParserException { |
| 159 | thrown.expect(ParserException.class); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 160 | thrown.expectMessage("YANG file error: Invalid cardinality of units in leaf \"invalid-interval\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 161 | YangNode node = manager.getDataModel("src/test/resources/UnitsStatementCardinality.yang"); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Checks valid units statement as sub-statement of leaf-list. |
| 166 | */ |
| 167 | @Test |
| 168 | public void processLeafListSubStatementUnits() throws IOException, ParserException { |
| 169 | |
| 170 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementUnits.yang"); |
| 171 | |
| 172 | // Check whether the data model tree returned is of type module. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 173 | assertThat(node instanceof YangModule, is(true)); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 174 | |
| 175 | // Check whether the node type is set properly to module. |
| 176 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 177 | |
| 178 | // Check whether the module name is set correctly. |
| 179 | YangModule yangNode = (YangModule) node; |
| 180 | assertThat(yangNode.getName(), is("Test")); |
| 181 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 182 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 183 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 184 | |
| 185 | // Check whether units value is set correctly. |
| 186 | assertThat(leafListInfo.getLeafName(), is("invalid-interval")); |
| 187 | assertThat(leafListInfo.getUnits(), is("\"seconds\"")); |
| 188 | } |
| 189 | } |