blob: 60da95e1ea68d8cf09229279d47afb31ef1e3e59 [file] [log] [blame]
Brian O'Connor75218c22016-04-09 02:13:23 -07001/*
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 Rama50b73a02016-02-13 22:22:12 +053016package org.onosproject.yangutils.parser.impl.listeners;
17
Vinod Kumar Sc26bf192016-02-23 22:36:57 +053018import java.io.IOException;
19import java.util.ListIterator;
janani bb08850e2016-05-17 18:20:33 +053020import org.junit.Rule;
Vidyashree Rama50b73a02016-02-13 22:22:12 +053021import org.junit.Test;
janani bb08850e2016-05-17 18:20:33 +053022import org.junit.rules.ExpectedException;
janani b0e4e8ae2016-07-13 21:06:41 +053023import org.onosproject.yangutils.datamodel.YangContainer;
Vidyashree Ramab3670472016-08-06 15:49:56 +053024import org.onosproject.yangutils.datamodel.YangLeafRef;
25import org.onosproject.yangutils.datamodel.YangTypeDef;
Gaurav Agrawala8391d02016-06-30 13:28:14 +053026import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vidyashree Rama50b73a02016-02-13 22:22:12 +053027import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar Sc26bf192016-02-23 22:36:57 +053028import org.onosproject.yangutils.datamodel.YangLeafList;
Vidyashree Rama50b73a02016-02-13 22:22:12 +053029import org.onosproject.yangutils.datamodel.YangModule;
30import org.onosproject.yangutils.datamodel.YangNode;
31import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama50b73a02016-02-13 22:22:12 +053032import org.onosproject.yangutils.parser.exceptions.ParserException;
33import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34
Vidyashree Rama50b73a02016-02-13 22:22:12 +053035import static org.hamcrest.MatcherAssert.assertThat;
36import static org.hamcrest.core.Is.is;
37
38/**
39 * Test case for type listener.
40 */
41public class TypeListenerTest {
42
janani bb08850e2016-05-17 18:20:33 +053043 @Rule
44 public ExpectedException thrown = ExpectedException.none();
45
Vidyashree Rama50b73a02016-02-13 22:22:12 +053046 private final YangUtilsParserManager manager = new YangUtilsParserManager();
47
48 /**
49 * Checks derived statement without contraints.
50 */
51 @Test
52 public void processDerivedTypeStatement() throws IOException, ParserException {
53
54 YangNode node = manager.getDataModel("src/test/resources/DerivedTypeStatement.yang");
55
56 // Check whether the data model tree returned is of type module.
57 assertThat((node instanceof YangModule), is(true));
58
59 // Check whether the node type is set properly to module.
60 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
61
62 // Check whether the module name is set correctly.
63 YangModule yangNode = (YangModule) node;
64 assertThat(yangNode.getName(), is("Test"));
65
Vinod Kumar Sc26bf192016-02-23 22:36:57 +053066 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
67 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama50b73a02016-02-13 22:22:12 +053068
Vinod Kumar S79a374b2016-04-30 21:09:15 +053069 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +053070 assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
Vidyashree Rama50b73a02016-02-13 22:22:12 +053071 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
72 }
73
74 /**
75 * Checks valid yang data type.
76 */
77 @Test
78 public void processIntegerTypeStatement() throws IOException, ParserException {
79
80 YangNode node = manager.getDataModel("src/test/resources/IntegerTypeStatement.yang");
81
82 // Check whether the data model tree returned is of type module.
83 assertThat((node instanceof YangModule), is(true));
84
85 // Check whether the node type is set properly to module.
86 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
87
88 // Check whether the module name is set correctly.
89 YangModule yangNode = (YangModule) node;
90 assertThat(yangNode.getName(), is("Test"));
91
Vinod Kumar Sc26bf192016-02-23 22:36:57 +053092 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
93 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama50b73a02016-02-13 22:22:12 +053094
Vinod Kumar S79a374b2016-04-30 21:09:15 +053095 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +053096 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama50b73a02016-02-13 22:22:12 +053097 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
98 }
99
100 /**
101 * Checks type for leaf-list.
102 */
103 @Test
104 public void processLeafListSubStatementType() throws IOException, ParserException {
105
106 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementType.yang");
107
108 // Check whether the data model tree returned is of type module.
109 assertThat((node instanceof YangModule), is(true));
110
111 // Check whether the node type is set properly to module.
112 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
113
114 // Check whether the module name is set correctly.
115 YangModule yangNode = (YangModule) node;
116 assertThat(yangNode.getName(), is("Test"));
117
Vinod Kumar Sc26bf192016-02-23 22:36:57 +0530118 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
119 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama50b73a02016-02-13 22:22:12 +0530120
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530121 assertThat(leafListInfo.getName(), is("invalid-interval"));
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +0530122 assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama50b73a02016-02-13 22:22:12 +0530123 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
124 }
janani bb08850e2016-05-17 18:20:33 +0530125
126 /**
janani b0e4e8ae2016-07-13 21:06:41 +0530127 * Checks for type instance-identifier.
janani bb08850e2016-05-17 18:20:33 +0530128 */
129 @Test
130 public void processInstanceIdentifierType() throws IOException, ParserException {
131
janani bb08850e2016-05-17 18:20:33 +0530132 YangNode node = manager
janani b0e4e8ae2016-07-13 21:06:41 +0530133 .getDataModel("src/test/resources/InstanceIdentifierListener.yang");
134
135 // Check whether the data model tree returned is of type module.
136 assertThat((node instanceof YangModule), is(true));
137
138 // Check whether the node type is set properly to module.
139 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
140
141 // Check whether the module name is set correctly.
142 YangModule yangNode = (YangModule) node;
143 assertThat(yangNode.getName(), is("Test"));
144 YangContainer container = (YangContainer) yangNode.getChild();
145 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
146 YangLeaf leafInfo = leafIterator.next();
147
148 assertThat(leafInfo.getName(), is("invalid-interval"));
149 assertThat(leafInfo.getDataType().getDataTypeName(), is("instance-identifier"));
150 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INSTANCE_IDENTIFIER));
janani bb08850e2016-05-17 18:20:33 +0530151 }
Vidyashree Ramab3670472016-08-06 15:49:56 +0530152
153 /**
154 * Checks for leaf ref path concatenation.
155 */
156 @Test
157 public void processLeafRefPathConcatenation() throws IOException, ParserException {
158
159 YangNode node = manager
160 .getDataModel("src/test/resources/leafRefPathConcatenation.yang");
161
162 assertThat((node instanceof YangModule), is(true));
163 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
164 YangModule yangNode = (YangModule) node;
165 assertThat(yangNode.getName(), is("test"));
166
167 YangTypeDef typeDef = (YangTypeDef) yangNode.getChild();
168 assertThat(typeDef.getName(), is("isis-instance-state-ref"));
169 assertThat(typeDef.getTypeDefBaseType().getDataType(), is(YangDataTypes.LEAFREF));
170 YangLeafRef leafRef = ((YangLeafRef) typeDef.getTypeDefBaseType().getDataTypeExtendedInfo());
171 assertThat(leafRef.getPath(), is("/isis-prefix-ipv4-std/default-metric"));
172 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530173}