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 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;
19 import com.google.common.collect.Sets;
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonNull;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonPrimitive;
25 import java.io.IOException;
26 import java.io.StringWriter;
27 import java.io.Writer;
28 import java.net.URISyntaxException;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import org.junit.BeforeClass;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 * Each test tests whether json output obtained after transformation contains is corect. The transformation takes
46 * normalized node data structure and transform it to json output. To make it easier validate json output it is loaded
47 * via gson as structure of json elements which are walked and compared with awaited values.
50 public class NormalizedNodeToJsonStreamTest {
52 private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
53 private static final QName EMPTY_LEAF = QName.create(CONT_1,"empty");
54 private static SchemaContext schemaContext;
56 public interface JsonValidator {
57 void validate(final String jsonOutput);
61 public static void initialization() throws IOException, URISyntaxException {
62 schemaContext = loadModules("/complexjson/yang");
66 * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
71 public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
76 * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
81 public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
86 public void leafNodeInContainer() throws IOException, URISyntaxException {
87 final Writer writer = new StringWriter();
88 final NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
89 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
93 public void validate(final String jsonOutput) {
94 final JsonObject cont1 = resolveCont1(jsonOutput);
97 final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
99 final int asInt = lf11.getAsInt();
100 assertEquals(453, asInt);
102 }.validate(jsonOutput);
107 public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
108 final Writer writer = new StringWriter();
109 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainerMultiline();
110 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
111 new JsonValidator() {
114 public void validate(final String jsonOutput) {
115 final JsonObject cont1 = resolveCont1(jsonOutput);
116 assertNotNull(cont1);
117 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
118 assertNotNull(lflst11);
120 final HashSet<Object> lflst11Values = Sets.newHashSet();
121 for (final JsonElement jsonElement : lflst11) {
122 assertTrue(jsonElement instanceof JsonPrimitive);
123 lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
126 assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"), lflst11Values);
128 }.validate(jsonOutput);
133 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
134 final Writer writer = new StringWriter();
135 final NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
136 .leafNodeViaAugmentationInContainer();
137 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
138 new JsonValidator() {
141 public void validate(final String jsonOutput) {
142 final JsonObject cont1 = resolveCont1(jsonOutput);
143 assertNotNull(cont1);
145 final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
146 assertNotNull(lf12_1);
147 final String asString = lf12_1.getAsString();
148 assertEquals("lf12 value", asString);
150 }.validate(jsonOutput);
155 public void leafListNodeInContainer() throws IOException, URISyntaxException {
156 final Writer writer = new StringWriter();
157 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainer();
158 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
159 new JsonValidator() {
162 public void validate(final String jsonOutput) {
163 final JsonObject cont1 = resolveCont1(jsonOutput);
164 assertNotNull(cont1);
165 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
166 assertNotNull(lflst11);
168 final HashSet<Object> lflst11Values = Sets.newHashSet();
169 for (final JsonElement jsonElement : lflst11) {
170 assertTrue(jsonElement instanceof JsonPrimitive);
171 lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
174 assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
176 }.validate(jsonOutput);
180 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
181 final Writer writer = new StringWriter();
182 final NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
183 .keyedListNodeInContainer();
184 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
185 new JsonValidator() {
188 public void validate(final String jsonOutput) {
189 final JsonObject cont1 = resolveCont1(jsonOutput);
190 assertNotNull(cont1);
191 final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
192 assertNotNull(lst11);
194 final Iterator<JsonElement> iterator = lst11.iterator();
195 assertTrue(iterator.hasNext());
196 final JsonElement lst11Entry1Raw = iterator.next();
197 assertFalse(iterator.hasNext());
198 assertTrue(lst11Entry1Raw instanceof JsonObject);
199 final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
201 final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
202 assertNotNull(key111);
203 final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
204 assertNotNull(lf112);
205 final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
206 assertNotNull(lf113);
207 final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
208 assertNotNull(lf111);
210 assertEquals("key111 value", key111.getAsString());
211 assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
212 assertEquals("lf113 value", lf113.getAsString());
213 assertEquals("lf111 value", lf111.getAsString());
215 }.validate(jsonOutput);
219 public void choiceNodeInContainer() throws IOException, URISyntaxException {
220 final Writer writer = new StringWriter();
221 final NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator.choiceNodeInContainer();
222 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
223 new JsonValidator() {
226 public void validate(final String jsonOutput) {
227 final JsonObject cont1 = resolveCont1(jsonOutput);
228 assertNotNull(cont1);
229 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
232 assertEquals("lf13 value", lf13.getAsString());
234 }.validate(jsonOutput);
238 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
241 * after running this test following exception is raised
243 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
244 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
249 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
250 final Writer writer = new StringWriter();
251 final NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
252 .caseNodeAugmentationInChoiceInContainer();
253 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, caseNodeAugmentationInChoiceInContainer);
254 new JsonValidator() {
257 public void validate(final String jsonOutput) {
258 final JsonObject cont1 = resolveCont1(jsonOutput);
259 assertNotNull(cont1);
261 final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
262 assertNotNull(lf15_21);
263 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
265 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
266 assertNotNull(lf15_11);
267 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
268 assertNotNull(lf15_12);
270 assertEquals("lf15_21 value", lf15_21.getAsString());
271 assertEquals("lf13 value", lf13.getAsString());
272 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
273 assertEquals("complexjson:lf11", lf15_12.getAsString());
276 }.validate(jsonOutput);
280 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
281 * with the same names externally (augment B) are added)
283 * after running this test following exception is raised
285 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
286 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
291 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
292 final Writer writer = new StringWriter();
293 final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
294 .caseNodeExternalAugmentationInChoiceInContainer();
295 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
296 caseNodeExternalAugmentationInChoiceInContainer);
297 new JsonValidator() {
300 public void validate(final String jsonOutput) {
301 final JsonObject cont1 = resolveCont1(jsonOutput);
302 assertNotNull(cont1);
304 final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
305 assertNotNull(lf15_11Augment);
306 final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
307 assertNotNull(lf15_12Augment);
308 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
310 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
311 assertNotNull(lf15_11);
312 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
313 assertNotNull(lf15_12);
315 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
316 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
317 assertEquals("lf13 value", lf13.getAsString());
318 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
319 assertEquals("complexjson:lf11", lf15_12.getAsString());
322 }.validate(jsonOutput);
326 * augmentation of choice - adding new case
328 * after running this test following exception is raised
330 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
331 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
336 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
337 final Writer writer = new StringWriter();
338 final NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
339 .choiceNodeAugmentationInContainer();
340 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
341 choiceNodeAugmentationInContainer);
342 new JsonValidator() {
345 public void validate(final String jsonOutput) {
346 final JsonObject cont1 = resolveCont1(jsonOutput);
347 assertNotNull(cont1);
349 final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17","lf17");
351 assertEquals("lf17 value",lf17.getAsString());
353 }.validate(jsonOutput);
357 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
358 final Writer writer = new StringWriter();
359 final NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
360 .unkeyedNodeInContainer();
361 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
362 unkeyedNodeInContainer);
363 new JsonValidator() {
366 public void validate(final String jsonOutput) {
367 final JsonObject cont1 = resolveCont1(jsonOutput);
368 assertNotNull(cont1);
370 final JsonArray lst12 = childArray(cont1, "complexjson:lst12","lst12");
371 assertNotNull(lst12);
373 final Iterator<JsonElement> iterator = lst12.iterator();
374 assertTrue(iterator.hasNext());
375 final JsonElement lst12Entry1Raw = iterator.next();
376 assertFalse(iterator.hasNext());
378 assertTrue(lst12Entry1Raw instanceof JsonObject);
379 final JsonObject lst12Entry1 = (JsonObject)lst12Entry1Raw;
380 final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
381 assertNotNull(lf121);
383 assertEquals("lf121 value",lf121.getAsString());
386 }.validate(jsonOutput);
391 public void emptyTypeTest() throws IOException, URISyntaxException {
392 final StringWriter writer = new StringWriter();
393 final ContainerNode emptyStructure = Builders.containerBuilder()
394 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
395 .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, null))
397 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
399 final JsonObject cont1 = resolveCont1(jsonOutput);
400 final JsonElement emptyObj = cont1.get("empty");
401 assertNotNull(emptyObj);
402 assertTrue(emptyObj instanceof JsonArray);
403 assertEquals(1,emptyObj.getAsJsonArray().size());
404 assertTrue(emptyObj.getAsJsonArray().get(0) instanceof JsonNull);
407 private String normalizedNodeToJsonStreamTransformation(final Writer writer,
408 final NormalizedNode<?, ?> inputStructure) throws IOException {
410 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
411 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
412 nodeWriter.write(inputStructure);
415 return writer.toString();