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