Bump versions to 4.0.0-SNAPSHOT
[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.netconf.sal.rest.impl.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().getIdentifier().getNodeType().getLocalName());
91
92         verityMultipleItemsInList(normalizedNodeContext);
93     }
94
95     @Test
96     public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
97         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
98                 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
99         assertNotNull(normalizedNodeContext);
100
101         assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
102
103         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
104         assertTrue(dataTree.contains("lf"));
105         assertTrue(dataTree.contains("empty"));
106     }
107
108     @Test
109     public void incorrectTopLevelElementsTest() throws Exception {
110         mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
111
112         InputStream inputStream = this.getClass().getResourceAsStream(
113                 "/json-to-nn/wrong-top-level1.json");
114
115         int countExceptions = 0;
116         RestconfDocumentedException exception = null;
117
118         try {
119             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
120                     inputStream);
121         } catch (final RestconfDocumentedException e) {
122             exception = e;
123             countExceptions++;
124         }
125         assertNotNull(exception);
126         assertEquals(
127                 "Error parsing input: Schema node with name wrong was not found under "
128                         + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
129                 exception.getErrors().get(0).getErrorMessage());
130
131         inputStream = this.getClass().getResourceAsStream(
132                 "/json-to-nn/wrong-top-level2.json");
133         exception = null;
134         try {
135             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
136                     inputStream);
137         } catch (final RestconfDocumentedException e) {
138             exception = e;
139             countExceptions++;
140         }
141         assertNotNull(exception);
142         assertEquals(
143                 "Error parsing input: Schema node with name lst1 was not found under "
144                         + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
145                 exception.getErrors().get(0).getErrorMessage());
146
147         inputStream = this.getClass().getResourceAsStream(
148                 "/json-to-nn/wrong-top-level3.json");
149         exception = null;
150         try {
151             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
152                     inputStream);
153         } catch (final RestconfDocumentedException e) {
154             exception = e;
155             countExceptions++;
156         }
157         assertNotNull(exception);
158         assertEquals(
159                 "Error parsing input: Schema node with name lf was not found under "
160                         + "(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 Exception {
167         final NormalizedNodeContext normalizedNodeContext = prepareNNC(
168                 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
169         assertNotNull(normalizedNodeContext);
170
171         assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
172
173         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
174
175         assertTrue(dataTree.contains("lflst1"));
176
177         assertTrue(dataTree.contains("lflst2 45"));
178
179         RestconfDocumentedException exception = null;
180         mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
181         final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/empty-data.json1");
182
183         try {
184             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,inputStream);
185         } catch (final RestconfDocumentedException e) {
186             exception = e;
187         }
188         assertNotNull(exception);
189         assertEquals("Error parsing input: null", exception.getErrors().get(0).getErrorMessage());
190     }
191
192     @Test
193     public void testJsonBlankInput() throws Exception {
194         final NormalizedNodeContext normalizedNodeContext = prepareNNC("", "array-with-null-yang:cont");
195         assertNull(normalizedNodeContext);
196     }
197
198     @Test
199     public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
200         final String uri = "simple-list-yang1" + ":" + "lst";
201
202         final NormalizedNodeContext normalizedNodeContext = prepareNNC("/json-to-nn/simple-list.json", uri);
203         assertNotNull(normalizedNodeContext);
204
205         verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
206
207         mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
208         final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/simple-list.json");
209
210         try {
211             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
212             fail("NormalizedNodeContext should not be create because of different namespace");
213         } catch (final RestconfDocumentedException e) {
214             LOG.warn("Read from InputStream failed. Message: {}. Status: {}", e.getMessage(), e.getStatus());
215         }
216
217         verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
218     }
219
220     @Test
221     public void dataAugmentedTest() throws Exception {
222         NormalizedNodeContext normalizedNodeContext = prepareNNC("/common/augment/json/dataa.json", "main:cont");
223
224         assertNotNull(normalizedNodeContext);
225         assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
226
227         String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
228                 .getData());
229         assertTrue(dataTree.contains("cont1"));
230         assertTrue(dataTree.contains("lf11 lf11 value from a"));
231
232         normalizedNodeContext = prepareNNC("/common/augment/json/datab.json", "main:cont");
233
234         assertNotNull(normalizedNodeContext);
235         assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
236         dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
237         assertTrue(dataTree.contains("cont1"));
238         assertTrue(dataTree.contains("lf11 lf11 value from b"));
239     }
240
241     private void simpleTest(final String jsonPath, final String topLevelElementName,
242             final String moduleName) throws Exception {
243         final String uri = moduleName + ":" + topLevelElementName;
244
245         final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
246         assertNotNull(normalizedNodeContext);
247
248         verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
249     }
250
251     private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
252         try {
253             mockBodyReader(uri, this.jsonBodyReader, false);
254         } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
255             LOG.warn("Operation failed due to: {}", e.getMessage());
256         }
257         final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
258
259         NormalizedNodeContext normalizedNodeContext = null;
260
261         try {
262             normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
263         } catch (WebApplicationException e) {
264             // TODO Auto-generated catch block
265         }
266
267         return normalizedNodeContext;
268     }
269
270     private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
271             final String topLevelElementName) {
272         assertEquals(topLevelElementName, normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
273
274         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
275         assertTrue(dataTree.contains("cont1"));
276         assertTrue(dataTree.contains("lst1"));
277         assertTrue(dataTree.contains("lflst1"));
278         assertTrue(dataTree.contains("lflst1_1"));
279         assertTrue(dataTree.contains("lflst1_2"));
280         assertTrue(dataTree.contains("lf1"));
281     }
282
283     private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
284         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
285         assertTrue(dataTree.contains("lf11"));
286         assertTrue(dataTree.contains("lf11_1"));
287         assertTrue(dataTree.contains("lflst11"));
288         assertTrue(dataTree.contains("45"));
289         assertTrue(dataTree.contains("cont11"));
290         assertTrue(dataTree.contains("lst11"));
291     }
292
293     @Test
294     public void unsupportedDataFormatTest() throws Exception {
295         mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
296
297         final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/unsupported-json-format.json");
298
299         RestconfDocumentedException exception = null;
300
301         try {
302             this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
303         } catch (final RestconfDocumentedException e) {
304             exception = e;
305         }
306         LOG.info(exception.getErrors().get(0).getErrorMessage());
307
308         assertTrue(exception.getErrors().get(0).getErrorMessage().contains("is not a simple type"));
309     }
310
311     @Test
312     public void invalidUriCharacterInValue() throws Exception {
313         mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
314
315         final InputStream inputStream = this.getClass().getResourceAsStream(
316                 "/json-to-nn/invalid-uri-character-in-value.json");
317
318         final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
319                 null, null, null, this.mediaType, null, inputStream);
320         assertNotNull(normalizedNodeContext);
321
322         assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
323
324         final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
325         assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
326         assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
327     }
328
329     @Override
330     protected MediaType getMediaType() {
331         return null;
332     }
333
334 }