blob: 7d252ba9b06c8135bd9f018d8c95cae0b0df9840 [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;
24import org.onosproject.yangutils.datamodel.YangDataTypes;
25import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangModule;
27import org.onosproject.yangutils.datamodel.YangNode;
28import org.onosproject.yangutils.datamodel.YangNodeType;
29import org.onosproject.yangutils.parser.exceptions.ParserException;
30import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
31
32import java.io.IOException;
33import java.util.ListIterator;
34import java.util.Set;
35
36/**
37 * Test cases for position listener.
38 */
39public class PositionListenerTest {
40
41 private final YangUtilsParserManager manager = new YangUtilsParserManager();
42
43 /**
44 * Checks explicitly configured value.
45 */
46 @Test
47 public void processPositionStatement() throws IOException, ParserException {
48
49 YangNode node = manager.getDataModel("src/test/resources/PositionStatement.yang");
50
51 // Check whether the data model tree returned is of type module.
52 assertThat((node instanceof YangModule), is(true));
53
54 // Check whether the node type is set properly to module.
55 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
56
57 // Check whether the module name is set correctly.
58 YangModule yangNode = (YangModule) node;
59 assertThat(yangNode.getName(), is("Test"));
60
61 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
62 YangLeaf leafInfo = leafIterator.next();
63
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053064 assertThat(leafInfo.getName(), is("mybits"));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053065 assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
66 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
67 assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
68 is("mybits"));
69
70 Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
71 for (YangBit tmp : bitSet) {
72 if (tmp.getBitName().equals("disable-nagle")) {
73 assertThat(tmp.getPosition(), is(0));
74 } else if (tmp.getBitName().equals("auto-sense-speed")) {
75 assertThat(tmp.getPosition(), is(1));
76 } else if (tmp.getBitName().equals("Ten-Mb-only")) {
77 assertThat(tmp.getPosition(), is(2));
78 }
79 }
80 }
81
82 /**
Vidyashree Rama468f8282016-03-04 19:08:35 +053083 * Checks position value with double quotes.
84 */
85 @Test
86 public void processPositionWithDoubleQuotes() throws IOException, ParserException {
87
88 YangNode node = manager.getDataModel("src/test/resources/PositionWithDoubleQuotes.yang");
89
90 // Check whether the data model tree returned is of type module.
91 assertThat((node instanceof YangModule), is(true));
92
93 // Check whether the node type is set properly to module.
94 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
95
96 // Check whether the module name is set correctly.
97 YangModule yangNode = (YangModule) node;
98 assertThat(yangNode.getName(), is("Test"));
99
100 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
101 YangLeaf leafInfo = leafIterator.next();
102
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530103 assertThat(leafInfo.getName(), is("mybits"));
Vidyashree Rama468f8282016-03-04 19:08:35 +0530104 assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
105 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
106 assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
107 is("mybits"));
108
109 Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
110 for (YangBit tmp : bitSet) {
111 if (tmp.getBitName().equals("disable-nagle")) {
112 assertThat(tmp.getPosition(), is(0));
113 } else if (tmp.getBitName().equals("auto-sense-speed")) {
114 assertThat(tmp.getPosition(), is(1));
115 } else if (tmp.getBitName().equals("Ten-Mb-only")) {
116 assertThat(tmp.getPosition(), is(2));
117 }
118 }
119 }
120
121 /**
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530122 * Checks explicit value and auto generated value.
123 */
124 @Test
125 public void processPositionImplicitAndExplicit() throws IOException, ParserException {
126
127 YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicit.yang");
128
129 // Check whether the data model tree returned is of type module.
130 assertThat((node instanceof YangModule), is(true));
131
132 // Check whether the node type is set properly to module.
133 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
134
135 // Check whether the module name is set correctly.
136 YangModule yangNode = (YangModule) node;
137 assertThat(yangNode.getName(), is("Test"));
138
139 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
140 YangLeaf leafInfo = leafIterator.next();
141
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 assertThat(leafInfo.getName(), is("mybits"));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530143 assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
144 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
145 assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
146 is("mybits"));
147
148 Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
149 for (YangBit tmp : bitSet) {
150 if (tmp.getBitName().equals("disable-nagle")) {
151 assertThat(tmp.getPosition(), is(0));
152 } else if (tmp.getBitName().equals("auto-sense-speed")) {
153 assertThat(tmp.getPosition(), is(1));
154 } else if (tmp.getBitName().equals("Ten-Mb-only")) {
155 assertThat(tmp.getPosition(), is(2));
156 }
157 }
158 }
159
160 /**
161 * Checks explicit value should not be repeated.
162 */
163 @Test(expected = ParserException.class)
164 public void processPositionDuplication() throws IOException, ParserException {
165
166 YangNode node = manager.getDataModel("src/test/resources/PositionDuplication.yang");
167 }
168
169 /**
170 * Checks explicit or auto generated value should not be repeated.
171 */
172 @Test(expected = ParserException.class)
173 public void processPositionImplicitAndExplicitDuplication() throws IOException, ParserException {
174
175 YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicitDuplication.yang");
176 }
177
178 /**
179 * Checks if negative value of position is not allowed.
180 */
181 @Test(expected = ParserException.class)
182 public void processPositionNegativeValue() throws IOException, ParserException {
183
184 YangNode node = manager.getDataModel("src/test/resources/PositionNegativeValue.yang");
185 }
186}