blob: d263bb0e4f539516395b7e76709ae8f339ee8442 [file] [log] [blame]
Vidyashree Rama50b73a02016-02-13 22:22:12 +05301package org.onosproject.yangutils.parser.impl.listeners;
2
Vinod Kumar Sc26bf192016-02-23 22:36:57 +05303import java.io.IOException;
4import java.util.ListIterator;
Vidyashree Rama50b73a02016-02-13 22:22:12 +05305import org.junit.Test;
Vinod Kumar Sc26bf192016-02-23 22:36:57 +05306import org.onosproject.yangutils.datamodel.YangDataTypes;
Vidyashree Rama50b73a02016-02-13 22:22:12 +05307import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar Sc26bf192016-02-23 22:36:57 +05308import org.onosproject.yangutils.datamodel.YangLeafList;
Vidyashree Rama50b73a02016-02-13 22:22:12 +05309import org.onosproject.yangutils.datamodel.YangModule;
10import org.onosproject.yangutils.datamodel.YangNode;
11import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama50b73a02016-02-13 22:22:12 +053012import org.onosproject.yangutils.parser.exceptions.ParserException;
13import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
14
Vidyashree Rama50b73a02016-02-13 22:22:12 +053015import static org.hamcrest.MatcherAssert.assertThat;
16import static org.hamcrest.core.Is.is;
17
18/**
19 * Test case for type listener.
20 */
21public 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 Sc26bf192016-02-23 22:36:57 +053043 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
44 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama50b73a02016-02-13 22:22:12 +053045
46 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +053047 assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
Vidyashree Rama50b73a02016-02-13 22:22:12 +053048 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 Sc26bf192016-02-23 22:36:57 +053069 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
70 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama50b73a02016-02-13 22:22:12 +053071
72 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +053073 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama50b73a02016-02-13 22:22:12 +053074 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 Sc26bf192016-02-23 22:36:57 +053095 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
96 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama50b73a02016-02-13 22:22:12 +053097
98 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +053099 assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama50b73a02016-02-13 22:22:12 +0530100 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
101 }
102}