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; |
| 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.YangLeafList; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangList; |
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; |
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; |
| 34 | import static org.hamcrest.core.Is.is; |
| 35 | |
| 36 | /** |
| 37 | * Test cases for testing max-elements listener. |
| 38 | */ |
| 39 | public class MaxElementsListenerTest { |
| 40 | |
| 41 | @Rule |
| 42 | public ExpectedException thrown = ExpectedException.none(); |
| 43 | |
| 44 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 45 | |
| 46 | /** |
| 47 | * Checks max-elements as sub-statements of leaf-list. |
| 48 | */ |
| 49 | @Test |
| 50 | public void processLeafListSubStatementMaxElements() throws IOException, ParserException { |
| 51 | |
| 52 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementMaxElements.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 | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 64 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 65 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 66 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 67 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 68 | assertThat(leafListInfo.getMaxElelements(), is(3)); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Checks max-elements as sub-statements of list. |
| 73 | */ |
| 74 | @Test |
| 75 | public void processListSubStatementMaxElements() throws IOException, ParserException { |
| 76 | |
| 77 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementMaxElements.yang"); |
| 78 | |
| 79 | assertThat((node instanceof YangModule), is(true)); |
| 80 | |
| 81 | // Check whether the node type is set properly to module. |
| 82 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 83 | |
| 84 | // Check whether the module name is set correctly. |
| 85 | YangModule yangNode = (YangModule) node; |
| 86 | assertThat(yangNode.getName(), is("Test")); |
| 87 | |
| 88 | // Check whether the list is child of module |
| 89 | YangList yangList = (YangList) yangNode.getChild(); |
| 90 | assertThat(yangList.getName(), is("valid")); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 91 | assertThat(yangList.getMaxElements(), is(3)); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 95 | * Checks whether exception is thrown when max-elements statement without |
| 96 | * statement end is given as input. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 97 | */ |
| 98 | @Test |
| 99 | public void processMaxElementsWithoutStatementEnd() throws IOException, ParserException { |
| 100 | thrown.expect(ParserException.class); |
| 101 | thrown.expectMessage("missing ';' at 'description'"); |
| 102 | YangNode node = manager.getDataModel("src/test/resources/MaxElementsWithoutStatementEnd.yang"); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Checks whether exception is thrown when max-elements cardinality is not |
| 107 | * as per the grammar. |
| 108 | */ |
| 109 | @Test |
| 110 | public void processMaxElementsCardinality() throws IOException, ParserException { |
| 111 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 112 | thrown.expectMessage("YANG file error: \"max-elements\" is defined more than once in \"leaf-list " + |
| 113 | "invalid-interval\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 114 | YangNode node = manager.getDataModel("src/test/resources/MaxElementsCardinality.yang"); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Checks unbounded value of max-elements statement. |
| 119 | */ |
| 120 | @Test |
| 121 | public void processMaxElementsUnbounded() throws IOException, ParserException { |
| 122 | |
| 123 | YangNode node = manager.getDataModel("src/test/resources/MaxElementsUnbounded.yang"); |
| 124 | |
| 125 | // Check whether the data model tree returned is of type module. |
| 126 | assertThat((node instanceof YangModule), is(true)); |
| 127 | |
| 128 | // Check whether the node type is set properly to module. |
| 129 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 130 | |
| 131 | // Check whether the module name is set correctly. |
| 132 | YangModule yangNode = (YangModule) node; |
| 133 | assertThat(yangNode.getName(), is("Test")); |
| 134 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 135 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 136 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 137 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 138 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 139 | assertThat(leafListInfo.getMaxElelements(), is(2147483647)); |
| 140 | } |
Vidyashree Rama | ec578d0 | 2016-02-24 11:26:42 +0530 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Checks default value of max-elements statement. |
| 144 | */ |
| 145 | @Test |
| 146 | public void processMaxElementsDefaultValue() throws IOException, ParserException { |
| 147 | |
| 148 | YangNode node = manager.getDataModel("src/test/resources/MaxElementsDefaultValue.yang"); |
| 149 | |
| 150 | // Check whether the data model tree returned is of type module. |
| 151 | assertThat((node instanceof YangModule), is(true)); |
| 152 | |
| 153 | // Check whether the node type is set properly to module. |
| 154 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 155 | |
| 156 | // Check whether the module name is set correctly. |
| 157 | YangModule yangNode = (YangModule) node; |
| 158 | assertThat(yangNode.getName(), is("Test")); |
| 159 | |
| 160 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 161 | YangLeafList leafListInfo = leafListIterator.next(); |
| 162 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 163 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | ec578d0 | 2016-02-24 11:26:42 +0530 | [diff] [blame] | 164 | assertThat(leafListInfo.getMaxElelements(), is(2147483647)); |
| 165 | } |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * Checks whether exception is thrown when invalid min-elements value is |
| 169 | * given as input. |
| 170 | */ |
| 171 | @Test |
| 172 | public void processMaxElementsMaxValue() throws IOException, ParserException { |
| 173 | thrown.expect(ParserException.class); |
| 174 | thrown.expectMessage("YANG file error : max-elements value 77777777777777777777777 is not valid."); |
| 175 | YangNode node = manager.getDataModel("src/test/resources/MaxElementsMaxValue.yang"); |
| 176 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 177 | } |