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