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;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Collection;
21 import javax.ws.rs.WebApplicationException;
22 import javax.ws.rs.core.MediaType;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
26 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
27 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
28 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class JsonToNnTest extends AbstractBodyReaderTest {
38 private static final Logger LOG = LoggerFactory.getLogger(AbstractBodyReaderTest.class);
40 private final JsonNormalizedNodeBodyReader jsonBodyReader;
41 private static SchemaContext schemaContext;
43 public JsonToNnTest() {
44 super(schemaContext, null);
45 this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
49 public static void initialize() throws FileNotFoundException {
50 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/1");
51 testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/3"));
52 testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/4"));
53 testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-container-yang"));
54 testFiles.addAll(TestRestconfUtils.loadFiles("/common/augment/yang"));
55 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
59 public void simpleListTest() throws Exception {
60 simpleTest("/json-to-nn/simple-list.json",
61 "lst", "simple-list-yang1");
65 public void simpleContainerTest() throws Exception {
66 simpleTest("/json-to-nn/simple-container.json",
67 "cont", "simple-container-yang");
71 public void multipleItemsInLeafListTest() throws Exception {
72 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
73 "/json-to-nn/multiple-leaflist-items.json",
74 "simple-list-yang1:lst");
75 assertNotNull(normalizedNodeContext);
77 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
79 assertTrue(dataTree.contains("45"));
80 assertTrue(dataTree.contains("55"));
81 assertTrue(dataTree.contains("66"));
85 public void multipleItemsInListTest() throws Exception {
86 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
87 "/json-to-nn/multiple-items-in-list.json",
88 "multiple-items-yang:lst");
89 assertNotNull(normalizedNodeContext);
91 assertEquals("lst", normalizedNodeContext.getData().getNodeType()
94 verityMultipleItemsInList(normalizedNodeContext);
98 public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
99 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
100 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
101 assertNotNull(normalizedNodeContext);
103 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
106 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
108 assertTrue(dataTree.contains("lf"));
109 assertTrue(dataTree.contains("empty"));
113 public void incorrectTopLevelElementsTest() throws Exception {
114 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
116 InputStream inputStream = this.getClass().getResourceAsStream(
117 "/json-to-nn/wrong-top-level1.json");
119 int countExceptions = 0;
120 RestconfDocumentedException exception = null;
123 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
125 } catch (final RestconfDocumentedException e) {
129 assertNotNull(exception);
131 "Error parsing input: Schema node with name wrong was not found under "
132 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
133 exception.getErrors().get(0).getErrorMessage());
135 inputStream = this.getClass().getResourceAsStream(
136 "/json-to-nn/wrong-top-level2.json");
139 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
141 } catch (final RestconfDocumentedException e) {
145 assertNotNull(exception);
147 "Error parsing input: Schema node with name lst1 was not found under "
148 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
149 exception.getErrors().get(0).getErrorMessage());
151 inputStream = this.getClass().getResourceAsStream(
152 "/json-to-nn/wrong-top-level3.json");
155 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
157 } catch (final RestconfDocumentedException e) {
161 assertNotNull(exception);
163 "Error parsing input: Schema node with name lf was not found under "
164 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
165 exception.getErrors().get(0).getErrorMessage());
166 assertEquals(3, countExceptions);
170 public void emptyDataReadTest() throws Exception {
171 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
172 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
173 assertNotNull(normalizedNodeContext);
175 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
178 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
181 assertTrue(dataTree.contains("lflst1"));
183 assertTrue(dataTree.contains("lflst2 45"));
185 RestconfDocumentedException exception = null;
186 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
187 final InputStream inputStream = this.getClass().getResourceAsStream(
188 "/json-to-nn/empty-data.json1");
191 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
193 } catch (final RestconfDocumentedException e) {
196 assertNotNull(exception);
197 assertEquals("Error parsing input: null", exception.getErrors().get(0)
202 public void testJsonBlankInput() throws Exception {
203 final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
204 "array-with-null-yang:cont");
205 assertNull(normalizedNodeContext);
209 public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
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) {
227 LOG.warn("Read from InputStream failed. Message: {}. Status: {}", e.getMessage(), e.getStatus());
230 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
234 public void dataAugmentedTest() throws Exception {
235 NormalizedNodeContext normalizedNodeContext = prepareNNC(
236 "/common/augment/json/dataa.json", "main:cont");
238 assertNotNull(normalizedNodeContext);
239 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
242 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
244 assertTrue(dataTree.contains("cont1"));
245 assertTrue(dataTree.contains("lf11 lf11 value from a"));
247 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
250 assertNotNull(normalizedNodeContext);
251 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
253 dataTree = NormalizedNodes
254 .toStringTree(normalizedNodeContext.getData());
255 assertTrue(dataTree.contains("cont1"));
256 assertTrue(dataTree.contains("lf11 lf11 value from b"));
259 private void simpleTest(final String jsonPath, final String topLevelElementName,
260 final String moduleName) throws Exception {
261 final String uri = moduleName + ":" + topLevelElementName;
263 final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
264 assertNotNull(normalizedNodeContext);
266 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
269 private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
271 mockBodyReader(uri, this.jsonBodyReader, false);
272 } catch (NoSuchFieldException | SecurityException
273 | IllegalArgumentException | IllegalAccessException e) {
274 LOG.warn("Operation failed due to: {}", e.getMessage());
276 final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
278 NormalizedNodeContext normalizedNodeContext = null;
281 normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
282 this.mediaType, null, inputStream);
283 } catch (WebApplicationException | IOException e) {
284 // TODO Auto-generated catch block
287 return normalizedNodeContext;
290 private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
291 final String topLevelElementName) {
292 assertEquals(topLevelElementName, normalizedNodeContext.getData()
293 .getNodeType().getLocalName());
295 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
297 assertTrue(dataTree.contains("cont1"));
298 assertTrue(dataTree.contains("lst1"));
299 assertTrue(dataTree.contains("lflst1"));
300 assertTrue(dataTree.contains("lflst1_1"));
301 assertTrue(dataTree.contains("lflst1_2"));
302 assertTrue(dataTree.contains("lf1"));
305 private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
307 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
309 assertTrue(dataTree.contains("lf11"));
310 assertTrue(dataTree.contains("lf11_1"));
311 assertTrue(dataTree.contains("lflst11"));
312 assertTrue(dataTree.contains("45"));
313 assertTrue(dataTree.contains("cont11"));
314 assertTrue(dataTree.contains("lst11"));
318 public void unsupportedDataFormatTest() throws Exception {
319 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
321 final InputStream inputStream = this.getClass().getResourceAsStream(
322 "/json-to-nn/unsupported-json-format.json");
324 RestconfDocumentedException exception = null;
327 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
329 } catch (final RestconfDocumentedException e) {
332 LOG.info(exception.getErrors().get(0).getErrorMessage());
334 assertTrue(exception.getErrors().get(0).getErrorMessage()
335 .contains("is not a simple type"));
339 public void invalidUriCharacterInValue() throws Exception {
340 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
342 final InputStream inputStream = this.getClass().getResourceAsStream(
343 "/json-to-nn/invalid-uri-character-in-value.json");
345 final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
346 null, null, null, this.mediaType, null, inputStream);
347 assertNotNull(normalizedNodeContext);
349 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
352 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
354 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
355 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
359 protected MediaType getMediaType() {