blob: b6b497ae4a290c4b2c775b0f1b15560c0ff044e6 [file] [log] [blame]
Brian O'Connor7cbbbb72016-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 Rama49abe712016-02-13 22:22:12 +053016package org.onosproject.yangutils.parser.impl.listeners;
17
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053018import java.io.IOException;
19import java.util.ListIterator;
janani b6240d292016-05-17 18:20:33 +053020import org.junit.Rule;
Vidyashree Rama49abe712016-02-13 22:22:12 +053021import org.junit.Test;
janani b6240d292016-05-17 18:20:33 +053022import org.junit.rules.ExpectedException;
janani be18b5342016-07-13 21:06:41 +053023import org.onosproject.yangutils.datamodel.YangContainer;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053024import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vidyashree Rama49abe712016-02-13 22:22:12 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053026import org.onosproject.yangutils.datamodel.YangLeafList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053027import org.onosproject.yangutils.datamodel.YangModule;
28import org.onosproject.yangutils.datamodel.YangNode;
29import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053030import org.onosproject.yangutils.parser.exceptions.ParserException;
31import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
32
Vidyashree Rama49abe712016-02-13 22:22:12 +053033import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.core.Is.is;
35
36/**
37 * Test case for type listener.
38 */
39public class TypeListenerTest {
40
janani b6240d292016-05-17 18:20:33 +053041 @Rule
42 public ExpectedException thrown = ExpectedException.none();
43
Vidyashree Rama49abe712016-02-13 22:22:12 +053044 private final YangUtilsParserManager manager = new YangUtilsParserManager();
45
46 /**
47 * Checks derived statement without contraints.
48 */
49 @Test
50 public void processDerivedTypeStatement() throws IOException, ParserException {
51
52 YangNode node = manager.getDataModel("src/test/resources/DerivedTypeStatement.yang");
53
54 // Check whether the data model tree returned is of type module.
55 assertThat((node instanceof YangModule), is(true));
56
57 // Check whether the node type is set properly to module.
58 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
59
60 // Check whether the module name is set correctly.
61 YangModule yangNode = (YangModule) node;
62 assertThat(yangNode.getName(), is("Test"));
63
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053064 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
65 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053066
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053068 assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053069 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
70 }
71
72 /**
73 * Checks valid yang data type.
74 */
75 @Test
76 public void processIntegerTypeStatement() throws IOException, ParserException {
77
78 YangNode node = manager.getDataModel("src/test/resources/IntegerTypeStatement.yang");
79
80 // Check whether the data model tree returned is of type module.
81 assertThat((node instanceof YangModule), is(true));
82
83 // Check whether the node type is set properly to module.
84 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
85
86 // Check whether the module name is set correctly.
87 YangModule yangNode = (YangModule) node;
88 assertThat(yangNode.getName(), is("Test"));
89
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053090 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
91 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053092
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053093 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053094 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053095 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
96 }
97
98 /**
99 * Checks type for leaf-list.
100 */
101 @Test
102 public void processLeafListSubStatementType() throws IOException, ParserException {
103
104 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementType.yang");
105
106 // Check whether the data model tree returned is of type module.
107 assertThat((node instanceof YangModule), is(true));
108
109 // Check whether the node type is set properly to module.
110 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
111
112 // Check whether the module name is set correctly.
113 YangModule yangNode = (YangModule) node;
114 assertThat(yangNode.getName(), is("Test"));
115
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530116 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
117 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530118
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530119 assertThat(leafListInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530120 assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530121 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
122 }
janani b6240d292016-05-17 18:20:33 +0530123
124 /**
janani be18b5342016-07-13 21:06:41 +0530125 * Checks for type instance-identifier.
janani b6240d292016-05-17 18:20:33 +0530126 */
127 @Test
128 public void processInstanceIdentifierType() throws IOException, ParserException {
129
janani b6240d292016-05-17 18:20:33 +0530130 YangNode node = manager
janani be18b5342016-07-13 21:06:41 +0530131 .getDataModel("src/test/resources/InstanceIdentifierListener.yang");
132
133 // Check whether the data model tree returned is of type module.
134 assertThat((node instanceof YangModule), is(true));
135
136 // Check whether the node type is set properly to module.
137 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
138
139 // Check whether the module name is set correctly.
140 YangModule yangNode = (YangModule) node;
141 assertThat(yangNode.getName(), is("Test"));
142 YangContainer container = (YangContainer) yangNode.getChild();
143 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
144 YangLeaf leafInfo = leafIterator.next();
145
146 assertThat(leafInfo.getName(), is("invalid-interval"));
147 assertThat(leafInfo.getDataType().getDataTypeName(), is("instance-identifier"));
148 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INSTANCE_IDENTIFIER));
janani b6240d292016-05-17 18:20:33 +0530149 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530150}