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.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
12 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
14 import com.google.gson.stream.JsonReader;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.net.URISyntaxException;
18 import org.junit.BeforeClass;
19 import org.junit.Ignore;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 * Each test tests whether json input is correctly transformed to normalized node structure
31 public class JsonStreamToNormalizedNodeTest {
33 private static SchemaContext schemaContext;
36 public static void initialization() throws IOException, URISyntaxException {
37 schemaContext = loadModules("/complexjson/yang");
41 * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
46 public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
51 * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
56 public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
61 public void leafNodeInContainer() throws IOException, URISyntaxException {
62 String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
63 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
67 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
68 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 String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
76 verifyTransformationToNormalizedNode(inputJson,
77 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
81 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
82 String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
83 verifyTransformationToNormalizedNode(inputJson,
84 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
88 public void choiceNodeInContainer() throws IOException, URISyntaxException {
89 String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
90 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
94 * Test of translating internal augmentations to normalized nodes structure
96 * 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 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
110 * @throws IOException
111 * @throws URISyntaxException
114 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
115 String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
116 verifyTransformationToNormalizedNode(inputJson,
117 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
121 * augmentation of choice - adding new case
124 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
125 String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
126 verifyTransformationToNormalizedNode(inputJson,
127 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
131 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
132 String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
133 verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
136 private void verifyTransformationToNormalizedNode(final String inputJson,
137 final NormalizedNode<?, ?> awaitedStructure) {
138 NormalizedNodeResult result = new NormalizedNodeResult();
139 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
140 JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
141 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
142 NormalizedNode<?, ?> transformedInput = result.getResult();
143 assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,