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 / NormalizedNodeToJsonStreamTest.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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.childArray;
15 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.childPrimitive;
16 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
17 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.resolveCont1;
18
19 import com.google.common.collect.Sets;
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonNull;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonPrimitive;
25 import java.io.IOException;
26 import java.io.StringWriter;
27 import java.io.Writer;
28 import java.net.URISyntaxException;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import org.junit.BeforeClass;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43
44 /**
45  * Each test tests whether json output obtained after transformation contains is corect. The transformation takes
46  * normalized node data structure and transform it to json output. To make it easier validate json output it is loaded
47  * via gson as structure of json elements which are walked and compared with awaited values.
48  *
49  */
50 public class NormalizedNodeToJsonStreamTest {
51
52     private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
53     private static final QName EMPTY_LEAF = QName.create(CONT_1,"empty");
54     private static SchemaContext schemaContext;
55
56     public interface JsonValidator {
57         void validate(final String jsonOutput);
58    }
59
60     @BeforeClass
61     public static void initialization() throws IOException, URISyntaxException {
62         schemaContext = loadModules("/complexjson/yang");
63     }
64
65     /**
66      * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
67      * specified
68      */
69     @Ignore
70     @Test
71     public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
72
73     }
74
75     /**
76      * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
77      * specified
78      */
79     @Ignore
80     @Test
81     public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
82
83     }
84
85     @Test
86     public void leafNodeInContainer() throws IOException, URISyntaxException {
87         final Writer writer = new StringWriter();
88         final NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
89         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
90         new JsonValidator() {
91
92             @Override
93             public void validate(final String jsonOutput) {
94                 final JsonObject cont1 = resolveCont1(jsonOutput);
95                 assertNotNull(cont1);
96
97                 final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
98                 assertNotNull(lf11);
99                 final int asInt = lf11.getAsInt();
100                 assertEquals(453, asInt);
101             }
102         }.validate(jsonOutput);
103
104     }
105
106     @Test
107     public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
108         final Writer writer = new StringWriter();
109         final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainerMultiline();
110         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
111         new JsonValidator() {
112
113             @Override
114             public void validate(final String jsonOutput) {
115                 final JsonObject cont1 = resolveCont1(jsonOutput);
116                 assertNotNull(cont1);
117                 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
118                 assertNotNull(lflst11);
119
120                 final HashSet<Object> lflst11Values = Sets.newHashSet();
121                 for (final JsonElement jsonElement : lflst11) {
122                     assertTrue(jsonElement instanceof JsonPrimitive);
123                     lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
124                 }
125
126                 assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"), lflst11Values);
127             }
128         }.validate(jsonOutput);
129
130     }
131
132     @Test
133     public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
134         final Writer writer = new StringWriter();
135         final NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
136                 .leafNodeViaAugmentationInContainer();
137         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
138         new JsonValidator() {
139
140             @Override
141             public void validate(final String jsonOutput) {
142                 final JsonObject cont1 = resolveCont1(jsonOutput);
143                 assertNotNull(cont1);
144
145                 final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
146                 assertNotNull(lf12_1);
147                 final String asString = lf12_1.getAsString();
148                 assertEquals("lf12 value", asString);
149             }
150         }.validate(jsonOutput);
151
152     }
153
154     @Test
155     public void leafListNodeInContainer() throws IOException, URISyntaxException {
156         final Writer writer = new StringWriter();
157         final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainer();
158         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
159         new JsonValidator() {
160
161             @Override
162             public void validate(final String jsonOutput) {
163                 final JsonObject cont1 = resolveCont1(jsonOutput);
164                 assertNotNull(cont1);
165                 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
166                 assertNotNull(lflst11);
167
168                 final HashSet<Object> lflst11Values = Sets.newHashSet();
169                 for (final JsonElement jsonElement : lflst11) {
170                     assertTrue(jsonElement instanceof JsonPrimitive);
171                     lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
172                 }
173
174                 assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
175             }
176         }.validate(jsonOutput);
177     }
178
179     @Test
180     public void keyedListNodeInContainer() throws IOException, URISyntaxException {
181         final Writer writer = new StringWriter();
182         final NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
183                 .keyedListNodeInContainer();
184         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
185         new JsonValidator() {
186
187             @Override
188             public void validate(final String jsonOutput) {
189                 final JsonObject cont1 = resolveCont1(jsonOutput);
190                 assertNotNull(cont1);
191                 final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
192                 assertNotNull(lst11);
193
194                 final Iterator<JsonElement> iterator = lst11.iterator();
195                 assertTrue(iterator.hasNext());
196                 final JsonElement lst11Entry1Raw = iterator.next();
197                 assertFalse(iterator.hasNext());
198                 assertTrue(lst11Entry1Raw instanceof JsonObject);
199                 final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
200
201                 final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
202                 assertNotNull(key111);
203                 final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
204                 assertNotNull(lf112);
205                 final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
206                 assertNotNull(lf113);
207                 final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
208                 assertNotNull(lf111);
209
210                 assertEquals("key111 value", key111.getAsString());
211                 assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
212                 assertEquals("lf113 value", lf113.getAsString());
213                 assertEquals("lf111 value", lf111.getAsString());
214             }
215         }.validate(jsonOutput);
216     }
217
218     @Test
219     public void choiceNodeInContainer() throws IOException, URISyntaxException {
220         final Writer writer = new StringWriter();
221         final NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator.choiceNodeInContainer();
222         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
223         new JsonValidator() {
224
225             @Override
226             public void validate(final String jsonOutput) {
227                 final JsonObject cont1 = resolveCont1(jsonOutput);
228                 assertNotNull(cont1);
229                 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
230                 assertNotNull(lf13);
231
232                 assertEquals("lf13 value", lf13.getAsString());
233             }
234         }.validate(jsonOutput);
235     }
236
237     /**
238      * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
239      * added)
240      *
241      * after running this test following exception is raised
242      *
243      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
244      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
245      *
246      */
247 //    @Ignore
248     @Test
249     public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
250         final Writer writer = new StringWriter();
251         final NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
252                 .caseNodeAugmentationInChoiceInContainer();
253         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, caseNodeAugmentationInChoiceInContainer);
254         new JsonValidator() {
255
256             @Override
257             public void validate(final String jsonOutput) {
258                 final JsonObject cont1 = resolveCont1(jsonOutput);
259                 assertNotNull(cont1);
260
261                 final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
262                 assertNotNull(lf15_21);
263                 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
264                 assertNotNull(lf13);
265                 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
266                 assertNotNull(lf15_11);
267                 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
268                 assertNotNull(lf15_12);
269
270                 assertEquals("lf15_21 value", lf15_21.getAsString());
271                 assertEquals("lf13 value", lf13.getAsString());
272                 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
273                 assertEquals("complexjson:lf11", lf15_12.getAsString());
274
275             }
276         }.validate(jsonOutput);
277     }
278
279     /**
280      * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
281      * with the same names externally (augment B) are added)
282      *
283      * after running this test following exception is raised
284      *
285      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
286      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
287      *
288      */
289 //    @Ignore
290     @Test
291     public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
292         final Writer writer = new StringWriter();
293         final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
294                 .caseNodeExternalAugmentationInChoiceInContainer();
295         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
296                 caseNodeExternalAugmentationInChoiceInContainer);
297         new JsonValidator() {
298
299             @Override
300             public void validate(final String jsonOutput) {
301                 final JsonObject cont1 = resolveCont1(jsonOutput);
302                 assertNotNull(cont1);
303
304                 final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
305                 assertNotNull(lf15_11Augment);
306                 final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
307                 assertNotNull(lf15_12Augment);
308                 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
309                 assertNotNull(lf13);
310                 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
311                 assertNotNull(lf15_11);
312                 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
313                 assertNotNull(lf15_12);
314
315                 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
316                 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
317                 assertEquals("lf13 value", lf13.getAsString());
318                 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
319                 assertEquals("complexjson:lf11", lf15_12.getAsString());
320
321             }
322         }.validate(jsonOutput);
323     }
324
325     /**
326      * augmentation of choice - adding new case
327      *
328      * after running this test following exception is raised
329      *
330      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
331      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
332      *
333      */
334 //    @Ignore
335     @Test
336     public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
337         final Writer writer = new StringWriter();
338         final NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
339                 .choiceNodeAugmentationInContainer();
340         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
341                 choiceNodeAugmentationInContainer);
342         new JsonValidator() {
343
344             @Override
345             public void validate(final String jsonOutput) {
346                 final JsonObject cont1 = resolveCont1(jsonOutput);
347                 assertNotNull(cont1);
348
349                 final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17","lf17");
350                 assertNotNull(lf17);
351                 assertEquals("lf17 value",lf17.getAsString());
352             }
353         }.validate(jsonOutput);
354     }
355
356     @Test
357     public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
358         final Writer writer = new StringWriter();
359         final NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
360                 .unkeyedNodeInContainer();
361         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
362                 unkeyedNodeInContainer);
363         new JsonValidator() {
364
365             @Override
366             public void validate(final String jsonOutput) {
367                 final JsonObject cont1 = resolveCont1(jsonOutput);
368                 assertNotNull(cont1);
369
370                 final JsonArray lst12 = childArray(cont1, "complexjson:lst12","lst12");
371                 assertNotNull(lst12);
372
373                 final Iterator<JsonElement> iterator = lst12.iterator();
374                 assertTrue(iterator.hasNext());
375                 final JsonElement lst12Entry1Raw = iterator.next();
376                 assertFalse(iterator.hasNext());
377
378                 assertTrue(lst12Entry1Raw instanceof JsonObject);
379                 final JsonObject lst12Entry1 = (JsonObject)lst12Entry1Raw;
380                 final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
381                 assertNotNull(lf121);
382
383                 assertEquals("lf121 value",lf121.getAsString());
384
385             }
386         }.validate(jsonOutput);
387
388     }
389
390     @Test
391     public void emptyTypeTest() throws IOException, URISyntaxException {
392         final StringWriter writer = new StringWriter();
393         final ContainerNode emptyStructure = Builders.containerBuilder()
394                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
395                 .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, null))
396                 .build();
397         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
398                 emptyStructure);
399         final JsonObject cont1 = resolveCont1(jsonOutput);
400         final JsonElement emptyObj = cont1.get("empty");
401         assertNotNull(emptyObj);
402         assertTrue(emptyObj instanceof JsonArray);
403         assertEquals(1,emptyObj.getAsJsonArray().size());
404         assertTrue(emptyObj.getAsJsonArray().get(0) instanceof JsonNull);
405     }
406
407     private String normalizedNodeToJsonStreamTransformation(final Writer writer,
408             final NormalizedNode<?, ?> inputStructure) throws IOException {
409
410         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
411         final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
412         nodeWriter.write(inputStructure);
413
414         nodeWriter.close();
415         return writer.toString();
416     }
417
418 }