2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.codec.gson;
10 import com.google.common.collect.Sets;
11 import com.google.gson.JsonArray;
12 import com.google.gson.JsonElement;
13 import com.google.gson.JsonObject;
14 import com.google.gson.JsonPrimitive;
15 import org.junit.BeforeClass;
16 import org.junit.Ignore;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import java.io.IOException;
24 import java.io.StringWriter;
25 import java.io.Writer;
26 import java.net.URISyntaxException;
27 import java.util.HashSet;
28 import java.util.Iterator;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertNotNull;
33 import static org.junit.Assert.assertTrue;
34 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.childArray;
35 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.childPrimitive;
36 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
37 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.resolveCont1;
40 * Each test tests whether json output obtained after transformation contains is corect. The transformation takes
41 * normalized node data structure and transform it to json output. To make it easier validate json output it is loaded
42 * via gson as structure of json elements which are walked and compared with awaited values.
45 public class NormalizedNodeToJsonStreamTest {
47 private static SchemaContext schemaContext;
49 public interface JsonValidator {
50 void validate(final String jsonOutput);
54 public static void initialization() throws IOException, URISyntaxException {
55 schemaContext = loadModules("/complexjson/yang");
59 * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
64 public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
69 * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
74 public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
79 public void leafNodeInContainer() throws IOException, URISyntaxException {
80 Writer writer = new StringWriter();
81 NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
82 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
86 public void validate(String jsonOutput) {
87 JsonObject cont1 = resolveCont1(jsonOutput);
90 JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
92 int asInt = lf11.getAsInt();
93 assertEquals(453, asInt);
95 }.validate(jsonOutput);
100 public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
101 Writer writer = new StringWriter();
102 NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainerMultiline();
103 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
104 new JsonValidator() {
107 public void validate(String jsonOutput) {
108 JsonObject cont1 = resolveCont1(jsonOutput);
109 assertNotNull(cont1);
110 JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
111 assertNotNull(lflst11);
113 HashSet<Object> lflst11Values = Sets.newHashSet();
114 for (JsonElement jsonElement : lflst11) {
115 assertTrue(jsonElement instanceof JsonPrimitive);
116 lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
119 assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"), lflst11Values);
121 }.validate(jsonOutput);
126 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
127 Writer writer = new StringWriter();
128 NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
129 .leafNodeViaAugmentationInContainer();
130 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
131 new JsonValidator() {
134 public void validate(String jsonOutput) {
135 JsonObject cont1 = resolveCont1(jsonOutput);
136 assertNotNull(cont1);
138 JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
139 assertNotNull(lf12_1);
140 String asString = lf12_1.getAsString();
141 assertEquals("lf12 value", asString);
143 }.validate(jsonOutput);
148 public void leafListNodeInContainer() throws IOException, URISyntaxException {
149 Writer writer = new StringWriter();
150 NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainer();
151 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
152 new JsonValidator() {
155 public void validate(String jsonOutput) {
156 JsonObject cont1 = resolveCont1(jsonOutput);
157 assertNotNull(cont1);
158 JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
159 assertNotNull(lflst11);
161 HashSet<Object> lflst11Values = Sets.newHashSet();
162 for (JsonElement jsonElement : lflst11) {
163 assertTrue(jsonElement instanceof JsonPrimitive);
164 lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
167 assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
169 }.validate(jsonOutput);
173 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
174 Writer writer = new StringWriter();
175 NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
176 .keyedListNodeInContainer();
177 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
178 new JsonValidator() {
181 public void validate(String jsonOutput) {
182 JsonObject cont1 = resolveCont1(jsonOutput);
183 assertNotNull(cont1);
184 JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
185 assertNotNull(lst11);
187 Iterator<JsonElement> iterator = lst11.iterator();
188 assertTrue(iterator.hasNext());
189 JsonElement lst11Entry1Raw = iterator.next();
190 assertFalse(iterator.hasNext());
191 assertTrue(lst11Entry1Raw instanceof JsonObject);
192 JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
194 JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
195 assertNotNull(key111);
196 JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
197 assertNotNull(lf112);
198 JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
199 assertNotNull(lf113);
200 JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
201 assertNotNull(lf111);
203 assertEquals("key111 value", key111.getAsString());
204 assertEquals("/complexjson:cont1/complexjson:lflst11", lf112.getAsString());
205 assertEquals("lf113 value", lf113.getAsString());
206 assertEquals("lf111 value", lf111.getAsString());
208 }.validate(jsonOutput);
212 public void choiceNodeInContainer() throws IOException, URISyntaxException {
213 Writer writer = new StringWriter();
214 NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator.choiceNodeInContainer();
215 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
216 new JsonValidator() {
219 public void validate(String jsonOutput) {
220 JsonObject cont1 = resolveCont1(jsonOutput);
221 assertNotNull(cont1);
222 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
225 assertEquals("lf13 value", lf13.getAsString());
227 }.validate(jsonOutput);
231 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
234 * after running this test following exception is raised
236 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
237 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
242 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
243 Writer writer = new StringWriter();
244 NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
245 .caseNodeAugmentationInChoiceInContainer();
246 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, caseNodeAugmentationInChoiceInContainer);
247 new JsonValidator() {
250 public void validate(String jsonOutput) {
251 JsonObject cont1 = resolveCont1(jsonOutput);
252 assertNotNull(cont1);
254 JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
255 assertNotNull(lf15_21);
256 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
258 JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
259 assertNotNull(lf15_11);
260 JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
261 assertNotNull(lf15_12);
263 assertEquals("lf15_21 value", lf15_21.getAsString());
264 assertEquals("lf13 value", lf13.getAsString());
265 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
266 assertEquals("complexjson:lf11", lf15_12.getAsString());
269 }.validate(jsonOutput);
273 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
274 * with the same names externally (augment B) are added)
276 * after running this test following exception is raised
278 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
279 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
284 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
285 Writer writer = new StringWriter();
286 NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
287 .caseNodeExternalAugmentationInChoiceInContainer();
288 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
289 caseNodeExternalAugmentationInChoiceInContainer);
290 new JsonValidator() {
293 public void validate(String jsonOutput) {
294 JsonObject cont1 = resolveCont1(jsonOutput);
295 assertNotNull(cont1);
297 JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
298 assertNotNull(lf15_11Augment);
299 JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
300 assertNotNull(lf15_12Augment);
301 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
303 JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
304 assertNotNull(lf15_11);
305 JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
306 assertNotNull(lf15_12);
308 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
309 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
310 assertEquals("lf13 value", lf13.getAsString());
311 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
312 assertEquals("complexjson:lf11", lf15_12.getAsString());
315 }.validate(jsonOutput);
319 * augmentation of choice - adding new case
321 * after running this test following exception is raised
323 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
324 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
329 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
330 Writer writer = new StringWriter();
331 NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
332 .choiceNodeAugmentationInContainer();
333 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
334 choiceNodeAugmentationInContainer);
335 new JsonValidator() {
338 public void validate(String jsonOutput) {
339 JsonObject cont1 = resolveCont1(jsonOutput);
340 assertNotNull(cont1);
342 JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17","lf17");
344 assertEquals("lf17 value",lf17.getAsString());
346 }.validate(jsonOutput);
350 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
351 Writer writer = new StringWriter();
352 NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
353 .unkeyedNodeInContainer();
354 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
355 unkeyedNodeInContainer);
356 new JsonValidator() {
359 public void validate(String jsonOutput) {
360 JsonObject cont1 = resolveCont1(jsonOutput);
361 assertNotNull(cont1);
363 JsonArray lst12 = childArray(cont1, "complexjson:lst12","lst12");
364 assertNotNull(lst12);
366 Iterator<JsonElement> iterator = lst12.iterator();
367 assertTrue(iterator.hasNext());
368 JsonElement lst12Entry1Raw = iterator.next();
369 assertFalse(iterator.hasNext());
371 assertTrue(lst12Entry1Raw instanceof JsonObject);
372 JsonObject lst12Entry1 = (JsonObject)lst12Entry1Raw;
373 JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
374 assertNotNull(lf121);
376 assertEquals("lf121 value",lf121.getAsString());
379 }.validate(jsonOutput);
383 private String normalizedNodeToJsonStreamTransformation(final Writer writer,
384 final NormalizedNode<?, ?> inputStructure) throws IOException {
386 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
387 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
388 nodeWriter.write(inputStructure);
391 return writer.toString();