Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +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 | 09e1ca5 | 2016-03-29 12:00:42 +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 | |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.yangutils.datamodel.YangModule; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 26 | import org.onosproject.yangutils.datamodel.YangRpc; |
| 27 | import org.onosproject.yangutils.datamodel.YangInput; |
| 28 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 29 | import org.onosproject.yangutils.datamodel.YangList; |
| 30 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 31 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 32 | import org.onosproject.yangutils.datamodel.YangStatusType; |
| 33 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 34 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 35 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 36 | |
| 37 | import static org.hamcrest.core.Is.is; |
| 38 | import static org.junit.Assert.assertThat; |
| 39 | |
| 40 | /** |
| 41 | * Test cases for testing Input listener functionality. |
| 42 | */ |
| 43 | public class InputListenerTest { |
| 44 | |
| 45 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 46 | |
| 47 | /** |
| 48 | * Checks input statements with data definition statements as sub-statements. |
| 49 | */ |
| 50 | @Test |
| 51 | public void processInputStatementWithDataDefinition() throws IOException, ParserException { |
| 52 | |
| 53 | YangNode node = manager.getDataModel("src/test/resources/InputStatementWithDataDefinition.yang"); |
| 54 | |
| 55 | assertThat((node instanceof YangModule), is(true)); |
| 56 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 57 | YangModule yangNode = (YangModule) node; |
| 58 | assertThat(yangNode.getName(), is("rock")); |
| 59 | |
| 60 | YangRpc yangRpc = (YangRpc) yangNode.getChild(); |
| 61 | assertThat(yangRpc.getName(), is("activate-software-image")); |
| 62 | |
| 63 | YangInput yangInput = (YangInput) yangRpc.getChild(); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 64 | assertThat(yangInput.getName(), is("activate-software-image_input")); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 65 | ListIterator<YangLeaf> leafIterator = yangInput.getListOfLeaf().listIterator(); |
| 66 | YangLeaf leafInfo = leafIterator.next(); |
| 67 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 68 | assertThat(leafInfo.getName(), is("image-name")); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 69 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 70 | |
| 71 | YangList yangList = (YangList) yangInput.getChild(); |
| 72 | assertThat(yangList.getName(), is("ospf")); |
| 73 | assertThat(yangList.getKeyList().contains("invalid-interval"), is(true)); |
| 74 | assertThat(yangList.isConfig(), is(true)); |
| 75 | assertThat(yangList.getMaxElements(), is(10)); |
| 76 | assertThat(yangList.getMinElements(), is(3)); |
| 77 | leafIterator = yangList.getListOfLeaf().listIterator(); |
| 78 | leafInfo = leafIterator.next(); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 79 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 80 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
| 81 | |
| 82 | YangContainer yangContainer = (YangContainer) yangList.getNextSibling(); |
| 83 | assertThat(yangContainer.getName(), is("isis")); |
| 84 | |
| 85 | leafIterator = yangContainer.getListOfLeaf().listIterator(); |
| 86 | leafInfo = leafIterator.next(); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 87 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 88 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Checks input statements with type-def statement as sub-statements. |
| 93 | */ |
| 94 | @Test |
| 95 | public void processInputStatementWithTypedef() throws IOException, ParserException { |
| 96 | |
| 97 | YangNode node = manager.getDataModel("src/test/resources/InputStatementWithTypedef.yang"); |
| 98 | YangModule yangNode = (YangModule) node; |
| 99 | assertThat(yangNode.getName(), is("rock")); |
| 100 | |
| 101 | YangRpc yangRpc = (YangRpc) yangNode.getChild(); |
| 102 | assertThat(yangRpc.getName(), is("activate-software-image")); |
| 103 | |
| 104 | YangInput yangInput = (YangInput) yangRpc.getChild(); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 105 | assertThat(yangInput.getName(), is("activate-software-image_input")); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 106 | YangTypeDef typeDef = (YangTypeDef) yangInput.getChild(); |
| 107 | assertThat(typeDef.getName(), is("my-type")); |
| 108 | assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED)); |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 109 | assertThat(typeDef.getTypeDefBaseType().getDataType(), is(YangDataTypes.INT32)); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 110 | } |
| 111 | } |