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.netconf.sal.rest.impl.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.EffectiveModelContext;
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 EffectiveModelContext 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().getIdentifier().getNodeType().getLocalName());
92 verityMultipleItemsInList(normalizedNodeContext);
96 public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
97 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
98 "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
99 assertNotNull(normalizedNodeContext);
101 assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
103 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
104 assertTrue(dataTree.contains("lf"));
105 assertTrue(dataTree.contains("empty"));
109 public void incorrectTopLevelElementsTest() throws Exception {
110 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
112 InputStream inputStream = this.getClass().getResourceAsStream(
113 "/json-to-nn/wrong-top-level1.json");
115 int countExceptions = 0;
116 RestconfDocumentedException exception = null;
119 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
121 } catch (final RestconfDocumentedException e) {
125 assertNotNull(exception);
127 "Error parsing input: Schema node with name wrong was not found under "
128 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
129 exception.getErrors().get(0).getErrorMessage());
131 inputStream = this.getClass().getResourceAsStream(
132 "/json-to-nn/wrong-top-level2.json");
135 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
137 } catch (final RestconfDocumentedException e) {
141 assertNotNull(exception);
143 "Error parsing input: Schema node with name lst1 was not found under "
144 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
145 exception.getErrors().get(0).getErrorMessage());
147 inputStream = this.getClass().getResourceAsStream(
148 "/json-to-nn/wrong-top-level3.json");
151 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
153 } catch (final RestconfDocumentedException e) {
157 assertNotNull(exception);
159 "Error parsing input: Schema node with name lf was not found under "
160 + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
161 exception.getErrors().get(0).getErrorMessage());
162 assertEquals(3, countExceptions);
166 public void emptyDataReadTest() throws Exception {
167 final NormalizedNodeContext normalizedNodeContext = prepareNNC(
168 "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
169 assertNotNull(normalizedNodeContext);
171 assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
173 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
175 assertTrue(dataTree.contains("lflst1"));
177 assertTrue(dataTree.contains("lflst2 45"));
179 RestconfDocumentedException exception = null;
180 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
181 final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/empty-data.json1");
184 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,inputStream);
185 } catch (final RestconfDocumentedException e) {
188 assertNotNull(exception);
189 assertEquals("Error parsing input: null", exception.getErrors().get(0).getErrorMessage());
193 public void testJsonBlankInput() throws Exception {
194 final NormalizedNodeContext normalizedNodeContext = prepareNNC("", "array-with-null-yang:cont");
195 assertNull(normalizedNodeContext);
199 public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
200 final String uri = "simple-list-yang1" + ":" + "lst";
202 final NormalizedNodeContext normalizedNodeContext = prepareNNC("/json-to-nn/simple-list.json", uri);
203 assertNotNull(normalizedNodeContext);
205 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
207 mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
208 final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/simple-list.json");
211 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
212 fail("NormalizedNodeContext should not be create because of different namespace");
213 } catch (final RestconfDocumentedException e) {
214 LOG.warn("Read from InputStream failed. Message: {}. Status: {}", e.getMessage(), e.getStatus());
217 verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
221 public void dataAugmentedTest() throws Exception {
222 NormalizedNodeContext normalizedNodeContext = prepareNNC("/common/augment/json/dataa.json", "main:cont");
224 assertNotNull(normalizedNodeContext);
225 assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
227 String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
229 assertTrue(dataTree.contains("cont1"));
230 assertTrue(dataTree.contains("lf11 lf11 value from a"));
232 normalizedNodeContext = prepareNNC("/common/augment/json/datab.json", "main:cont");
234 assertNotNull(normalizedNodeContext);
235 assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
236 dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
237 assertTrue(dataTree.contains("cont1"));
238 assertTrue(dataTree.contains("lf11 lf11 value from b"));
241 private void simpleTest(final String jsonPath, final String topLevelElementName,
242 final String moduleName) throws Exception {
243 final String uri = moduleName + ":" + topLevelElementName;
245 final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
246 assertNotNull(normalizedNodeContext);
248 verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
251 private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
253 mockBodyReader(uri, this.jsonBodyReader, false);
254 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
255 LOG.warn("Operation failed due to: {}", e.getMessage());
257 final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
259 NormalizedNodeContext normalizedNodeContext = null;
262 normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
263 } catch (WebApplicationException e) {
264 // TODO Auto-generated catch block
267 return normalizedNodeContext;
270 private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
271 final String topLevelElementName) {
272 assertEquals(topLevelElementName, normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
274 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
275 assertTrue(dataTree.contains("cont1"));
276 assertTrue(dataTree.contains("lst1"));
277 assertTrue(dataTree.contains("lflst1"));
278 assertTrue(dataTree.contains("lflst1_1"));
279 assertTrue(dataTree.contains("lflst1_2"));
280 assertTrue(dataTree.contains("lf1"));
283 private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
284 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
285 assertTrue(dataTree.contains("lf11"));
286 assertTrue(dataTree.contains("lf11_1"));
287 assertTrue(dataTree.contains("lflst11"));
288 assertTrue(dataTree.contains("45"));
289 assertTrue(dataTree.contains("cont11"));
290 assertTrue(dataTree.contains("lst11"));
294 public void unsupportedDataFormatTest() throws Exception {
295 mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
297 final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/unsupported-json-format.json");
299 RestconfDocumentedException exception = null;
302 this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
303 } catch (final RestconfDocumentedException e) {
306 LOG.info(exception.getErrors().get(0).getErrorMessage());
308 assertTrue(exception.getErrors().get(0).getErrorMessage().contains("is not a simple type"));
312 public void invalidUriCharacterInValue() throws Exception {
313 mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
315 final InputStream inputStream = this.getClass().getResourceAsStream(
316 "/json-to-nn/invalid-uri-character-in-value.json");
318 final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
319 null, null, null, this.mediaType, null, inputStream);
320 assertNotNull(normalizedNodeContext);
322 assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
324 final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
325 assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
326 assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
330 protected MediaType getMediaType() {