2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.json.to.nn.test;
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;
16 import java.io.IOException;
17 import java.io.InputStream;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.MediaType;
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;
31 public class JsonToNnTest extends AbstractBodyReaderTest {
33 private JsonNormalizedNodeBodyReader jsonBodyReader;
34 private SchemaContext schemaContext;
36 public JsonToNnTest() throws NoSuchFieldException, SecurityException {
40 public static void initialize(final String path, SchemaContext schemaContext) {
41 schemaContext = schemaContextLoader(path, schemaContext);
42 controllerContext.setSchemas(schemaContext);
45 //TODO unignore once yangtools bug is fixed
48 public void simpleListTest() {
49 simpleTest("/json-to-nn/simple-list.json",
50 "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
54 public void simpleContainerTest() {
55 simpleTest("/json-to-nn/simple-container.json",
56 "/json-to-nn/simple-container-yang", "cont",
57 "simple-container-yang");
61 public void multipleItemsInLeafListTest() {
63 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
65 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
66 "/json-to-nn/multiple-leaflist-items.json",
67 "simple-list-yang1:lst");
68 assertNotNull(normalizedNodeContext);
70 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
72 assertTrue(dataTree.contains("45"));
73 assertTrue(dataTree.contains("55"));
74 assertTrue(dataTree.contains("66"));
78 public void multipleItemsInListTest() {
79 initialize("/json-to-nn/simple-list-yang/3", schemaContext);
81 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
82 "/json-to-nn/multiple-items-in-list.json",
83 "multiple-items-yang:lst");
84 assertNotNull(normalizedNodeContext);
86 assertEquals("lst", normalizedNodeContext.getData().getNodeType()
89 verityMultipleItemsInList(normalizedNodeContext);
93 public void nullArrayToSimpleNodeWithNullValueTest() {
94 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
96 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
97 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
98 assertNotNull(normalizedNodeContext);
100 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
103 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
105 assertTrue(dataTree.contains("lf"));
106 assertTrue(dataTree.contains("null"));
110 public void incorrectTopLevelElementsTest() throws WebApplicationException,
111 IOException, NoSuchFieldException, SecurityException,
112 IllegalArgumentException, IllegalAccessException {
114 jsonBodyReader = new JsonNormalizedNodeBodyReader();
115 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
116 mockBodyReader("simple-list-yang1:lst", jsonBodyReader, false);
118 InputStream inputStream = this.getClass().getResourceAsStream(
119 "/json-to-nn/wrong-top-level1.json");
121 int countExceptions = 0;
122 RestconfDocumentedException exception = null;
125 jsonBodyReader.readFrom(null, null, null, mediaType, null,
127 } catch (final RestconfDocumentedException e) {
131 assertNotNull(exception);
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());
136 inputStream = this.getClass().getResourceAsStream(
137 "/json-to-nn/wrong-top-level2.json");
140 jsonBodyReader.readFrom(null, null, null, mediaType, null,
142 } catch (final RestconfDocumentedException e) {
146 assertNotNull(exception);
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());
151 inputStream = this.getClass().getResourceAsStream(
152 "/json-to-nn/wrong-top-level3.json");
155 jsonBodyReader.readFrom(null, null, null, mediaType, null,
157 } catch (final RestconfDocumentedException e) {
161 assertNotNull(exception);
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);
169 public void emptyDataReadTest() throws WebApplicationException,
170 IOException, NoSuchFieldException, SecurityException,
171 IllegalArgumentException, IllegalAccessException {
173 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
175 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
176 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
177 assertNotNull(normalizedNodeContext);
179 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
182 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
185 assertTrue(dataTree.contains("lflst1"));
187 assertTrue(dataTree.contains("lflst2 45"));
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");
196 jsonBodyReader.readFrom(null, null, null, mediaType, null,
198 } catch (final RestconfDocumentedException e) {
201 assertNotNull(exception);
202 assertEquals("Error parsing input: null", exception.getErrors().get(0)
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);
216 //TODO unignore once yangtools bug is fixed
219 public void notSupplyNamespaceIfAlreadySupplied()
220 throws WebApplicationException, IOException, NoSuchFieldException,
221 SecurityException, IllegalArgumentException, IllegalAccessException {
223 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
225 final String uri = "simple-list-yang1" + ":" + "lst";
227 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
228 "/json-to-nn/simple-list.json", uri);
229 assertNotNull(normalizedNodeContext);
231 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
233 mockBodyReader("simple-list-yang2:lst", jsonBodyReader, false);
234 final InputStream inputStream = this.getClass().getResourceAsStream(
235 "/json-to-nn/simple-list.json");
238 jsonBodyReader.readFrom(null, null, null, mediaType, null,
240 fail("NormalizedNodeContext should not be create because of different namespace");
241 } catch (final RestconfDocumentedException e) {
244 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
248 public void dataAugmentedTest() {
250 initialize("/common/augment/yang", schemaContext);
252 NormalizedNodeContext normalizedNodeContext = prepareNNC(
253 "/common/augment/json/dataa.json", "main:cont");
255 assertNotNull(normalizedNodeContext);
256 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
259 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
261 assertTrue(dataTree.contains("cont1"));
262 assertTrue(dataTree.contains("lf11 lf11 value from a"));
264 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
267 assertNotNull(normalizedNodeContext);
268 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
270 dataTree = NormalizedNodes
271 .toStringTree(normalizedNodeContext.getData());
272 assertTrue(dataTree.contains("cont1"));
273 assertTrue(dataTree.contains("lf11 lf11 value from b"));
276 private void simpleTest(final String jsonPath, final String yangPath,
277 final String topLevelElementName, final String moduleName) {
279 initialize(yangPath, schemaContext);
281 final String uri = moduleName + ":" + topLevelElementName;
283 final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
284 assertNotNull(normalizedNodeContext);
286 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
289 private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) {
290 jsonBodyReader = new JsonNormalizedNodeBodyReader();
292 mockBodyReader(uri, jsonBodyReader, false);
293 } catch (NoSuchFieldException | SecurityException
294 | IllegalArgumentException | IllegalAccessException e) {
295 // TODO Auto-generated catch block
298 final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
300 NormalizedNodeContext normalizedNodeContext = null;
303 normalizedNodeContext = jsonBodyReader.readFrom(null, null, null,
304 mediaType, null, inputStream);
305 } catch (WebApplicationException | IOException e) {
306 // TODO Auto-generated catch block
310 return normalizedNodeContext;
313 private void verifyNormaluizedNodeContext(
314 final NormalizedNodeContext normalizedNodeContext,
315 final String topLevelElementName) {
316 assertEquals(topLevelElementName, normalizedNodeContext.getData()
317 .getNodeType().getLocalName());
319 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
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"));
329 private void verityMultipleItemsInList(
330 final NormalizedNodeContext normalizedNodeContext) {
332 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
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"));
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);
350 final InputStream inputStream = this.getClass().getResourceAsStream(
351 "/json-to-nn/unsupported-json-format.json");
353 RestconfDocumentedException exception = null;
356 jsonBodyReader.readFrom(null, null, null, mediaType, null,
358 } catch (final RestconfDocumentedException e) {
361 System.out.println(exception.getErrors().get(0).getErrorMessage());
363 assertTrue(exception.getErrors().get(0).getErrorMessage()
364 .contains("is not a simple type"));
368 public void invalidUriCharacterInValue() throws NoSuchFieldException,
369 SecurityException, IllegalArgumentException,
370 IllegalAccessException, WebApplicationException, IOException {
372 jsonBodyReader = new JsonNormalizedNodeBodyReader();
373 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
374 mockBodyReader("array-with-null-yang:cont", jsonBodyReader, false);
376 final InputStream inputStream = this.getClass().getResourceAsStream(
377 "/json-to-nn/invalid-uri-character-in-value.json");
379 final NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom(
380 null, null, null, mediaType, null, inputStream);
381 assertNotNull(normalizedNodeContext);
383 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
386 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
388 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
389 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
393 protected MediaType getMediaType() {
394 // TODO Auto-generated method stub