Update MRI projects for Aluminium
[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.File;
17 import java.io.FileNotFoundException;
18 import java.io.InputStream;
19 import java.util.Collection;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
26 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class JsonToNnTest extends AbstractBodyReaderTest {
36
37     private static final Logger LOG = LoggerFactory.getLogger(AbstractBodyReaderTest.class);
38
39     private final JsonNormalizedNodeBodyReader jsonBodyReader;
40     private static EffectiveModelContext schemaContext;
41
42     public JsonToNnTest() {
43         super(schemaContext, null);
44         this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
45     }
46
47     @BeforeClass
48     public static void initialize() throws FileNotFoundException {
49         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/1");
50         testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/3"));
51         testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/4"));
52         testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-container-yang"));
53         testFiles.addAll(TestRestconfUtils.loadFiles("/common/augment/yang"));
54         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
55     }
56
57     @Test
58     public void simpleListTest() throws Exception {
59         simpleTest("/json-to-nn/simple-list.json",
60                 "lst", "simple-list-yang1");
61     }
62
63     @Test
64     public void simpleContainerTest() throws Exception {
65         simpleTest("/json-to-nn/simple-container.json",
66                 "cont", "simple-container-yang");
67     }
68
69     @Test
70     public void multipleItemsInLeafListTest() throws Exception {
71         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
72                 "/json-to-nn/multiple-leaflist-items.json",
73                 "simple-list-yang1:lst");
74         assertNotNull(normalizedNodeContext);
75
76         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
77                 .getData());
78         assertTrue(dataTree.contains("45"));
79         assertTrue(dataTree.contains("55"));
80         assertTrue(dataTree.contains("66"));
81     }
82
83     @Test
84     public void multipleItemsInListTest() throws Exception {
85         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
86                 "/json-to-nn/multiple-items-in-list.json",
87                 "multiple-items-yang:lst");
88         assertNotNull(normalizedNodeContext);
89
90         assertEquals("lst", normalizedNodeContext.getData().getNodeType()
91                 .getLocalName());
92
93         verityMultipleItemsInList(normalizedNodeContext);
94     }
95
96     @Test
97     public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
98         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
99                 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
100         assertNotNull(normalizedNodeContext);
101
102         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
103                 .getLocalName());
104
105         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
106                 .getData());
107         assertTrue(dataTree.contains("lf"));
108         assertTrue(dataTree.contains("empty"));
109     }
110
111     @Test
112     public void incorrectTopLevelElementsTest() throws Exception {
113         mockBodyReader("simple-list-yang1:lst", this.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             this.jsonBodyReader.readFrom(null, null, null, this.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 wrong was not found under "
131                         + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
132                 exception.getErrors().get(0).getErrorMessage());
133
134         inputStream = this.getClass().getResourceAsStream(
135                 "/json-to-nn/wrong-top-level2.json");
136         exception = null;
137         try {
138             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
139                     inputStream);
140         } catch (final RestconfDocumentedException e) {
141             exception = e;
142             countExceptions++;
143         }
144         assertNotNull(exception);
145         assertEquals(
146                 "Error parsing input: Schema node with name lst1 was not found under "
147                         + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
148                 exception.getErrors().get(0).getErrorMessage());
149
150         inputStream = this.getClass().getResourceAsStream(
151                 "/json-to-nn/wrong-top-level3.json");
152         exception = null;
153         try {
154             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
155                     inputStream);
156         } catch (final RestconfDocumentedException e) {
157             exception = e;
158             countExceptions++;
159         }
160         assertNotNull(exception);
161         assertEquals(
162                 "Error parsing input: Schema node with name lf was not found under "
163                         + "(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 Exception {
170         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
171                 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
172         assertNotNull(normalizedNodeContext);
173
174         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
175                 .getLocalName());
176
177         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
178                 .getData());
179
180         assertTrue(dataTree.contains("lflst1"));
181
182         assertTrue(dataTree.contains("lflst2 45"));
183
184         RestconfDocumentedException exception = null;
185         mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
186         final InputStream inputStream = this.getClass().getResourceAsStream(
187                 "/json-to-nn/empty-data.json1");
188
189         try {
190             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
191                     inputStream);
192         } catch (final RestconfDocumentedException e) {
193             exception = e;
194         }
195         assertNotNull(exception);
196         assertEquals("Error parsing input: null", exception.getErrors().get(0)
197                 .getErrorMessage());
198     }
199
200     @Test
201     public void testJsonBlankInput() throws Exception {
202         final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
203                 "array-with-null-yang:cont");
204         assertNull(normalizedNodeContext);
205     }
206
207     @Test
208     public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
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             LOG.warn("Read from InputStream failed. Message: {}. Status: {}", e.getMessage(), e.getStatus());
227         }
228
229         verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
230     }
231
232     @Test
233     public void dataAugmentedTest() throws Exception {
234         NormalizedNodeContext normalizedNodeContext = prepareNNC(
235                 "/common/augment/json/dataa.json", "main:cont");
236
237         assertNotNull(normalizedNodeContext);
238         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
239                 .getLocalName());
240
241         String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
242                 .getData());
243         assertTrue(dataTree.contains("cont1"));
244         assertTrue(dataTree.contains("lf11 lf11 value from a"));
245
246         normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
247                 "main:cont");
248
249         assertNotNull(normalizedNodeContext);
250         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
251                 .getLocalName());
252         dataTree = NormalizedNodes
253                 .toStringTree(normalizedNodeContext.getData());
254         assertTrue(dataTree.contains("cont1"));
255         assertTrue(dataTree.contains("lf11 lf11 value from b"));
256     }
257
258     private void simpleTest(final String jsonPath, final String topLevelElementName,
259             final String moduleName) throws Exception {
260         final String uri = moduleName + ":" + topLevelElementName;
261
262         final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
263         assertNotNull(normalizedNodeContext);
264
265         verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
266     }
267
268     private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
269         try {
270             mockBodyReader(uri, this.jsonBodyReader, false);
271         } catch (NoSuchFieldException | SecurityException
272                 | IllegalArgumentException | IllegalAccessException e) {
273             LOG.warn("Operation failed due to: {}", e.getMessage());
274         }
275         final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
276
277         NormalizedNodeContext normalizedNodeContext = null;
278
279         try {
280             normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
281                     this.mediaType, null, inputStream);
282         } catch (WebApplicationException e) {
283             // TODO Auto-generated catch block
284         }
285
286         return normalizedNodeContext;
287     }
288
289     private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
290             final String topLevelElementName) {
291         assertEquals(topLevelElementName, normalizedNodeContext.getData()
292                 .getNodeType().getLocalName());
293
294         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
295                 .getData());
296         assertTrue(dataTree.contains("cont1"));
297         assertTrue(dataTree.contains("lst1"));
298         assertTrue(dataTree.contains("lflst1"));
299         assertTrue(dataTree.contains("lflst1_1"));
300         assertTrue(dataTree.contains("lflst1_2"));
301         assertTrue(dataTree.contains("lf1"));
302     }
303
304     private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
305
306         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
307                 .getData());
308         assertTrue(dataTree.contains("lf11"));
309         assertTrue(dataTree.contains("lf11_1"));
310         assertTrue(dataTree.contains("lflst11"));
311         assertTrue(dataTree.contains("45"));
312         assertTrue(dataTree.contains("cont11"));
313         assertTrue(dataTree.contains("lst11"));
314     }
315
316     @Test
317     public void unsupportedDataFormatTest() throws Exception {
318         mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
319
320         final InputStream inputStream = this.getClass().getResourceAsStream(
321                 "/json-to-nn/unsupported-json-format.json");
322
323         RestconfDocumentedException exception = null;
324
325         try {
326             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
327                     inputStream);
328         } catch (final RestconfDocumentedException e) {
329             exception = e;
330         }
331         LOG.info(exception.getErrors().get(0).getErrorMessage());
332
333         assertTrue(exception.getErrors().get(0).getErrorMessage()
334                 .contains("is not a simple type"));
335     }
336
337     @Test
338     public void invalidUriCharacterInValue() throws Exception {
339         mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
340
341         final InputStream inputStream = this.getClass().getResourceAsStream(
342                 "/json-to-nn/invalid-uri-character-in-value.json");
343
344         final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
345                 null, null, null, this.mediaType, null, inputStream);
346         assertNotNull(normalizedNodeContext);
347
348         assertEquals("cont", normalizedNodeContext.getData().getNodeType()
349                 .getLocalName());
350
351         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
352                 .getData());
353         assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
354         assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
355     }
356
357     @Override
358     protected MediaType getMediaType() {
359         return null;
360     }
361
362 }