BUG 1440 - additional tests for data-codec-gson
[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 leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
100         Writer writer = new StringWriter();
101         NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
102                 .leafNodeViaAugmentationInContainer();
103         String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
104         new JsonValidator() {
105
106             @Override
107             public void validate(String jsonOutput) {
108                 JsonObject cont1 = resolveCont1(jsonOutput);
109                 assertNotNull(cont1);
110
111                 JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
112                 assertNotNull(lf12_1);
113                 String asString = lf12_1.getAsString();
114                 assertEquals("lf12 value", asString);
115             }
116         }.validate(jsonOutput);
117
118     }
119
120     @Test
121     public void leafListNodeInContainer() throws IOException, URISyntaxException {
122         Writer writer = new StringWriter();
123         NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainer();
124         String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
125         new JsonValidator() {
126
127             @Override
128             public void validate(String jsonOutput) {
129                 JsonObject cont1 = resolveCont1(jsonOutput);
130                 assertNotNull(cont1);
131                 JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
132                 assertNotNull(lflst11);
133
134                 HashSet<Object> lflst11Values = Sets.newHashSet();
135                 for (JsonElement jsonElement : lflst11) {
136                     assertTrue(jsonElement instanceof JsonPrimitive);
137                     lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
138                 }
139
140                 assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
141             }
142         }.validate(jsonOutput);
143     }
144
145     @Test
146     public void keyedListNodeInContainer() throws IOException, URISyntaxException {
147         Writer writer = new StringWriter();
148         NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
149                 .keyedListNodeInContainer();
150         String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
151         new JsonValidator() {
152
153             @Override
154             public void validate(String jsonOutput) {
155                 JsonObject cont1 = resolveCont1(jsonOutput);
156                 assertNotNull(cont1);
157                 JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
158                 assertNotNull(lst11);
159
160                 Iterator<JsonElement> iterator = lst11.iterator();
161                 assertTrue(iterator.hasNext());
162                 JsonElement lst11Entry1Raw = iterator.next();
163                 assertFalse(iterator.hasNext());
164                 assertTrue(lst11Entry1Raw instanceof JsonObject);
165                 JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
166
167                 JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
168                 assertNotNull(key111);
169                 JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
170                 assertNotNull(lf112);
171                 JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
172                 assertNotNull(lf113);
173                 JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
174                 assertNotNull(lf111);
175
176                 assertEquals("key111 value", key111.getAsString());
177                 assertEquals("/complexjson:cont1/complexjson:lflst11", lf112.getAsString());
178                 assertEquals("lf113 value", lf113.getAsString());
179                 assertEquals("lf111 value", lf111.getAsString());
180             }
181         }.validate(jsonOutput);
182     }
183
184     @Test
185     public void choiceNodeInContainer() throws IOException, URISyntaxException {
186         Writer writer = new StringWriter();
187         NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator.choiceNodeInContainer();
188         String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
189         new JsonValidator() {
190
191             @Override
192             public void validate(String jsonOutput) {
193                 JsonObject cont1 = resolveCont1(jsonOutput);
194                 assertNotNull(cont1);
195                 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
196                 assertNotNull(lf13);
197
198                 assertEquals("lf13 value", lf13.getAsString());
199             }
200         }.validate(jsonOutput);
201     }
202
203     /**
204      * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
205      * added)
206      *
207      * after running this test following exception is raised
208      *
209      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
210      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
211      *
212      */
213 //    @Ignore
214     @Test
215     public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
216         Writer writer = new StringWriter();
217         NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
218                 .caseNodeAugmentationInChoiceInContainer();
219         String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, caseNodeAugmentationInChoiceInContainer);
220         new JsonValidator() {
221
222             @Override
223             public void validate(String jsonOutput) {
224                 JsonObject cont1 = resolveCont1(jsonOutput);
225                 assertNotNull(cont1);
226
227                 JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
228                 assertNotNull(lf15_21);
229                 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
230                 assertNotNull(lf13);
231                 JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
232                 assertNotNull(lf15_11);
233                 JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
234                 assertNotNull(lf15_12);
235
236                 assertEquals("lf15_21 value", lf15_21.getAsString());
237                 assertEquals("lf13 value", lf13.getAsString());
238                 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
239                 assertEquals("complexjson:lf11", lf15_12.getAsString());
240
241             }
242         }.validate(jsonOutput);
243     }
244
245     /**
246      * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
247      * with the same names externally (augment B) are 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 caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
258         Writer writer = new StringWriter();
259         NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
260                 .caseNodeExternalAugmentationInChoiceInContainer();
261         String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
262                 caseNodeExternalAugmentationInChoiceInContainer);
263         new JsonValidator() {
264
265             @Override
266             public void validate(String jsonOutput) {
267                 JsonObject cont1 = resolveCont1(jsonOutput);
268                 assertNotNull(cont1);
269
270                 JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
271                 assertNotNull(lf15_11Augment);
272                 JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
273                 assertNotNull(lf15_12Augment);
274                 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
275                 assertNotNull(lf13);
276                 JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
277                 assertNotNull(lf15_11);
278                 JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
279                 assertNotNull(lf15_12);
280
281                 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
282                 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
283                 assertEquals("lf13 value", lf13.getAsString());
284                 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
285                 assertEquals("complexjson:lf11", lf15_12.getAsString());
286
287             }
288         }.validate(jsonOutput);
289     }
290
291     /**
292      * augmentation of choice - adding new case
293      *
294      * after running this test following exception is raised
295      *
296      * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
297      * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
298      *
299      */
300 //    @Ignore
301     @Test
302     public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
303         Writer writer = new StringWriter();
304         NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
305                 .choiceNodeAugmentationInContainer();
306         String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
307                 choiceNodeAugmentationInContainer);
308         new JsonValidator() {
309
310             @Override
311             public void validate(String jsonOutput) {
312                 JsonObject cont1 = resolveCont1(jsonOutput);
313                 assertNotNull(cont1);
314
315                 JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17","lf17");
316                 assertNotNull(lf17);
317                 assertEquals("lf17 value",lf17.getAsString());
318             }
319         }.validate(jsonOutput);
320     }
321
322     @Test
323     public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
324         Writer writer = new StringWriter();
325         NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
326                 .unkeyedNodeInContainer();
327         String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
328                 unkeyedNodeInContainer);
329         new JsonValidator() {
330
331             @Override
332             public void validate(String jsonOutput) {
333                 JsonObject cont1 = resolveCont1(jsonOutput);
334                 assertNotNull(cont1);
335
336                 JsonArray lst12 = childArray(cont1, "complexjson:lst12","lst12");
337                 assertNotNull(lst12);
338
339                 Iterator<JsonElement> iterator = lst12.iterator();
340                 assertTrue(iterator.hasNext());
341                 JsonElement lst12Entry1Raw = iterator.next();
342                 assertFalse(iterator.hasNext());
343
344                 assertTrue(lst12Entry1Raw instanceof JsonObject);
345                 JsonObject lst12Entry1 = (JsonObject)lst12Entry1Raw;
346                 JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
347                 assertNotNull(lf121);
348
349                 assertEquals("lf121 value",lf121.getAsString());
350
351             }
352         }.validate(jsonOutput);
353
354     }
355
356     private String normalizedNodeToJsonStreamTransformation(final Writer writer,
357             final NormalizedNode<?, ?> inputStructure) throws IOException {
358         writer.write("{\n");
359         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
360         final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
361         nodeWriter.write(inputStructure);
362         writer.write("\n}");
363         nodeWriter.close();
364         return writer.toString();
365     }
366
367 }