2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.restconf.utils.schema.context;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.restconf.Draft11.MonitoringModule;
15 import static org.opendaylight.restconf.Draft11.RestconfModule;
17 import com.google.common.collect.Sets;
18 import java.util.NoSuchElementException;
19 import org.junit.Before;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.ExpectedException;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
26 import org.opendaylight.restconf.Draft11;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
33 * Unit tests for {@link RestconfSchemaUtil}
35 public class RestconfSchemaUtilTest {
36 // schema with testing modules
37 private SchemaContext schemaContext;
40 public ExpectedException thrown = ExpectedException.none();
43 public void setup() throws Exception {
44 schemaContext = TestRestconfUtils.loadSchemaContext("/modules/restconf-module-testing");
48 * Positive test for getting <code>DataSchemaNode</code> from Restconf module for schema node name equals to
49 * {@link RestconfModule#MODULE_LIST_SCHEMA_NODE} when this node can be found.
52 public void getRestconfSchemaNodeListModuleTest() {
53 final DataSchemaNode dataSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
54 getTestingRestconfModule("ietf-restconf"),
55 RestconfModule.MODULE_LIST_SCHEMA_NODE);
57 assertNotNull("Existing schema node "+ RestconfModule.MODULE_LIST_SCHEMA_NODE + " should be found",
59 assertEquals("Incorrect schema node was returned",
60 dataSchemaNode.getQName().getLocalName(), RestconfModule.MODULE_LIST_SCHEMA_NODE);
61 assertEquals("Incorrect schema node was returned",
62 dataSchemaNode.getQName().getNamespace().toString(), RestconfModule.NAMESPACE);
63 assertEquals("Incorrect schema node was returned",
64 dataSchemaNode.getQName().getFormattedRevision(), RestconfModule.REVISION);
68 * Positive test for getting <code>DataSchemaNode</code> from Restconf module for schema node name equals to
69 * {@link MonitoringModule#STREAM_LIST_SCHEMA_NODE} when this node can be found.
72 public void getRestconfSchemaNodeListStreamTest() {
73 final DataSchemaNode dataSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
74 getTestingRestconfModule("ietf-restconf"),
75 MonitoringModule.STREAM_LIST_SCHEMA_NODE);
77 assertNotNull("Existing schema node " + MonitoringModule.STREAM_LIST_SCHEMA_NODE + " should be found",
79 assertEquals("Incorrect schema node was returned",
80 dataSchemaNode.getQName().getLocalName(), MonitoringModule.STREAM_LIST_SCHEMA_NODE);
81 assertEquals("Incorrect schema node was returned",
82 dataSchemaNode.getQName().getNamespace().toString(), RestconfModule.NAMESPACE);
83 assertEquals("Incorrect schema node was returned",
84 dataSchemaNode.getQName().getFormattedRevision(), RestconfModule.REVISION);
88 * Positive test for getting <code>DataSchemaNode</code> from Restconf module for schema node name equals to
89 * {@link RestconfModule#MODULES_CONTAINER_SCHEMA_NODE} when this node can be found.
92 public void getRestconfSchemaNodeContainerModulesTest() {
93 final DataSchemaNode dataSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
94 getTestingRestconfModule("ietf-restconf"),
95 RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
97 assertNotNull("Existing schema node " + RestconfModule.MODULES_CONTAINER_SCHEMA_NODE + "should be found",
99 assertEquals("Incorrect schema node was returned",
100 dataSchemaNode.getQName().getLocalName(), RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
101 assertEquals("Incorrect schema node was returned",
102 dataSchemaNode.getQName().getNamespace().toString(), RestconfModule.NAMESPACE);
103 assertEquals("Incorrect schema node was returned",
104 dataSchemaNode.getQName().getFormattedRevision(), RestconfModule.REVISION);
108 * Positive test for getting <code>DataSchemaNode</code> from Restconf module for schema node name equals to
109 * {@link MonitoringModule#STREAMS_CONTAINER_SCHEMA_NODE} when this node can be found.
112 public void getRestconfSchemaNodeContainerStreamsTest() {
113 final DataSchemaNode dataSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
114 getTestingRestconfModule("ietf-restconf"),
115 MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE);
117 assertNotNull("Existing schema node " + MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE + " should be found",
119 assertEquals("Incorrect schema node was returned",
120 dataSchemaNode.getQName().getLocalName(), MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE);
121 assertEquals("Incorrect schema node was returned",
122 dataSchemaNode.getQName().getNamespace().toString(), RestconfModule.NAMESPACE);
123 assertEquals("Incorrect schema node was returned",
124 dataSchemaNode.getQName().getFormattedRevision(), RestconfModule.REVISION);
128 * Negative test for getting <code>DataSchemaNode</code> from Restconf module when Restconf module is
129 * <code>null</code>. Test is expected to fail catching <code>NullPointerException</code>.
132 public void getRestconfSchemaNodeNullRestconfModuleNegativeTest() {
133 thrown.expect(NullPointerException.class);
134 RestconfSchemaUtil.getRestconfSchemaNode(null, RestconfModule.RESTCONF_CONTAINER_SCHEMA_NODE);
138 * Negative test for getting <code>DataSchemaNode</code> from Restconf module when name of the schema node name is
139 * <code>null</code>. Test is expected to fail with <code>NullPointerException</code>.
142 public void getRestconfSchemaNodeNullSchemaNodeNameNegativeTest() {
143 thrown.expect(NullPointerException.class);
144 RestconfSchemaUtil.getRestconfSchemaNode(getTestingRestconfModule("ietf-restconf"), null);
148 * Negative test for getting <code>DataSchemaNode</code> from Restconf module when name of the schema node name
149 * references to not existing node. Test is expected to fail catching code>RestconfDocumentedException</code> and
150 * checking expected error type, error tag and error status code.
153 public void getRestconfSchemaNodeNotExistingSchemaNodeNameNegativeTest() {
155 RestconfSchemaUtil.getRestconfSchemaNode(getTestingRestconfModule("ietf-restconf"), "not-existing-node");
156 fail("Test should fail due to not-existing node name");
157 } catch (final RestconfDocumentedException e) {
158 assertEquals("Error type is not correct",
159 RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
160 assertEquals("Error tag is not correct",
161 RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
162 assertEquals("Error status code is not correct",
163 404, e.getErrors().get(0).getErrorTag().getStatusCode());
168 * Negative test for getting <code>DataSchemaNode</code> from Restconf module for schema node name equals to
169 * {@link RestconfModule#MODULES_CONTAINER_SCHEMA_NODE} when this node cannot be found.
170 * <code>RestconfDocumentedException</code> is expected and error type, error tag and error status code are
171 * compared to expected values.
174 public void getRestconfSchemaNodeContainerModulesNegativeTest() {
176 RestconfSchemaUtil.getRestconfSchemaNode(getTestingRestconfModule(
177 "restconf-module-with-missing-container-modules"), RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
178 fail("Test should fail due to missing " + RestconfModule.MODULES_CONTAINER_SCHEMA_NODE + " node");
179 } catch (final RestconfDocumentedException e) {
180 assertEquals("Error type is not correct",
181 RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
182 assertEquals("Error tag is not correct",
183 RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
184 assertEquals("Error status code is not correct",
185 404, e.getErrors().get(0).getErrorTag().getStatusCode());
190 * Negative test for getting <code>DataSchemaNode</code> from Restconf module for schema node name equals to
191 * {@link RestconfModule#MODULE_LIST_SCHEMA_NODE} when this node cannot be found.
192 * <code>RestconfDocumentedException</code> is expected and error type, error tag and error status code are
193 * compared to expected values.
196 public void getRestconfSchemaNodeListModuleNegativeTest() {
198 RestconfSchemaUtil.getRestconfSchemaNode(
199 getTestingRestconfModule("restconf-module-with-missing-list-module"),
200 RestconfModule.MODULE_LIST_SCHEMA_NODE);
201 fail("Test should fail due to missing " + RestconfModule.MODULE_LIST_SCHEMA_NODE + " node");
202 } catch (final RestconfDocumentedException e) {
203 assertEquals("Error type is not correct",
204 RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
205 assertEquals("Error tag is not correct",
206 RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
207 assertEquals("Error status code is not correct",
208 404, e.getErrors().get(0).getErrorTag().getStatusCode());
213 * Negative test for getting <code>DataSchemaNode</code> from Restconf module for schema node name equals to
214 * {@link MonitoringModule#STREAMS_CONTAINER_SCHEMA_NODE} when this node cannot
215 * be found. <code>RestconfDocumentedException</code> is expected and error type, error tag and error status code
216 * are compared to expected values.
219 public void getRestconfSchemaNodeContainerStreamsNegativeTest() {
221 RestconfSchemaUtil.getRestconfSchemaNode(
222 getTestingRestconfModule("restconf-module-with-missing-container-streams"),
223 MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE);
224 fail("Test should fail due to missing " + MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE + " node");
225 } catch (final RestconfDocumentedException e) {
226 assertEquals("Error type is not correct",
227 RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
228 assertEquals("Error tag is not correct",
229 RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
230 assertEquals("Error status code is not correct",
231 404, e.getErrors().get(0).getErrorTag().getStatusCode());
236 * Negative test for getting <code>DataSchemaNode</code> from Restconf module for schema node name equals to
237 * {@link MonitoringModule#STREAM_LIST_SCHEMA_NODE} when this node cannot be found.
238 * <code>RestconfDocumentedException</code> is expected and error type, error tag and error status code
239 * are compared to expected values.
242 public void getRestconfSchemaNodeListStreamNegativeTest() {
244 RestconfSchemaUtil.getRestconfSchemaNode(
245 getTestingRestconfModule("restconf-module-with-missing-list-stream"),
246 MonitoringModule.STREAM_LIST_SCHEMA_NODE);
247 fail("Test should fail due to missing " + MonitoringModule.STREAM_LIST_SCHEMA_NODE + " node");
248 } catch (final RestconfDocumentedException e) {
249 assertEquals("Error type is not correct",
250 RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
251 assertEquals("Error tag is not correct",
252 RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
253 assertEquals("Error status code is not correct",
254 404, e.getErrors().get(0).getErrorTag().getStatusCode());
259 * Negative test for getting <code>DataSchemaNode</code> from Restconf module when Restconf module does not
260 * contains restconf grouping. Test is expected to fail with <code>RestconfDocumentedException</code> and error
261 * type, error tag and error status code are compared to expected values.
264 public void getRestconfSchemaNodeMissingRestconfGroupingNegativeTest() {
266 RestconfSchemaUtil.getRestconfSchemaNode(
267 getTestingRestconfModule("restconf-module-with-missing-grouping-restconf"),
268 RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
269 fail("Test should fail due to missing restconf grouping in Restconf module");
270 } catch (final RestconfDocumentedException e) {
271 assertEquals("Error type is not correct",
272 RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
273 assertEquals("Error tag is not correct",
274 RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
275 assertEquals("Error status code is not correct",
276 404, e.getErrors().get(0).getErrorTag().getStatusCode());
281 * Negative test for getting <code>DataSchemaNode</code> from Restconf module when Restconf module contains
282 * restconf grouping which does not contain any child nodes. Test is expected to fail with
283 * <code>NoSuchElementException</code>.
286 public void getRestconfSchemaNodeEmptyRestconfGroupingNegativeTest() {
287 thrown.expect(NoSuchElementException.class);
288 RestconfSchemaUtil.getRestconfSchemaNode(
289 getTestingRestconfModule("restconf-module-with-empty-grouping-restconf"),
290 RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
294 * Positive test trying to find <code>DataSchemaNode</code> of {@link RestconfModule#RESTCONF_GROUPING_SCHEMA_NODE}
295 * in Restconf module groupings collection.
298 public void findSchemaNodeInCollectionTest() {
299 final SchemaNode schemaNode = RestconfSchemaUtil.findSchemaNodeInCollection(
300 getTestingRestconfModule("ietf-restconf").getGroupings(),
301 RestconfModule.RESTCONF_GROUPING_SCHEMA_NODE);
303 assertNotNull("Restconf grouping schema node should be found", schemaNode);
304 assertEquals("Incorrect grouping was returned",
305 schemaNode.getQName().getLocalName(), RestconfModule.RESTCONF_GROUPING_SCHEMA_NODE);
306 assertEquals("Incorrect grouping was returned",
307 schemaNode.getQName().getNamespace().toString(), RestconfModule.NAMESPACE);
308 assertEquals("Incorrect grouping was returned",
309 schemaNode.getQName().getFormattedRevision(), RestconfModule.REVISION);
313 * Negative test trying to find <code>DataSchemaNode</code> of not existing groupings schema node name in Restconf
314 * module grouping collection. Test is expected to fail catching <code>RestconfDocumentedException</code> and
315 * checking for correct error type, error tag and error status code.
318 public void findSchemaNodeInCollectionNegativeTest() {
320 RestconfSchemaUtil.findSchemaNodeInCollection(
321 getTestingRestconfModule("ietf-restconf").getGroupings(), "not-existing-grouping");
322 fail("Test should fail due to missing not-existing grouping in Restconf grouping collection");
323 } catch (final RestconfDocumentedException e) {
324 assertEquals("Error type is not correct",
325 RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
326 assertEquals("Error tag is not correct",
327 RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
328 assertEquals("Error status code is not correct",
329 404, e.getErrors().get(0).getErrorTag().getStatusCode());
334 * Negative test trying to find <code>DataSchemaNode</code> of existing schema node name in <code>null</code>
335 * collection. Test is expected to fail with <code>NullPointerException</code>.
338 public void findSchemaNodeInCollectionNullCollectionNegativeTest() {
339 thrown.expect(NullPointerException.class);
340 RestconfSchemaUtil.findSchemaNodeInCollection(null, RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
344 * Negative test trying to find <code>DataSchemaNode</code> for schema node name in empty collection. Test is
345 * expected to fail with <code>RestconfDocumentedException</code>. Error type, error tag and error status code
346 * are compared to expected values.
349 public void findSchemaNodeInCollectionEmptyCollectionNegativeTest() {
351 RestconfSchemaUtil.findSchemaNodeInCollection(
352 Sets.newHashSet(), RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
353 fail("Test should fail due to empty schema nodes collection");
354 } catch (final RestconfDocumentedException e) {
355 assertEquals("Error type is not correct",
356 RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
357 assertEquals("Error tag is not correct",
358 RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
359 assertEquals("Error status code is not correct",
360 404, e.getErrors().get(0).getErrorTag().getStatusCode());
365 * Negative test trying to find <code>DataSchemaNode</code> of <code>null</code> schema node name in Restconf module
366 * groupings collection. Test is expected to fail with <code>RestconfDocumentedException</code>. Error type, error
367 * tag and error status code are compared to expected values.
370 public void findSchemaNodeInCollectionNullSchemaNodeName() {
372 RestconfSchemaUtil.findSchemaNodeInCollection(
373 getTestingRestconfModule("ietf-restconf").getGroupings(), null);
374 fail("Test should fail due to null schema node name");
375 } catch (final RestconfDocumentedException e) {
376 assertEquals("Error type is not correct",
377 RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
378 assertEquals("Error tag is not correct",
379 RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
380 assertEquals("Error status code is not correct",
381 404, e.getErrors().get(0).getErrorTag().getStatusCode());
386 * There are multiple testing Restconf modules for different test cases. It is possible to distinguish them by
387 * name or by namespace. This method is looking for Restconf test module by its name.
388 * @param s Testing Restconf module name
389 * @return Restconf module
391 private Module getTestingRestconfModule(final String s) {
392 return schemaContext.findModuleByName(s, Draft11.RestconfModule.IETF_RESTCONF_QNAME.getRevision());