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;
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 {
41 public static void initialize(final String path, SchemaContext schemaContext) {
42 schemaContext = schemaContextLoader(path, schemaContext);
43 CONTROLLER_CONTEXT.setSchemas(schemaContext);
47 public void simpleListTest() throws Exception {
48 simpleTest("/json-to-nn/simple-list.json",
49 "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
53 public void simpleContainerTest() throws Exception {
54 simpleTest("/json-to-nn/simple-container.json",
55 "/json-to-nn/simple-container-yang", "cont",
56 "simple-container-yang");
60 public void multipleItemsInLeafListTest() throws Exception {
62 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
64 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
65 "/json-to-nn/multiple-leaflist-items.json",
66 "simple-list-yang1:lst");
67 assertNotNull(normalizedNodeContext);
69 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
71 assertTrue(dataTree.contains("45"));
72 assertTrue(dataTree.contains("55"));
73 assertTrue(dataTree.contains("66"));
77 public void multipleItemsInListTest() throws Exception {
78 initialize("/json-to-nn/simple-list-yang/3", this.schemaContext);
80 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
81 "/json-to-nn/multiple-items-in-list.json",
82 "multiple-items-yang:lst");
83 assertNotNull(normalizedNodeContext);
85 assertEquals("lst", normalizedNodeContext.getData().getNodeType()
88 verityMultipleItemsInList(normalizedNodeContext);
92 public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
93 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
95 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
96 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
97 assertNotNull(normalizedNodeContext);
99 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
102 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
104 assertTrue(dataTree.contains("lf"));
105 assertTrue(dataTree.contains("null"));
109 public void incorrectTopLevelElementsTest() throws Exception {
111 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
112 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
113 mockBodyReader("simple-list-yang1:lst", this.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 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
124 } catch (final RestconfDocumentedException e) {
128 assertNotNull(exception);
130 "Error parsing input: Schema node with name cont wasn't found under "
131 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
132 exception.getErrors().get(0).getErrorMessage());
134 inputStream = this.getClass().getResourceAsStream(
135 "/json-to-nn/wrong-top-level2.json");
138 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
140 } catch (final RestconfDocumentedException e) {
144 assertNotNull(exception);
146 "Error parsing input: Schema node with name lst1 wasn't found under "
147 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
148 exception.getErrors().get(0).getErrorMessage());
150 inputStream = this.getClass().getResourceAsStream(
151 "/json-to-nn/wrong-top-level3.json");
154 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
156 } catch (final RestconfDocumentedException e) {
160 assertNotNull(exception);
162 "Error parsing input: Schema node with name lf wasn't found under "
163 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
164 exception.getErrors().get(0).getErrorMessage());
165 assertEquals(3, countExceptions);
169 public void emptyDataReadTest() throws Exception {
171 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
173 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
174 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
175 assertNotNull(normalizedNodeContext);
177 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
180 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
183 assertTrue(dataTree.contains("lflst1"));
185 assertTrue(dataTree.contains("lflst2 45"));
187 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
188 RestconfDocumentedException exception = null;
189 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
190 final InputStream inputStream = this.getClass().getResourceAsStream(
191 "/json-to-nn/empty-data.json1");
194 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
196 } catch (final RestconfDocumentedException e) {
199 assertNotNull(exception);
200 assertEquals("Error parsing input: null", exception.getErrors().get(0)
205 public void testJsonBlankInput() throws Exception {
206 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
207 final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
208 "array-with-null-yang:cont");
209 assertNull(normalizedNodeContext);
213 public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
215 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
217 final String uri = "simple-list-yang1" + ":" + "lst";
219 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
220 "/json-to-nn/simple-list.json", uri);
221 assertNotNull(normalizedNodeContext);
223 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
225 mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
226 final InputStream inputStream = this.getClass().getResourceAsStream(
227 "/json-to-nn/simple-list.json");
230 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
232 fail("NormalizedNodeContext should not be create because of different namespace");
233 } catch (final RestconfDocumentedException e) {
234 LOG.warn("Read from InputStream failed. Message: {}. Status: {}", e.getMessage(), e.getStatus());
237 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
241 public void dataAugmentedTest() throws Exception {
243 initialize("/common/augment/yang", this.schemaContext);
245 NormalizedNodeContext normalizedNodeContext = prepareNNC(
246 "/common/augment/json/dataa.json", "main:cont");
248 assertNotNull(normalizedNodeContext);
249 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
252 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
254 assertTrue(dataTree.contains("cont1"));
255 assertTrue(dataTree.contains("lf11 lf11 value from a"));
257 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
260 assertNotNull(normalizedNodeContext);
261 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
263 dataTree = NormalizedNodes
264 .toStringTree(normalizedNodeContext.getData());
265 assertTrue(dataTree.contains("cont1"));
266 assertTrue(dataTree.contains("lf11 lf11 value from b"));
269 private void simpleTest(final String jsonPath, final String yangPath,
270 final String topLevelElementName, final String moduleName) throws Exception {
272 initialize(yangPath, this.schemaContext);
274 final String uri = moduleName + ":" + topLevelElementName;
276 final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
277 assertNotNull(normalizedNodeContext);
279 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
282 private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
283 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
285 mockBodyReader(uri, this.jsonBodyReader, false);
286 } catch (NoSuchFieldException | SecurityException
287 | IllegalArgumentException | IllegalAccessException e) {
288 LOG.warn("Operation failed due to: {}", e.getMessage());
290 final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
292 NormalizedNodeContext normalizedNodeContext = null;
295 normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
296 this.mediaType, null, inputStream);
297 } catch (WebApplicationException | IOException e) {
298 // TODO Auto-generated catch block
301 return normalizedNodeContext;
304 private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
305 final String topLevelElementName) {
306 assertEquals(topLevelElementName, normalizedNodeContext.getData()
307 .getNodeType().getLocalName());
309 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
311 assertTrue(dataTree.contains("cont1"));
312 assertTrue(dataTree.contains("lst1"));
313 assertTrue(dataTree.contains("lflst1"));
314 assertTrue(dataTree.contains("lflst1_1"));
315 assertTrue(dataTree.contains("lflst1_2"));
316 assertTrue(dataTree.contains("lf1"));
319 private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
321 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
323 assertTrue(dataTree.contains("lf11"));
324 assertTrue(dataTree.contains("lf11_1"));
325 assertTrue(dataTree.contains("lflst11"));
326 assertTrue(dataTree.contains("45"));
327 assertTrue(dataTree.contains("cont11"));
328 assertTrue(dataTree.contains("lst11"));
332 public void unsupportedDataFormatTest() throws Exception {
333 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
334 initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
335 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
337 final InputStream inputStream = this.getClass().getResourceAsStream(
338 "/json-to-nn/unsupported-json-format.json");
340 RestconfDocumentedException exception = null;
343 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
345 } catch (final RestconfDocumentedException e) {
348 LOG.info(exception.getErrors().get(0).getErrorMessage());
350 assertTrue(exception.getErrors().get(0).getErrorMessage()
351 .contains("is not a simple type"));
355 public void invalidUriCharacterInValue() throws Exception {
357 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
358 initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
359 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
361 final InputStream inputStream = this.getClass().getResourceAsStream(
362 "/json-to-nn/invalid-uri-character-in-value.json");
364 final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
365 null, null, null, this.mediaType, null, inputStream);
366 assertNotNull(normalizedNodeContext);
368 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
371 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
373 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
374 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
378 protected MediaType getMediaType() {