2 * Copyright (c) 2016 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 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 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.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
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.
51 public class NormalizedNodeToJsonStreamTest {
53 private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
54 private static final QName EMPTY_LEAF = QName.create(CONT_1, "empty");
55 private static SchemaContext schemaContext;
57 public interface JsonValidator {
58 void validate(final String jsonOutput);
62 public static void initialization() throws IOException, URISyntaxException, ReactorException {
63 schemaContext = loadModules("/complexjson/yang");
67 * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
72 public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
77 * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
82 public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
87 public void leafNodeInContainer() throws IOException, URISyntaxException {
88 final Writer writer = new StringWriter();
89 final NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
90 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
94 public void validate(final String jsonOutput) {
95 final JsonObject cont1 = resolveCont1(jsonOutput);
98 final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
100 final int asInt = lf11.getAsInt();
101 assertEquals(453, asInt);
103 }.validate(jsonOutput);
108 public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
109 final Writer writer = new StringWriter();
110 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
111 .leafListNodeInContainerMultiline();
112 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
113 new JsonValidator() {
116 public void validate(final String jsonOutput) {
117 final JsonObject cont1 = resolveCont1(jsonOutput);
118 assertNotNull(cont1);
119 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
120 assertNotNull(lflst11);
122 final HashSet<Object> lflst11Values = Sets.newHashSet();
123 for (final JsonElement jsonElement : lflst11) {
124 assertTrue(jsonElement instanceof JsonPrimitive);
125 lflst11Values.add(jsonElement.getAsString());
128 assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"),
131 }.validate(jsonOutput);
136 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
137 final Writer writer = new StringWriter();
138 final NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
139 .leafNodeViaAugmentationInContainer();
140 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
141 new JsonValidator() {
144 public void validate(final String jsonOutput) {
145 final JsonObject cont1 = resolveCont1(jsonOutput);
146 assertNotNull(cont1);
148 final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
149 assertNotNull(lf12_1);
150 final String asString = lf12_1.getAsString();
151 assertEquals("lf12 value", asString);
153 }.validate(jsonOutput);
158 public void leafListNodeInContainer() throws IOException, URISyntaxException {
159 final Writer writer = new StringWriter();
160 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
161 .leafListNodeInContainer();
162 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
163 new JsonValidator() {
166 public void validate(final String jsonOutput) {
167 final JsonObject cont1 = resolveCont1(jsonOutput);
168 assertNotNull(cont1);
169 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
170 assertNotNull(lflst11);
172 final HashSet<Object> lflst11Values = Sets.newHashSet();
173 for (final JsonElement jsonElement : lflst11) {
174 assertTrue(jsonElement instanceof JsonPrimitive);
175 lflst11Values.add(jsonElement.getAsString());
178 assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
180 }.validate(jsonOutput);
184 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
185 final Writer writer = new StringWriter();
186 final NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
187 .keyedListNodeInContainer();
188 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
189 new JsonValidator() {
192 public void validate(final String jsonOutput) {
193 final JsonObject cont1 = resolveCont1(jsonOutput);
194 assertNotNull(cont1);
195 final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
196 assertNotNull(lst11);
198 final Iterator<JsonElement> iterator = lst11.iterator();
199 assertTrue(iterator.hasNext());
200 final JsonElement lst11Entry1Raw = iterator.next();
201 assertFalse(iterator.hasNext());
202 assertTrue(lst11Entry1Raw instanceof JsonObject);
203 final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
205 final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
206 assertNotNull(key111);
207 final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
208 assertNotNull(lf112);
209 final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
210 assertNotNull(lf113);
211 final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
212 assertNotNull(lf111);
214 assertEquals("key111 value", key111.getAsString());
215 assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
216 assertEquals("lf113 value", lf113.getAsString());
217 assertEquals("lf111 value", lf111.getAsString());
219 }.validate(jsonOutput);
223 public void choiceNodeInContainer() throws IOException, URISyntaxException {
224 final Writer writer = new StringWriter();
225 final NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator
226 .choiceNodeInContainer();
227 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
228 new JsonValidator() {
231 public void validate(final String jsonOutput) {
232 final JsonObject cont1 = resolveCont1(jsonOutput);
233 assertNotNull(cont1);
234 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
237 assertEquals("lf13 value", lf13.getAsString());
239 }.validate(jsonOutput);
243 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
246 * after running this test following exception is raised
248 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
249 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
254 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
255 final Writer writer = new StringWriter();
256 final NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
257 .caseNodeAugmentationInChoiceInContainer();
258 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
259 caseNodeAugmentationInChoiceInContainer);
260 new JsonValidator() {
263 public void validate(final String jsonOutput) {
264 final JsonObject cont1 = resolveCont1(jsonOutput);
265 assertNotNull(cont1);
267 final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
268 assertNotNull(lf15_21);
269 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
271 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
272 assertNotNull(lf15_11);
273 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
274 assertNotNull(lf15_12);
276 assertEquals("lf15_21 value", lf15_21.getAsString());
277 assertEquals("lf13 value", lf13.getAsString());
278 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
279 assertEquals("complexjson:lf11", lf15_12.getAsString());
282 }.validate(jsonOutput);
286 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
287 * with the same names externally (augment B) are added)
289 * after running this test following exception is raised
291 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
292 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
297 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
298 final Writer writer = new StringWriter();
299 final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
300 .caseNodeExternalAugmentationInChoiceInContainer();
301 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
302 caseNodeExternalAugmentationInChoiceInContainer);
303 new JsonValidator() {
306 public void validate(final String jsonOutput) {
307 final JsonObject cont1 = resolveCont1(jsonOutput);
308 assertNotNull(cont1);
310 final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
311 assertNotNull(lf15_11Augment);
312 final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
313 assertNotNull(lf15_12Augment);
314 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
316 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
317 assertNotNull(lf15_11);
318 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
319 assertNotNull(lf15_12);
321 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
322 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
323 assertEquals("lf13 value", lf13.getAsString());
324 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
325 assertEquals("complexjson:lf11", lf15_12.getAsString());
328 }.validate(jsonOutput);
332 * augmentation of choice - adding new case
334 * after running this test following exception is raised
336 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
337 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
342 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
343 final Writer writer = new StringWriter();
344 final NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
345 .choiceNodeAugmentationInContainer();
346 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeAugmentationInContainer);
347 new JsonValidator() {
350 public void validate(final String jsonOutput) {
351 final JsonObject cont1 = resolveCont1(jsonOutput);
352 assertNotNull(cont1);
354 final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17", "lf17");
356 assertEquals("lf17 value", lf17.getAsString());
358 }.validate(jsonOutput);
362 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
363 final Writer writer = new StringWriter();
364 final NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
365 .unkeyedNodeInContainer();
366 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, unkeyedNodeInContainer);
367 new JsonValidator() {
370 public void validate(final String jsonOutput) {
371 final JsonObject cont1 = resolveCont1(jsonOutput);
372 assertNotNull(cont1);
374 final JsonArray lst12 = childArray(cont1, "complexjson:lst12", "lst12");
375 assertNotNull(lst12);
377 final Iterator<JsonElement> iterator = lst12.iterator();
378 assertTrue(iterator.hasNext());
379 final JsonElement lst12Entry1Raw = iterator.next();
380 assertFalse(iterator.hasNext());
382 assertTrue(lst12Entry1Raw instanceof JsonObject);
383 final JsonObject lst12Entry1 = (JsonObject) lst12Entry1Raw;
384 final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
385 assertNotNull(lf121);
387 assertEquals("lf121 value", lf121.getAsString());
390 }.validate(jsonOutput);
395 public void emptyTypeTest() throws IOException, URISyntaxException {
396 final StringWriter writer = new StringWriter();
397 final ContainerNode emptyStructure = Builders.containerBuilder()
398 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
399 .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, null)).build();
400 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, emptyStructure);
401 final JsonObject cont1 = resolveCont1(jsonOutput);
402 final JsonElement emptyObj = cont1.get("empty");
403 assertNotNull(emptyObj);
404 assertTrue(emptyObj instanceof JsonArray);
405 assertEquals(1, emptyObj.getAsJsonArray().size());
406 assertTrue(emptyObj.getAsJsonArray().get(0) instanceof JsonNull);
409 private static String normalizedNodeToJsonStreamTransformation(final Writer writer,
410 final NormalizedNode<?, ?> inputStructure) throws IOException {
412 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.
413 createExclusiveWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null,
414 JsonWriterFactory.createJsonWriter(writer, 2));
415 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
416 nodeWriter.write(inputStructure);
419 return writer.toString();