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.junit.Assert.fail;
14 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
15 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
17 import com.google.gson.stream.JsonReader;
18 import java.io.IOException;
19 import java.io.StringReader;
20 import java.net.URISyntaxException;
21 import org.junit.BeforeClass;
22 import org.junit.Ignore;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
38 * Each test tests whether json input is correctly transformed to normalized node structure
40 public class JsonStreamToNormalizedNodeTest {
42 private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
43 private static final QName EMPTY_LEAF = QName.create(CONT_1,"empty");
44 private static SchemaContext schemaContext;
47 public static void initialization() throws IOException, URISyntaxException {
48 schemaContext = loadModules("/complexjson/yang");
52 * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
57 public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
62 * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
67 public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
72 public void leafNodeInContainer() throws IOException, URISyntaxException {
73 final String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
74 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
78 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
79 final String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
80 verifyTransformationToNormalizedNode(inputJson,
81 TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
85 public void leafListNodeInContainer() throws IOException, URISyntaxException {
86 final String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
87 verifyTransformationToNormalizedNode(inputJson,
88 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
92 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
93 final String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
94 verifyTransformationToNormalizedNode(inputJson,
95 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
99 public void choiceNodeInContainer() throws IOException, URISyntaxException {
100 final String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
101 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
105 * Test of translating internal augmentations to normalized nodes structure
107 * 2 nodes are added via internal augmentation A, 1 node via internal augmentation B and one node is originally
112 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
113 final String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
114 verifyTransformationToNormalizedNode(inputJson,
115 TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
119 * also test using of namesakes (equal local names with different
121 * @throws IOException
122 * @throws URISyntaxException
125 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
126 final String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
127 verifyTransformationToNormalizedNode(inputJson,
128 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
132 * augmentation of choice - adding new case
135 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
136 final String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
137 verifyTransformationToNormalizedNode(inputJson,
138 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
142 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
143 final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
144 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
148 * Top level JSON element contains no information about module name.
150 * It should be possible to find out potential module name from available schema context.
154 public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
155 final String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
156 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
161 * Exception expected.
163 * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
164 * without module name prefix.
167 public void leafNamesakes() throws IOException, URISyntaxException {
168 final String inputJson = loadTextFile("/complexjson/namesakes.json");
170 //second parameter isn't necessary because error will be raised before it is used.
171 verifyTransformationToNormalizedNode(inputJson, null);
172 fail("Expected exception not raised");
173 } catch (final IllegalStateException e) {
174 final String errorMessage = e.getMessage();
175 assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
176 assertTrue(errorMessage.contains("complexjson-augmentation"));
177 assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
182 public void emptyTypeTest() throws IOException, URISyntaxException {
183 final String inputJson = loadTextFile("/complexjson/type-empty.json");
184 final ContainerNode awaitedStructure = Builders.containerBuilder()
185 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
186 .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, null))
189 verifyTransformationToNormalizedNode(inputJson, awaitedStructure);
194 * Exception expected.
196 * Json input contains element which doesn't exist in YANG schema
199 public void parsingNotExistingElement() throws IOException, URISyntaxException {
200 final String inputJson = loadTextFile("/complexjson/not-existing-element.json");
202 //second parameter isn't necessary because error will be raised before it is used.
203 verifyTransformationToNormalizedNode(inputJson, null);
204 } catch (final IllegalStateException e) {
205 assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found."));
211 public void listItemWithoutArray() throws IOException, URISyntaxException {
212 final String inputJson = loadTextFile("/complexjson/keyed-list-restconf-behaviour.json");
214 final NormalizedNodeResult result = new NormalizedNodeResult();
215 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
216 final SchemaNode parentNode = schemaContext.getDataChildByName("cont1");
217 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
218 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
219 final NormalizedNode<?, ?> transformedInput = result.getResult();
220 assertNotNull(transformedInput);
224 public void listItemWithArray() throws IOException, URISyntaxException {
225 final String inputJson = loadTextFile("/complexjson/keyed-list-yang-json-behaviour.json");
227 final NormalizedNodeResult result = new NormalizedNodeResult();
228 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
229 final SchemaNode parentNode = schemaContext.getDataChildByName("cont1");
230 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
231 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
232 final NormalizedNode<?, ?> transformedInput = result.getResult();
233 assertNotNull(transformedInput);
236 private void verifyTransformationToNormalizedNode(final String inputJson,
237 final NormalizedNode<?, ?> awaitedStructure) {
238 final NormalizedNodeResult result = new NormalizedNodeResult();
239 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
240 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
241 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
242 final NormalizedNode<?, ?> transformedInput = result.getResult();
243 assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,