Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +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 | |
| 19 | import java.io.IOException; |
| 20 | import java.util.ListIterator; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 23 | import org.onosproject.yangutils.datamodel.YangGrouping; |
| 24 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 25 | import org.onosproject.yangutils.datamodel.YangList; |
| 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; |
| 30 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 31 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 32 | |
| 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.core.Is.is; |
| 35 | |
| 36 | /** |
| 37 | * Test cases for testing grouping listener. |
| 38 | */ |
| 39 | public class GroupingListenerTest { |
| 40 | |
| 41 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 42 | |
| 43 | /** |
| 44 | * Checks grouping statement inside module. |
| 45 | */ |
| 46 | @Test |
| 47 | public void processGroupingInModule() throws IOException, ParserException { |
| 48 | |
| 49 | YangNode node = manager.getDataModel("src/test/resources/GroupingInModule.yang"); |
| 50 | |
| 51 | // Check whether the data model tree returned is of type module. |
| 52 | assertThat((node instanceof YangModule), is(true)); |
| 53 | |
| 54 | // Check whether the node type is set properly to module. |
| 55 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 56 | |
| 57 | // Check whether the module name is set correctly. |
| 58 | YangModule yangNode = (YangModule) node; |
| 59 | assertThat(yangNode.getName(), is("Test")); |
| 60 | |
| 61 | YangGrouping yangGrouping = (YangGrouping) yangNode.getChild(); |
| 62 | assertThat(yangGrouping.getName(), is("endpoint")); |
| 63 | |
| 64 | ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator(); |
| 65 | YangLeaf leafInfo = leafIterator.next(); |
| 66 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 67 | assertThat(leafInfo.getName(), is("address")); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Checks grouping statement inside container. |
| 72 | */ |
| 73 | @Test |
| 74 | public void processGroupingInContainer() throws IOException, ParserException { |
| 75 | |
| 76 | YangNode node = manager.getDataModel("src/test/resources/GroupingInContainer.yang"); |
| 77 | |
| 78 | // Check whether the data model tree returned is of type module. |
| 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 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 89 | assertThat(yangContainer.getName(), is("valid")); |
| 90 | |
| 91 | YangGrouping yangGrouping = (YangGrouping) yangContainer.getChild(); |
| 92 | assertThat(yangGrouping.getName(), is("endpoint")); |
| 93 | |
| 94 | ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator(); |
| 95 | YangLeaf leafInfo = leafIterator.next(); |
| 96 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 97 | assertThat(leafInfo.getName(), is("address")); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Checks grouping statement inside list. |
| 102 | */ |
| 103 | @Test |
| 104 | public void processGroupingInList() throws IOException, ParserException { |
| 105 | |
| 106 | YangNode node = manager.getDataModel("src/test/resources/GroupingInList.yang"); |
| 107 | |
| 108 | // Check whether the data model tree returned is of type module. |
| 109 | assertThat((node instanceof YangModule), is(true)); |
| 110 | |
| 111 | // Check whether the node type is set properly to module. |
| 112 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 113 | |
| 114 | // Check whether the module name is set correctly. |
| 115 | YangModule yangNode = (YangModule) node; |
| 116 | assertThat(yangNode.getName(), is("Test")); |
| 117 | |
| 118 | YangList yangList = (YangList) yangNode.getChild(); |
| 119 | assertThat(yangList.getName(), is("valid")); |
| 120 | |
| 121 | YangGrouping yangGrouping = (YangGrouping) yangList.getChild(); |
| 122 | assertThat(yangGrouping.getName(), is("endpoint")); |
| 123 | |
| 124 | ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator(); |
| 125 | YangLeaf leafInfo = leafIterator.next(); |
| 126 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 127 | assertThat(leafInfo.getName(), is("address")); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Checks grouping with attributes. |
| 132 | */ |
| 133 | @Test |
| 134 | public void processGroupingAttributes() throws IOException, ParserException { |
| 135 | |
| 136 | YangNode node = manager.getDataModel("src/test/resources/GroupingAttributes.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 | |
| 148 | YangList yangList = (YangList) yangNode.getChild(); |
| 149 | assertThat(yangList.getName(), is("valid")); |
| 150 | |
| 151 | YangGrouping yangGrouping = (YangGrouping) yangList.getChild(); |
| 152 | assertThat(yangGrouping.getName(), is("endpoint")); |
| 153 | assertThat(yangGrouping.getStatus(), is(YangStatusType.CURRENT)); |
| 154 | assertThat(yangGrouping.getReference(), is("\"RFC 6020\"")); |
| 155 | assertThat(yangGrouping.getDescription(), is("\"grouping under test\"")); |
| 156 | |
| 157 | ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator(); |
| 158 | YangLeaf leafInfo = leafIterator.next(); |
| 159 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 160 | assertThat(leafInfo.getName(), is("address")); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Checks duplicate grouping in list. |
| 165 | */ |
| 166 | @Test(expected = ParserException.class) |
| 167 | public void processDuplicateGroupingInList() throws IOException, ParserException { |
| 168 | |
| 169 | YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInList.yang"); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Checks duplicate grouping in container. |
| 174 | */ |
| 175 | @Test (expected = ParserException.class) |
| 176 | public void processDuplicateGroupingInContainer() throws IOException, ParserException { |
| 177 | |
| 178 | YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInContainer.yang"); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Checks duplicate grouping in module. |
| 183 | */ |
| 184 | @Test (expected = ParserException.class) |
| 185 | public void processDuplicateGroupingInModule() throws IOException, ParserException { |
| 186 | |
| 187 | YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInModule.yang"); |
| 188 | } |
| 189 | } |