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