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.restconf.common.context.NormalizedNodeContext;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class JsonToNnTest extends AbstractBodyReaderTest {
32 private static final Logger LOG = LoggerFactory.getLogger(AbstractBodyReaderTest.class);
34 private JsonNormalizedNodeBodyReader jsonBodyReader;
35 private SchemaContext schemaContext;
37 public JsonToNnTest() throws NoSuchFieldException, SecurityException {
40 public static void initialize(final String path, SchemaContext schemaContext) {
41 schemaContext = schemaContextLoader(path, schemaContext);
42 CONTROLLER_CONTEXT.setSchemas(schemaContext);
46 public void simpleListTest() throws Exception {
47 simpleTest("/json-to-nn/simple-list.json",
48 "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
52 public void simpleContainerTest() throws Exception {
53 simpleTest("/json-to-nn/simple-container.json",
54 "/json-to-nn/simple-container-yang", "cont",
55 "simple-container-yang");
59 public void multipleItemsInLeafListTest() throws Exception {
61 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
63 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
64 "/json-to-nn/multiple-leaflist-items.json",
65 "simple-list-yang1:lst");
66 assertNotNull(normalizedNodeContext);
68 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
70 assertTrue(dataTree.contains("45"));
71 assertTrue(dataTree.contains("55"));
72 assertTrue(dataTree.contains("66"));
76 public void multipleItemsInListTest() throws Exception {
77 initialize("/json-to-nn/simple-list-yang/3", this.schemaContext);
79 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
80 "/json-to-nn/multiple-items-in-list.json",
81 "multiple-items-yang:lst");
82 assertNotNull(normalizedNodeContext);
84 assertEquals("lst", normalizedNodeContext.getData().getNodeType()
87 verityMultipleItemsInList(normalizedNodeContext);
91 public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
92 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
94 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
95 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
96 assertNotNull(normalizedNodeContext);
98 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
101 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
103 assertTrue(dataTree.contains("lf"));
104 assertTrue(dataTree.contains("null"));
108 public void incorrectTopLevelElementsTest() throws Exception {
110 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
111 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
112 mockBodyReader("simple-list-yang1:lst", this.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 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
123 } catch (final RestconfDocumentedException e) {
127 assertNotNull(exception);
129 "Error parsing input: Schema node with name cont wasn't found under "
130 + "(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 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
139 } catch (final RestconfDocumentedException e) {
143 assertNotNull(exception);
145 "Error parsing input: Schema node with name lst1 wasn't found under "
146 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
147 exception.getErrors().get(0).getErrorMessage());
149 inputStream = this.getClass().getResourceAsStream(
150 "/json-to-nn/wrong-top-level3.json");
153 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
155 } catch (final RestconfDocumentedException e) {
159 assertNotNull(exception);
161 "Error parsing input: Schema node with name lf wasn't found under "
162 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
163 exception.getErrors().get(0).getErrorMessage());
164 assertEquals(3, countExceptions);
168 public void emptyDataReadTest() throws Exception {
170 initialize("/json-to-nn/simple-list-yang/4", this.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 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
187 RestconfDocumentedException exception = null;
188 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
189 final InputStream inputStream = this.getClass().getResourceAsStream(
190 "/json-to-nn/empty-data.json1");
193 this.jsonBodyReader.readFrom(null, null, null, this.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 Exception {
205 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
206 final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
207 "array-with-null-yang:cont");
208 assertNull(normalizedNodeContext);
212 public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
214 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
216 final String uri = "simple-list-yang1" + ":" + "lst";
218 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
219 "/json-to-nn/simple-list.json", uri);
220 assertNotNull(normalizedNodeContext);
222 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
224 mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
225 final InputStream inputStream = this.getClass().getResourceAsStream(
226 "/json-to-nn/simple-list.json");
229 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
231 fail("NormalizedNodeContext should not be create because of different namespace");
232 } catch (final RestconfDocumentedException e) {
233 LOG.warn("Read from InputStream failed. Message: {}. Status: {}", e.getMessage(), e.getStatus());
236 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
240 public void dataAugmentedTest() throws Exception {
242 initialize("/common/augment/yang", this.schemaContext);
244 NormalizedNodeContext normalizedNodeContext = prepareNNC(
245 "/common/augment/json/dataa.json", "main:cont");
247 assertNotNull(normalizedNodeContext);
248 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
251 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
253 assertTrue(dataTree.contains("cont1"));
254 assertTrue(dataTree.contains("lf11 lf11 value from a"));
256 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
259 assertNotNull(normalizedNodeContext);
260 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
262 dataTree = NormalizedNodes
263 .toStringTree(normalizedNodeContext.getData());
264 assertTrue(dataTree.contains("cont1"));
265 assertTrue(dataTree.contains("lf11 lf11 value from b"));
268 private void simpleTest(final String jsonPath, final String yangPath,
269 final String topLevelElementName, final String moduleName) throws Exception {
271 initialize(yangPath, this.schemaContext);
273 final String uri = moduleName + ":" + topLevelElementName;
275 final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
276 assertNotNull(normalizedNodeContext);
278 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
281 private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
282 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
284 mockBodyReader(uri, this.jsonBodyReader, false);
285 } catch (NoSuchFieldException | SecurityException
286 | IllegalArgumentException | IllegalAccessException e) {
287 LOG.warn("Operation failed due to: {}", e.getMessage());
289 final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
291 NormalizedNodeContext normalizedNodeContext = null;
294 normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
295 this.mediaType, null, inputStream);
296 } catch (WebApplicationException | IOException e) {
297 // TODO Auto-generated catch block
300 return normalizedNodeContext;
303 private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
304 final String topLevelElementName) {
305 assertEquals(topLevelElementName, normalizedNodeContext.getData()
306 .getNodeType().getLocalName());
308 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
310 assertTrue(dataTree.contains("cont1"));
311 assertTrue(dataTree.contains("lst1"));
312 assertTrue(dataTree.contains("lflst1"));
313 assertTrue(dataTree.contains("lflst1_1"));
314 assertTrue(dataTree.contains("lflst1_2"));
315 assertTrue(dataTree.contains("lf1"));
318 private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
320 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
322 assertTrue(dataTree.contains("lf11"));
323 assertTrue(dataTree.contains("lf11_1"));
324 assertTrue(dataTree.contains("lflst11"));
325 assertTrue(dataTree.contains("45"));
326 assertTrue(dataTree.contains("cont11"));
327 assertTrue(dataTree.contains("lst11"));
331 public void unsupportedDataFormatTest() throws Exception {
332 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
333 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
334 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
336 final InputStream inputStream = this.getClass().getResourceAsStream(
337 "/json-to-nn/unsupported-json-format.json");
339 RestconfDocumentedException exception = null;
342 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
344 } catch (final RestconfDocumentedException e) {
347 LOG.info(exception.getErrors().get(0).getErrorMessage());
349 assertTrue(exception.getErrors().get(0).getErrorMessage()
350 .contains("is not a simple type"));
354 public void invalidUriCharacterInValue() throws Exception {
356 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
357 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
358 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
360 final InputStream inputStream = this.getClass().getResourceAsStream(
361 "/json-to-nn/invalid-uri-character-in-value.json");
363 final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
364 null, null, null, this.mediaType, null, inputStream);
365 assertNotNull(normalizedNodeContext);
367 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
370 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
372 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
373 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
377 protected MediaType getMediaType() {