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.Test;
23 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
24 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
25 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
26 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 public class JsonToNnTest extends AbstractBodyReaderTest {
32 private JsonNormalizedNodeBodyReader jsonBodyReader;
33 private SchemaContext schemaContext;
35 public JsonToNnTest() throws NoSuchFieldException, SecurityException {
39 public static void initialize(final String path, SchemaContext schemaContext) {
40 schemaContext = schemaContextLoader(path, schemaContext);
41 controllerContext.setSchemas(schemaContext);
45 public void simpleListTest() {
46 simpleTest("/json-to-nn/simple-list.json",
47 "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
51 public void simpleContainerTest() {
52 simpleTest("/json-to-nn/simple-container.json",
53 "/json-to-nn/simple-container-yang", "cont",
54 "simple-container-yang");
58 public void multipleItemsInLeafListTest() {
60 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
62 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
63 "/json-to-nn/multiple-leaflist-items.json",
64 "simple-list-yang1:lst");
65 assertNotNull(normalizedNodeContext);
67 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
69 assertTrue(dataTree.contains("45"));
70 assertTrue(dataTree.contains("55"));
71 assertTrue(dataTree.contains("66"));
75 public void multipleItemsInListTest() {
76 initialize("/json-to-nn/simple-list-yang/3", schemaContext);
78 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
79 "/json-to-nn/multiple-items-in-list.json",
80 "multiple-items-yang:lst");
81 assertNotNull(normalizedNodeContext);
83 assertEquals("lst", normalizedNodeContext.getData().getNodeType()
86 verityMultipleItemsInList(normalizedNodeContext);
90 public void nullArrayToSimpleNodeWithNullValueTest() {
91 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
93 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
94 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
95 assertNotNull(normalizedNodeContext);
97 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
100 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
102 assertTrue(dataTree.contains("lf"));
103 assertTrue(dataTree.contains("null"));
107 public void incorrectTopLevelElementsTest() throws WebApplicationException,
108 IOException, NoSuchFieldException, SecurityException,
109 IllegalArgumentException, IllegalAccessException {
111 jsonBodyReader = new JsonNormalizedNodeBodyReader();
112 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
113 mockBodyReader("simple-list-yang1:lst", jsonBodyReader, false);
115 InputStream inputStream = this.getClass().getResourceAsStream(
116 "/json-to-nn/wrong-top-level1.json");
118 int countExceptions = 0;
119 RestconfDocumentedException exception = null;
122 jsonBodyReader.readFrom(null, null, null, mediaType, null,
124 } catch (final RestconfDocumentedException e) {
128 assertNotNull(exception);
130 "Error parsing input: Schema node with name cont wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
131 exception.getErrors().get(0).getErrorMessage());
133 inputStream = this.getClass().getResourceAsStream(
134 "/json-to-nn/wrong-top-level2.json");
137 jsonBodyReader.readFrom(null, null, null, mediaType, null,
139 } catch (final RestconfDocumentedException e) {
143 assertNotNull(exception);
145 "Error parsing input: Schema node with name lst1 wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
146 exception.getErrors().get(0).getErrorMessage());
148 inputStream = this.getClass().getResourceAsStream(
149 "/json-to-nn/wrong-top-level3.json");
152 jsonBodyReader.readFrom(null, null, null, mediaType, null,
154 } catch (final RestconfDocumentedException e) {
158 assertNotNull(exception);
160 "Error parsing input: Schema node with name lf wasn't found under (urn:ietf:params:xml:ns:netconf:base:1.0)data.",
161 exception.getErrors().get(0).getErrorMessage());
162 assertEquals(3, countExceptions);
166 public void emptyDataReadTest() throws WebApplicationException,
167 IOException, NoSuchFieldException, SecurityException,
168 IllegalArgumentException, IllegalAccessException {
170 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
172 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
173 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
174 assertNotNull(normalizedNodeContext);
176 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
179 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
182 assertTrue(dataTree.contains("lflst1"));
184 assertTrue(dataTree.contains("lflst2 45"));
186 jsonBodyReader = new JsonNormalizedNodeBodyReader();
187 RestconfDocumentedException exception = null;
188 mockBodyReader("array-with-null-yang:cont", jsonBodyReader, false);
189 final InputStream inputStream = this.getClass().getResourceAsStream(
190 "/json-to-nn/empty-data.json1");
193 jsonBodyReader.readFrom(null, null, null, mediaType, null,
195 } catch (final RestconfDocumentedException e) {
198 assertNotNull(exception);
199 assertEquals("Error parsing input: null", exception.getErrors().get(0)
204 public void testJsonBlankInput() throws NoSuchFieldException,
205 SecurityException, IllegalArgumentException,
206 IllegalAccessException, WebApplicationException, IOException {
207 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
208 final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
209 "array-with-null-yang:cont");
210 assertNull(normalizedNodeContext);
214 public void notSupplyNamespaceIfAlreadySupplied()
215 throws WebApplicationException, IOException, NoSuchFieldException,
216 SecurityException, IllegalArgumentException, IllegalAccessException {
218 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
220 final String uri = "simple-list-yang1" + ":" + "lst";
222 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
223 "/json-to-nn/simple-list.json", uri);
224 assertNotNull(normalizedNodeContext);
226 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
228 mockBodyReader("simple-list-yang2:lst", jsonBodyReader, false);
229 final InputStream inputStream = this.getClass().getResourceAsStream(
230 "/json-to-nn/simple-list.json");
233 jsonBodyReader.readFrom(null, null, null, mediaType, null,
235 fail("NormalizedNodeContext should not be create because of different namespace");
236 } catch (final RestconfDocumentedException e) {
239 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
243 public void dataAugmentedTest() {
245 initialize("/common/augment/yang", schemaContext);
247 NormalizedNodeContext normalizedNodeContext = prepareNNC(
248 "/common/augment/json/dataa.json", "main:cont");
250 assertNotNull(normalizedNodeContext);
251 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
254 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
256 assertTrue(dataTree.contains("cont1"));
257 assertTrue(dataTree.contains("lf11 lf11 value from a"));
259 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
262 assertNotNull(normalizedNodeContext);
263 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
265 dataTree = NormalizedNodes
266 .toStringTree(normalizedNodeContext.getData());
267 assertTrue(dataTree.contains("cont1"));
268 assertTrue(dataTree.contains("lf11 lf11 value from b"));
271 private void simpleTest(final String jsonPath, final String yangPath,
272 final String topLevelElementName, final String moduleName) {
274 initialize(yangPath, schemaContext);
276 final String uri = moduleName + ":" + topLevelElementName;
278 final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
279 assertNotNull(normalizedNodeContext);
281 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
284 private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) {
285 jsonBodyReader = new JsonNormalizedNodeBodyReader();
287 mockBodyReader(uri, jsonBodyReader, false);
288 } catch (NoSuchFieldException | SecurityException
289 | IllegalArgumentException | IllegalAccessException e) {
290 // TODO Auto-generated catch block
293 final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
295 NormalizedNodeContext normalizedNodeContext = null;
298 normalizedNodeContext = jsonBodyReader.readFrom(null, null, null,
299 mediaType, null, inputStream);
300 } catch (WebApplicationException | IOException e) {
301 // TODO Auto-generated catch block
305 return normalizedNodeContext;
308 private void verifyNormaluizedNodeContext(
309 final NormalizedNodeContext normalizedNodeContext,
310 final String topLevelElementName) {
311 assertEquals(topLevelElementName, normalizedNodeContext.getData()
312 .getNodeType().getLocalName());
314 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
316 assertTrue(dataTree.contains("cont1"));
317 assertTrue(dataTree.contains("lst1"));
318 assertTrue(dataTree.contains("lflst1"));
319 assertTrue(dataTree.contains("lflst1_1"));
320 assertTrue(dataTree.contains("lflst1_2"));
321 assertTrue(dataTree.contains("lf1"));
324 private void verityMultipleItemsInList(
325 final NormalizedNodeContext normalizedNodeContext) {
327 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
329 assertTrue(dataTree.contains("lf11"));
330 assertTrue(dataTree.contains("lf11_1"));
331 assertTrue(dataTree.contains("lflst11"));
332 assertTrue(dataTree.contains("45"));
333 assertTrue(dataTree.contains("cont11"));
334 assertTrue(dataTree.contains("lst11"));
338 public void unsupportedDataFormatTest() throws NoSuchFieldException,
339 SecurityException, IllegalArgumentException,
340 IllegalAccessException, WebApplicationException, IOException {
341 jsonBodyReader = new JsonNormalizedNodeBodyReader();
342 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
343 mockBodyReader("simple-list-yang1:lst", jsonBodyReader, false);
345 final InputStream inputStream = this.getClass().getResourceAsStream(
346 "/json-to-nn/unsupported-json-format.json");
348 RestconfDocumentedException exception = null;
351 jsonBodyReader.readFrom(null, null, null, mediaType, null,
353 } catch (final RestconfDocumentedException e) {
356 System.out.println(exception.getErrors().get(0).getErrorMessage());
358 assertTrue(exception.getErrors().get(0).getErrorMessage()
359 .contains("is not a simple type"));
363 public void invalidUriCharacterInValue() throws NoSuchFieldException,
364 SecurityException, IllegalArgumentException,
365 IllegalAccessException, WebApplicationException, IOException {
367 jsonBodyReader = new JsonNormalizedNodeBodyReader();
368 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
369 mockBodyReader("array-with-null-yang:cont", jsonBodyReader, false);
371 final InputStream inputStream = this.getClass().getResourceAsStream(
372 "/json-to-nn/invalid-uri-character-in-value.json");
374 final NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom(
375 null, null, null, mediaType, null, inputStream);
376 assertNotNull(normalizedNodeContext);
378 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
381 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
383 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
384 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
388 protected MediaType getMediaType() {
389 // TODO Auto-generated method stub