Vidyashree Rama | 49abe71 | 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 | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame^] | 19 | import java.io.IOException; |
| 20 | import java.util.ListIterator; |
| 21 | |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 22 | import org.junit.Rule; |
| 23 | import org.junit.Test; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 24 | import org.junit.rules.ExpectedException; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame^] | 25 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 26 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangLeaf; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame^] | 28 | import org.onosproject.yangutils.datamodel.YangList; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangModule; |
| 30 | import org.onosproject.yangutils.datamodel.YangNode; |
| 31 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.YangStatusType; |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 34 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 35 | |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 36 | import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | import static org.hamcrest.core.Is.is; |
| 38 | |
| 39 | /** |
| 40 | * Test cases for testing container listener. |
| 41 | */ |
| 42 | public class ContainerListenerTest { |
| 43 | |
| 44 | @Rule |
| 45 | public ExpectedException thrown = ExpectedException.none(); |
| 46 | |
| 47 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 48 | |
| 49 | /** |
| 50 | * Checks container statement as sub-statement of module. |
| 51 | */ |
| 52 | @Test |
| 53 | public void processModuleSubStatementContainer() throws IOException, ParserException { |
| 54 | |
| 55 | YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementContainer.yang"); |
| 56 | |
| 57 | assertThat((node instanceof YangModule), is(true)); |
| 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 | |
| 66 | // Check whether the container is child of module |
| 67 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 68 | assertThat(yangContainer.getName(), is("valid")); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Checks container statement as sub-statement of container. |
| 73 | */ |
| 74 | @Test |
| 75 | public void processContainerSubStatementContainer() throws IOException, ParserException { |
| 76 | |
| 77 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementContainer.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 container is child of module |
| 89 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 90 | assertThat(yangContainer.getName(), is("ospf")); |
| 91 | |
| 92 | // Check whether the container is child of container |
| 93 | YangContainer yangContainer1 = (YangContainer) yangContainer.getChild(); |
| 94 | assertThat(yangContainer1.getName(), is("valid")); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Checks container statement as sub-statement of list. |
| 99 | */ |
| 100 | @Test |
| 101 | public void processListSubStatementContainer() throws IOException, ParserException { |
| 102 | |
| 103 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementContainer.yang"); |
| 104 | |
| 105 | assertThat((node instanceof YangModule), is(true)); |
| 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 | |
| 114 | // Check whether the list is child of module |
| 115 | YangList yangList1 = (YangList) yangNode.getChild(); |
| 116 | assertThat(yangList1.getName(), is("ospf")); |
| 117 | |
| 118 | ListIterator<String> keyList = yangList1.getKeyList().listIterator(); |
| 119 | assertThat(keyList.next(), is("process-id")); |
| 120 | |
| 121 | // Check whether the list is child of list |
| 122 | YangContainer yangContainer = (YangContainer) yangList1.getChild(); |
| 123 | assertThat(yangContainer.getName(), is("interface")); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Checks container with all its sub-statements. |
| 128 | */ |
| 129 | @Test |
| 130 | public void processContainerSubStatements() throws IOException, ParserException { |
| 131 | |
| 132 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatements.yang"); |
| 133 | |
| 134 | assertThat((node instanceof YangModule), is(true)); |
| 135 | |
| 136 | // Check whether the node type is set properly to module. |
| 137 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 138 | |
| 139 | // Check whether the module name is set correctly. |
| 140 | YangModule yangNode = (YangModule) node; |
| 141 | assertThat(yangNode.getName(), is("Test")); |
| 142 | |
| 143 | // Check whether the container is child of module |
| 144 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 145 | |
| 146 | // Check whether container properties as set correctly. |
| 147 | assertThat(yangContainer.getName(), is("ospf")); |
| 148 | |
| 149 | assertThat(yangContainer.isConfig(), is(true)); |
| 150 | assertThat(yangContainer.getPresence(), is("\"ospf logs\"")); |
| 151 | assertThat(yangContainer.getDescription(), is("\"container description\"")); |
| 152 | assertThat(yangContainer.getStatus(), is(YangStatusType.CURRENT)); |
| 153 | assertThat(yangContainer.getReference(), is("\"container reference\"")); |
| 154 | |
| 155 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame^] | 156 | ListIterator<YangLeaf> leafIterator = yangContainer.getListOfLeaf().listIterator(); |
| 157 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 49abe71 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 158 | |
| 159 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 160 | assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\"")); |
| 161 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 162 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 163 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 164 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Checks cardinality of sub-statements of container. |
| 169 | */ |
| 170 | @Test |
| 171 | public void processContainerSubStatementCardinality() throws IOException, ParserException { |
| 172 | thrown.expect(ParserException.class); |
| 173 | thrown.expectMessage("Internal parser error detected: Invalid cardinality in reference before processing."); |
| 174 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementCardinality.yang"); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Checks container as root node. |
| 179 | */ |
| 180 | @Test |
| 181 | public void processContainerRootNode() throws IOException, ParserException { |
| 182 | thrown.expect(ParserException.class); |
| 183 | thrown.expectMessage("no viable alternative at input 'container'"); |
| 184 | YangNode node = manager.getDataModel("src/test/resources/ContainerRootNode.yang"); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Checks invalid identifier for container statement. |
| 189 | */ |
| 190 | @Test |
| 191 | public void processContainerInvalidIdentifier() throws IOException, ParserException { |
| 192 | thrown.expect(ParserException.class); |
| 193 | thrown.expectMessage("mismatched input '1valid' expecting IDENTIFIER"); |
| 194 | YangNode node = manager.getDataModel("src/test/resources/ContainerInvalidIdentifier.yang"); |
| 195 | } |
| 196 | } |