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