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