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