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