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