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.resolveCont1;
18 import com.google.common.collect.ImmutableSet;
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.Test;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
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.
49 public class NormalizedNodeToJsonStreamTest {
51 private static final QName CONT_1 = QName.create("ns:complex:json", "2014-08-11", "cont1");
52 private static final QName EMPTY_LEAF = QName.create(CONT_1, "empty");
53 private static SchemaContext schemaContext;
56 public static void initialization() {
57 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/complexjson/yang");
61 public void leafNodeInContainer() throws IOException, URISyntaxException {
62 final Writer writer = new StringWriter();
63 final NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
64 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
65 final JsonObject cont1 = resolveCont1(jsonOutput);
68 final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
70 final int asInt = lf11.getAsInt();
71 assertEquals(453, asInt);
75 public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
76 final Writer writer = new StringWriter();
77 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
78 .leafListNodeInContainerMultiline();
79 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
80 final JsonObject cont1 = resolveCont1(jsonOutput);
82 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
83 assertNotNull(lflst11);
85 final HashSet<Object> lflst11Values = new HashSet<>();
86 for (final JsonElement jsonElement : lflst11) {
87 assertTrue(jsonElement instanceof JsonPrimitive);
88 lflst11Values.add(jsonElement.getAsString());
91 assertEquals(ImmutableSet.of("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"),
96 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
97 final Writer writer = new StringWriter();
98 final NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
99 .leafNodeViaAugmentationInContainer();
100 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
101 final JsonObject cont1 = resolveCont1(jsonOutput);
102 assertNotNull(cont1);
104 final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
105 assertNotNull(lf12_1);
106 final String asString = lf12_1.getAsString();
107 assertEquals("lf12 value", asString);
111 public void leafListNodeInContainer() throws IOException, URISyntaxException {
112 final Writer writer = new StringWriter();
113 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
114 .leafListNodeInContainer();
115 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
116 final JsonObject cont1 = resolveCont1(jsonOutput);
117 assertNotNull(cont1);
118 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
119 assertNotNull(lflst11);
121 final HashSet<Object> lflst11Values = new HashSet<>();
122 for (final JsonElement jsonElement : lflst11) {
123 assertTrue(jsonElement instanceof JsonPrimitive);
124 lflst11Values.add(jsonElement.getAsString());
127 assertEquals(ImmutableSet.of("lflst11 value2", "lflst11 value1"), lflst11Values);
131 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
132 final Writer writer = new StringWriter();
133 final NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
134 .keyedListNodeInContainer();
135 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
136 final JsonObject cont1 = resolveCont1(jsonOutput);
137 assertNotNull(cont1);
138 final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
139 assertNotNull(lst11);
141 final Iterator<JsonElement> iterator = lst11.iterator();
142 assertTrue(iterator.hasNext());
143 final JsonElement lst11Entry1Raw = iterator.next();
144 assertFalse(iterator.hasNext());
145 assertTrue(lst11Entry1Raw instanceof JsonObject);
146 final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
148 final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
149 assertNotNull(key111);
150 final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
151 assertNotNull(lf112);
152 final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
153 assertNotNull(lf113);
154 final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
155 assertNotNull(lf111);
157 assertEquals("key111 value", key111.getAsString());
158 assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
159 assertEquals("lf113 value", lf113.getAsString());
160 assertEquals("lf111 value", lf111.getAsString());
164 public void choiceNodeInContainer() throws IOException, URISyntaxException {
165 final Writer writer = new StringWriter();
166 final NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator
167 .choiceNodeInContainer();
168 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
169 final JsonObject cont1 = resolveCont1(jsonOutput);
170 assertNotNull(cont1);
171 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
174 assertEquals("lf13 value", lf13.getAsString());
178 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
182 * after running this test following exception is raised:
183 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
184 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
187 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
188 final Writer writer = new StringWriter();
189 final NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
190 .caseNodeAugmentationInChoiceInContainer();
191 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
192 caseNodeAugmentationInChoiceInContainer);
193 final JsonObject cont1 = resolveCont1(jsonOutput);
194 assertNotNull(cont1);
196 final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
197 assertNotNull(lf15_21);
198 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
200 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
201 assertNotNull(lf15_11);
202 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
203 assertNotNull(lf15_12);
205 assertEquals("lf15_21 value", lf15_21.getAsString());
206 assertEquals("lf13 value", lf13.getAsString());
207 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
208 assertEquals("complexjson:lf11", lf15_12.getAsString());
212 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
213 * with the same names externally (augment B) are added).
216 * after running this test following exception is raised:
217 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
218 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
221 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
222 final Writer writer = new StringWriter();
223 final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer =
224 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer();
225 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
226 caseNodeExternalAugmentationInChoiceInContainer);
227 final JsonObject cont1 = resolveCont1(jsonOutput);
228 assertNotNull(cont1);
230 final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
231 assertNotNull(lf15_11Augment);
232 final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
233 assertNotNull(lf15_12Augment);
234 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
236 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
237 assertNotNull(lf15_11);
238 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
239 assertNotNull(lf15_12);
241 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
242 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
243 assertEquals("lf13 value", lf13.getAsString());
244 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
245 assertEquals("complexjson:lf11", lf15_12.getAsString());
249 * augmentation of choice - adding new case.
252 * after running this test following exception is raised:
253 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
254 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
257 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
258 final Writer writer = new StringWriter();
259 final NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
260 .choiceNodeAugmentationInContainer();
261 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeAugmentationInContainer);
262 final JsonObject cont1 = resolveCont1(jsonOutput);
263 assertNotNull(cont1);
265 final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17", "lf17");
267 assertEquals("lf17 value", lf17.getAsString());
271 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
272 final Writer writer = new StringWriter();
273 final NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
274 .unkeyedNodeInContainer();
275 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, unkeyedNodeInContainer);
276 final JsonObject cont1 = resolveCont1(jsonOutput);
277 assertNotNull(cont1);
279 final JsonArray lst12 = childArray(cont1, "complexjson:lst12", "lst12");
280 assertNotNull(lst12);
282 final Iterator<JsonElement> iterator = lst12.iterator();
283 assertTrue(iterator.hasNext());
284 final JsonElement lst12Entry1Raw = iterator.next();
285 assertFalse(iterator.hasNext());
287 assertTrue(lst12Entry1Raw instanceof JsonObject);
288 final JsonObject lst12Entry1 = (JsonObject) lst12Entry1Raw;
289 final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
290 assertNotNull(lf121);
292 assertEquals("lf121 value", lf121.getAsString());
296 public void emptyTypeTest() throws IOException, URISyntaxException {
297 final StringWriter writer = new StringWriter();
298 final ContainerNode emptyStructure = Builders.containerBuilder()
299 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
300 .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, null)).build();
301 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, emptyStructure);
302 final JsonObject cont1 = resolveCont1(jsonOutput);
303 final JsonElement emptyObj = cont1.get("empty");
304 assertNotNull(emptyObj);
305 assertTrue(emptyObj instanceof JsonArray);
306 assertEquals(1, emptyObj.getAsJsonArray().size());
307 assertTrue(emptyObj.getAsJsonArray().get(0) instanceof JsonNull);
310 private static String normalizedNodeToJsonStreamTransformation(final Writer writer,
311 final NormalizedNode<?, ?> inputStructure) throws IOException {
313 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
314 JSONCodecFactory.getShared(schemaContext), SchemaPath.ROOT, null,
315 JsonWriterFactory.createJsonWriter(writer, 2));
316 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
317 nodeWriter.write(inputStructure);
320 return writer.toString();