1a71142be5553ecee451b42edb5fce7d02bf2612
[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.junit.Assert.assertTrue;
12 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
13 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
14
15 import com.google.gson.stream.JsonReader;
16 import java.io.IOException;
17 import java.io.StringReader;
18 import java.net.URISyntaxException;
19 import org.junit.BeforeClass;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
25 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27
28 /**
29  *
30  * Each test tests whether json input is correctly transformed to normalized node structure
31  */
32 public class JsonStreamToNormalizedNodeTest {
33
34     private static SchemaContext schemaContext;
35
36     @BeforeClass
37     public static void initialization() throws IOException, URISyntaxException {
38         schemaContext = loadModules("/complexjson/yang");
39     }
40
41     /**
42      * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
43      * specified
44      */
45     @Ignore
46     @Test
47     public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
48
49     }
50
51     /**
52      * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
53      * specified
54      */
55     @Ignore
56     @Test
57     public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
58
59     }
60
61     @Test
62     public void leafNodeInContainer() throws IOException, URISyntaxException {
63         String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
64         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
65     }
66
67     @Test
68     public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
69         String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
70         verifyTransformationToNormalizedNode(inputJson,
71                 TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
72     }
73
74     @Test
75     public void leafListNodeInContainer() throws IOException, URISyntaxException {
76         String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
77         verifyTransformationToNormalizedNode(inputJson,
78                 TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
79     }
80
81     @Test
82     public void keyedListNodeInContainer() throws IOException, URISyntaxException {
83         String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
84         verifyTransformationToNormalizedNode(inputJson,
85                 TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
86     }
87
88     @Test
89     public void choiceNodeInContainer() throws IOException, URISyntaxException {
90         String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
91         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
92     }
93
94     /**
95      * Test of translating internal augmentations to normalized nodes structure
96      *
97      * 2 nodes are added via internal augmentation A, 1 node via internal augmentation B and one node is originally
98      * member of case.
99      *
100      */
101     @Test
102     public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
103         String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
104         verifyTransformationToNormalizedNode(inputJson,
105                 TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
106     }
107
108     /**
109      * also test using of namesakes (equal local names with different
110      *
111      * @throws IOException
112      * @throws URISyntaxException
113      */
114     @Test
115     public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
116         String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
117         verifyTransformationToNormalizedNode(inputJson,
118                 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
119     }
120
121     /**
122      * augmentation of choice - adding new case
123      */
124     @Test
125     public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
126         String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
127         verifyTransformationToNormalizedNode(inputJson,
128                 TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
129     }
130
131     @Test
132     public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
133         String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
134         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
135     }
136
137     /**
138      * Top level JSON element contains no information about module name.
139      *
140      * It should be possible to find out potential module name from available schema context.
141      *
142      */
143     @Test
144     public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
145         String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
146         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
147     }
148
149     /**
150      *
151      * Exception expected.
152      *
153      * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
154      * without module name prefix.
155      */
156     @Test
157     public void leafNamesakes() throws IOException, URISyntaxException {
158         String inputJson = loadTextFile("/complexjson/namesakes.json");
159         try {
160             //second parameter isn't necessary because error will be raised before it is used.
161             verifyTransformationToNormalizedNode(inputJson, null);
162         } catch (IllegalStateException e) {
163             final String errorMessage = e.getMessage();
164             assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
165             assertTrue(errorMessage.contains("complexjson-augmentation"));
166             assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
167         }
168     }
169
170     /**
171      *
172      * Exception expected.
173      *
174      * Json input contains element which doesn't exist in YANG schema
175      */
176     @Test
177     public void parsingNotExistingElement() throws IOException, URISyntaxException {
178         String inputJson = loadTextFile("/complexjson/not-existing-element.json");
179         try {
180             //second parameter isn't necessary because error will be raised before it is used.
181             verifyTransformationToNormalizedNode(inputJson, null);
182         } catch (IllegalStateException e) {
183             assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found."));
184         }
185     }
186
187
188     private void verifyTransformationToNormalizedNode(final String inputJson,
189             final NormalizedNode<?, ?> awaitedStructure) {
190         NormalizedNodeResult result = new NormalizedNodeResult();
191         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
192         JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
193         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
194         NormalizedNode<?, ?> transformedInput = result.getResult();
195         assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,
196                 transformedInput);
197     }
198
199 }