1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.provisioning.utils;
26 import java.io.CharArrayWriter;
27 import java.io.IOException;
28 import java.io.Writer;
29 import org.json.JSONArray;
30 import org.json.JSONTokener;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.powermock.modules.junit4.PowerMockRunner;
35 import java.util.HashMap;
37 import static org.hamcrest.Matchers.is;
38 import static org.junit.Assert.assertThat;
41 @RunWith(PowerMockRunner.class)
42 public class LOGJSONObjectTest {
44 private static LOGJSONObject logJO;
47 public void setUp() throws Exception {
48 Map<String, Object> map = new HashMap<>();
50 logJO = new LOGJSONObject(map);
54 public void Construct_JSONObject_From_A_Subset_Of_Values_From_Another_JSONObject() {
55 Map<String, Object> map = new HashMap<>();
56 map.put("key1", "value1");
57 map.put("key2", "value2");
58 map.put("key3", "value3");
59 LOGJSONObject ljo= new LOGJSONObject(map);
60 String[] sA = {"key1", "key3"};
61 LOGJSONObject logJObject = new LOGJSONObject(ljo, sA);
62 assertThat(logJObject.toString(), is("{\"key1\":\"value1\",\"key3\":\"value3\"}"));
66 public void Construct_JSONObject_From_A_JSONTokener() {
67 JSONTokener x = new JSONTokener("{\"key1\":\"value1\",\"key3\":\"value3\"}");
68 LOGJSONObject logJObject = new LOGJSONObject(x);
69 assertThat(logJObject.toString(), is("{\"key1\":\"value1\",\"key3\":\"value3\"}"));
73 public void Construct_JSONObject_From_A_Bean_Object_And_Populate_From_Its_Getters_And_Setters() {
74 Map<String, Object> map = new HashMap<>();
75 map.put("key1", "value1");
76 map.put("key2", "value2");
77 map.put("key3", "value3");
78 LOGJSONObject logJObject = new LOGJSONObject((Object) map);
79 assertThat(logJObject.toString(), is("{\"empty\":false}"));
83 public void Given_Method_Is_Accumulate_And_Value_Is_Valid_Put_Value_Into_New_JSONArray() {
84 Map<String, Object> map = new HashMap<>();
86 LOGJSONObject logJObject = new LOGJSONObject(map);
88 logJObject.accumulate(s, null);
89 assertThat(logJObject.get("key").toString(), is("[3,null]"));
93 public void Given_Method_Is_Accumulate_And_Value_Is_Null_Dont_Add_Key_Value_Pair() {
95 logJO.accumulate(s, null);
96 assertThat(logJO.has("key"), is(false));
100 public void Given_Method_Is_Append_And_Value_Is_Null_Append_New_Value() {
104 assertThat(logJO.getJSONArray("key").get(0), is(2.0));
109 public void Given_Method_Is_DoubleToString_And_Value_Is_NaN_Return_Null() {
111 assertThat(LOGJSONObject.doubleToString(d), is("2"));
116 public void Given_Method_Is_GetBoolean_And_Value_Is_False_Return_False() {
117 Map<String, Object> map = new HashMap<>();
118 map.put("key", false);
119 LOGJSONObject logJObject = new LOGJSONObject(map);
121 assertThat(logJObject.getBoolean(s), is(false));
125 public void Given_Method_Is_GetBoolean_And_Value_Is_True_Return_True() {
126 Map<String, Object> map = new HashMap<>();
127 map.put("key", true);
128 LOGJSONObject logJObject = new LOGJSONObject(map);
130 assertThat(logJObject.getBoolean(s), is(true));
134 public void Given_Method_Is_GetDouble_And_Value_Is_A_Double_Return_Value() {
135 Map<String, Object> map = new HashMap<>();
137 LOGJSONObject logJObject = new LOGJSONObject(map);
139 assertThat(logJObject.getDouble(s), is(2.0));
143 public void Given_Method_Is_GetInt_And_Value_Is_An_Int_Return_Value() {
144 Map<String, Object> map = new HashMap<>();
146 LOGJSONObject logJObject = new LOGJSONObject(map);
148 assertThat(logJObject.getInt(s), is(3));
152 public void Given_Method_Is_GetJSONArray_And_Value_Is_A_JSONArray_Return_Value() {
153 JSONArray jA = new JSONArray();
154 Map<String, Object> map = new HashMap<>();
156 LOGJSONObject logJObject = new LOGJSONObject(map);
158 assertThat(logJObject.getJSONArray(s), is(jA));
162 public void Given_Method_Is_GetJSONObject_And_Value_Is_A_JSONObject_Return_Value() {
163 LOGJSONObject logJObj = new LOGJSONObject();
164 logJObj.put("stub_key", 1);
165 Map<String, Object> map = new HashMap<>();
166 map.put("key", logJObj);
167 LOGJSONObject logJObject = new LOGJSONObject(map);
169 assertThat(logJObject.getJSONObject(s), is(logJObj));
173 public void Given_Method_Is_GetLong_And_Value_Is_A_Long_Return_Value() {
175 Map<String, Object> map = new HashMap<>();
177 LOGJSONObject logJObject = new LOGJSONObject(map);
179 assertThat(logJObject.getLong(s), is(5L));
183 public void Given_Method_Is_getNames_And_Value_Is_A_LOGJSONObject_Return_StringArray() {
184 LOGJSONObject logJObj = new LOGJSONObject();
185 logJObj.put("name1", "elyk");
186 String[] sArray = new String[logJObj.length()];
188 assertThat(LOGJSONObject.getNames(logJObj), is(sArray));
192 public void Given_Method_Is_GetString_And_Value_Is_A_String_Return_Value() {
193 String val = "value";
194 Map<String, Object> map = new HashMap<>();
196 LOGJSONObject logJObject = new LOGJSONObject(map);
198 assertThat(logJObject.getString(s), is("value"));
202 public void Given_Method_Is_Increment_And_Value_Is_Null_Put_Defualt_Value() {
203 Map<String, Object> mapResult = new HashMap<>();
204 mapResult.put("key", 1);
205 LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
207 Map<String, Object> map = new HashMap<>();
209 LOGJSONObject logJObject = new LOGJSONObject(map);
211 logJObject.increment(s);
212 assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
216 public void Given_Method_Is_Increment_And_Value_Is_An_Int_Put_Value_Plus_One() {
217 Map<String, Object> mapResult = new HashMap<>();
218 mapResult.put("key", 3);
219 LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
221 Map<String, Object> map = new HashMap<>();
223 LOGJSONObject logJObject = new LOGJSONObject(map);
225 logJObject.increment(s);
226 assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
230 public void Given_Method_Is_Increment_And_Value_Is_A_Long_Put_Value_Plus_One() {
231 Map<String, Object> mapResult = new HashMap<>();
232 mapResult.put("key", 4L);
233 LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
235 Map<String, Object> map = new HashMap<>();
237 LOGJSONObject logJObject = new LOGJSONObject(map);
239 logJObject.increment(s);
240 assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
244 public void Given_Method_Is_Increment_And_Value_Is_A_Double_Put_Value_Plus_One() {
245 Map<String, Object> mapResult = new HashMap<>();
246 mapResult.put("key", 5.0);
247 LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
249 Map<String, Object> map = new HashMap<>();
251 LOGJSONObject logJObject = new LOGJSONObject(map);
253 logJObject.increment(s);
254 assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
258 public void Given_Method_Is_Increment_And_Value_Is_A_Float_Put_Value_Plus_One() {
259 Map<String, Object> mapResult = new HashMap<>();
260 mapResult.put("key", 5.0);
261 LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
263 Map<String, Object> map = new HashMap<>();
265 LOGJSONObject logJObject = new LOGJSONObject(map);
267 logJObject.increment(s);
268 assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
272 public void Given_Method_Is_Names_And_Object_Contains_Keys_Put_Keys_Into_New_JSONArray() {
273 JSONArray ja = new JSONArray();
275 String val = "value";
276 Map<String, Object> map = new HashMap<>();
278 LOGJSONObject logJObject = new LOGJSONObject(map);
279 assertThat(logJObject.names().get(0), is(ja.get(0)));
283 public void Given_Method_Is_NumberToString_And_Number_is_Not_Null_Return_Reformatted_Number_As_String() {
285 String val = "value";
286 Map<String, Object> map = new HashMap<>();
288 LOGJSONObject logJObject = new LOGJSONObject(map);
290 assertThat(logJObject.numberToString(num), is("3"));
294 public void Given_Method_Is_OptBoolean_And_Value_is_Boolean_Return_Value() {
296 Map<String, Object> map = new HashMap<>();
298 LOGJSONObject logJObject = new LOGJSONObject(map);
300 assertThat(logJObject.optBoolean(s, false), is(true));
304 public void Given_Method_Is_OptBoolean_And_Value_is_Not_Boolean_Return_Default_Value() {
305 String val = "not_boolean";
306 Map<String, Object> map = new HashMap<>();
308 LOGJSONObject logJObject = new LOGJSONObject(map);
310 assertThat(logJObject.optBoolean(s, false), is(false));
314 public void Given_Method_Is_OptDouble_And_Value_is_Double_Return_Value() {
316 Map<String, Object> map = new HashMap<>();
318 LOGJSONObject logJObject = new LOGJSONObject(map);
320 assertThat(logJObject.optDouble(s, 0.0), is(2.0));
324 public void Given_Method_Is_OptDouble_And_Value_is_Not_Double_Return_Default_Value() {
325 String val = "not_double";
326 Map<String, Object> map = new HashMap<>();
328 LOGJSONObject logJObject = new LOGJSONObject(map);
330 assertThat(logJObject.optDouble(s, 0.0), is(0.0));
334 public void Given_Method_Is_OptInt_And_Value_is_Int_Return_Value() {
336 Map<String, Object> map = new HashMap<>();
338 LOGJSONObject logJObject = new LOGJSONObject(map);
340 assertThat(logJObject.optInt(s, 0), is(1));
344 public void Given_Method_Is_OptInt_And_Value_Is_Null_Return_Default_Value() {
345 Map<String, Object> map = new HashMap<>();
346 map.put("key", null);
347 LOGJSONObject logJObject = new LOGJSONObject(map);
349 assertThat(logJObject.optInt(s, 0), is(0));
353 public void Given_Method_Is_OptLong_And_Value_is_Long_Return_Value() {
355 Map<String, Object> map = new HashMap<>();
357 LOGJSONObject logJObject = new LOGJSONObject(map);
359 assertThat(logJObject.optLong(s, 0), is(4L));
363 public void Given_Method_Is_OptLong_And_Value_is_Not_Long_Return_Default_Value() {
364 Map<String, Object> map = new HashMap<>();
365 map.put("key", null);
366 LOGJSONObject logJObject = new LOGJSONObject(map);
368 assertThat(logJObject.optLong(s, 0), is(0L));
372 public void Given_Method_Is_OptString_And_Value_is_String_Return_Value() {
373 String val = "value";
374 Map<String, Object> map = new HashMap<>();
376 LOGJSONObject logJObject = new LOGJSONObject(map);
378 assertThat(logJObject.optString(s, "default_value"), is("value"));
382 public void Given_Method_Is_putOnce_And_KeyValuePair_Does_Not_Exist_In_logJObject_Put_KeyValuePair_Into_logJObject() {
383 String val = "value";
384 Map<String, Object> map = new HashMap<>();
385 LOGJSONObject logJObject = new LOGJSONObject(map);
387 assertThat(logJObject.putOnce(s, val).get("key"), is("value"));
391 public void Given_Method_Is_StringToValue_And_Value_Is_Number_Return_Number() {
393 Map<String, Object> map = new HashMap<>();
394 assertThat(LOGJSONObject.stringToValue(val), is(312));
398 public void Given_Method_Is_ToJSONArray_And_KeyValue_Exists_Return_Value_Array() {
399 JSONArray names = new JSONArray();
400 Map<String, Object> map = new HashMap<>();
401 map.put("name", "value");
403 LOGJSONObject logJObject = new LOGJSONObject(map);
404 assertThat(logJObject.toJSONArray(names).get(0), is("value"));
408 public void Given_Method_Is_ValueToString_And_Value_Is_JSONArray_Return_Value_To_String() {
409 JSONArray val = new JSONArray();
411 assertThat(LOGJSONObject.valueToString(val), is("[\"value\"]"));
415 public void Given_Method_Is_writeValue_And_Value_IS_Not_Null_Return_Writer_With_Value() throws IOException {
416 Writer writer = new CharArrayWriter();
417 String val = "value";
418 assertThat(LOGJSONObject.writeValue(writer, val, 3, 1).toString(), is("\"value\""));
422 public void Given_Method_Is_write_And_Length_Of_logJObject_Is_One_Write_Value_With_Indent() {
423 Writer writer = new CharArrayWriter();
424 Map<String, Object> map = new HashMap<>();
425 map.put("key", "value");
426 LOGJSONObject logJObject = new LOGJSONObject(map);
427 assertThat(logJObject.write(writer, 3, 1).toString(), is("{\"key\": \"value\"}"));
431 public void Given_Method_Is_write_And_Length_Of_logJObject_Is_Not_One_Or_Zero_Write_Value_With_New_Indent() {
432 Writer writer = new CharArrayWriter();
433 Map<String, Object> map = new HashMap<>();
434 map.put("key", "value");
435 map.put("key1", "value1");
436 LOGJSONObject logJObject = new LOGJSONObject(map);
437 assertThat(logJObject.write(writer, 3, 1).toString(), is("{\n \"key1\": \"value1\",\n \"key\": \"value\"\n }"));