bd8c81d871d62645901ddcc7cdd27f166f63c7ad
[smo/teiv.git] /
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.data.parser.test;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.util.Map;
29
30 import org.junit.Test;
31
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;
39
40 public class JsonWriterTest extends YangTestCommon {
41
42     @Test
43     public void test_write_simple_json1() throws IOException {
44
45         final JsonPrimitive valueOf = JsonPrimitive.valueOf(null);
46
47         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
48         JsonWriter.write(valueOf, baos);
49
50         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
51
52         assertTrue(rootValue instanceof JsonPrimitive);
53         assertTrue(rootValue.equals(JsonPrimitive.NULL));
54     }
55
56     @Test
57     public void test_write_simple_json2() throws IOException {
58
59         final JsonPrimitive valueOf = JsonPrimitive.valueOf(true);
60
61         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
62         JsonWriter.write(valueOf, baos);
63
64         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
65
66         assertTrue(rootValue instanceof JsonPrimitive);
67         assertTrue(rootValue.equals(JsonPrimitive.TRUE));
68     }
69
70     @Test
71     public void test_write_simple_json3() throws IOException {
72
73         final JsonPrimitive valueOf = JsonPrimitive.valueOf(1);
74
75         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
76         JsonWriter.write(valueOf, baos);
77
78         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
79
80         assertTrue(rootValue instanceof JsonPrimitive);
81         assertTrue(rootValue.equals(JsonPrimitive.ONE));
82     }
83
84     @SuppressWarnings("unlikely-arg-type")
85     @Test
86     public void test_write_simple_json4() throws IOException {
87
88         final JsonPrimitive valueOf = JsonPrimitive.valueOf("");
89
90         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
91         JsonWriter.write(valueOf, baos);
92
93         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
94
95         assertTrue(rootValue instanceof JsonPrimitive);
96         assertTrue(rootValue.equals(""));
97     }
98
99     @SuppressWarnings("unlikely-arg-type")
100     @Test
101     public void test_write_simple_json5() throws IOException {
102
103         final JsonPrimitive valueOf = JsonPrimitive.valueOf("abcd\n\b\t\f\r\\\"/");
104
105         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
106         JsonWriter.write(valueOf, baos);
107
108         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
109
110         assertTrue(rootValue instanceof JsonPrimitive);
111         assertTrue(rootValue.equals("abcd\n\b\t\f\r\\\"/"));
112     }
113
114     @SuppressWarnings("unlikely-arg-type")
115     @Test
116     public void test_write_simple_json6() throws IOException {
117
118         final JsonPrimitive valueOf = JsonPrimitive.valueOf("✉🌕");
119
120         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
121         JsonWriter.write(valueOf, baos);
122
123         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
124
125         assertTrue(rootValue instanceof JsonPrimitive);
126         assertTrue(rootValue.equals("✉🌕"));
127     }
128
129     @SuppressWarnings("unlikely-arg-type")
130     @Test
131     public void test_write_simple_json7() throws IOException {
132
133         final JsonPrimitive valueOf = JsonPrimitive.valueOf("©®");
134
135         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
136         JsonWriter.write(valueOf, baos);
137
138         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
139
140         assertTrue(rootValue instanceof JsonPrimitive);
141         assertTrue(rootValue.equals("©®"));
142     }
143
144     @Test
145     public void test_write_array1() throws IOException {
146
147         final JsonArray array = new JsonArray();
148
149         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
150         JsonWriter.write(array, baos);
151
152         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
153
154         assertTrue(rootValue instanceof JsonArray);
155         assertTrue(((JsonArray) rootValue).getValues().isEmpty());
156     }
157
158     @Test
159     public void test_write_array2() throws IOException {
160
161         final JsonArray array = new JsonArray();
162         array.addValue(JsonPrimitive.valueOf(0));
163         array.addValue(JsonPrimitive.valueOf(false));
164         array.addValue(JsonPrimitive.valueOf(null));
165
166         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
167         JsonWriter.write(array, baos);
168
169         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
170
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));
176     }
177
178     @Test
179     public void test_write_object1() throws IOException {
180
181         final JsonObject object = new JsonObject();
182
183         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
184         JsonWriter.write(object, baos);
185
186         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
187
188         assertTrue(rootValue instanceof JsonObject);
189         assertTrue(((JsonObject) rootValue).getValues().isEmpty());
190     }
191
192     @SuppressWarnings("unlikely-arg-type")
193     @Test
194     public void test_write_object2() throws IOException {
195
196         final JsonObject object = new JsonObject();
197
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");
203
204         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
205         JsonWriter.write(object, baos);
206
207         final JsonValue rootValue = new JsonParser(null, null, new ByteArrayInputStream(baos.toByteArray())).parse();
208
209         assertTrue(rootValue instanceof JsonObject);
210         final Map<String, JsonValue> values = ((JsonObject) rootValue).getValues();
211
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"));
217     }
218
219 }