blob: 957a90b80ff3ac87ec315e3d51d3878fa07164e7 [file] [log] [blame]
Vidyashree Rama49abe712016-02-13 22:22:12 +05301/*
2 * Copyright 2016 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 */
16
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import org.junit.Rule;
20import org.junit.Test;
21
22import org.junit.rules.ExpectedException;
23import org.onosproject.yangutils.datamodel.YangLeaf;
24import org.onosproject.yangutils.datamodel.YangModule;
25import org.onosproject.yangutils.datamodel.YangNode;
26import org.onosproject.yangutils.datamodel.YangNodeType;
27import org.onosproject.yangutils.datamodel.YangDataTypes;
28import org.onosproject.yangutils.datamodel.YangStatusType;
29import org.onosproject.yangutils.datamodel.YangContainer;
30import org.onosproject.yangutils.datamodel.YangList;
31import org.onosproject.yangutils.parser.exceptions.ParserException;
32import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
33
34import java.io.IOException;
35import java.util.ListIterator;
36
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.core.Is.is;
39
40/**
41 * Test cases for testing container listener.
42 */
43public class ContainerListenerTest {
44
45 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
48 private final YangUtilsParserManager manager = new YangUtilsParserManager();
49
50 /**
51 * Checks container statement as sub-statement of module.
52 */
53 @Test
54 public void processModuleSubStatementContainer() throws IOException, ParserException {
55
56 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementContainer.yang");
57
58 assertThat((node instanceof YangModule), is(true));
59
60 // Check whether the node type is set properly to module.
61 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
62
63 // Check whether the module name is set correctly.
64 YangModule yangNode = (YangModule) node;
65 assertThat(yangNode.getName(), is("Test"));
66
67 // Check whether the container is child of module
68 YangContainer yangContainer = (YangContainer) yangNode.getChild();
69 assertThat(yangContainer.getName(), is("valid"));
70 }
71
72 /**
73 * Checks container statement as sub-statement of container.
74 */
75 @Test
76 public void processContainerSubStatementContainer() throws IOException, ParserException {
77
78 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementContainer.yang");
79
80 assertThat((node instanceof YangModule), is(true));
81
82 // Check whether the node type is set properly to module.
83 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
84
85 // Check whether the module name is set correctly.
86 YangModule yangNode = (YangModule) node;
87 assertThat(yangNode.getName(), is("Test"));
88
89 // Check whether the container is child of module
90 YangContainer yangContainer = (YangContainer) yangNode.getChild();
91 assertThat(yangContainer.getName(), is("ospf"));
92
93 // Check whether the container is child of container
94 YangContainer yangContainer1 = (YangContainer) yangContainer.getChild();
95 assertThat(yangContainer1.getName(), is("valid"));
96 }
97
98 /**
99 * Checks container statement as sub-statement of list.
100 */
101 @Test
102 public void processListSubStatementContainer() throws IOException, ParserException {
103
104 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementContainer.yang");
105
106 assertThat((node instanceof YangModule), is(true));
107
108 // Check whether the node type is set properly to module.
109 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
110
111 // Check whether the module name is set correctly.
112 YangModule yangNode = (YangModule) node;
113 assertThat(yangNode.getName(), is("Test"));
114
115 // Check whether the list is child of module
116 YangList yangList1 = (YangList) yangNode.getChild();
117 assertThat(yangList1.getName(), is("ospf"));
118
119 ListIterator<String> keyList = yangList1.getKeyList().listIterator();
120 assertThat(keyList.next(), is("process-id"));
121
122 // Check whether the list is child of list
123 YangContainer yangContainer = (YangContainer) yangList1.getChild();
124 assertThat(yangContainer.getName(), is("interface"));
125 }
126
127 /**
128 * Checks container with all its sub-statements.
129 */
130 @Test
131 public void processContainerSubStatements() throws IOException, ParserException {
132
133 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatements.yang");
134
135 assertThat((node instanceof YangModule), is(true));
136
137 // Check whether the node type is set properly to module.
138 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
139
140 // Check whether the module name is set correctly.
141 YangModule yangNode = (YangModule) node;
142 assertThat(yangNode.getName(), is("Test"));
143
144 // Check whether the container is child of module
145 YangContainer yangContainer = (YangContainer) yangNode.getChild();
146
147 // Check whether container properties as set correctly.
148 assertThat(yangContainer.getName(), is("ospf"));
149
150 assertThat(yangContainer.isConfig(), is(true));
151 assertThat(yangContainer.getPresence(), is("\"ospf logs\""));
152 assertThat(yangContainer.getDescription(), is("\"container description\""));
153 assertThat(yangContainer.getStatus(), is(YangStatusType.CURRENT));
154 assertThat(yangContainer.getReference(), is("\"container reference\""));
155
156 // Check whether leaf properties as set correctly.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530157 ListIterator<YangLeaf<?>> leafIterator = yangContainer.getListOfLeaf().listIterator();
158 YangLeaf<?> leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530159
160 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
161 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
162 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
163 assertThat(leafInfo.getUnits(), is("\"seconds\""));
164 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
165 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
166 }
167
168 /**
169 * Checks cardinality of sub-statements of container.
170 */
171 @Test
172 public void processContainerSubStatementCardinality() throws IOException, ParserException {
173 thrown.expect(ParserException.class);
174 thrown.expectMessage("Internal parser error detected: Invalid cardinality in reference before processing.");
175 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementCardinality.yang");
176 }
177
178 /**
179 * Checks container as root node.
180 */
181 @Test
182 public void processContainerRootNode() throws IOException, ParserException {
183 thrown.expect(ParserException.class);
184 thrown.expectMessage("no viable alternative at input 'container'");
185 YangNode node = manager.getDataModel("src/test/resources/ContainerRootNode.yang");
186 }
187
188 /**
189 * Checks invalid identifier for container statement.
190 */
191 @Test
192 public void processContainerInvalidIdentifier() throws IOException, ParserException {
193 thrown.expect(ParserException.class);
194 thrown.expectMessage("mismatched input '1valid' expecting IDENTIFIER");
195 YangNode node = manager.getDataModel("src/test/resources/ContainerInvalidIdentifier.yang");
196 }
197}