blob: 4b2b18511e9851f902b67e90eee67e0a2b3076c7 [file] [log] [blame]
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +05303 *
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 */
16
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import static org.hamcrest.MatcherAssert.assertThat;
20import static org.hamcrest.core.Is.is;
21import org.junit.Test;
22import org.onosproject.yangutils.datamodel.YangBit;
23import org.onosproject.yangutils.datamodel.YangBits;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053024import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangModule;
27import org.onosproject.yangutils.datamodel.YangNode;
28import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053029import org.onosproject.yangutils.datamodel.YangType;
30import org.onosproject.yangutils.datamodel.YangTypeDef;
31import org.onosproject.yangutils.datamodel.YangUnion;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053032import org.onosproject.yangutils.parser.exceptions.ParserException;
33import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34
35import java.io.IOException;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053036import java.util.List;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053037import java.util.ListIterator;
38import java.util.Set;
39
40/**
41 * Test cases for bit listener.
42 */
43public class BitListenerTest {
44
45 private final YangUtilsParserManager manager = new YangUtilsParserManager();
46
47 /**
48 * Checks bit statement without position.
49 */
50 @Test
51 public void processBitTypeStatement() throws IOException, ParserException {
52
53 YangNode node = manager.getDataModel("src/test/resources/BitTypeStatement.yang");
54
55 // Check whether the data model tree returned is of type module.
56 assertThat((node instanceof YangModule), is(true));
57
58 // Check whether the node type is set properly to module.
59 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
60
61 // Check whether the module name is set correctly.
62 YangModule yangNode = (YangModule) node;
63 assertThat(yangNode.getName(), is("Test"));
64
65 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
66 YangLeaf leafInfo = leafIterator.next();
67
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053068 assertThat(leafInfo.getName(), is("mybits"));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053069 assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
70 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
71 assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
72 is("mybits"));
73
74 Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
75 for (YangBit tmp : bitSet) {
76 if (tmp.getBitName().equals("disable-nagle")) {
77 assertThat(tmp.getPosition(), is(0));
78 } else if (tmp.getBitName().equals("auto-sense-speed")) {
79 assertThat(tmp.getPosition(), is(1));
80 } else if (tmp.getBitName().equals("Ten-Mb-only")) {
81 assertThat(tmp.getPosition(), is(2));
82 }
83 }
84 }
85
86 /**
Vidyashree Rama210c01d2016-05-20 16:29:25 +053087 * Checks bit statement with typedef.
88 */
89 @Test
90 public void processBitTypedefStatement() throws IOException, ParserException {
91
92 YangNode node = manager.getDataModel("src/test/resources/BitTypedefStatement.yang");
93
94 // Check whether the data model tree returned is of type module.
95 assertThat((node instanceof YangModule), is(true));
96
97 // Check whether the node type is set properly to module.
98 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
99
100 // Check whether the module name is set correctly.
101 YangModule yangNode = (YangModule) node;
102 assertThat(yangNode.getName(), is("Test"));
103
104 YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
105 assertThat(typedef.getName(), is("type15"));
106
107 YangType type = typedef.getTypeList().iterator().next();
108 assertThat(type.getDataType(), is(YangDataTypes.BITS));
109 assertThat(type.getDataTypeName(), is("bits"));
110 Set<YangBit> bitSet = ((YangBits) type.getDataTypeExtendedInfo()).getBitSet();
111 for (YangBit tmp : bitSet) {
112 if (tmp.getBitName().equals("disable-nagle")) {
113 assertThat(tmp.getPosition(), is(0));
114 } else if (tmp.getBitName().equals("auto-sense-speed")) {
115 assertThat(tmp.getPosition(), is(1));
116 }
117 }
118 }
119
120 /**
121 * Checks bit statement with union.
122 */
123 @Test
124 public void processBitUnionStatement() throws IOException, ParserException {
125
126 YangNode node = manager.getDataModel("src/test/resources/BitUnionStatement.yang");
127
128 // Check whether the data model tree returned is of type module.
129 assertThat((node instanceof YangModule), is(true));
130
131 // Check whether the node type is set properly to module.
132 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
133
134 // Check whether the module name is set correctly.
135 YangModule yangNode = (YangModule) node;
136 assertThat(yangNode.getName(), is("Test"));
137
138 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
139 YangLeaf leafInfo = leafIterator.next();
140
141 assertThat(leafInfo.getName(), is("type15"));
142
143 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION));
144 assertThat(leafInfo.getDataType().getDataTypeName(), is("union"));
145
146 YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo();
147
148 List<YangType<?>> typeList = yangUnion.getTypeList();
149 ListIterator<YangType<?>> typeListIterator = typeList.listIterator();
150 YangType<?> yangType = typeListIterator.next();
151
152 assertThat(yangType.getDataType(), is(YangDataTypes.BITS));
153 assertThat(yangType.getDataTypeName(), is("bits"));
154 Set<YangBit> bitSet = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitSet();
155 for (YangBit tmp : bitSet) {
156 if (tmp.getBitName().equals("disable-nagle")) {
157 assertThat(tmp.getPosition(), is(0));
158 } else if (tmp.getBitName().equals("auto-sense-speed")) {
159 assertThat(tmp.getPosition(), is(1));
160 }
161 }
162 }
163
164 /**
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530165 * Checks if enum with same name is not allowed.
166 */
167 @Test(expected = ParserException.class)
168 public void processBitWithDuplicateName() throws IOException, ParserException {
169
170 YangNode node = manager.getDataModel("src/test/resources/BitWithDuplicateName.yang");
171 }
172}