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.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.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
35 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 * Each test tests whether json output obtained after transformation contains is corect. The transformation takes
40 * normalized node data structure and transform it to json output. To make it easier validate json output it is loaded
41 * via gson as structure of json elements which are walked and compared with awaited values.
44 public class NormalizedNodeToJsonStreamTest {
46 private static SchemaContext schemaContext;
48 public interface JsonValidator {
49 void validate(final String jsonOutput);
53 public static void initialization() throws IOException, URISyntaxException {
54 schemaContext = loadModules("/complexjson/yang");
58 * case when anyxml contains simple value will be implemented when anyxml normalized node reprezentation will be
63 public void anyXmlNodeWithSimpleValueInContainer() throws IOException, URISyntaxException {
68 * case when anyxml contains complex xml will be implemented when anyxml normalized node reprezentation will be
73 public void anyXmlNodeWithCompositeValueInContainer() throws IOException, URISyntaxException {
78 public void leafNodeInContainer() throws IOException, URISyntaxException {
79 Writer writer = new StringWriter();
80 NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
81 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
85 public void validate(String jsonOutput) {
86 JsonObject cont1 = resolveCont1(jsonOutput);
89 JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
91 int asInt = lf11.getAsInt();
92 assertEquals(453, asInt);
94 }.validate(jsonOutput);
99 public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
100 Writer writer = new StringWriter();
101 NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainerMultiline();
102 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
103 new JsonValidator() {
106 public void validate(String jsonOutput) {
107 JsonObject cont1 = resolveCont1(jsonOutput);
108 assertNotNull(cont1);
109 JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
110 assertNotNull(lflst11);
112 HashSet<Object> lflst11Values = Sets.newHashSet();
113 for (JsonElement jsonElement : lflst11) {
114 assertTrue(jsonElement instanceof JsonPrimitive);
115 lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
118 assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"), lflst11Values);
120 }.validate(jsonOutput);
125 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
126 Writer writer = new StringWriter();
127 NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
128 .leafNodeViaAugmentationInContainer();
129 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
130 new JsonValidator() {
133 public void validate(String jsonOutput) {
134 JsonObject cont1 = resolveCont1(jsonOutput);
135 assertNotNull(cont1);
137 JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
138 assertNotNull(lf12_1);
139 String asString = lf12_1.getAsString();
140 assertEquals("lf12 value", asString);
142 }.validate(jsonOutput);
147 public void leafListNodeInContainer() throws IOException, URISyntaxException {
148 Writer writer = new StringWriter();
149 NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainer();
150 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
151 new JsonValidator() {
154 public void validate(String jsonOutput) {
155 JsonObject cont1 = resolveCont1(jsonOutput);
156 assertNotNull(cont1);
157 JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
158 assertNotNull(lflst11);
160 HashSet<Object> lflst11Values = Sets.newHashSet();
161 for (JsonElement jsonElement : lflst11) {
162 assertTrue(jsonElement instanceof JsonPrimitive);
163 lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
166 assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
168 }.validate(jsonOutput);
172 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
173 Writer writer = new StringWriter();
174 NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
175 .keyedListNodeInContainer();
176 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
177 new JsonValidator() {
180 public void validate(String jsonOutput) {
181 JsonObject cont1 = resolveCont1(jsonOutput);
182 assertNotNull(cont1);
183 JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
184 assertNotNull(lst11);
186 Iterator<JsonElement> iterator = lst11.iterator();
187 assertTrue(iterator.hasNext());
188 JsonElement lst11Entry1Raw = iterator.next();
189 assertFalse(iterator.hasNext());
190 assertTrue(lst11Entry1Raw instanceof JsonObject);
191 JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
193 JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
194 assertNotNull(key111);
195 JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
196 assertNotNull(lf112);
197 JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
198 assertNotNull(lf113);
199 JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
200 assertNotNull(lf111);
202 assertEquals("key111 value", key111.getAsString());
203 assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
204 assertEquals("lf113 value", lf113.getAsString());
205 assertEquals("lf111 value", lf111.getAsString());
207 }.validate(jsonOutput);
211 public void choiceNodeInContainer() throws IOException, URISyntaxException {
212 Writer writer = new StringWriter();
213 NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator.choiceNodeInContainer();
214 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
215 new JsonValidator() {
218 public void validate(String jsonOutput) {
219 JsonObject cont1 = resolveCont1(jsonOutput);
220 assertNotNull(cont1);
221 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
224 assertEquals("lf13 value", lf13.getAsString());
226 }.validate(jsonOutput);
230 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
233 * after running this test following exception is raised
235 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
236 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
241 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
242 Writer writer = new StringWriter();
243 NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
244 .caseNodeAugmentationInChoiceInContainer();
245 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, caseNodeAugmentationInChoiceInContainer);
246 new JsonValidator() {
249 public void validate(String jsonOutput) {
250 JsonObject cont1 = resolveCont1(jsonOutput);
251 assertNotNull(cont1);
253 JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
254 assertNotNull(lf15_21);
255 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
257 JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
258 assertNotNull(lf15_11);
259 JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
260 assertNotNull(lf15_12);
262 assertEquals("lf15_21 value", lf15_21.getAsString());
263 assertEquals("lf13 value", lf13.getAsString());
264 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
265 assertEquals("complexjson:lf11", lf15_12.getAsString());
268 }.validate(jsonOutput);
272 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
273 * with the same names externally (augment B) are added)
275 * after running this test following exception is raised
277 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
278 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
283 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
284 Writer writer = new StringWriter();
285 NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
286 .caseNodeExternalAugmentationInChoiceInContainer();
287 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
288 caseNodeExternalAugmentationInChoiceInContainer);
289 new JsonValidator() {
292 public void validate(String jsonOutput) {
293 JsonObject cont1 = resolveCont1(jsonOutput);
294 assertNotNull(cont1);
296 JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
297 assertNotNull(lf15_11Augment);
298 JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
299 assertNotNull(lf15_12Augment);
300 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
302 JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
303 assertNotNull(lf15_11);
304 JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
305 assertNotNull(lf15_12);
307 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
308 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
309 assertEquals("lf13 value", lf13.getAsString());
310 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
311 assertEquals("complexjson:lf11", lf15_12.getAsString());
314 }.validate(jsonOutput);
318 * augmentation of choice - adding new case
320 * after running this test following exception is raised
322 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
323 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
328 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
329 Writer writer = new StringWriter();
330 NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
331 .choiceNodeAugmentationInContainer();
332 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
333 choiceNodeAugmentationInContainer);
334 new JsonValidator() {
337 public void validate(String jsonOutput) {
338 JsonObject cont1 = resolveCont1(jsonOutput);
339 assertNotNull(cont1);
341 JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17","lf17");
343 assertEquals("lf17 value",lf17.getAsString());
345 }.validate(jsonOutput);
349 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
350 Writer writer = new StringWriter();
351 NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
352 .unkeyedNodeInContainer();
353 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
354 unkeyedNodeInContainer);
355 new JsonValidator() {
358 public void validate(String jsonOutput) {
359 JsonObject cont1 = resolveCont1(jsonOutput);
360 assertNotNull(cont1);
362 JsonArray lst12 = childArray(cont1, "complexjson:lst12","lst12");
363 assertNotNull(lst12);
365 Iterator<JsonElement> iterator = lst12.iterator();
366 assertTrue(iterator.hasNext());
367 JsonElement lst12Entry1Raw = iterator.next();
368 assertFalse(iterator.hasNext());
370 assertTrue(lst12Entry1Raw instanceof JsonObject);
371 JsonObject lst12Entry1 = (JsonObject)lst12Entry1Raw;
372 JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
373 assertNotNull(lf121);
375 assertEquals("lf121 value",lf121.getAsString());
378 }.validate(jsonOutput);
382 private String normalizedNodeToJsonStreamTransformation(final Writer writer,
383 final NormalizedNode<?, ?> inputStructure) throws IOException {
385 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
386 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
387 nodeWriter.write(inputStructure);
390 return writer.toString();