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);
91 ((JsonValidator) jsonOutput1 -> {
92 final JsonObject cont1 = resolveCont1(jsonOutput1);
95 final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
97 final int asInt = lf11.getAsInt();
98 assertEquals(453, asInt);
99 }).validate(jsonOutput);
104 public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
105 final Writer writer = new StringWriter();
106 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
107 .leafListNodeInContainerMultiline();
108 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
109 ((JsonValidator) jsonOutput1 -> {
110 final JsonObject cont1 = resolveCont1(jsonOutput1);
111 assertNotNull(cont1);
112 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
113 assertNotNull(lflst11);
115 final HashSet<Object> lflst11Values = Sets.newHashSet();
116 for (final JsonElement jsonElement : lflst11) {
117 assertTrue(jsonElement instanceof JsonPrimitive);
118 lflst11Values.add(jsonElement.getAsString());
121 assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"),
123 }).validate(jsonOutput);
128 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
129 final Writer writer = new StringWriter();
130 final NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
131 .leafNodeViaAugmentationInContainer();
132 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
133 ((JsonValidator) jsonOutput1 -> {
134 final JsonObject cont1 = resolveCont1(jsonOutput1);
135 assertNotNull(cont1);
137 final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
138 assertNotNull(lf12_1);
139 final String asString = lf12_1.getAsString();
140 assertEquals("lf12 value", asString);
141 }).validate(jsonOutput);
146 public void leafListNodeInContainer() throws IOException, URISyntaxException {
147 final Writer writer = new StringWriter();
148 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
149 .leafListNodeInContainer();
150 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
151 ((JsonValidator) jsonOutput1 -> {
152 final JsonObject cont1 = resolveCont1(jsonOutput1);
153 assertNotNull(cont1);
154 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
155 assertNotNull(lflst11);
157 final HashSet<Object> lflst11Values = Sets.newHashSet();
158 for (final JsonElement jsonElement : lflst11) {
159 assertTrue(jsonElement instanceof JsonPrimitive);
160 lflst11Values.add(jsonElement.getAsString());
163 assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
164 }).validate(jsonOutput);
168 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
169 final Writer writer = new StringWriter();
170 final NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
171 .keyedListNodeInContainer();
172 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
173 ((JsonValidator) jsonOutput1 -> {
174 final JsonObject cont1 = resolveCont1(jsonOutput1);
175 assertNotNull(cont1);
176 final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
177 assertNotNull(lst11);
179 final Iterator<JsonElement> iterator = lst11.iterator();
180 assertTrue(iterator.hasNext());
181 final JsonElement lst11Entry1Raw = iterator.next();
182 assertFalse(iterator.hasNext());
183 assertTrue(lst11Entry1Raw instanceof JsonObject);
184 final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
186 final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
187 assertNotNull(key111);
188 final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
189 assertNotNull(lf112);
190 final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
191 assertNotNull(lf113);
192 final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
193 assertNotNull(lf111);
195 assertEquals("key111 value", key111.getAsString());
196 assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
197 assertEquals("lf113 value", lf113.getAsString());
198 assertEquals("lf111 value", lf111.getAsString());
199 }).validate(jsonOutput);
203 public void choiceNodeInContainer() throws IOException, URISyntaxException {
204 final Writer writer = new StringWriter();
205 final NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator
206 .choiceNodeInContainer();
207 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
208 ((JsonValidator) jsonOutput1 -> {
209 final JsonObject cont1 = resolveCont1(jsonOutput1);
210 assertNotNull(cont1);
211 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
214 assertEquals("lf13 value", lf13.getAsString());
215 }).validate(jsonOutput);
219 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
222 * after running this test following exception is raised
224 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
225 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
230 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
231 final Writer writer = new StringWriter();
232 final NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
233 .caseNodeAugmentationInChoiceInContainer();
234 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
235 caseNodeAugmentationInChoiceInContainer);
236 ((JsonValidator) jsonOutput1 -> {
237 final JsonObject cont1 = resolveCont1(jsonOutput1);
238 assertNotNull(cont1);
240 final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
241 assertNotNull(lf15_21);
242 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
244 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
245 assertNotNull(lf15_11);
246 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
247 assertNotNull(lf15_12);
249 assertEquals("lf15_21 value", lf15_21.getAsString());
250 assertEquals("lf13 value", lf13.getAsString());
251 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
252 assertEquals("complexjson:lf11", lf15_12.getAsString());
254 }).validate(jsonOutput);
258 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
259 * with the same names externally (augment B) are added)
261 * after running this test following exception is raised
263 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
264 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
269 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
270 final Writer writer = new StringWriter();
271 final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
272 .caseNodeExternalAugmentationInChoiceInContainer();
273 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
274 caseNodeExternalAugmentationInChoiceInContainer);
275 ((JsonValidator) jsonOutput1 -> {
276 final JsonObject cont1 = resolveCont1(jsonOutput1);
277 assertNotNull(cont1);
279 final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
280 assertNotNull(lf15_11Augment);
281 final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
282 assertNotNull(lf15_12Augment);
283 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
285 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
286 assertNotNull(lf15_11);
287 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
288 assertNotNull(lf15_12);
290 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
291 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
292 assertEquals("lf13 value", lf13.getAsString());
293 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
294 assertEquals("complexjson:lf11", lf15_12.getAsString());
296 }).validate(jsonOutput);
300 * augmentation of choice - adding new case
302 * after running this test following exception is raised
304 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
305 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
310 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
311 final Writer writer = new StringWriter();
312 final NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
313 .choiceNodeAugmentationInContainer();
314 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeAugmentationInContainer);
315 ((JsonValidator) jsonOutput1 -> {
316 final JsonObject cont1 = resolveCont1(jsonOutput1);
317 assertNotNull(cont1);
319 final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17", "lf17");
321 assertEquals("lf17 value", lf17.getAsString());
322 }).validate(jsonOutput);
326 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
327 final Writer writer = new StringWriter();
328 final NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
329 .unkeyedNodeInContainer();
330 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, unkeyedNodeInContainer);
331 ((JsonValidator) jsonOutput1 -> {
332 final JsonObject cont1 = resolveCont1(jsonOutput1);
333 assertNotNull(cont1);
335 final JsonArray lst12 = childArray(cont1, "complexjson:lst12", "lst12");
336 assertNotNull(lst12);
338 final Iterator<JsonElement> iterator = lst12.iterator();
339 assertTrue(iterator.hasNext());
340 final JsonElement lst12Entry1Raw = iterator.next();
341 assertFalse(iterator.hasNext());
343 assertTrue(lst12Entry1Raw instanceof JsonObject);
344 final JsonObject lst12Entry1 = (JsonObject) lst12Entry1Raw;
345 final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
346 assertNotNull(lf121);
348 assertEquals("lf121 value", lf121.getAsString());
350 }).validate(jsonOutput);
355 public void emptyTypeTest() throws IOException, URISyntaxException {
356 final StringWriter writer = new StringWriter();
357 final ContainerNode emptyStructure = Builders.containerBuilder()
358 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
359 .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, null)).build();
360 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, emptyStructure);
361 final JsonObject cont1 = resolveCont1(jsonOutput);
362 final JsonElement emptyObj = cont1.get("empty");
363 assertNotNull(emptyObj);
364 assertTrue(emptyObj instanceof JsonArray);
365 assertEquals(1, emptyObj.getAsJsonArray().size());
366 assertTrue(emptyObj.getAsJsonArray().get(0) instanceof JsonNull);
369 private static String normalizedNodeToJsonStreamTransformation(final Writer writer,
370 final NormalizedNode<?, ?> inputStructure) throws IOException {
372 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.
373 createExclusiveWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null,
374 JsonWriterFactory.createJsonWriter(writer, 2));
375 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
376 nodeWriter.write(inputStructure);
379 return writer.toString();