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.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.MediaType;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
23 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
24 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
25 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 public class JsonToNnTest extends AbstractBodyReaderTest {
31 private JsonNormalizedNodeBodyReader jsonBodyReader;
32 private SchemaContext schemaContext;
34 public JsonToNnTest() throws NoSuchFieldException, SecurityException {
38 public static void initialize(String path, SchemaContext schemaContext) {
39 schemaContext = schemaContextLoader(path, schemaContext);
40 controllerContext.setSchemas(schemaContext);
44 public void simpleListTest() {
45 simpleTest("/json-to-nn/simple-list.json",
46 "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
50 public void simpleContainerTest() {
51 simpleTest("/json-to-nn/simple-container.json",
52 "/json-to-nn/simple-container-yang", "cont",
53 "simple-container-yang");
57 public void multipleItemsInLeafListTest() {
59 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
61 NormalizedNodeContext normalizedNodeContext = prepareNNC(
62 "/json-to-nn/multiple-leaflist-items.json",
63 "simple-list-yang1:lst");
64 assertNotNull(normalizedNodeContext);
66 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
68 assertTrue(dataTree.contains("45"));
69 assertTrue(dataTree.contains("55"));
70 assertTrue(dataTree.contains("66"));
74 public void multipleItemsInListTest() {
75 initialize("/json-to-nn/simple-list-yang/3", schemaContext);
77 NormalizedNodeContext normalizedNodeContext = prepareNNC(
78 "/json-to-nn/multiple-items-in-list.json",
79 "multiple-items-yang:lst");
80 assertNotNull(normalizedNodeContext);
82 assertEquals("lst", normalizedNodeContext.getData().getNodeType()
85 verityMultipleItemsInList(normalizedNodeContext);
89 public void nullArrayToSimpleNodeWithNullValueTest() {
90 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
92 NormalizedNodeContext normalizedNodeContext = prepareNNC(
93 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
94 assertNotNull(normalizedNodeContext);
96 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
99 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
101 assertTrue(dataTree.contains("lf"));
102 assertTrue(dataTree.contains("null"));
106 public void incorrectTopLevelElementsTest() throws WebApplicationException,
107 IOException, NoSuchFieldException, SecurityException,
108 IllegalArgumentException, IllegalAccessException {
110 jsonBodyReader = new JsonNormalizedNodeBodyReader();
111 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
112 mockBodyReader("simple-list-yang1:lst", jsonBodyReader, false);
114 InputStream inputStream = this.getClass().getResourceAsStream(
115 "/json-to-nn/wrong-top-level1.json");
117 int countExceptions = 0;
118 RestconfDocumentedException exception = null;
121 jsonBodyReader.readFrom(null, null, null, mediaType, null,
123 } catch (RestconfDocumentedException e) {
127 assertNotNull(exception);
128 assertThat(exception.getErrors().get(0).getErrorMessage(),
129 containsString("Error parsing input: Schema node with name cont wasn't found"));
131 inputStream = this.getClass().getResourceAsStream(
132 "/json-to-nn/wrong-top-level2.json");
135 jsonBodyReader.readFrom(null, null, null, mediaType, null,
137 } catch (RestconfDocumentedException e) {
141 assertNotNull(exception);
142 assertThat(exception.getErrors().get(0).getErrorMessage(),
143 containsString("Error parsing input: Schema node with name lst1 wasn't found"));
145 inputStream = this.getClass().getResourceAsStream(
146 "/json-to-nn/wrong-top-level3.json");
149 jsonBodyReader.readFrom(null, null, null, mediaType, null,
151 } catch (RestconfDocumentedException e) {
155 assertNotNull(exception);
156 assertThat(exception.getErrors().get(0).getErrorMessage(),
157 containsString("Error parsing input: Schema node with name lf wasn't found"));
158 assertEquals(3, countExceptions);
162 public void emptyDataReadTest() throws WebApplicationException,
163 IOException, NoSuchFieldException, SecurityException,
164 IllegalArgumentException, IllegalAccessException {
166 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
168 NormalizedNodeContext normalizedNodeContext = prepareNNC(
169 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
170 assertNotNull(normalizedNodeContext);
172 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
175 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
178 assertTrue(dataTree.contains("lflst1"));
180 assertTrue(dataTree.contains("lflst2 45"));
182 jsonBodyReader = new JsonNormalizedNodeBodyReader();
183 RestconfDocumentedException exception = null;
184 mockBodyReader("array-with-null-yang:cont", jsonBodyReader, false);
185 InputStream inputStream = this.getClass().getResourceAsStream(
186 "/json-to-nn/empty-data.json1");
189 jsonBodyReader.readFrom(null, null, null, mediaType, null,
191 } catch (RestconfDocumentedException e) {
194 assertNotNull(exception);
195 assertEquals("Error parsing input: null", exception.getErrors().get(0)
200 public void testJsonBlankInput() throws NoSuchFieldException,
201 SecurityException, IllegalArgumentException,
202 IllegalAccessException, WebApplicationException, IOException {
203 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
204 NormalizedNodeContext normalizedNodeContext = prepareNNC("",
205 "array-with-null-yang:cont");
206 assertNull(normalizedNodeContext);
210 public void notSupplyNamespaceIfAlreadySupplied()
211 throws WebApplicationException, IOException, NoSuchFieldException,
212 SecurityException, IllegalArgumentException, IllegalAccessException {
214 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
216 String uri = "simple-list-yang1" + ":" + "lst";
218 NormalizedNodeContext normalizedNodeContext = prepareNNC(
219 "/json-to-nn/simple-list.json", uri);
220 assertNotNull(normalizedNodeContext);
222 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
224 mockBodyReader("simple-list-yang2:lst", jsonBodyReader, false);
225 InputStream inputStream = this.getClass().getResourceAsStream(
226 "/json-to-nn/simple-list.json");
229 jsonBodyReader.readFrom(null, null, null, mediaType, null,
231 fail("NormalizedNodeContext should not be create because of different namespace");
232 } catch (RestconfDocumentedException e) {
235 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
239 public void dataAugmentedTest() {
241 initialize("/common/augment/yang", schemaContext);
243 NormalizedNodeContext normalizedNodeContext = prepareNNC(
244 "/common/augment/json/dataa.json", "main:cont");
246 assertNotNull(normalizedNodeContext);
247 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
250 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
252 assertTrue(dataTree.contains("cont1"));
253 assertTrue(dataTree.contains("lf11 lf11 value from a"));
255 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
258 assertNotNull(normalizedNodeContext);
259 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
261 dataTree = NormalizedNodes
262 .toStringTree(normalizedNodeContext.getData());
263 assertTrue(dataTree.contains("cont1"));
264 assertTrue(dataTree.contains("lf11 lf11 value from b"));
267 private void simpleTest(final String jsonPath, final String yangPath,
268 final String topLevelElementName, final String moduleName) {
270 initialize(yangPath, schemaContext);
272 String uri = moduleName + ":" + topLevelElementName;
274 NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
275 assertNotNull(normalizedNodeContext);
277 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
280 private NormalizedNodeContext prepareNNC(String jsonPath, String uri) {
281 jsonBodyReader = new JsonNormalizedNodeBodyReader();
283 mockBodyReader(uri, jsonBodyReader, false);
284 } catch (NoSuchFieldException | SecurityException
285 | IllegalArgumentException | IllegalAccessException e) {
286 // TODO Auto-generated catch block
289 InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
291 NormalizedNodeContext normalizedNodeContext = null;
294 normalizedNodeContext = jsonBodyReader.readFrom(null, null, null,
295 mediaType, null, inputStream);
296 } catch (WebApplicationException | IOException e) {
297 // TODO Auto-generated catch block
301 return normalizedNodeContext;
304 private void verifyNormaluizedNodeContext(
305 NormalizedNodeContext normalizedNodeContext,
306 String topLevelElementName) {
307 assertEquals(topLevelElementName, normalizedNodeContext.getData()
308 .getNodeType().getLocalName());
310 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
312 assertTrue(dataTree.contains("cont1"));
313 assertTrue(dataTree.contains("lst1"));
314 assertTrue(dataTree.contains("lflst1"));
315 assertTrue(dataTree.contains("lflst1_1"));
316 assertTrue(dataTree.contains("lflst1_2"));
317 assertTrue(dataTree.contains("lf1"));
320 private void verityMultipleItemsInList(
321 final NormalizedNodeContext normalizedNodeContext) {
323 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
325 assertTrue(dataTree.contains("lf11"));
326 assertTrue(dataTree.contains("lf11_1"));
327 assertTrue(dataTree.contains("lflst11"));
328 assertTrue(dataTree.contains("45"));
329 assertTrue(dataTree.contains("cont11"));
330 assertTrue(dataTree.contains("lst11"));
334 public void unsupportedDataFormatTest() throws NoSuchFieldException,
335 SecurityException, IllegalArgumentException,
336 IllegalAccessException, WebApplicationException, IOException {
337 jsonBodyReader = new JsonNormalizedNodeBodyReader();
338 initialize("/json-to-nn/simple-list-yang/1", schemaContext);
339 mockBodyReader("simple-list-yang1:lst", jsonBodyReader, false);
341 InputStream inputStream = this.getClass().getResourceAsStream(
342 "/json-to-nn/unsupported-json-format.json");
344 RestconfDocumentedException exception = null;
347 jsonBodyReader.readFrom(null, null, null, mediaType, null,
349 } catch (RestconfDocumentedException e) {
352 System.out.println(exception.getErrors().get(0).getErrorMessage());
354 assertTrue(exception.getErrors().get(0).getErrorMessage()
355 .contains("is not a simple type"));
359 public void invalidUriCharacterInValue() throws NoSuchFieldException,
360 SecurityException, IllegalArgumentException,
361 IllegalAccessException, WebApplicationException, IOException {
363 jsonBodyReader = new JsonNormalizedNodeBodyReader();
364 initialize("/json-to-nn/simple-list-yang/4", schemaContext);
365 mockBodyReader("array-with-null-yang:cont", jsonBodyReader, false);
367 InputStream inputStream = this.getClass().getResourceAsStream(
368 "/json-to-nn/invalid-uri-character-in-value.json");
370 NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom(
371 null, null, null, mediaType, null, inputStream);
372 assertNotNull(normalizedNodeContext);
374 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
377 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
379 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
380 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
384 protected MediaType getMediaType() {
385 // TODO Auto-generated method stub