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