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; |
| 24 | import org.junit.rules.ExpectedException; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 26 | import org.onosproject.yangutils.datamodel.YangModule; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangNode; |
| 28 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | 50b73a0 | 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 | 50b73a0 | 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 mandatory listener. |
| 37 | */ |
| 38 | public class MandatoryListenerTest { |
| 39 | |
| 40 | @Rule |
| 41 | public ExpectedException thrown = ExpectedException.none(); |
| 42 | |
| 43 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 44 | |
| 45 | /** |
| 46 | * Checks valid mandatory with value true statement. |
| 47 | */ |
| 48 | @Test |
| 49 | public void processMandatoryTrue() throws IOException, ParserException { |
| 50 | |
| 51 | YangNode node = manager.getDataModel("src/test/resources/MandatoryTrue.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 | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 63 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 64 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 65 | |
| 66 | // Check whether the mandatory value is set correctly. |
| 67 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 68 | assertThat(leafInfo.isMandatory(), is(true)); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Checks valid mandatory with value false statement. |
| 73 | */ |
| 74 | @Test |
| 75 | public void processMandatoryFalse() throws IOException, ParserException { |
| 76 | |
| 77 | YangNode node = manager.getDataModel("src/test/resources/MandatoryFalse.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 | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 89 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 90 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 91 | |
| 92 | // Check whether the mandatory value is set correctly. |
| 93 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 94 | assertThat(leafInfo.isMandatory(), is(false)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Checks default value of mandatory statement. |
| 99 | */ |
| 100 | @Test |
| 101 | public void processMandatoryDefaultValue() throws IOException, ParserException { |
| 102 | |
| 103 | YangNode node = manager.getDataModel("src/test/resources/MandatoryDefaultValue.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 | 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 the mandatory value is set correctly. |
| 119 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 120 | assertThat(leafInfo.isMandatory(), is(false)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Checks invalid of mandatory statement and expects exception. |
| 125 | */ |
| 126 | @Test |
| 127 | public void processMandatoryEmptyStatement() throws IOException, ParserException { |
| 128 | thrown.expect(ParserException.class); |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 129 | thrown.expectMessage("no viable alternative at input ';'"); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 130 | YangNode node = manager.getDataModel("src/test/resources/MandatoryEmptyStatement.yang"); |
| 131 | } |
| 132 | |
| 133 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 134 | * Checks invalid mandatory statement(without statement end) and expects |
| 135 | * exception. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 136 | */ |
| 137 | @Test |
| 138 | public void processMandatoryWithoutStatementEnd() throws IOException, ParserException { |
| 139 | thrown.expect(ParserException.class); |
| 140 | thrown.expectMessage("missing ';' at '}'"); |
| 141 | YangNode node = manager.getDataModel("src/test/resources/MandatoryWithoutStatementEnd.yang"); |
| 142 | } |
| 143 | |
| 144 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 145 | * Checks mandatory statement as sub-statement of module and expects |
| 146 | * exception. |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 147 | */ |
| 148 | @Test |
| 149 | public void processModuleSubStatementMandatory() throws IOException, ParserException { |
| 150 | thrown.expect(ParserException.class); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 151 | thrown.expectMessage("mismatched input 'mandatory' expecting {'augment', 'choice', 'contact', 'container'," |
| 152 | + " 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include'," |
Vidyashree Rama | 07021a2 | 2016-03-09 20:41:44 +0530 | [diff] [blame] | 153 | + " 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference'," |
| 154 | + " 'revision', 'rpc', 'typedef', 'uses', '}'}"); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 155 | YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementMandatory.yang"); |
| 156 | } |
| 157 | } |