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