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