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