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 | |
| 19 | import org.junit.Rule; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import org.junit.rules.ExpectedException; |
| 23 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 24 | import org.onosproject.yangutils.datamodel.YangModule; |
| 25 | import org.onosproject.yangutils.datamodel.YangNode; |
| 26 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 27 | import org.onosproject.yangutils.datamodel.YangList; |
| 28 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 29 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | import java.util.ListIterator; |
| 33 | |
| 34 | import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | import static org.hamcrest.core.Is.is; |
| 36 | |
| 37 | /** |
| 38 | * Test cases for testing max-elements listener. |
| 39 | */ |
| 40 | public class MaxElementsListenerTest { |
| 41 | |
| 42 | @Rule |
| 43 | public ExpectedException thrown = ExpectedException.none(); |
| 44 | |
| 45 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 46 | |
| 47 | /** |
| 48 | * Checks max-elements as sub-statements of leaf-list. |
| 49 | */ |
| 50 | @Test |
| 51 | public void processLeafListSubStatementMaxElements() throws IOException, ParserException { |
| 52 | |
| 53 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementMaxElements.yang"); |
| 54 | |
| 55 | // Check whether the data model tree returned is of type module. |
| 56 | assertThat((node instanceof YangModule), is(true)); |
| 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 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 65 | ListIterator<YangLeafList<?>> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 66 | YangLeafList<?> leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 67 | |
| 68 | assertThat(leafListInfo.getLeafName(), is("invalid-interval")); |
| 69 | assertThat(leafListInfo.getMaxElelements(), is(3)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Checks max-elements as sub-statements of list. |
| 74 | */ |
| 75 | @Test |
| 76 | public void processListSubStatementMaxElements() throws IOException, ParserException { |
| 77 | |
| 78 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementMaxElements.yang"); |
| 79 | |
| 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 | |
| 89 | // Check whether the list is child of module |
| 90 | YangList yangList = (YangList) yangNode.getChild(); |
| 91 | assertThat(yangList.getName(), is("valid")); |
| 92 | assertThat(yangList.getMaxElelements(), is(3)); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Checks whether exception is thrown when invalid max-elements keyword |
| 97 | * is given as input. |
| 98 | */ |
| 99 | @Test |
| 100 | public void processMaxElementsInvalidStatement() throws IOException, ParserException { |
| 101 | thrown.expect(ParserException.class); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 102 | thrown.expectMessage("extraneous input 'max-element' expecting {'config', 'description', 'if-feature'," |
| 103 | + " 'max-elements', 'min-elements', 'must', 'ordered-by', 'reference', 'status', 'type', 'units', " |
| 104 | + "'when', '}'}"); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 105 | YangNode node = manager.getDataModel("src/test/resources/MaxElementsInvalidStatement.yang"); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Checks whether exception is thrown when max-elements statement without statement |
| 110 | * end is given as input. |
| 111 | */ |
| 112 | @Test |
| 113 | public void processMaxElementsWithoutStatementEnd() throws IOException, ParserException { |
| 114 | thrown.expect(ParserException.class); |
| 115 | thrown.expectMessage("missing ';' at 'description'"); |
| 116 | YangNode node = manager.getDataModel("src/test/resources/MaxElementsWithoutStatementEnd.yang"); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Checks whether exception is thrown when max-elements cardinality is not |
| 121 | * as per the grammar. |
| 122 | */ |
| 123 | @Test |
| 124 | public void processMaxElementsCardinality() throws IOException, ParserException { |
| 125 | thrown.expect(ParserException.class); |
| 126 | thrown.expectMessage("Internal parser error detected: Invalid cardinality in max-elements before processing."); |
| 127 | YangNode node = manager.getDataModel("src/test/resources/MaxElementsCardinality.yang"); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Checks unbounded value of max-elements statement. |
| 132 | */ |
| 133 | @Test |
| 134 | public void processMaxElementsUnbounded() throws IOException, ParserException { |
| 135 | |
| 136 | YangNode node = manager.getDataModel("src/test/resources/MaxElementsUnbounded.yang"); |
| 137 | |
| 138 | // Check whether the data model tree returned is of type module. |
| 139 | assertThat((node instanceof YangModule), is(true)); |
| 140 | |
| 141 | // Check whether the node type is set properly to module. |
| 142 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 143 | |
| 144 | // Check whether the module name is set correctly. |
| 145 | YangModule yangNode = (YangModule) node; |
| 146 | assertThat(yangNode.getName(), is("Test")); |
| 147 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 148 | ListIterator<YangLeafList<?>> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 149 | YangLeafList<?> leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 150 | |
| 151 | assertThat(leafListInfo.getLeafName(), is("invalid-interval")); |
| 152 | assertThat(leafListInfo.getMaxElelements(), is(2147483647)); |
| 153 | } |
| 154 | } |