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.assertTrue;
12 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
13 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
15 import com.google.gson.stream.JsonReader;
16 import java.io.IOException;
17 import java.io.StringReader;
18 import java.net.URISyntaxException;
19 import org.junit.BeforeClass;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
25 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 * Each test tests whether json input is correctly transformed to normalized node structure
32 public class JsonStreamToNormalizedNodeTest {
34 private static SchemaContext schemaContext;
37 public static void initialization() throws IOException, URISyntaxException {
38 schemaContext = loadModules("/complexjson/yang");
42 * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
47 public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
52 * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
57 public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
62 public void leafNodeInContainer() throws IOException, URISyntaxException {
63 String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
64 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
68 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
69 String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
70 verifyTransformationToNormalizedNode(inputJson,
71 TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
75 public void leafListNodeInContainer() throws IOException, URISyntaxException {
76 String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
77 verifyTransformationToNormalizedNode(inputJson,
78 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
82 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
83 String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
84 verifyTransformationToNormalizedNode(inputJson,
85 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
89 public void choiceNodeInContainer() throws IOException, URISyntaxException {
90 String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
91 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
95 * Test of translating internal augmentations to normalized nodes structure
97 * 2 nodes are added via internal augmentation A, 1 node via internal augmentation B and one node is originally
102 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
103 String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
104 verifyTransformationToNormalizedNode(inputJson,
105 TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
109 * also test using of namesakes (equal local names with different
111 * @throws IOException
112 * @throws URISyntaxException
115 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
116 String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
117 verifyTransformationToNormalizedNode(inputJson,
118 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
122 * augmentation of choice - adding new case
125 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
126 String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
127 verifyTransformationToNormalizedNode(inputJson,
128 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
132 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
133 String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
134 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
138 * Top level JSON element contains no information about module name.
140 * It should be possible to find out potential module name from available schema context.
144 public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
145 String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
146 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
151 * Exception expected.
153 * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
154 * without module name prefix.
157 public void leafNamesakes() throws IOException, URISyntaxException {
158 String inputJson = loadTextFile("/complexjson/namesakes.json");
160 //second parameter isn't necessary because error will be raised before it is used.
161 verifyTransformationToNormalizedNode(inputJson, null);
162 } catch (IllegalStateException e) {
163 final String errorMessage = e.getMessage();
164 assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
165 assertTrue(errorMessage.contains("complexjson-augmentation"));
166 assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
172 * Exception expected.
174 * Json input contains element which doesn't exist in YANG schema
177 public void parsingNotExistingElement() throws IOException, URISyntaxException {
178 String inputJson = loadTextFile("/complexjson/not-existing-element.json");
180 //second parameter isn't necessary because error will be raised before it is used.
181 verifyTransformationToNormalizedNode(inputJson, null);
182 } catch (IllegalStateException e) {
183 assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found."));
188 private void verifyTransformationToNormalizedNode(final String inputJson,
189 final NormalizedNode<?, ?> awaitedStructure) {
190 NormalizedNodeResult result = new NormalizedNodeResult();
191 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
192 JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
193 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
194 NormalizedNode<?, ?> transformedInput = result.getResult();
195 assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,