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;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.MediaType;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
21 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
22 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
23 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 public class JsonToNnTest extends AbstractBodyReaderTest {
29 private JsonNormalizedNodeBodyReader jsonBodyReader;
30 private SchemaContext schemaContext;
32 public JsonToNnTest() throws NoSuchFieldException, SecurityException {
36 public static void initialize(final String path, SchemaContext schemaContext) {
37 schemaContext = schemaContextLoader(path, schemaContext);
38 controllerContext.setSchemas(schemaContext);
42 public void simpleListTest() throws Exception {
43 simpleTest("/json-to-nn/simple-list.json",
44 "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
48 public void simpleContainerTest() throws Exception {
49 simpleTest("/json-to-nn/simple-container.json",
50 "/json-to-nn/simple-container-yang", "cont",
51 "simple-container-yang");
55 public void multipleItemsInLeafListTest() throws Exception {
57 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
59 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
60 "/json-to-nn/multiple-leaflist-items.json",
61 "simple-list-yang1:lst");
62 assertNotNull(normalizedNodeContext);
64 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
66 assertTrue(dataTree.contains("45"));
67 assertTrue(dataTree.contains("55"));
68 assertTrue(dataTree.contains("66"));
72 public void multipleItemsInListTest() throws Exception {
73 initialize("/json-to-nn/simple-list-yang/3", this.schemaContext);
75 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
76 "/json-to-nn/multiple-items-in-list.json",
77 "multiple-items-yang:lst");
78 assertNotNull(normalizedNodeContext);
80 assertEquals("lst", normalizedNodeContext.getData().getNodeType()
83 verityMultipleItemsInList(normalizedNodeContext);
87 public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
88 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
90 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
91 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
92 assertNotNull(normalizedNodeContext);
94 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
97 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
99 assertTrue(dataTree.contains("lf"));
100 assertTrue(dataTree.contains("null"));
104 public void incorrectTopLevelElementsTest() throws Exception {
106 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
107 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
108 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
110 InputStream inputStream = this.getClass().getResourceAsStream(
111 "/json-to-nn/wrong-top-level1.json");
113 int countExceptions = 0;
114 RestconfDocumentedException exception = null;
117 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
119 } catch (final RestconfDocumentedException e) {
123 assertNotNull(exception);
125 "Error parsing input: Schema node with name cont wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
126 exception.getErrors().get(0).getErrorMessage());
128 inputStream = this.getClass().getResourceAsStream(
129 "/json-to-nn/wrong-top-level2.json");
132 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
134 } catch (final RestconfDocumentedException e) {
138 assertNotNull(exception);
140 "Error parsing input: Schema node with name lst1 wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
141 exception.getErrors().get(0).getErrorMessage());
143 inputStream = this.getClass().getResourceAsStream(
144 "/json-to-nn/wrong-top-level3.json");
147 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
149 } catch (final RestconfDocumentedException e) {
153 assertNotNull(exception);
155 "Error parsing input: Schema node with name lf wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
156 exception.getErrors().get(0).getErrorMessage());
157 assertEquals(3, countExceptions);
161 public void emptyDataReadTest() throws Exception {
163 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
165 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
166 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
167 assertNotNull(normalizedNodeContext);
169 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
172 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
175 assertTrue(dataTree.contains("lflst1"));
177 assertTrue(dataTree.contains("lflst2 45"));
179 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
180 RestconfDocumentedException exception = null;
181 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
182 final InputStream inputStream = this.getClass().getResourceAsStream(
183 "/json-to-nn/empty-data.json1");
186 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
188 } catch (final RestconfDocumentedException e) {
191 assertNotNull(exception);
192 assertEquals("Error parsing input: null", exception.getErrors().get(0)
197 public void testJsonBlankInput() throws Exception {
198 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
199 final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
200 "array-with-null-yang:cont");
201 assertNull(normalizedNodeContext);
205 public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
207 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
209 final String uri = "simple-list-yang1" + ":" + "lst";
211 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
212 "/json-to-nn/simple-list.json", uri);
213 assertNotNull(normalizedNodeContext);
215 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
217 mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
218 final InputStream inputStream = this.getClass().getResourceAsStream(
219 "/json-to-nn/simple-list.json");
222 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
224 fail("NormalizedNodeContext should not be create because of different namespace");
225 } catch (final RestconfDocumentedException e) {
228 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
232 public void dataAugmentedTest() throws Exception {
234 initialize("/common/augment/yang", this.schemaContext);
236 NormalizedNodeContext normalizedNodeContext = prepareNNC(
237 "/common/augment/json/dataa.json", "main:cont");
239 assertNotNull(normalizedNodeContext);
240 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
243 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
245 assertTrue(dataTree.contains("cont1"));
246 assertTrue(dataTree.contains("lf11 lf11 value from a"));
248 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
251 assertNotNull(normalizedNodeContext);
252 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
254 dataTree = NormalizedNodes
255 .toStringTree(normalizedNodeContext.getData());
256 assertTrue(dataTree.contains("cont1"));
257 assertTrue(dataTree.contains("lf11 lf11 value from b"));
260 private void simpleTest(final String jsonPath, final String yangPath,
261 final String topLevelElementName, final String moduleName) throws Exception {
263 initialize(yangPath, this.schemaContext);
265 final String uri = moduleName + ":" + topLevelElementName;
267 final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
268 assertNotNull(normalizedNodeContext);
270 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
273 private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
274 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
276 mockBodyReader(uri, this.jsonBodyReader, false);
277 } catch (NoSuchFieldException | SecurityException
278 | IllegalArgumentException | IllegalAccessException e) {
279 // TODO Auto-generated catch block
282 final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
284 NormalizedNodeContext normalizedNodeContext = null;
287 normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
288 this.mediaType, null, inputStream);
289 } catch (WebApplicationException | IOException e) {
290 // TODO Auto-generated catch block
294 return normalizedNodeContext;
297 private void verifyNormaluizedNodeContext(
298 final NormalizedNodeContext normalizedNodeContext,
299 final String topLevelElementName) {
300 assertEquals(topLevelElementName, normalizedNodeContext.getData()
301 .getNodeType().getLocalName());
303 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
305 assertTrue(dataTree.contains("cont1"));
306 assertTrue(dataTree.contains("lst1"));
307 assertTrue(dataTree.contains("lflst1"));
308 assertTrue(dataTree.contains("lflst1_1"));
309 assertTrue(dataTree.contains("lflst1_2"));
310 assertTrue(dataTree.contains("lf1"));
313 private void verityMultipleItemsInList(
314 final NormalizedNodeContext normalizedNodeContext) {
316 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
318 assertTrue(dataTree.contains("lf11"));
319 assertTrue(dataTree.contains("lf11_1"));
320 assertTrue(dataTree.contains("lflst11"));
321 assertTrue(dataTree.contains("45"));
322 assertTrue(dataTree.contains("cont11"));
323 assertTrue(dataTree.contains("lst11"));
327 public void unsupportedDataFormatTest() throws Exception {
328 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
329 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
330 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
332 final InputStream inputStream = this.getClass().getResourceAsStream(
333 "/json-to-nn/unsupported-json-format.json");
335 RestconfDocumentedException exception = null;
338 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
340 } catch (final RestconfDocumentedException e) {
343 System.out.println(exception.getErrors().get(0).getErrorMessage());
345 assertTrue(exception.getErrors().get(0).getErrorMessage()
346 .contains("is not a simple type"));
350 public void invalidUriCharacterInValue() throws Exception {
352 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
353 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
354 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
356 final InputStream inputStream = this.getClass().getResourceAsStream(
357 "/json-to-nn/invalid-uri-character-in-value.json");
359 final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
360 null, null, null, this.mediaType, null, inputStream);
361 assertNotNull(normalizedNodeContext);
363 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
366 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
368 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
369 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
373 protected MediaType getMediaType() {