2 * Copyright (c) 2016 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.loadTextFile;
15 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.augmentationBuilder;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.choiceBuilder;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.containerBuilder;
18 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
20 import com.google.common.collect.Sets;
21 import com.google.gson.stream.JsonReader;
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.net.URISyntaxException;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40 * Each test tests whether json input is correctly transformed to normalized node structure.
42 public class JsonStreamToNormalizedNodeTest {
44 private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
45 private static final QName EMPTY_LEAF = QName.create(CONT_1,"empty");
46 private static SchemaContext schemaContext;
49 public static void initialization() throws IOException, URISyntaxException, ReactorException {
50 schemaContext = YangParserTestUtils.parseYangSources("/complexjson/yang");
54 public void leafNodeInContainer() throws IOException, URISyntaxException {
55 final String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
56 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
60 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
61 final String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
62 verifyTransformationToNormalizedNode(inputJson,
63 TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
67 public void leafListNodeInContainer() throws IOException, URISyntaxException {
68 final String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
69 verifyTransformationToNormalizedNode(inputJson,
70 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
74 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
75 final String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
76 verifyTransformationToNormalizedNode(inputJson,
77 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
81 public void choiceNodeInContainer() throws IOException, URISyntaxException {
82 final String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
83 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
87 * Test of translating internal augmentations to normalized nodes structure.
90 * 2 nodes are added via internal augmentation A, 1 node via internal augmentation B and one node is originally
94 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
95 final String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
96 verifyTransformationToNormalizedNode(inputJson,
97 TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
101 * also test using of namesakes (equal local names with different.
104 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
105 final String inputJson =
106 loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
107 verifyTransformationToNormalizedNode(inputJson,
108 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
112 * augmentation of choice - adding new case.
115 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
116 final String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
117 verifyTransformationToNormalizedNode(inputJson,
118 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
122 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
123 final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
124 verifyTransformationToNormalizedNode(inputJson,
125 TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
129 * Top level JSON element contains no information about module name.
132 * It should be possible to find out potential module name from available schema context.
135 public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
136 final String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
137 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
141 * Exception expected.
144 * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
145 * without module name prefix.
148 public void leafNamesakes() throws IOException, URISyntaxException {
149 final String inputJson = loadTextFile("/complexjson/namesakes.json");
151 //second parameter isn't necessary because error will be raised before it is used.
152 verifyTransformationToNormalizedNode(inputJson, null);
153 fail("Expected exception not raised");
154 } catch (final IllegalStateException e) {
155 final String errorMessage = e.getMessage();
156 assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
157 assertTrue(errorMessage.contains("complexjson-augmentation"));
158 assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
163 public void emptyTypeTest() throws IOException, URISyntaxException {
164 final String inputJson = loadTextFile("/complexjson/type-empty.json");
165 final ContainerNode awaitedStructure = containerBuilder()
166 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
167 .addChild(leafNode(EMPTY_LEAF, null))
170 verifyTransformationToNormalizedNode(inputJson, awaitedStructure);
174 * Exception expected.
177 * Json input contains element which doesn't exist in YANG schema
180 public void parsingNotExistingElement() throws IOException, URISyntaxException {
181 final String inputJson = loadTextFile("/complexjson/not-existing-element.json");
183 //second parameter isn't necessary because error will be raised before it is used.
184 verifyTransformationToNormalizedNode(inputJson, null);
185 } catch (final IllegalStateException e) {
186 assertTrue(e.getMessage().contains("Schema node with name dummy-element was not 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(CONT_1);
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(CONT_1);
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);
217 public void multipleChoiceAugmentation() throws IOException, URISyntaxException {
218 final String inputJson = loadTextFile("/complexjson/multiple-choice-augmentation-in-container.json");
220 final NormalizedNodeResult result = new NormalizedNodeResult();
221 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
222 final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
224 final QName augmentChoice1QName = QName.create(parentNode.getQName(), "augment-choice1");
225 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
226 final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
227 final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
229 final YangInstanceIdentifier.AugmentationIdentifier aug1Id =
230 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
231 final YangInstanceIdentifier.AugmentationIdentifier aug2Id =
232 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
233 final YangInstanceIdentifier.NodeIdentifier augmentChoice1Id =
234 new YangInstanceIdentifier.NodeIdentifier(augmentChoice1QName);
235 final YangInstanceIdentifier.NodeIdentifier augmentChoice2Id =
236 new YangInstanceIdentifier.NodeIdentifier(augmentChoice2QName);
237 final YangInstanceIdentifier.NodeIdentifier containerId =
238 new YangInstanceIdentifier.NodeIdentifier(containerQName);
240 final NormalizedNode<?, ?> cont1Normalized =
241 containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(parentNode.getQName()))
242 .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
243 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
244 .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
245 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id)
246 .withChild(containerBuilder().withNodeIdentifier(containerId)
247 .withChild(leafNode(leafQName, "leaf-value"))
254 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
255 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
256 final NormalizedNode<?, ?> transformedInput = result.getResult();
257 assertNotNull(transformedInput);
258 assertEquals(cont1Normalized, transformedInput);
261 private static void verifyTransformationToNormalizedNode(final String inputJson,
262 final NormalizedNode<?, ?> awaitedStructure) {
263 final NormalizedNodeResult result = new NormalizedNodeResult();
264 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
265 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
266 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
267 final NormalizedNode<?, ?> transformedInput = result.getResult();
268 assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,