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.loadModules;
15 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.augmentationBuilder;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.choiceBuilder;
18 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.containerBuilder;
19 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
21 import com.google.common.collect.Sets;
22 import com.google.gson.stream.JsonReader;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.net.URISyntaxException;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 * Each test tests whether json input is correctly transformed to normalized node structure
43 public class JsonStreamToNormalizedNodeTest {
45 private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
46 private static final QName EMPTY_LEAF = QName.create(CONT_1,"empty");
47 private static SchemaContext schemaContext;
50 public static void initialization() throws IOException, URISyntaxException, ReactorException {
51 schemaContext = loadModules("/complexjson/yang");
55 public void leafNodeInContainer() throws IOException, URISyntaxException {
56 final String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
57 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
61 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
62 final String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
63 verifyTransformationToNormalizedNode(inputJson,
64 TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
68 public void leafListNodeInContainer() throws IOException, URISyntaxException {
69 final String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
70 verifyTransformationToNormalizedNode(inputJson,
71 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
75 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
76 final String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
77 verifyTransformationToNormalizedNode(inputJson,
78 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
82 public void choiceNodeInContainer() throws IOException, URISyntaxException {
83 final String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
84 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
88 * 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
95 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
96 final String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
97 verifyTransformationToNormalizedNode(inputJson,
98 TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
102 * also test using of namesakes (equal local names with different
104 * @throws IOException
105 * @throws URISyntaxException
108 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
109 final String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
110 verifyTransformationToNormalizedNode(inputJson,
111 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
115 * augmentation of choice - adding new case
118 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
119 final String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
120 verifyTransformationToNormalizedNode(inputJson,
121 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
125 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
126 final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
127 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
131 * Top level JSON element contains no information about module name.
133 * It should be possible to find out potential module name from available schema context.
137 public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
138 final String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
139 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
144 * Exception expected.
146 * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
147 * without module name prefix.
150 public void leafNamesakes() throws IOException, URISyntaxException {
151 final String inputJson = loadTextFile("/complexjson/namesakes.json");
153 //second parameter isn't necessary because error will be raised before it is used.
154 verifyTransformationToNormalizedNode(inputJson, null);
155 fail("Expected exception not raised");
156 } catch (final IllegalStateException e) {
157 final String errorMessage = e.getMessage();
158 assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
159 assertTrue(errorMessage.contains("complexjson-augmentation"));
160 assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
165 public void emptyTypeTest() throws IOException, URISyntaxException {
166 final String inputJson = loadTextFile("/complexjson/type-empty.json");
167 final ContainerNode awaitedStructure = containerBuilder()
168 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
169 .addChild(leafNode(EMPTY_LEAF, null))
172 verifyTransformationToNormalizedNode(inputJson, awaitedStructure);
177 * Exception expected.
179 * Json input contains element which doesn't exist in YANG schema
182 public void parsingNotExistingElement() throws IOException, URISyntaxException {
183 final String inputJson = loadTextFile("/complexjson/not-existing-element.json");
185 //second parameter isn't necessary because error will be raised before it is used.
186 verifyTransformationToNormalizedNode(inputJson, null);
187 } catch (final IllegalStateException e) {
188 assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found"));
194 public void listItemWithoutArray() throws IOException, URISyntaxException {
195 final String inputJson = loadTextFile("/complexjson/keyed-list-restconf-behaviour.json");
197 final NormalizedNodeResult result = new NormalizedNodeResult();
198 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
199 final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
200 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
201 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
202 final NormalizedNode<?, ?> transformedInput = result.getResult();
203 assertNotNull(transformedInput);
207 public void listItemWithArray() throws IOException, URISyntaxException {
208 final String inputJson = loadTextFile("/complexjson/keyed-list-yang-json-behaviour.json");
210 final NormalizedNodeResult result = new NormalizedNodeResult();
211 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
212 final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
213 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
214 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
215 final NormalizedNode<?, ?> transformedInput = result.getResult();
216 assertNotNull(transformedInput);
220 public void multipleChoiceAugmentation() throws IOException, URISyntaxException {
221 final String inputJson = loadTextFile("/complexjson/multiple-choice-augmentation-in-container.json");
223 final NormalizedNodeResult result = new NormalizedNodeResult();
224 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
225 final SchemaNode parentNode = schemaContext.getDataChildByName(CONT_1);
227 final QName augmentChoice1QName = QName.create(parentNode.getQName(), "augment-choice1");
228 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
229 final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
230 final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
232 final YangInstanceIdentifier.AugmentationIdentifier aug1Id =
233 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
234 final YangInstanceIdentifier.AugmentationIdentifier aug2Id =
235 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
236 final YangInstanceIdentifier.NodeIdentifier augmentChoice1Id =
237 new YangInstanceIdentifier.NodeIdentifier(augmentChoice1QName);
238 final YangInstanceIdentifier.NodeIdentifier augmentChoice2Id =
239 new YangInstanceIdentifier.NodeIdentifier(augmentChoice2QName);
240 final YangInstanceIdentifier.NodeIdentifier containerId =
241 new YangInstanceIdentifier.NodeIdentifier(containerQName);
243 final NormalizedNode<?, ?> cont1Normalized =
244 containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(parentNode.getQName()))
245 .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
246 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
247 .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
248 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id)
249 .withChild(containerBuilder().withNodeIdentifier(containerId)
250 .withChild(leafNode(leafQName, "leaf-value"))
257 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
258 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
259 final NormalizedNode<?, ?> transformedInput = result.getResult();
260 assertNotNull(transformedInput);
261 assertEquals(cont1Normalized, transformedInput);
264 private static void verifyTransformationToNormalizedNode(final String inputJson,
265 final NormalizedNode<?, ?> awaitedStructure) {
266 final NormalizedNodeResult result = new NormalizedNodeResult();
267 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
268 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
269 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
270 final NormalizedNode<?, ?> transformedInput = result.getResult();
271 assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,