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