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;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
22 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
23 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
24 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 public class JsonToNnTest extends AbstractBodyReaderTest {
30 private JsonNormalizedNodeBodyReader jsonBodyReader;
31 private SchemaContext schemaContext;
33 public JsonToNnTest() throws NoSuchFieldException, SecurityException {
37 public static void initialize(final String path, SchemaContext schemaContext) {
38 schemaContext = schemaContextLoader(path, schemaContext);
39 controllerContext.setSchemas(schemaContext);
43 public void simpleListTest() throws Exception {
44 simpleTest("/json-to-nn/simple-list.json",
45 "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
49 public void simpleContainerTest() throws Exception {
50 simpleTest("/json-to-nn/simple-container.json",
51 "/json-to-nn/simple-container-yang", "cont",
52 "simple-container-yang");
56 public void multipleItemsInLeafListTest() throws Exception {
58 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
60 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
61 "/json-to-nn/multiple-leaflist-items.json",
62 "simple-list-yang1:lst");
63 assertNotNull(normalizedNodeContext);
65 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
67 assertTrue(dataTree.contains("45"));
68 assertTrue(dataTree.contains("55"));
69 assertTrue(dataTree.contains("66"));
73 public void multipleItemsInListTest() throws Exception {
74 initialize("/json-to-nn/simple-list-yang/3", this.schemaContext);
76 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
77 "/json-to-nn/multiple-items-in-list.json",
78 "multiple-items-yang:lst");
79 assertNotNull(normalizedNodeContext);
81 assertEquals("lst", normalizedNodeContext.getData().getNodeType()
84 verityMultipleItemsInList(normalizedNodeContext);
88 public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
89 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
91 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
92 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
93 assertNotNull(normalizedNodeContext);
95 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
98 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
100 assertTrue(dataTree.contains("lf"));
101 assertTrue(dataTree.contains("null"));
105 public void incorrectTopLevelElementsTest() throws Exception {
107 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
108 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
109 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
111 InputStream inputStream = this.getClass().getResourceAsStream(
112 "/json-to-nn/wrong-top-level1.json");
114 int countExceptions = 0;
115 RestconfDocumentedException exception = null;
118 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
120 } catch (final RestconfDocumentedException e) {
124 assertNotNull(exception);
126 "Error parsing input: Schema node with name cont wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
127 exception.getErrors().get(0).getErrorMessage());
129 inputStream = this.getClass().getResourceAsStream(
130 "/json-to-nn/wrong-top-level2.json");
133 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
135 } catch (final RestconfDocumentedException e) {
139 assertNotNull(exception);
141 "Error parsing input: Schema node with name lst1 wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
142 exception.getErrors().get(0).getErrorMessage());
144 inputStream = this.getClass().getResourceAsStream(
145 "/json-to-nn/wrong-top-level3.json");
148 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
150 } catch (final RestconfDocumentedException e) {
154 assertNotNull(exception);
156 "Error parsing input: Schema node with name lf wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
157 exception.getErrors().get(0).getErrorMessage());
158 assertEquals(3, countExceptions);
162 public void emptyDataReadTest() throws Exception {
164 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
166 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
167 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
168 assertNotNull(normalizedNodeContext);
170 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
173 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
176 assertTrue(dataTree.contains("lflst1"));
178 assertTrue(dataTree.contains("lflst2 45"));
180 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
181 RestconfDocumentedException exception = null;
182 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
183 final InputStream inputStream = this.getClass().getResourceAsStream(
184 "/json-to-nn/empty-data.json1");
187 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
189 } catch (final RestconfDocumentedException e) {
192 assertNotNull(exception);
193 assertEquals("Error parsing input: null", exception.getErrors().get(0)
198 public void testJsonBlankInput() throws Exception {
199 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
200 final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
201 "array-with-null-yang:cont");
202 assertNull(normalizedNodeContext);
206 public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
208 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
210 final String uri = "simple-list-yang1" + ":" + "lst";
212 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
213 "/json-to-nn/simple-list.json", uri);
214 assertNotNull(normalizedNodeContext);
216 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
218 mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
219 final InputStream inputStream = this.getClass().getResourceAsStream(
220 "/json-to-nn/simple-list.json");
223 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
225 fail("NormalizedNodeContext should not be create because of different namespace");
226 } catch (final RestconfDocumentedException e) {
229 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
233 public void dataAugmentedTest() throws Exception {
235 initialize("/common/augment/yang", this.schemaContext);
237 NormalizedNodeContext normalizedNodeContext = prepareNNC(
238 "/common/augment/json/dataa.json", "main:cont");
240 assertNotNull(normalizedNodeContext);
241 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
244 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
246 assertTrue(dataTree.contains("cont1"));
247 assertTrue(dataTree.contains("lf11 lf11 value from a"));
249 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
252 assertNotNull(normalizedNodeContext);
253 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
255 dataTree = NormalizedNodes
256 .toStringTree(normalizedNodeContext.getData());
257 assertTrue(dataTree.contains("cont1"));
258 assertTrue(dataTree.contains("lf11 lf11 value from b"));
261 private void simpleTest(final String jsonPath, final String yangPath,
262 final String topLevelElementName, final String moduleName) throws Exception {
264 initialize(yangPath, this.schemaContext);
266 final String uri = moduleName + ":" + topLevelElementName;
268 final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
269 assertNotNull(normalizedNodeContext);
271 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
274 private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
275 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
277 mockBodyReader(uri, this.jsonBodyReader, false);
278 } catch (NoSuchFieldException | SecurityException
279 | IllegalArgumentException | IllegalAccessException e) {
280 // TODO Auto-generated catch block
283 final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
285 NormalizedNodeContext normalizedNodeContext = null;
288 normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
289 this.mediaType, null, inputStream);
290 } catch (WebApplicationException | IOException e) {
291 // TODO Auto-generated catch block
295 return normalizedNodeContext;
298 private static void verifyNormaluizedNodeContext(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 static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
315 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
317 assertTrue(dataTree.contains("lf11"));
318 assertTrue(dataTree.contains("lf11_1"));
319 assertTrue(dataTree.contains("lflst11"));
320 assertTrue(dataTree.contains("45"));
321 assertTrue(dataTree.contains("cont11"));
322 assertTrue(dataTree.contains("lst11"));
326 public void unsupportedDataFormatTest() throws Exception {
327 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
328 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
329 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
331 final InputStream inputStream = this.getClass().getResourceAsStream(
332 "/json-to-nn/unsupported-json-format.json");
334 RestconfDocumentedException exception = null;
337 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
339 } catch (final RestconfDocumentedException e) {
342 System.out.println(exception.getErrors().get(0).getErrorMessage());
344 assertTrue(exception.getErrors().get(0).getErrorMessage()
345 .contains("is not a simple type"));
349 public void invalidUriCharacterInValue() throws Exception {
351 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
352 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
353 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
355 final InputStream inputStream = this.getClass().getResourceAsStream(
356 "/json-to-nn/invalid-uri-character-in-value.json");
358 final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
359 null, null, null, this.mediaType, null, inputStream);
360 assertNotNull(normalizedNodeContext);
362 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
365 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
367 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
368 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
372 protected MediaType getMediaType() {