Make sure root JSON context is properly flushed
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / NormalizedNodeToJsonStreamTest.java
1 /*
2  * Copyright (c) 2016 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.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.childArray;
16 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.childPrimitive;
17 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.resolveCont1;
18 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.resolveCont2;
19
20 import com.google.common.collect.ImmutableSet;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonNull;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonPrimitive;
26 import java.io.IOException;
27 import java.io.StringWriter;
28 import java.io.Writer;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import org.junit.Test;
32 import org.opendaylight.yangtools.yang.common.Empty;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41
42 /**
43  * Each test tests whether json output obtained after transformation contains is corect. The transformation takes
44  * normalized node data structure and transform it to json output. To make it easier validate json output it is loaded
45  * via gson as structure of json elements which are walked and compared with awaited values.
46  */
47 public class NormalizedNodeToJsonStreamTest extends AbstractComplexJsonTest {
48
49     private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
50     private static final QName EMPTY_LEAF = QName.create(CONT_1, "empty");
51
52     @Test
53     public void leafNodeInContainer() throws IOException {
54         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
55             TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
56         final JsonObject cont1 = resolveCont1(jsonOutput);
57         assertNotNull(cont1);
58
59         final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
60         assertNotNull(lf11);
61         final int asInt = lf11.getAsInt();
62         assertEquals(453, asInt);
63     }
64
65     @Test
66     public void leafListNodeInContainerMultiline() throws IOException {
67         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
68             TestingNormalizedNodeStructuresCreator.leafListNodeInContainerMultiline());
69         final JsonObject cont1 = resolveCont1(jsonOutput);
70         assertNotNull(cont1);
71         final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
72         assertNotNull(lflst11);
73
74         final HashSet<Object> lflst11Values = new HashSet<>();
75         for (final JsonElement jsonElement : lflst11) {
76             assertTrue(jsonElement instanceof JsonPrimitive);
77             lflst11Values.add(jsonElement.getAsString());
78         }
79
80         assertEquals(ImmutableSet.of("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"),
81             lflst11Values);
82     }
83
84     @Test
85     public void leafNodeViaAugmentationInContainer() throws IOException {
86         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
87             TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
88         final JsonObject cont1 = resolveCont1(jsonOutput);
89         assertNotNull(cont1);
90
91         final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
92         assertNotNull(lf12_1);
93         final String asString = lf12_1.getAsString();
94         assertEquals("lf12 value", asString);
95     }
96
97     @Test
98     public void leafListNodeInContainer() throws IOException {
99         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
100             TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
101         final JsonObject cont1 = resolveCont1(jsonOutput);
102         assertNotNull(cont1);
103         final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
104         assertNotNull(lflst11);
105
106         final HashSet<Object> lflst11Values = new HashSet<>();
107         for (final JsonElement jsonElement : lflst11) {
108             assertTrue(jsonElement instanceof JsonPrimitive);
109             lflst11Values.add(jsonElement.getAsString());
110         }
111
112         assertEquals(ImmutableSet.of("lflst11 value2", "lflst11 value1"), lflst11Values);
113     }
114
115     @Test
116     public void keyedListNodeInContainer() throws IOException {
117         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
118             TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
119         final JsonObject cont1 = resolveCont1(jsonOutput);
120         assertNotNull(cont1);
121         final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
122         assertNotNull(lst11);
123
124         final Iterator<JsonElement> iterator = lst11.iterator();
125         assertTrue(iterator.hasNext());
126         final JsonElement lst11Entry1Raw = iterator.next();
127         assertFalse(iterator.hasNext());
128         assertTrue(lst11Entry1Raw instanceof JsonObject);
129         final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
130
131         final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
132         assertNotNull(key111);
133         final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
134         assertNotNull(lf112);
135         final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
136         assertNotNull(lf113);
137         final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
138         assertNotNull(lf111);
139
140         assertEquals("key111 value", key111.getAsString());
141         assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
142         assertEquals("lf113 value", lf113.getAsString());
143         assertEquals("lf111 value", lf111.getAsString());
144     }
145
146     @Test
147     public void choiceNodeInContainer() throws IOException {
148         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
149             TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
150         final JsonObject cont1 = resolveCont1(jsonOutput);
151         assertNotNull(cont1);
152         final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
153         assertNotNull(lf13);
154
155         assertEquals("lf13 value", lf13.getAsString());
156     }
157
158     /**
159      * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
160      * added).
161      *
162      * <p>
163      * after running this test following exception is raised:
164      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
165      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
166      */
167     @Test
168     public void caseNodeAugmentationInChoiceInContainer() throws IOException {
169         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
170             TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
171         final JsonObject cont1 = resolveCont1(jsonOutput);
172         assertNotNull(cont1);
173
174         final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
175         assertNotNull(lf15_21);
176         final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
177         assertNotNull(lf13);
178         final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
179         assertNotNull(lf15_11);
180         final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
181         assertNotNull(lf15_12);
182
183         assertEquals("lf15_21 value", lf15_21.getAsString());
184         assertEquals("lf13 value", lf13.getAsString());
185         assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
186         assertEquals("complexjson:ident", lf15_12.getAsString());
187     }
188
189     /**
190      * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
191      * with the same names externally (augment B) are added).
192      *
193      * <p>
194      * after running this test following exception is raised:
195      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
196      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
197      */
198     @Test
199     public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException {
200         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
201             TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
202         final JsonObject cont1 = resolveCont1(jsonOutput);
203         assertNotNull(cont1);
204
205         final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
206         assertNotNull(lf15_11Augment);
207         final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
208         assertNotNull(lf15_12Augment);
209         final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
210         assertNotNull(lf13);
211         final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
212         assertNotNull(lf15_11);
213         final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
214         assertNotNull(lf15_12);
215
216         assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
217         assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
218         assertEquals("lf13 value", lf13.getAsString());
219         assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
220         assertEquals("complexjson:ident", lf15_12.getAsString());
221     }
222
223     /**
224      * augmentation of choice - adding new case.
225      *
226      * <p>
227      * after running this test following exception is raised:
228      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
229      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
230      */
231     @Test
232     public void choiceNodeAugmentationInContainer() throws IOException {
233         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
234             TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
235         final JsonObject cont1 = resolveCont1(jsonOutput);
236         assertNotNull(cont1);
237
238         final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17", "lf17");
239         assertNotNull(lf17);
240         assertEquals("lf17 value", lf17.getAsString());
241     }
242
243     @Test
244     public void unkeyedNodeInContainer() throws IOException {
245         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
246             TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
247         final JsonObject cont1 = resolveCont1(jsonOutput);
248         assertNotNull(cont1);
249
250         final JsonArray lst12 = childArray(cont1, "complexjson:lst12", "lst12");
251         assertNotNull(lst12);
252
253         final Iterator<JsonElement> iterator = lst12.iterator();
254         assertTrue(iterator.hasNext());
255         final JsonElement lst12Entry1Raw = iterator.next();
256         assertFalse(iterator.hasNext());
257
258         assertTrue(lst12Entry1Raw instanceof JsonObject);
259         final JsonObject lst12Entry1 = (JsonObject) lst12Entry1Raw;
260         final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
261         assertNotNull(lf121);
262
263         assertEquals("lf121 value", lf121.getAsString());
264     }
265
266     @Test
267     public void emptyTypeTest() throws IOException {
268         final String jsonOutput = normalizedNodeToJsonStreamTransformation(Builders.containerBuilder()
269             .withNodeIdentifier(new NodeIdentifier(CONT_1))
270             .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, Empty.getInstance()))
271             .build());
272         final JsonObject cont1 = resolveCont1(jsonOutput);
273         final JsonElement emptyObj = cont1.get("empty");
274         assertNotNull(emptyObj);
275         assertTrue(emptyObj instanceof JsonArray);
276         assertEquals(1, emptyObj.getAsJsonArray().size());
277         assertTrue(emptyObj.getAsJsonArray().get(0) instanceof JsonNull);
278     }
279
280     @Test
281     public void emptyNonPresenceContainerTest() throws IOException {
282         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
283             TestingNormalizedNodeStructuresCreator.topLevelContainer());
284         final JsonObject cont1 = resolveCont1(jsonOutput);
285         assertNull(cont1);
286     }
287
288     @Test
289     public void emptyNonPresenceContainerInContainerTest() throws IOException {
290         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
291             TestingNormalizedNodeStructuresCreator.emptyContainerInContainer());
292         final JsonObject cont1 = resolveCont1(jsonOutput);
293         assertNull(cont1);
294     }
295
296     @Test
297     public void emptyPresenceContainerTest() throws IOException {
298         final String jsonOutput = normalizedNodeToJsonStreamTransformation(
299             TestingNormalizedNodeStructuresCreator.cont2Node());
300         final JsonObject cont2 = resolveCont2(jsonOutput);
301         assertNotNull(cont2);
302     }
303
304     private static String normalizedNodeToJsonStreamTransformation(final NormalizedNode<?, ?> inputStructure)
305             throws IOException {
306         final Writer writer = new StringWriter();
307         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
308             lhotkaCodecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
309         try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
310             nodeWriter.write(inputStructure);
311         }
312
313         return writer.toString();
314     }
315 }