2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 Ericsson
4 * Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.yangtools.parser.data.parser.test;
23 import static org.junit.Assert.assertTrue;
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
30 import org.junit.Test;
32 import org.oran.smo.yangtools.parser.data.parser.JsonParser;
33 import org.oran.smo.yangtools.parser.data.parser.JsonParser.JsonArray;
34 import org.oran.smo.yangtools.parser.data.parser.JsonParser.JsonObject;
35 import org.oran.smo.yangtools.parser.data.parser.JsonParser.JsonPrimitive;
36 import org.oran.smo.yangtools.parser.data.parser.JsonParser.JsonValue;
37 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
38 import org.oran.smo.yangtools.parser.data.parser.JsonWriter;
40 public class JsonWriterTest extends YangTestCommon {
43 public void test_write_simple_json1() throws IOException {
45 final JsonPrimitive valueOf = JsonPrimitive.valueOf(null);
47 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
48 JsonWriter.write(valueOf, baos);
50 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
52 assertTrue(rootValue instanceof JsonPrimitive);
53 assertTrue(rootValue.equals(JsonPrimitive.NULL));
57 public void test_write_simple_json2() throws IOException {
59 final JsonPrimitive valueOf = JsonPrimitive.valueOf(true);
61 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
62 JsonWriter.write(valueOf, baos);
64 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
66 assertTrue(rootValue instanceof JsonPrimitive);
67 assertTrue(rootValue.equals(JsonPrimitive.TRUE));
71 public void test_write_simple_json3() throws IOException {
73 final JsonPrimitive valueOf = JsonPrimitive.valueOf(1);
75 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
76 JsonWriter.write(valueOf, baos);
78 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
80 assertTrue(rootValue instanceof JsonPrimitive);
81 assertTrue(rootValue.equals(JsonPrimitive.ONE));
84 @SuppressWarnings("unlikely-arg-type")
86 public void test_write_simple_json4() throws IOException {
88 final JsonPrimitive valueOf = JsonPrimitive.valueOf("");
90 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
91 JsonWriter.write(valueOf, baos);
93 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
95 assertTrue(rootValue instanceof JsonPrimitive);
96 assertTrue(rootValue.equals(""));
99 @SuppressWarnings("unlikely-arg-type")
101 public void test_write_simple_json5() throws IOException {
103 final JsonPrimitive valueOf = JsonPrimitive.valueOf("abcd\n\b\t\f\r\\\"/");
105 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
106 JsonWriter.write(valueOf, baos);
108 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
110 assertTrue(rootValue instanceof JsonPrimitive);
111 assertTrue(rootValue.equals("abcd\n\b\t\f\r\\\"/"));
114 @SuppressWarnings("unlikely-arg-type")
116 public void test_write_simple_json6() throws IOException {
118 final JsonPrimitive valueOf = JsonPrimitive.valueOf("✉🌕");
120 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
121 JsonWriter.write(valueOf, baos);
123 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
125 assertTrue(rootValue instanceof JsonPrimitive);
126 assertTrue(rootValue.equals("✉🌕"));
129 @SuppressWarnings("unlikely-arg-type")
131 public void test_write_simple_json7() throws IOException {
133 final JsonPrimitive valueOf = JsonPrimitive.valueOf("©®");
135 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
136 JsonWriter.write(valueOf, baos);
138 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
140 assertTrue(rootValue instanceof JsonPrimitive);
141 assertTrue(rootValue.equals("©®"));
145 public void test_write_array1() throws IOException {
147 final JsonArray array = new JsonArray();
149 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
150 JsonWriter.write(array, baos);
152 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
154 assertTrue(rootValue instanceof JsonArray);
155 assertTrue(((JsonArray) rootValue).getValues().isEmpty());
159 public void test_write_array2() throws IOException {
161 final JsonArray array = new JsonArray();
162 array.addValue(JsonPrimitive.valueOf(0));
163 array.addValue(JsonPrimitive.valueOf(false));
164 array.addValue(JsonPrimitive.valueOf(null));
166 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
167 JsonWriter.write(array, baos);
169 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
171 assertTrue(rootValue instanceof JsonArray);
172 assertTrue(((JsonArray) rootValue).getValues().size() == 3);
173 assertTrue(((JsonArray) rootValue).getValues().get(0).equals(JsonPrimitive.ZERO));
174 assertTrue(((JsonArray) rootValue).getValues().get(1).equals(JsonPrimitive.FALSE));
175 assertTrue(((JsonArray) rootValue).getValues().get(2).equals(JsonPrimitive.NULL));
179 public void test_write_object1() throws IOException {
181 final JsonObject object = new JsonObject();
183 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
184 JsonWriter.write(object, baos);
186 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
188 assertTrue(rootValue instanceof JsonObject);
189 assertTrue(((JsonObject) rootValue).getValues().isEmpty());
192 @SuppressWarnings("unlikely-arg-type")
194 public void test_write_object2() throws IOException {
196 final JsonObject object = new JsonObject();
198 object.putMember("one", 1);
199 object.putMember("two", null);
200 object.putMember("three", true);
201 object.putMember("four", JsonPrimitive.valueOf("abc"));
202 object.putMember("five", "def");
204 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
205 JsonWriter.write(object, baos);
207 final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
209 assertTrue(rootValue instanceof JsonObject);
210 final Map<String, JsonValue> values = ((JsonObject) rootValue).getValues();
212 assertTrue(values.get("one").equals(JsonPrimitive.ONE));
213 assertTrue(values.get("two").equals(JsonPrimitive.NULL));
214 assertTrue(values.get("three").equals(JsonPrimitive.TRUE));
215 assertTrue(values.get("four").equals("abc"));
216 assertTrue(values.get("five").equals("def"));