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