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 leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
100 Writer writer = new StringWriter();
101 NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
102 .leafNodeViaAugmentationInContainer();
103 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
104 new JsonValidator() {
107 public void validate(String jsonOutput) {
108 JsonObject cont1 = resolveCont1(jsonOutput);
109 assertNotNull(cont1);
111 JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
112 assertNotNull(lf12_1);
113 String asString = lf12_1.getAsString();
114 assertEquals("lf12 value", asString);
116 }.validate(jsonOutput);
121 public void leafListNodeInContainer() throws IOException, URISyntaxException {
122 Writer writer = new StringWriter();
123 NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator.leafListNodeInContainer();
124 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
125 new JsonValidator() {
128 public void validate(String jsonOutput) {
129 JsonObject cont1 = resolveCont1(jsonOutput);
130 assertNotNull(cont1);
131 JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
132 assertNotNull(lflst11);
134 HashSet<Object> lflst11Values = Sets.newHashSet();
135 for (JsonElement jsonElement : lflst11) {
136 assertTrue(jsonElement instanceof JsonPrimitive);
137 lflst11Values.add(((JsonPrimitive) jsonElement).getAsString());
140 assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
142 }.validate(jsonOutput);
146 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
147 Writer writer = new StringWriter();
148 NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
149 .keyedListNodeInContainer();
150 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
151 new JsonValidator() {
154 public void validate(String jsonOutput) {
155 JsonObject cont1 = resolveCont1(jsonOutput);
156 assertNotNull(cont1);
157 JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
158 assertNotNull(lst11);
160 Iterator<JsonElement> iterator = lst11.iterator();
161 assertTrue(iterator.hasNext());
162 JsonElement lst11Entry1Raw = iterator.next();
163 assertFalse(iterator.hasNext());
164 assertTrue(lst11Entry1Raw instanceof JsonObject);
165 JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
167 JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
168 assertNotNull(key111);
169 JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
170 assertNotNull(lf112);
171 JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
172 assertNotNull(lf113);
173 JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
174 assertNotNull(lf111);
176 assertEquals("key111 value", key111.getAsString());
177 assertEquals("/complexjson:cont1/complexjson:lflst11", lf112.getAsString());
178 assertEquals("lf113 value", lf113.getAsString());
179 assertEquals("lf111 value", lf111.getAsString());
181 }.validate(jsonOutput);
185 public void choiceNodeInContainer() throws IOException, URISyntaxException {
186 Writer writer = new StringWriter();
187 NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator.choiceNodeInContainer();
188 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
189 new JsonValidator() {
192 public void validate(String jsonOutput) {
193 JsonObject cont1 = resolveCont1(jsonOutput);
194 assertNotNull(cont1);
195 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
198 assertEquals("lf13 value", lf13.getAsString());
200 }.validate(jsonOutput);
204 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
207 * after running this test following exception is raised
209 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
210 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
215 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
216 Writer writer = new StringWriter();
217 NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
218 .caseNodeAugmentationInChoiceInContainer();
219 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, caseNodeAugmentationInChoiceInContainer);
220 new JsonValidator() {
223 public void validate(String jsonOutput) {
224 JsonObject cont1 = resolveCont1(jsonOutput);
225 assertNotNull(cont1);
227 JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
228 assertNotNull(lf15_21);
229 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
231 JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
232 assertNotNull(lf15_11);
233 JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
234 assertNotNull(lf15_12);
236 assertEquals("lf15_21 value", lf15_21.getAsString());
237 assertEquals("lf13 value", lf13.getAsString());
238 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
239 assertEquals("complexjson:lf11", lf15_12.getAsString());
242 }.validate(jsonOutput);
246 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
247 * with the same names externally (augment B) are added)
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 caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
258 Writer writer = new StringWriter();
259 NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
260 .caseNodeExternalAugmentationInChoiceInContainer();
261 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
262 caseNodeExternalAugmentationInChoiceInContainer);
263 new JsonValidator() {
266 public void validate(String jsonOutput) {
267 JsonObject cont1 = resolveCont1(jsonOutput);
268 assertNotNull(cont1);
270 JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
271 assertNotNull(lf15_11Augment);
272 JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
273 assertNotNull(lf15_12Augment);
274 JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
276 JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
277 assertNotNull(lf15_11);
278 JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
279 assertNotNull(lf15_12);
281 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
282 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
283 assertEquals("lf13 value", lf13.getAsString());
284 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
285 assertEquals("complexjson:lf11", lf15_12.getAsString());
288 }.validate(jsonOutput);
292 * augmentation of choice - adding new case
294 * after running this test following exception is raised
296 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
297 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
302 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
303 Writer writer = new StringWriter();
304 NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
305 .choiceNodeAugmentationInContainer();
306 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
307 choiceNodeAugmentationInContainer);
308 new JsonValidator() {
311 public void validate(String jsonOutput) {
312 JsonObject cont1 = resolveCont1(jsonOutput);
313 assertNotNull(cont1);
315 JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17","lf17");
317 assertEquals("lf17 value",lf17.getAsString());
319 }.validate(jsonOutput);
323 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
324 Writer writer = new StringWriter();
325 NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
326 .unkeyedNodeInContainer();
327 String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
328 unkeyedNodeInContainer);
329 new JsonValidator() {
332 public void validate(String jsonOutput) {
333 JsonObject cont1 = resolveCont1(jsonOutput);
334 assertNotNull(cont1);
336 JsonArray lst12 = childArray(cont1, "complexjson:lst12","lst12");
337 assertNotNull(lst12);
339 Iterator<JsonElement> iterator = lst12.iterator();
340 assertTrue(iterator.hasNext());
341 JsonElement lst12Entry1Raw = iterator.next();
342 assertFalse(iterator.hasNext());
344 assertTrue(lst12Entry1Raw instanceof JsonObject);
345 JsonObject lst12Entry1 = (JsonObject)lst12Entry1Raw;
346 JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
347 assertNotNull(lf121);
349 assertEquals("lf121 value",lf121.getAsString());
352 }.validate(jsonOutput);
356 private String normalizedNodeToJsonStreamTransformation(final Writer writer,
357 final NormalizedNode<?, ?> inputStructure) throws IOException {
359 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
360 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
361 nodeWriter.write(inputStructure);
364 return writer.toString();