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