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.InputStream;
19 import java.util.Collection;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
26 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class JsonToNnTest extends AbstractBodyReaderTest {
37 private static final Logger LOG = LoggerFactory.getLogger(AbstractBodyReaderTest.class);
39 private final JsonNormalizedNodeBodyReader jsonBodyReader;
40 private static SchemaContext schemaContext;
42 public JsonToNnTest() {
43 super(schemaContext, null);
44 this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
48 public static void initialize() throws FileNotFoundException {
49 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/1");
50 testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/3"));
51 testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/4"));
52 testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-container-yang"));
53 testFiles.addAll(TestRestconfUtils.loadFiles("/common/augment/yang"));
54 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
58 public void simpleListTest() throws Exception {
59 simpleTest("/json-to-nn/simple-list.json",
60 "lst", "simple-list-yang1");
64 public void simpleContainerTest() throws Exception {
65 simpleTest("/json-to-nn/simple-container.json",
66 "cont", "simple-container-yang");
70 public void multipleItemsInLeafListTest() throws Exception {
71 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
72 "/json-to-nn/multiple-leaflist-items.json",
73 "simple-list-yang1:lst");
74 assertNotNull(normalizedNodeContext);
76 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
78 assertTrue(dataTree.contains("45"));
79 assertTrue(dataTree.contains("55"));
80 assertTrue(dataTree.contains("66"));
84 public void multipleItemsInListTest() throws Exception {
85 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
86 "/json-to-nn/multiple-items-in-list.json",
87 "multiple-items-yang:lst");
88 assertNotNull(normalizedNodeContext);
90 assertEquals("lst", normalizedNodeContext.getData().getNodeType()
93 verityMultipleItemsInList(normalizedNodeContext);
97 public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
98 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
99 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
100 assertNotNull(normalizedNodeContext);
102 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
105 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
107 assertTrue(dataTree.contains("lf"));
108 assertTrue(dataTree.contains("empty"));
112 public void incorrectTopLevelElementsTest() throws Exception {
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 wrong was not 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 was not 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 was not 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 {
170 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
171 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
172 assertNotNull(normalizedNodeContext);
174 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
177 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
180 assertTrue(dataTree.contains("lflst1"));
182 assertTrue(dataTree.contains("lflst2 45"));
184 RestconfDocumentedException exception = null;
185 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
186 final InputStream inputStream = this.getClass().getResourceAsStream(
187 "/json-to-nn/empty-data.json1");
190 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
192 } catch (final RestconfDocumentedException e) {
195 assertNotNull(exception);
196 assertEquals("Error parsing input: null", exception.getErrors().get(0)
201 public void testJsonBlankInput() throws Exception {
202 final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
203 "array-with-null-yang:cont");
204 assertNull(normalizedNodeContext);
208 public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
209 final String uri = "simple-list-yang1" + ":" + "lst";
211 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
212 "/json-to-nn/simple-list.json", uri);
213 assertNotNull(normalizedNodeContext);
215 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
217 mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
218 final InputStream inputStream = this.getClass().getResourceAsStream(
219 "/json-to-nn/simple-list.json");
222 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
224 fail("NormalizedNodeContext should not be create because of different namespace");
225 } catch (final RestconfDocumentedException e) {
226 LOG.warn("Read from InputStream failed. Message: {}. Status: {}", e.getMessage(), e.getStatus());
229 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
233 public void dataAugmentedTest() throws Exception {
234 NormalizedNodeContext normalizedNodeContext = prepareNNC(
235 "/common/augment/json/dataa.json", "main:cont");
237 assertNotNull(normalizedNodeContext);
238 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
241 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
243 assertTrue(dataTree.contains("cont1"));
244 assertTrue(dataTree.contains("lf11 lf11 value from a"));
246 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
249 assertNotNull(normalizedNodeContext);
250 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
252 dataTree = NormalizedNodes
253 .toStringTree(normalizedNodeContext.getData());
254 assertTrue(dataTree.contains("cont1"));
255 assertTrue(dataTree.contains("lf11 lf11 value from b"));
258 private void simpleTest(final String jsonPath, final String topLevelElementName,
259 final String moduleName) throws Exception {
260 final String uri = moduleName + ":" + topLevelElementName;
262 final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
263 assertNotNull(normalizedNodeContext);
265 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
268 private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
270 mockBodyReader(uri, this.jsonBodyReader, false);
271 } catch (NoSuchFieldException | SecurityException
272 | IllegalArgumentException | IllegalAccessException e) {
273 LOG.warn("Operation failed due to: {}", e.getMessage());
275 final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
277 NormalizedNodeContext normalizedNodeContext = null;
280 normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
281 this.mediaType, null, inputStream);
282 } catch (WebApplicationException e) {
283 // TODO Auto-generated catch block
286 return normalizedNodeContext;
289 private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
290 final String topLevelElementName) {
291 assertEquals(topLevelElementName, normalizedNodeContext.getData()
292 .getNodeType().getLocalName());
294 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
296 assertTrue(dataTree.contains("cont1"));
297 assertTrue(dataTree.contains("lst1"));
298 assertTrue(dataTree.contains("lflst1"));
299 assertTrue(dataTree.contains("lflst1_1"));
300 assertTrue(dataTree.contains("lflst1_2"));
301 assertTrue(dataTree.contains("lf1"));
304 private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
306 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
308 assertTrue(dataTree.contains("lf11"));
309 assertTrue(dataTree.contains("lf11_1"));
310 assertTrue(dataTree.contains("lflst11"));
311 assertTrue(dataTree.contains("45"));
312 assertTrue(dataTree.contains("cont11"));
313 assertTrue(dataTree.contains("lst11"));
317 public void unsupportedDataFormatTest() throws Exception {
318 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
320 final InputStream inputStream = this.getClass().getResourceAsStream(
321 "/json-to-nn/unsupported-json-format.json");
323 RestconfDocumentedException exception = null;
326 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
328 } catch (final RestconfDocumentedException e) {
331 LOG.info(exception.getErrors().get(0).getErrorMessage());
333 assertTrue(exception.getErrors().get(0).getErrorMessage()
334 .contains("is not a simple type"));
338 public void invalidUriCharacterInValue() throws Exception {
339 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
341 final InputStream inputStream = this.getClass().getResourceAsStream(
342 "/json-to-nn/invalid-uri-character-in-value.json");
344 final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
345 null, null, null, this.mediaType, null, inputStream);
346 assertNotNull(normalizedNodeContext);
348 assertEquals("cont", normalizedNodeContext.getData().getNodeType()
351 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
353 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
354 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
358 protected MediaType getMediaType() {