BUG 1440 - additional tests for data-codec-gson
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / JsonStreamToNormalizedNodeTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson;
9
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;
13
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;
26
27 /**
28  *
29  * Each test tests whether json input is correctly transformed to normalized node structure
30  */
31 public class JsonStreamToNormalizedNodeTest {
32
33     private static SchemaContext schemaContext;
34
35     @BeforeClass
36     public static void initialization() throws IOException, URISyntaxException {
37         schemaContext = loadModules("/complexjson/yang");
38     }
39
40     /**
41      * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
42      * specified
43      */
44     @Ignore
45     @Test
46     public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
47
48     }
49
50     /**
51      * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
52      * specified
53      */
54     @Ignore
55     @Test
56     public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
57
58     }
59
60     @Test
61     public void leafNodeInContainer() throws IOException, URISyntaxException {
62         String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
63         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
64     }
65
66     @Test
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());
71     }
72
73     @Test
74     public void leafListNodeInContainer() throws IOException, URISyntaxException {
75         String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
76         verifyTransformationToNormalizedNode(inputJson,
77                 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
78     }
79
80     @Test
81     public void keyedListNodeInContainer() throws IOException, URISyntaxException {
82         String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
83         verifyTransformationToNormalizedNode(inputJson,
84                 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
85     }
86
87     @Test
88     public void choiceNodeInContainer() throws IOException, URISyntaxException {
89         String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
90         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
91     }
92
93     /**
94      * Test of translating internal augmentations to normalized nodes structure
95      *
96      * 2 nodes are added via internal augmentation A, 1 node via internal augmentation B and one node is originally
97      * member of case.
98      *
99      */
100     @Test
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());
105     }
106
107     /**
108      * also test using of namesakes (equal local names with different
109      *
110      * @throws IOException
111      * @throws URISyntaxException
112      */
113     @Test
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());
118     }
119
120     /**
121      * augmentation of choice - adding new case
122      */
123     @Test
124     public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
125         String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
126         verifyTransformationToNormalizedNode(inputJson,
127                 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
128     }
129
130     @Test
131     public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
132         String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
133         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
134     }
135
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,
144                 transformedInput);
145     }
146
147 }