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.gson.stream.JsonReader;
21 import java.io.IOException;
22 import java.io.StringReader;
23 import java.net.URISyntaxException;
24 import java.util.Collections;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.common.Empty;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42 * Each test tests whether json input is correctly transformed to normalized node structure.
44 public class JsonStreamToNormalizedNodeTest {
46 private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
47 private static final QName EMPTY_LEAF = QName.create(CONT_1,"empty");
48 private static SchemaContext schemaContext;
51 public static void initialization() {
52 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/complexjson/yang");
56 public static void cleanup() {
61 public void leafNodeInContainer() throws IOException, URISyntaxException {
62 final String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
63 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
67 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
68 final String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
69 verifyTransformationToNormalizedNode(inputJson,
70 TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
74 public void leafListNodeInContainer() throws IOException, URISyntaxException {
75 final String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
76 verifyTransformationToNormalizedNode(inputJson,
77 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
81 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
82 final String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
83 verifyTransformationToNormalizedNode(inputJson,
84 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
88 public void choiceNodeInContainer() throws IOException, URISyntaxException {
89 final String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
90 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
94 * 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
101 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
102 final String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
103 verifyTransformationToNormalizedNode(inputJson,
104 TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
108 * also test using of namesakes (equal local names with different.
111 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
112 final String inputJson =
113 loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
114 verifyTransformationToNormalizedNode(inputJson,
115 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
119 * augmentation of choice - adding new case.
122 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
123 final String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
124 verifyTransformationToNormalizedNode(inputJson,
125 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
129 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
130 final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
131 verifyTransformationToNormalizedNode(inputJson,
132 TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
136 * Top level JSON element contains no information about module name.
139 * It should be possible to find out potential module name from available schema context.
142 public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
143 final String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
144 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
148 * Exception expected.
151 * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
152 * without module name prefix.
155 public void leafNamesakes() throws IOException, URISyntaxException {
156 final String inputJson = loadTextFile("/complexjson/namesakes.json");
158 //second parameter isn't necessary because error will be raised before it is used.
159 verifyTransformationToNormalizedNode(inputJson, null);
160 fail("Expected exception not raised");
161 } catch (final IllegalStateException e) {
162 final String errorMessage = e.getMessage();
163 assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
164 assertTrue(errorMessage.contains("complexjson-augmentation"));
165 assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
170 public void emptyTypeTest() throws IOException, URISyntaxException {
171 final String inputJson = loadTextFile("/complexjson/type-empty.json");
172 final ContainerNode awaitedStructure = containerBuilder()
173 .withNodeIdentifier(new NodeIdentifier(CONT_1))
174 .addChild(leafNode(EMPTY_LEAF, Empty.getInstance()))
177 verifyTransformationToNormalizedNode(inputJson, awaitedStructure);
181 * Exception expected.
184 * Json input contains element which doesn't exist in YANG schema
187 public void parsingNotExistingElement() throws IOException, URISyntaxException {
188 final String inputJson = loadTextFile("/complexjson/not-existing-element.json");
190 //second parameter isn't necessary because error will be raised before it is used.
191 verifyTransformationToNormalizedNode(inputJson, null);
192 } catch (final IllegalStateException e) {
193 assertTrue(e.getMessage().contains("Schema node with name dummy-element was not found"));
198 public void listItemWithoutArray() throws IOException, URISyntaxException {
199 final String inputJson = loadTextFile("/complexjson/keyed-list-restconf-behaviour.json");
201 final NormalizedNodeResult result = new NormalizedNodeResult();
202 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
203 final SchemaNode parentNode = schemaContext.findDataChildByName(CONT_1).get();
204 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
205 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), parentNode);
206 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
207 final NormalizedNode<?, ?> transformedInput = result.getResult();
208 assertNotNull(transformedInput);
212 public void listItemWithArray() throws IOException, URISyntaxException {
213 final String inputJson = loadTextFile("/complexjson/keyed-list-yang-json-behaviour.json");
215 final NormalizedNodeResult result = new NormalizedNodeResult();
216 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
217 final SchemaNode parentNode = schemaContext.findDataChildByName(CONT_1).get();
218 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
219 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), parentNode);
220 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
221 final NormalizedNode<?, ?> transformedInput = result.getResult();
222 assertNotNull(transformedInput);
226 public void multipleChoiceAugmentation() throws IOException, URISyntaxException {
227 final String inputJson = loadTextFile("/complexjson/multiple-choice-augmentation-in-container.json");
229 final NormalizedNodeResult result = new NormalizedNodeResult();
230 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
231 final SchemaNode parentNode = schemaContext.findDataChildByName(CONT_1).get();
233 final QName augmentChoice1QName = QName.create(parentNode.getQName(), "augment-choice1");
234 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
235 final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
236 final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
238 final AugmentationIdentifier aug1Id = new AugmentationIdentifier(Collections.singleton(augmentChoice1QName));
239 final AugmentationIdentifier aug2Id = new AugmentationIdentifier(Collections.singleton(augmentChoice2QName));
240 final NodeIdentifier augmentChoice1Id = new NodeIdentifier(augmentChoice1QName);
241 final NodeIdentifier augmentChoice2Id = new NodeIdentifier(augmentChoice2QName);
242 final NodeIdentifier containerId = new NodeIdentifier(containerQName);
244 final NormalizedNode<?, ?> cont1Normalized =
245 containerBuilder().withNodeIdentifier(new NodeIdentifier(parentNode.getQName()))
246 .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
247 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
248 .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
249 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id)
250 .withChild(containerBuilder().withNodeIdentifier(containerId)
251 .withChild(leafNode(leafQName, "leaf-value"))
258 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
259 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext));
260 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
261 final NormalizedNode<?, ?> transformedInput = result.getResult();
262 assertNotNull(transformedInput);
263 assertEquals(cont1Normalized, transformedInput);
266 private static void verifyTransformationToNormalizedNode(final String inputJson,
267 final NormalizedNode<?, ?> awaitedStructure) {
268 final NormalizedNodeResult result = new NormalizedNodeResult();
269 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
270 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
271 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext));
272 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
273 final NormalizedNode<?, ?> transformedInput = result.getResult();
274 assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,