Bug 5526 - Testing - Utils - Schema util
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / utils / schema / context / RestconfSchemaUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.restconf.utils.schema.context;
10
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;
16
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;
31
32 /**
33  * Unit tests for {@link RestconfSchemaUtil}
34  */
35 public class RestconfSchemaUtilTest {
36     // schema with testing modules
37     private SchemaContext schemaContext;
38
39     @Rule
40     public ExpectedException thrown = ExpectedException.none();
41
42     @Before
43     public void setup() throws Exception {
44         schemaContext = TestRestconfUtils.loadSchemaContext("/modules/restconf-module-testing");
45     }
46
47     /**
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.
50      */
51     @Test
52     public void getRestconfSchemaNodeListModuleTest() {
53         final DataSchemaNode dataSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
54                 getTestingRestconfModule("ietf-restconf"),
55                 RestconfModule.MODULE_LIST_SCHEMA_NODE);
56
57         assertNotNull("Existing schema node "+ RestconfModule.MODULE_LIST_SCHEMA_NODE + " should be found",
58                 dataSchemaNode);
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);
65     }
66
67     /**
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.
70      */
71     @Test
72     public void getRestconfSchemaNodeListStreamTest() {
73         final DataSchemaNode dataSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
74                 getTestingRestconfModule("ietf-restconf"),
75                 MonitoringModule.STREAM_LIST_SCHEMA_NODE);
76
77         assertNotNull("Existing schema node " + MonitoringModule.STREAM_LIST_SCHEMA_NODE + " should be found",
78                 dataSchemaNode);
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);
85     }
86
87     /**
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.
90      */
91     @Test
92     public void getRestconfSchemaNodeContainerModulesTest() {
93         final DataSchemaNode dataSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
94                 getTestingRestconfModule("ietf-restconf"),
95                 RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
96
97         assertNotNull("Existing schema node " + RestconfModule.MODULES_CONTAINER_SCHEMA_NODE + "should be found",
98                 dataSchemaNode);
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);
105     }
106
107     /**
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.
110      */
111     @Test
112     public void getRestconfSchemaNodeContainerStreamsTest() {
113         final DataSchemaNode dataSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
114                 getTestingRestconfModule("ietf-restconf"),
115                 MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE);
116
117         assertNotNull("Existing schema node " + MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE + " should be found",
118                 dataSchemaNode);
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);
125     }
126
127     /**
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>.
130      */
131     @Test
132     public void getRestconfSchemaNodeNullRestconfModuleNegativeTest() {
133         thrown.expect(NullPointerException.class);
134         RestconfSchemaUtil.getRestconfSchemaNode(null, RestconfModule.RESTCONF_CONTAINER_SCHEMA_NODE);
135     }
136
137     /**
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>.
140      */
141     @Test
142     public void getRestconfSchemaNodeNullSchemaNodeNameNegativeTest() {
143         thrown.expect(NullPointerException.class);
144         RestconfSchemaUtil.getRestconfSchemaNode(getTestingRestconfModule("ietf-restconf"), null);
145     }
146
147     /**
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.
151      */
152     @Test
153     public void getRestconfSchemaNodeNotExistingSchemaNodeNameNegativeTest() {
154         try {
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());
164         }
165     }
166
167     /**
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.
172      */
173     @Test
174     public void getRestconfSchemaNodeContainerModulesNegativeTest() {
175         try {
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());
186         }
187     }
188
189     /**
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.
194      */
195     @Test
196     public void getRestconfSchemaNodeListModuleNegativeTest() {
197         try {
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());
209         }
210     }
211
212     /**
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.
217      */
218     @Test
219     public void getRestconfSchemaNodeContainerStreamsNegativeTest() {
220         try {
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());
232         }
233     }
234
235     /**
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.
240      */
241     @Test
242     public void getRestconfSchemaNodeListStreamNegativeTest() {
243         try {
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());
255         }
256     }
257
258     /**
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.
262      */
263     @Test
264     public void getRestconfSchemaNodeMissingRestconfGroupingNegativeTest() {
265         try {
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());
277         }
278     }
279
280     /**
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>.
284      */
285     @Test
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);
291     }
292
293     /**
294      * Positive test trying to find <code>DataSchemaNode</code> of {@link RestconfModule#RESTCONF_GROUPING_SCHEMA_NODE}
295      * in Restconf module groupings collection.
296      */
297     @Test
298     public void findSchemaNodeInCollectionTest() {
299         final SchemaNode schemaNode = RestconfSchemaUtil.findSchemaNodeInCollection(
300                 getTestingRestconfModule("ietf-restconf").getGroupings(),
301                 RestconfModule.RESTCONF_GROUPING_SCHEMA_NODE);
302
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);
310     }
311
312     /**
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.
316      */
317     @Test
318     public void findSchemaNodeInCollectionNegativeTest() {
319         try {
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());
330         }
331     }
332
333     /**
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>.
336      */
337     @Test
338     public void findSchemaNodeInCollectionNullCollectionNegativeTest() {
339         thrown.expect(NullPointerException.class);
340         RestconfSchemaUtil.findSchemaNodeInCollection(null, RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
341     }
342
343     /**
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.
347      */
348     @Test
349     public void findSchemaNodeInCollectionEmptyCollectionNegativeTest() {
350         try {
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());
361         }
362     }
363
364     /**
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.
368      */
369     @Test
370     public void findSchemaNodeInCollectionNullSchemaNodeName() {
371         try {
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());
382         }
383     }
384
385     /**
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
390      */
391     private Module getTestingRestconfModule(final String s) {
392         return schemaContext.findModuleByName(s, Draft11.RestconfModule.IETF_RESTCONF_QNAME.getRevision());
393     }
394 }