2 * Copyright (c) 2015 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.retest;
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.retest.TestUtils.childArray;
15 import static org.opendaylight.yangtools.yang.data.codec.gson.retest.TestUtils.childPrimitive;
16 import static org.opendaylight.yangtools.yang.data.codec.gson.retest.TestUtils.loadModules;
17 import static org.opendaylight.yangtools.yang.data.codec.gson.retest.TestUtils.resolveCont1;
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.Ignore;
32 import org.junit.Test;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
39 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
40 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
41 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
49 * Each test tests whether json output obtained after transformation contains is corect. The transformation takes
50 * normalized node data structure and transform it to json output. To make it easier validate json output it is loaded
51 * via gson as structure of json elements which are walked and compared with awaited values.
54 public class NormalizedNodeToJsonStreamTest {
56 private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
57 private static final QName EMPTY_LEAF = QName.create(CONT_1, "empty");
58 private static SchemaContext schemaContext;
60 public interface JsonValidator {
61 void validate(final String jsonOutput);
65 public static void initialization() throws IOException, URISyntaxException, ReactorException {
66 schemaContext = loadModules("/complexjson/yang");
70 * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
75 public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
80 * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
85 public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
90 public void leafNodeInContainer() throws IOException, URISyntaxException {
91 final Writer writer = new StringWriter();
92 final NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
93 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
97 public void validate(final String jsonOutput) {
98 final JsonObject cont1 = resolveCont1(jsonOutput);
101 final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
103 final int asInt = lf11.getAsInt();
104 assertEquals(453, asInt);
106 }.validate(jsonOutput);
111 public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
112 final Writer writer = new StringWriter();
113 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
114 .leafListNodeInContainerMultiline();
115 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
116 new JsonValidator() {
119 public void validate(final String jsonOutput) {
120 final JsonObject cont1 = resolveCont1(jsonOutput);
121 assertNotNull(cont1);
122 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
123 assertNotNull(lflst11);
125 final HashSet<Object> lflst11Values = Sets.newHashSet();
126 for (final JsonElement jsonElement : lflst11) {
127 assertTrue(jsonElement instanceof JsonPrimitive);
128 lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
131 assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"),
134 }.validate(jsonOutput);
139 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
140 final Writer writer = new StringWriter();
141 final NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
142 .leafNodeViaAugmentationInContainer();
143 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
144 new JsonValidator() {
147 public void validate(final String jsonOutput) {
148 final JsonObject cont1 = resolveCont1(jsonOutput);
149 assertNotNull(cont1);
151 final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
152 assertNotNull(lf12_1);
153 final String asString = lf12_1.getAsString();
154 assertEquals("lf12 value", asString);
156 }.validate(jsonOutput);
161 public void leafListNodeInContainer() throws IOException, URISyntaxException {
162 final Writer writer = new StringWriter();
163 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
164 .leafListNodeInContainer();
165 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
166 new JsonValidator() {
169 public void validate(final String jsonOutput) {
170 final JsonObject cont1 = resolveCont1(jsonOutput);
171 assertNotNull(cont1);
172 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
173 assertNotNull(lflst11);
175 final HashSet<Object> lflst11Values = Sets.newHashSet();
176 for (final JsonElement jsonElement : lflst11) {
177 assertTrue(jsonElement instanceof JsonPrimitive);
178 lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
181 assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
183 }.validate(jsonOutput);
187 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
188 final Writer writer = new StringWriter();
189 final NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
190 .keyedListNodeInContainer();
191 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
192 new JsonValidator() {
195 public void validate(final String jsonOutput) {
196 final JsonObject cont1 = resolveCont1(jsonOutput);
197 assertNotNull(cont1);
198 final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
199 assertNotNull(lst11);
201 final Iterator<JsonElement> iterator = lst11.iterator();
202 assertTrue(iterator.hasNext());
203 final JsonElement lst11Entry1Raw = iterator.next();
204 assertFalse(iterator.hasNext());
205 assertTrue(lst11Entry1Raw instanceof JsonObject);
206 final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
208 final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
209 assertNotNull(key111);
210 final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
211 assertNotNull(lf112);
212 final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
213 assertNotNull(lf113);
214 final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
215 assertNotNull(lf111);
217 assertEquals("key111 value", key111.getAsString());
218 assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
219 assertEquals("lf113 value", lf113.getAsString());
220 assertEquals("lf111 value", lf111.getAsString());
222 }.validate(jsonOutput);
226 public void choiceNodeInContainer() throws IOException, URISyntaxException {
227 final Writer writer = new StringWriter();
228 final NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator
229 .choiceNodeInContainer();
230 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
231 new JsonValidator() {
234 public void validate(final String jsonOutput) {
235 final JsonObject cont1 = resolveCont1(jsonOutput);
236 assertNotNull(cont1);
237 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
240 assertEquals("lf13 value", lf13.getAsString());
242 }.validate(jsonOutput);
246 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
249 * after running this test following exception is raised
251 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
252 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
257 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
258 final Writer writer = new StringWriter();
259 final NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
260 .caseNodeAugmentationInChoiceInContainer();
261 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
262 caseNodeAugmentationInChoiceInContainer);
263 new JsonValidator() {
266 public void validate(final String jsonOutput) {
267 final JsonObject cont1 = resolveCont1(jsonOutput);
268 assertNotNull(cont1);
270 final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
271 assertNotNull(lf15_21);
272 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
274 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
275 assertNotNull(lf15_11);
276 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
277 assertNotNull(lf15_12);
279 assertEquals("lf15_21 value", lf15_21.getAsString());
280 assertEquals("lf13 value", lf13.getAsString());
281 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
282 assertEquals("complexjson:lf11", lf15_12.getAsString());
285 }.validate(jsonOutput);
289 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
290 * with the same names externally (augment B) are added)
292 * after running this test following exception is raised
294 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
295 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
300 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
301 final Writer writer = new StringWriter();
302 final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
303 .caseNodeExternalAugmentationInChoiceInContainer();
304 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
305 caseNodeExternalAugmentationInChoiceInContainer);
306 new JsonValidator() {
309 public void validate(final String jsonOutput) {
310 final JsonObject cont1 = resolveCont1(jsonOutput);
311 assertNotNull(cont1);
313 final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
314 assertNotNull(lf15_11Augment);
315 final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
316 assertNotNull(lf15_12Augment);
317 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
319 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
320 assertNotNull(lf15_11);
321 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
322 assertNotNull(lf15_12);
324 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
325 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
326 assertEquals("lf13 value", lf13.getAsString());
327 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
328 assertEquals("complexjson:lf11", lf15_12.getAsString());
331 }.validate(jsonOutput);
335 * augmentation of choice - adding new case
337 * after running this test following exception is raised
339 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
340 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
345 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
346 final Writer writer = new StringWriter();
347 final NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
348 .choiceNodeAugmentationInContainer();
349 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeAugmentationInContainer);
350 new JsonValidator() {
353 public void validate(final String jsonOutput) {
354 final JsonObject cont1 = resolveCont1(jsonOutput);
355 assertNotNull(cont1);
357 final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17", "lf17");
359 assertEquals("lf17 value", lf17.getAsString());
361 }.validate(jsonOutput);
365 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
366 final Writer writer = new StringWriter();
367 final NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
368 .unkeyedNodeInContainer();
369 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, unkeyedNodeInContainer);
370 new JsonValidator() {
373 public void validate(final String jsonOutput) {
374 final JsonObject cont1 = resolveCont1(jsonOutput);
375 assertNotNull(cont1);
377 final JsonArray lst12 = childArray(cont1, "complexjson:lst12", "lst12");
378 assertNotNull(lst12);
380 final Iterator<JsonElement> iterator = lst12.iterator();
381 assertTrue(iterator.hasNext());
382 final JsonElement lst12Entry1Raw = iterator.next();
383 assertFalse(iterator.hasNext());
385 assertTrue(lst12Entry1Raw instanceof JsonObject);
386 final JsonObject lst12Entry1 = (JsonObject) lst12Entry1Raw;
387 final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
388 assertNotNull(lf121);
390 assertEquals("lf121 value", lf121.getAsString());
393 }.validate(jsonOutput);
398 public void emptyTypeTest() throws IOException, URISyntaxException {
399 final StringWriter writer = new StringWriter();
400 final ContainerNode emptyStructure = Builders.containerBuilder()
401 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
402 .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, null)).build();
403 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, emptyStructure);
404 final JsonObject cont1 = resolveCont1(jsonOutput);
405 final JsonElement emptyObj = cont1.get("empty");
406 assertNotNull(emptyObj);
407 assertTrue(emptyObj instanceof JsonArray);
408 assertEquals(1, emptyObj.getAsJsonArray().size());
409 assertTrue(emptyObj.getAsJsonArray().get(0) instanceof JsonNull);
412 private static String normalizedNodeToJsonStreamTransformation(final Writer writer,
413 final NormalizedNode<?, ?> inputStructure) throws IOException {
415 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.
416 createExclusiveWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null,
417 JsonWriterFactory.createJsonWriter(writer, 2));
418 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
419 nodeWriter.write(inputStructure);
422 return writer.toString();