Mass conversion to static methods
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / json / to / nn / test / JsonToNnTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.restconf.impl.json.to.nn.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
22 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
23 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
24 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27
28 public class JsonToNnTest extends AbstractBodyReaderTest {
29
30     private JsonNormalizedNodeBodyReader jsonBodyReader;
31     private SchemaContext schemaContext;
32
33     public JsonToNnTest() throws NoSuchFieldException, SecurityException {
34         super();
35     }
36
37     public static void initialize(final String path, SchemaContext schemaContext) {
38         schemaContext = schemaContextLoader(path, schemaContext);
39         controllerContext.setSchemas(schemaContext);
40     }
41
42     @Test
43     public void simpleListTest() throws Exception {
44         simpleTest("/json-to-nn/simple-list.json",
45                 "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
46     }
47
48     @Test
49     public void simpleContainerTest() throws Exception {
50         simpleTest("/json-to-nn/simple-container.json",
51                 "/json-to-nn/simple-container-yang", "cont",
52                 "simple-container-yang");
53     }
54
55     @Test
56     public void multipleItemsInLeafListTest() throws Exception {
57
58         initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
59
60         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
61                 "/json-to-nn/multiple-leaflist-items.json",
62                 "simple-list-yang1:lst");
63         assertNotNull(normalizedNodeContext);
64
65         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
66                 .getData());
67         assertTrue(dataTree.contains("45"));
68         assertTrue(dataTree.contains("55"));
69         assertTrue(dataTree.contains("66"));
70     }
71
72     @Test
73     public void multipleItemsInListTest() throws Exception {
74         initialize("/json-to-nn/simple-list-yang/3", this.schemaContext);
75
76         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
77                 "/json-to-nn/multiple-items-in-list.json",
78                 "multiple-items-yang:lst");
79         assertNotNull(normalizedNodeContext);
80
81         assertEquals("lst", normalizedNodeContext.getData().getNodeType()
82                 .getLocalName());
83
84         verityMultipleItemsInList(normalizedNodeContext);
85     }
86
87     @Test
88     public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
89         initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
90
91         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
92                 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
93         assertNotNull(normalizedNodeContext);
94
95         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
96                 .getLocalName());
97
98         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
99                 .getData());
100         assertTrue(dataTree.contains("lf"));
101         assertTrue(dataTree.contains("null"));
102     }
103
104     @Test
105     public void incorrectTopLevelElementsTest() throws Exception {
106
107         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
108         initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
109         mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
110
111         InputStream inputStream = this.getClass().getResourceAsStream(
112                 "/json-to-nn/wrong-top-level1.json");
113
114         int countExceptions = 0;
115         RestconfDocumentedException exception = null;
116
117         try {
118             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
119                     inputStream);
120         } catch (final RestconfDocumentedException e) {
121             exception = e;
122             countExceptions++;
123         }
124         assertNotNull(exception);
125         assertEquals(
126                 "Error parsing input: Schema node with name cont wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
127                 exception.getErrors().get(0).getErrorMessage());
128
129         inputStream = this.getClass().getResourceAsStream(
130                 "/json-to-nn/wrong-top-level2.json");
131         exception = null;
132         try {
133             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
134                     inputStream);
135         } catch (final RestconfDocumentedException e) {
136             exception = e;
137             countExceptions++;
138         }
139         assertNotNull(exception);
140         assertEquals(
141                 "Error parsing input: Schema node with name lst1 wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
142                 exception.getErrors().get(0).getErrorMessage());
143
144         inputStream = this.getClass().getResourceAsStream(
145                 "/json-to-nn/wrong-top-level3.json");
146         exception = null;
147         try {
148             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
149                     inputStream);
150         } catch (final RestconfDocumentedException e) {
151             exception = e;
152             countExceptions++;
153         }
154         assertNotNull(exception);
155         assertEquals(
156                 "Error parsing input: Schema node with name lf wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
157                 exception.getErrors().get(0).getErrorMessage());
158         assertEquals(3, countExceptions);
159     }
160
161     @Test
162     public void emptyDataReadTest() throws Exception {
163
164         initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
165
166         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
167                 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
168         assertNotNull(normalizedNodeContext);
169
170         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
171                 .getLocalName());
172
173         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
174                 .getData());
175
176         assertTrue(dataTree.contains("lflst1"));
177
178         assertTrue(dataTree.contains("lflst2 45"));
179
180         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
181         RestconfDocumentedException exception = null;
182         mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
183         final InputStream inputStream = this.getClass().getResourceAsStream(
184                 "/json-to-nn/empty-data.json1");
185
186         try {
187             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
188                     inputStream);
189         } catch (final RestconfDocumentedException e) {
190             exception = e;
191         }
192         assertNotNull(exception);
193         assertEquals("Error parsing input: null", exception.getErrors().get(0)
194                 .getErrorMessage());
195     }
196
197     @Test
198     public void testJsonBlankInput() throws Exception {
199         initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
200         final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
201                 "array-with-null-yang:cont");
202         assertNull(normalizedNodeContext);
203     }
204
205     @Test
206     public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
207
208         initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
209
210         final String uri = "simple-list-yang1" + ":" + "lst";
211
212         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
213                 "/json-to-nn/simple-list.json", uri);
214         assertNotNull(normalizedNodeContext);
215
216         verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
217
218         mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
219         final InputStream inputStream = this.getClass().getResourceAsStream(
220                 "/json-to-nn/simple-list.json");
221
222         try {
223             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
224                     inputStream);
225             fail("NormalizedNodeContext should not be create because of different namespace");
226         } catch (final RestconfDocumentedException e) {
227         }
228
229         verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
230     }
231
232     @Test
233     public void dataAugmentedTest() throws Exception {
234
235         initialize("/common/augment/yang", this.schemaContext);
236
237         NormalizedNodeContext normalizedNodeContext = prepareNNC(
238                 "/common/augment/json/dataa.json", "main:cont");
239
240         assertNotNull(normalizedNodeContext);
241         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
242                 .getLocalName());
243
244         String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
245                 .getData());
246         assertTrue(dataTree.contains("cont1"));
247         assertTrue(dataTree.contains("lf11 lf11 value from a"));
248
249         normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
250                 "main:cont");
251
252         assertNotNull(normalizedNodeContext);
253         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
254                 .getLocalName());
255         dataTree = NormalizedNodes
256                 .toStringTree(normalizedNodeContext.getData());
257         assertTrue(dataTree.contains("cont1"));
258         assertTrue(dataTree.contains("lf11 lf11 value from b"));
259     }
260
261     private void simpleTest(final String jsonPath, final String yangPath,
262             final String topLevelElementName, final String moduleName) throws Exception {
263
264         initialize(yangPath, this.schemaContext);
265
266         final String uri = moduleName + ":" + topLevelElementName;
267
268         final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
269         assertNotNull(normalizedNodeContext);
270
271         verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
272     }
273
274     private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
275         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
276         try {
277             mockBodyReader(uri, this.jsonBodyReader, false);
278         } catch (NoSuchFieldException | SecurityException
279                 | IllegalArgumentException | IllegalAccessException e) {
280             // TODO Auto-generated catch block
281             e.printStackTrace();
282         }
283         final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
284
285         NormalizedNodeContext normalizedNodeContext = null;
286
287         try {
288             normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
289                     this.mediaType, null, inputStream);
290         } catch (WebApplicationException | IOException e) {
291             // TODO Auto-generated catch block
292             e.printStackTrace();
293         }
294
295         return normalizedNodeContext;
296     }
297
298     private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
299             final String topLevelElementName) {
300         assertEquals(topLevelElementName, normalizedNodeContext.getData()
301                 .getNodeType().getLocalName());
302
303         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
304                 .getData());
305         assertTrue(dataTree.contains("cont1"));
306         assertTrue(dataTree.contains("lst1"));
307         assertTrue(dataTree.contains("lflst1"));
308         assertTrue(dataTree.contains("lflst1_1"));
309         assertTrue(dataTree.contains("lflst1_2"));
310         assertTrue(dataTree.contains("lf1"));
311     }
312
313     private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
314
315         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
316                 .getData());
317         assertTrue(dataTree.contains("lf11"));
318         assertTrue(dataTree.contains("lf11_1"));
319         assertTrue(dataTree.contains("lflst11"));
320         assertTrue(dataTree.contains("45"));
321         assertTrue(dataTree.contains("cont11"));
322         assertTrue(dataTree.contains("lst11"));
323     }
324
325     @Test
326     public void unsupportedDataFormatTest() throws Exception {
327         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
328         initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
329         mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
330
331         final InputStream inputStream = this.getClass().getResourceAsStream(
332                 "/json-to-nn/unsupported-json-format.json");
333
334         RestconfDocumentedException exception = null;
335
336         try {
337             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
338                     inputStream);
339         } catch (final RestconfDocumentedException e) {
340             exception = e;
341         }
342         System.out.println(exception.getErrors().get(0).getErrorMessage());
343
344         assertTrue(exception.getErrors().get(0).getErrorMessage()
345                 .contains("is not a simple type"));
346     }
347
348     @Test
349     public void invalidUriCharacterInValue() throws Exception {
350
351         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
352         initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
353         mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
354
355         final InputStream inputStream = this.getClass().getResourceAsStream(
356                 "/json-to-nn/invalid-uri-character-in-value.json");
357
358         final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
359                 null, null, null, this.mediaType, null, inputStream);
360         assertNotNull(normalizedNodeContext);
361
362         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
363                 .getLocalName());
364
365         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
366                 .getData());
367         assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
368         assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
369     }
370
371     @Override
372     protected MediaType getMediaType() {
373         return null;
374     }
375
376 }