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