Brian O'Connor | 75218c2 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 16 | package org.onosproject.yangutils.parser.impl.listeners; |
| 17 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | import java.util.ListIterator; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 20 | import org.junit.Test; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangLeaf; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangModule; |
| 25 | import org.onosproject.yangutils.datamodel.YangNode; |
| 26 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 28 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 29 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.core.Is.is; |
| 32 | |
| 33 | /** |
| 34 | * Test case for type listener. |
| 35 | */ |
| 36 | public class TypeListenerTest { |
| 37 | |
| 38 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 39 | |
| 40 | /** |
| 41 | * Checks derived statement without contraints. |
| 42 | */ |
| 43 | @Test |
| 44 | public void processDerivedTypeStatement() throws IOException, ParserException { |
| 45 | |
| 46 | YangNode node = manager.getDataModel("src/test/resources/DerivedTypeStatement.yang"); |
| 47 | |
| 48 | // Check whether the data model tree returned is of type module. |
| 49 | assertThat((node instanceof YangModule), is(true)); |
| 50 | |
| 51 | // Check whether the node type is set properly to module. |
| 52 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 53 | |
| 54 | // Check whether the module name is set correctly. |
| 55 | YangModule yangNode = (YangModule) node; |
| 56 | assertThat(yangNode.getName(), is("Test")); |
| 57 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 58 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 59 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 60 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 61 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 62 | assertThat(leafInfo.getDataType().getDataTypeName(), is("hello")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 63 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED)); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Checks valid yang data type. |
| 68 | */ |
| 69 | @Test |
| 70 | public void processIntegerTypeStatement() throws IOException, ParserException { |
| 71 | |
| 72 | YangNode node = manager.getDataModel("src/test/resources/IntegerTypeStatement.yang"); |
| 73 | |
| 74 | // Check whether the data model tree returned is of type module. |
| 75 | assertThat((node instanceof YangModule), is(true)); |
| 76 | |
| 77 | // Check whether the node type is set properly to module. |
| 78 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 79 | |
| 80 | // Check whether the module name is set correctly. |
| 81 | YangModule yangNode = (YangModule) node; |
| 82 | assertThat(yangNode.getName(), is("Test")); |
| 83 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 84 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 85 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 86 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 87 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 88 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 89 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Checks type for leaf-list. |
| 94 | */ |
| 95 | @Test |
| 96 | public void processLeafListSubStatementType() throws IOException, ParserException { |
| 97 | |
| 98 | YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementType.yang"); |
| 99 | |
| 100 | // Check whether the data model tree returned is of type module. |
| 101 | assertThat((node instanceof YangModule), is(true)); |
| 102 | |
| 103 | // Check whether the node type is set properly to module. |
| 104 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 105 | |
| 106 | // Check whether the module name is set correctly. |
| 107 | YangModule yangNode = (YangModule) node; |
| 108 | assertThat(yangNode.getName(), is("Test")); |
| 109 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 110 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 111 | YangLeafList leafListInfo = leafListIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 112 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 113 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 114 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 115 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 116 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 117 | } |