Bug 6814: Fix (de)serialization of anyxml in JSON
[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.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 import com.google.common.collect.Sets;
19 import com.google.gson.JsonArray;
20 import com.google.gson.JsonElement;
21 import com.google.gson.JsonNull;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonPrimitive;
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.io.Writer;
27 import java.net.URISyntaxException;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
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, ReactorException {
62         schemaContext = loadModules("/complexjson/yang");
63     }
64
65     @Test
66     public void leafNodeInContainer() throws IOException, URISyntaxException {
67         final Writer writer = new StringWriter();
68         final NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
69         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
70         ((JsonValidator) jsonOutput1 -> {
71             final JsonObject cont1 = resolveCont1(jsonOutput1);
72             assertNotNull(cont1);
73
74             final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
75             assertNotNull(lf11);
76             final int asInt = lf11.getAsInt();
77             assertEquals(453, asInt);
78         }).validate(jsonOutput);
79
80     }
81
82     @Test
83     public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
84         final Writer writer = new StringWriter();
85         final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
86                 .leafListNodeInContainerMultiline();
87         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
88         ((JsonValidator) jsonOutput1 -> {
89             final JsonObject cont1 = resolveCont1(jsonOutput1);
90             assertNotNull(cont1);
91             final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
92             assertNotNull(lflst11);
93
94             final HashSet<Object> lflst11Values = Sets.newHashSet();
95             for (final JsonElement jsonElement : lflst11) {
96                 assertTrue(jsonElement instanceof JsonPrimitive);
97                 lflst11Values.add(jsonElement.getAsString());
98             }
99
100             assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"),
101                     lflst11Values);
102         }).validate(jsonOutput);
103
104     }
105
106     @Test
107     public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
108         final Writer writer = new StringWriter();
109         final NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
110                 .leafNodeViaAugmentationInContainer();
111         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
112         ((JsonValidator) jsonOutput1 -> {
113             final JsonObject cont1 = resolveCont1(jsonOutput1);
114             assertNotNull(cont1);
115
116             final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
117             assertNotNull(lf12_1);
118             final String asString = lf12_1.getAsString();
119             assertEquals("lf12 value", asString);
120         }).validate(jsonOutput);
121
122     }
123
124     @Test
125     public void leafListNodeInContainer() throws IOException, URISyntaxException {
126         final Writer writer = new StringWriter();
127         final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
128                 .leafListNodeInContainer();
129         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
130         ((JsonValidator) jsonOutput1 -> {
131             final JsonObject cont1 = resolveCont1(jsonOutput1);
132             assertNotNull(cont1);
133             final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
134             assertNotNull(lflst11);
135
136             final HashSet<Object> lflst11Values = Sets.newHashSet();
137             for (final JsonElement jsonElement : lflst11) {
138                 assertTrue(jsonElement instanceof JsonPrimitive);
139                 lflst11Values.add(jsonElement.getAsString());
140             }
141
142             assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
143         }).validate(jsonOutput);
144     }
145
146     @Test
147     public void keyedListNodeInContainer() throws IOException, URISyntaxException {
148         final Writer writer = new StringWriter();
149         final NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
150                 .keyedListNodeInContainer();
151         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
152         ((JsonValidator) jsonOutput1 -> {
153             final JsonObject cont1 = resolveCont1(jsonOutput1);
154             assertNotNull(cont1);
155             final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
156             assertNotNull(lst11);
157
158             final Iterator<JsonElement> iterator = lst11.iterator();
159             assertTrue(iterator.hasNext());
160             final JsonElement lst11Entry1Raw = iterator.next();
161             assertFalse(iterator.hasNext());
162             assertTrue(lst11Entry1Raw instanceof JsonObject);
163             final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
164
165             final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
166             assertNotNull(key111);
167             final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
168             assertNotNull(lf112);
169             final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
170             assertNotNull(lf113);
171             final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
172             assertNotNull(lf111);
173
174             assertEquals("key111 value", key111.getAsString());
175             assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
176             assertEquals("lf113 value", lf113.getAsString());
177             assertEquals("lf111 value", lf111.getAsString());
178         }).validate(jsonOutput);
179     }
180
181     @Test
182     public void choiceNodeInContainer() throws IOException, URISyntaxException {
183         final Writer writer = new StringWriter();
184         final NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator
185                 .choiceNodeInContainer();
186         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
187         ((JsonValidator) jsonOutput1 -> {
188             final JsonObject cont1 = resolveCont1(jsonOutput1);
189             assertNotNull(cont1);
190             final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
191             assertNotNull(lf13);
192
193             assertEquals("lf13 value", lf13.getAsString());
194         }).validate(jsonOutput);
195     }
196
197     /**
198      * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
199      * added)
200      *
201      * after running this test following exception is raised
202      *
203      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
204      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
205      *
206      */
207     // @Ignore
208     @Test
209     public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
210         final Writer writer = new StringWriter();
211         final NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
212                 .caseNodeAugmentationInChoiceInContainer();
213         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
214                 caseNodeAugmentationInChoiceInContainer);
215         ((JsonValidator) jsonOutput1 -> {
216             final JsonObject cont1 = resolveCont1(jsonOutput1);
217             assertNotNull(cont1);
218
219             final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
220             assertNotNull(lf15_21);
221             final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
222             assertNotNull(lf13);
223             final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
224             assertNotNull(lf15_11);
225             final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
226             assertNotNull(lf15_12);
227
228             assertEquals("lf15_21 value", lf15_21.getAsString());
229             assertEquals("lf13 value", lf13.getAsString());
230             assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
231             assertEquals("complexjson:lf11", lf15_12.getAsString());
232
233         }).validate(jsonOutput);
234     }
235
236     /**
237      * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
238      * with the same names externally (augment B) are added)
239      *
240      * after running this test following exception is raised
241      *
242      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
243      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
244      *
245      */
246     // @Ignore
247     @Test
248     public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
249         final Writer writer = new StringWriter();
250         final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
251                 .caseNodeExternalAugmentationInChoiceInContainer();
252         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
253                 caseNodeExternalAugmentationInChoiceInContainer);
254         ((JsonValidator) jsonOutput1 -> {
255             final JsonObject cont1 = resolveCont1(jsonOutput1);
256             assertNotNull(cont1);
257
258             final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
259             assertNotNull(lf15_11Augment);
260             final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
261             assertNotNull(lf15_12Augment);
262             final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
263             assertNotNull(lf13);
264             final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
265             assertNotNull(lf15_11);
266             final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
267             assertNotNull(lf15_12);
268
269             assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
270             assertEquals("lf15_12 value from augmentation", lf15_12Augment.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         }).validate(jsonOutput);
276     }
277
278     /**
279      * augmentation of choice - adding new case
280      *
281      * after running this test following exception is raised
282      *
283      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
284      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
285      *
286      */
287     // @Ignore
288     @Test
289     public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
290         final Writer writer = new StringWriter();
291         final NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
292                 .choiceNodeAugmentationInContainer();
293         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeAugmentationInContainer);
294         ((JsonValidator) jsonOutput1 -> {
295             final JsonObject cont1 = resolveCont1(jsonOutput1);
296             assertNotNull(cont1);
297
298             final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17", "lf17");
299             assertNotNull(lf17);
300             assertEquals("lf17 value", lf17.getAsString());
301         }).validate(jsonOutput);
302     }
303
304     @Test
305     public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
306         final Writer writer = new StringWriter();
307         final NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
308                 .unkeyedNodeInContainer();
309         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, unkeyedNodeInContainer);
310         ((JsonValidator) jsonOutput1 -> {
311             final JsonObject cont1 = resolveCont1(jsonOutput1);
312             assertNotNull(cont1);
313
314             final JsonArray lst12 = childArray(cont1, "complexjson:lst12", "lst12");
315             assertNotNull(lst12);
316
317             final Iterator<JsonElement> iterator = lst12.iterator();
318             assertTrue(iterator.hasNext());
319             final JsonElement lst12Entry1Raw = iterator.next();
320             assertFalse(iterator.hasNext());
321
322             assertTrue(lst12Entry1Raw instanceof JsonObject);
323             final JsonObject lst12Entry1 = (JsonObject) lst12Entry1Raw;
324             final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
325             assertNotNull(lf121);
326
327             assertEquals("lf121 value", lf121.getAsString());
328
329         }).validate(jsonOutput);
330
331     }
332
333     @Test
334     public void emptyTypeTest() throws IOException, URISyntaxException {
335         final StringWriter writer = new StringWriter();
336         final ContainerNode emptyStructure = Builders.containerBuilder()
337                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
338                 .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, null)).build();
339         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, emptyStructure);
340         final JsonObject cont1 = resolveCont1(jsonOutput);
341         final JsonElement emptyObj = cont1.get("empty");
342         assertNotNull(emptyObj);
343         assertTrue(emptyObj instanceof JsonArray);
344         assertEquals(1, emptyObj.getAsJsonArray().size());
345         assertTrue(emptyObj.getAsJsonArray().get(0) instanceof JsonNull);
346     }
347
348     private static String normalizedNodeToJsonStreamTransformation(final Writer writer,
349             final NormalizedNode<?, ?> inputStructure) throws IOException {
350
351         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.
352                 createExclusiveWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null,
353                     JsonWriterFactory.createJsonWriter(writer, 2));
354         final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
355         nodeWriter.write(inputStructure);
356
357         nodeWriter.close();
358         return writer.toString();
359     }
360
361 }