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