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.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.opendaylight.yangtools.yang.common.Empty;
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;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 * Each test tests whether json output obtained after transformation contains is corect. The transformation takes
48 * normalized node data structure and transform it to json output. To make it easier validate json output it is loaded
49 * 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;
58 public static void initialization() {
59 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/complexjson/yang");
63 public static void cleanup() {
68 public void leafNodeInContainer() throws IOException, URISyntaxException {
69 final Writer writer = new StringWriter();
70 final NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
71 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
72 final JsonObject cont1 = resolveCont1(jsonOutput);
75 final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
77 final int asInt = lf11.getAsInt();
78 assertEquals(453, asInt);
82 public void leafListNodeInContainerMultiline() throws IOException, URISyntaxException {
83 final Writer writer = new StringWriter();
84 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
85 .leafListNodeInContainerMultiline();
86 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
87 final JsonObject cont1 = resolveCont1(jsonOutput);
89 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
90 assertNotNull(lflst11);
92 final HashSet<Object> lflst11Values = new HashSet<>();
93 for (final JsonElement jsonElement : lflst11) {
94 assertTrue(jsonElement instanceof JsonPrimitive);
95 lflst11Values.add(jsonElement.getAsString());
98 assertEquals(ImmutableSet.of("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"),
103 public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
104 final Writer writer = new StringWriter();
105 final NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
106 .leafNodeViaAugmentationInContainer();
107 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
108 final JsonObject cont1 = resolveCont1(jsonOutput);
109 assertNotNull(cont1);
111 final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
112 assertNotNull(lf12_1);
113 final String asString = lf12_1.getAsString();
114 assertEquals("lf12 value", asString);
118 public void leafListNodeInContainer() throws IOException, URISyntaxException {
119 final Writer writer = new StringWriter();
120 final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
121 .leafListNodeInContainer();
122 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
123 final JsonObject cont1 = resolveCont1(jsonOutput);
124 assertNotNull(cont1);
125 final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
126 assertNotNull(lflst11);
128 final HashSet<Object> lflst11Values = new HashSet<>();
129 for (final JsonElement jsonElement : lflst11) {
130 assertTrue(jsonElement instanceof JsonPrimitive);
131 lflst11Values.add(jsonElement.getAsString());
134 assertEquals(ImmutableSet.of("lflst11 value2", "lflst11 value1"), lflst11Values);
138 public void keyedListNodeInContainer() throws IOException, URISyntaxException {
139 final Writer writer = new StringWriter();
140 final NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
141 .keyedListNodeInContainer();
142 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
143 final JsonObject cont1 = resolveCont1(jsonOutput);
144 assertNotNull(cont1);
145 final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
146 assertNotNull(lst11);
148 final Iterator<JsonElement> iterator = lst11.iterator();
149 assertTrue(iterator.hasNext());
150 final JsonElement lst11Entry1Raw = iterator.next();
151 assertFalse(iterator.hasNext());
152 assertTrue(lst11Entry1Raw instanceof JsonObject);
153 final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
155 final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
156 assertNotNull(key111);
157 final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
158 assertNotNull(lf112);
159 final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
160 assertNotNull(lf113);
161 final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
162 assertNotNull(lf111);
164 assertEquals("key111 value", key111.getAsString());
165 assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
166 assertEquals("lf113 value", lf113.getAsString());
167 assertEquals("lf111 value", lf111.getAsString());
171 public void choiceNodeInContainer() throws IOException, URISyntaxException {
172 final Writer writer = new StringWriter();
173 final NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator
174 .choiceNodeInContainer();
175 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
176 final JsonObject cont1 = resolveCont1(jsonOutput);
177 assertNotNull(cont1);
178 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
181 assertEquals("lf13 value", lf13.getAsString());
185 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
189 * after running this test following exception is raised:
190 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
191 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
194 public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
195 final Writer writer = new StringWriter();
196 final NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
197 .caseNodeAugmentationInChoiceInContainer();
198 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
199 caseNodeAugmentationInChoiceInContainer);
200 final JsonObject cont1 = resolveCont1(jsonOutput);
201 assertNotNull(cont1);
203 final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
204 assertNotNull(lf15_21);
205 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
207 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
208 assertNotNull(lf15_11);
209 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
210 assertNotNull(lf15_12);
212 assertEquals("lf15_21 value", lf15_21.getAsString());
213 assertEquals("lf13 value", lf13.getAsString());
214 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
215 assertEquals("complexjson:ident", lf15_12.getAsString());
219 * tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
220 * with the same names externally (augment B) are added).
223 * 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]]
228 public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
229 final Writer writer = new StringWriter();
230 final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer =
231 TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer();
232 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
233 caseNodeExternalAugmentationInChoiceInContainer);
234 final JsonObject cont1 = resolveCont1(jsonOutput);
235 assertNotNull(cont1);
237 final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
238 assertNotNull(lf15_11Augment);
239 final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
240 assertNotNull(lf15_12Augment);
241 final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
243 final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
244 assertNotNull(lf15_11);
245 final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
246 assertNotNull(lf15_12);
248 assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
249 assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
250 assertEquals("lf13 value", lf13.getAsString());
251 assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
252 assertEquals("complexjson:ident", lf15_12.getAsString());
256 * augmentation of choice - adding new case.
259 * after running this test following exception is raised:
260 * java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
261 * [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
264 public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
265 final Writer writer = new StringWriter();
266 final NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
267 .choiceNodeAugmentationInContainer();
268 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeAugmentationInContainer);
269 final JsonObject cont1 = resolveCont1(jsonOutput);
270 assertNotNull(cont1);
272 final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17", "lf17");
274 assertEquals("lf17 value", lf17.getAsString());
278 public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
279 final Writer writer = new StringWriter();
280 final NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
281 .unkeyedNodeInContainer();
282 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, unkeyedNodeInContainer);
283 final JsonObject cont1 = resolveCont1(jsonOutput);
284 assertNotNull(cont1);
286 final JsonArray lst12 = childArray(cont1, "complexjson:lst12", "lst12");
287 assertNotNull(lst12);
289 final Iterator<JsonElement> iterator = lst12.iterator();
290 assertTrue(iterator.hasNext());
291 final JsonElement lst12Entry1Raw = iterator.next();
292 assertFalse(iterator.hasNext());
294 assertTrue(lst12Entry1Raw instanceof JsonObject);
295 final JsonObject lst12Entry1 = (JsonObject) lst12Entry1Raw;
296 final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
297 assertNotNull(lf121);
299 assertEquals("lf121 value", lf121.getAsString());
303 public void emptyTypeTest() throws IOException, URISyntaxException {
304 final StringWriter writer = new StringWriter();
305 final ContainerNode emptyStructure = Builders.containerBuilder()
306 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONT_1))
307 .addChild(ImmutableNodes.leafNode(EMPTY_LEAF, Empty.getInstance())).build();
308 final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, emptyStructure);
309 final JsonObject cont1 = resolveCont1(jsonOutput);
310 final JsonElement emptyObj = cont1.get("empty");
311 assertNotNull(emptyObj);
312 assertTrue(emptyObj instanceof JsonArray);
313 assertEquals(1, emptyObj.getAsJsonArray().size());
314 assertTrue(emptyObj.getAsJsonArray().get(0) instanceof JsonNull);
317 private static String normalizedNodeToJsonStreamTransformation(final Writer writer,
318 final NormalizedNode<?, ?> inputStructure) throws IOException {
320 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
321 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), SchemaPath.ROOT, null,
322 JsonWriterFactory.createJsonWriter(writer, 2));
323 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
324 nodeWriter.write(inputStructure);
327 return writer.toString();