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.yangtools.yang.data.codec.gson;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
14 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
16 import com.google.gson.stream.JsonReader;
17 import java.io.IOException;
18 import java.io.StringReader;
19 import java.net.URISyntaxException;
20 import org.junit.BeforeClass;
21 import org.junit.Ignore;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
26 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
32 * Each test tests whether json input is correctly transformed to normalized node structure
34 public class JsonStreamToNormalizedNodeTest {
36 private static SchemaContext schemaContext;
39 public static void initialization() throws IOException, URISyntaxException {
40 schemaContext = loadModules("/complexjson/yang");
44 * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
49 public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
54 * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
59 public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
64 public void leafNodeInContainer() throws IOException, URISyntaxException {
65 final String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
66 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
70 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
71 final String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
72 verifyTransformationToNormalizedNode(inputJson,
73 TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
77 public void leafListNodeInContainer() throws IOException, URISyntaxException {
78 final String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
79 verifyTransformationToNormalizedNode(inputJson,
80 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
84 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
85 final String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
86 verifyTransformationToNormalizedNode(inputJson,
87 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
91 public void choiceNodeInContainer() throws IOException, URISyntaxException {
92 final String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
93 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
97 * Test of translating internal augmentations to normalized nodes structure
99 * 2 nodes are added via internal augmentation A, 1 node via internal augmentation B and one node is originally
104 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
105 final String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
106 verifyTransformationToNormalizedNode(inputJson,
107 TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
111 * also test using of namesakes (equal local names with different
113 * @throws IOException
114 * @throws URISyntaxException
117 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
118 final String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
119 verifyTransformationToNormalizedNode(inputJson,
120 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
124 * augmentation of choice - adding new case
127 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
128 final String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
129 verifyTransformationToNormalizedNode(inputJson,
130 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
134 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
135 final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
136 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
140 * Top level JSON element contains no information about module name.
142 * It should be possible to find out potential module name from available schema context.
146 public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
147 final String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
148 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
153 * Exception expected.
155 * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
156 * without module name prefix.
159 public void leafNamesakes() throws IOException, URISyntaxException {
160 final String inputJson = loadTextFile("/complexjson/namesakes.json");
162 //second parameter isn't necessary because error will be raised before it is used.
163 verifyTransformationToNormalizedNode(inputJson, null);
164 } catch (final IllegalStateException e) {
165 final String errorMessage = e.getMessage();
166 assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
167 assertTrue(errorMessage.contains("complexjson-augmentation"));
168 assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
174 * Exception expected.
176 * Json input contains element which doesn't exist in YANG schema
179 public void parsingNotExistingElement() throws IOException, URISyntaxException {
180 final String inputJson = loadTextFile("/complexjson/not-existing-element.json");
182 //second parameter isn't necessary because error will be raised before it is used.
183 verifyTransformationToNormalizedNode(inputJson, null);
184 } catch (final IllegalStateException e) {
185 assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found."));
191 public void listItemWithoutArray() throws IOException, URISyntaxException {
192 final String inputJson = loadTextFile("/complexjson/keyed-list-restconf-behaviour.json");
194 final NormalizedNodeResult result = new NormalizedNodeResult();
195 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
196 final SchemaNode parentNode = schemaContext.getDataChildByName("cont1");
197 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
198 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
199 final NormalizedNode<?, ?> transformedInput = result.getResult();
200 assertNotNull(transformedInput);
204 public void listItemWithArray() throws IOException, URISyntaxException {
205 final String inputJson = loadTextFile("/complexjson/keyed-list-yang-json-behaviour.json");
207 final NormalizedNodeResult result = new NormalizedNodeResult();
208 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
209 final SchemaNode parentNode = schemaContext.getDataChildByName("cont1");
210 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
211 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
212 final NormalizedNode<?, ?> transformedInput = result.getResult();
213 assertNotNull(transformedInput);
216 private void verifyTransformationToNormalizedNode(final String inputJson,
217 final NormalizedNode<?, ?> awaitedStructure) {
218 final NormalizedNodeResult result = new NormalizedNodeResult();
219 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
220 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
221 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
222 final NormalizedNode<?, ?> transformedInput = result.getResult();
223 assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,