[DMaaP DR] JKD 11 migration
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / utils / LOGJSONObjectTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.provisioning.utils;
25
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.core.classloader.annotations.PowerMockIgnore;
35 import org.powermock.modules.junit4.PowerMockRunner;
36 import java.util.HashMap;
37 import java.util.Map;
38 import static org.hamcrest.Matchers.is;
39 import static org.junit.Assert.assertThat;
40
41
42 @RunWith(PowerMockRunner.class)
43 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
44 public class LOGJSONObjectTest {
45
46   private static LOGJSONObject logJO;
47
48   @Before
49   public void setUp() throws Exception {
50     Map<String, Object> map = new HashMap<>();
51     map.put("key", null);
52     logJO = new LOGJSONObject(map);
53   }
54
55   @Test
56   public void Construct_JSONObject_From_A_Subset_Of_Values_From_Another_JSONObject()  {
57     Map<String, Object> map = new HashMap<>();
58     map.put("key1", "value1");
59     map.put("key2", "value2");
60     map.put("key3", "value3");
61     LOGJSONObject ljo= new LOGJSONObject(map);
62     String[] sA = {"key1", "key3"};
63     LOGJSONObject logJObject = new LOGJSONObject(ljo, sA);
64     assertThat(logJObject.toString(), is("{\"key1\":\"value1\",\"key3\":\"value3\"}"));
65   }
66
67   @Test
68   public void Construct_JSONObject_From_A_JSONTokener()  {
69     JSONTokener x = new JSONTokener("{\"key1\":\"value1\",\"key3\":\"value3\"}");
70     LOGJSONObject logJObject = new LOGJSONObject(x);
71     assertThat(logJObject.toString(), is("{\"key1\":\"value1\",\"key3\":\"value3\"}"));
72   }
73
74   @Test
75   public void Construct_JSONObject_From_A_Bean_Object_And_Populate_From_Its_Getters_And_Setters()  {
76     Map<String, Object> map = new HashMap<>();
77     map.put("key1", "value1");
78     map.put("key2", "value2");
79     map.put("key3", "value3");
80     LOGJSONObject logJObject = new LOGJSONObject((Object) map);
81     assertThat(logJObject.toString(), is("{\"empty\":false}"));
82   }
83
84   @Test
85   public void Given_Method_Is_Accumulate_And_Value_Is_Valid_Put_Value_Into_New_JSONArray()  {
86     Map<String, Object> map = new HashMap<>();
87     map.put("key", 3);
88     LOGJSONObject logJObject = new LOGJSONObject(map);
89     String s = "key";
90     logJObject.accumulate(s, null);
91     assertThat(logJObject.get("key").toString(), is("[3,null]"));
92   }
93
94   @Test
95   public void Given_Method_Is_Accumulate_And_Value_Is_Null_Dont_Add_Key_Value_Pair()  {
96     String s = "key";
97     logJO.accumulate(s, null);
98     assertThat(logJO.has("key"), is(false));
99   }
100
101   @Test
102   public void Given_Method_Is_Append_And_Value_Is_Null_Append_New_Value()  {
103     String s = "key";
104     double d = 2.0;
105     logJO.append(s, d);
106     assertThat(logJO.getJSONArray("key").get(0), is(2.0));
107   }
108
109
110   @Test
111   public void Given_Method_Is_DoubleToString_And_Value_Is_NaN_Return_Null()  {
112     double d = 2.0;
113     assertThat(LOGJSONObject.doubleToString(d), is("2"));
114   }
115
116
117   @Test
118   public void Given_Method_Is_GetBoolean_And_Value_Is_False_Return_False()  {
119     Map<String, Object> map = new HashMap<>();
120     map.put("key", false);
121     LOGJSONObject logJObject = new LOGJSONObject(map);
122     String s = "key";
123     assertThat(logJObject.getBoolean(s), is(false));
124   }
125
126   @Test
127   public void Given_Method_Is_GetBoolean_And_Value_Is_True_Return_True()  {
128     Map<String, Object> map = new HashMap<>();
129     map.put("key", true);
130     LOGJSONObject logJObject = new LOGJSONObject(map);
131     String s = "key";
132     assertThat(logJObject.getBoolean(s), is(true));
133   }
134
135   @Test
136   public void Given_Method_Is_GetDouble_And_Value_Is_A_Double_Return_Value()  {
137     Map<String, Object> map = new HashMap<>();
138     map.put("key", 2.0);
139     LOGJSONObject logJObject = new LOGJSONObject(map);
140     String s = "key";
141     assertThat(logJObject.getDouble(s), is(2.0));
142   }
143
144   @Test
145   public void Given_Method_Is_GetInt_And_Value_Is_An_Int_Return_Value()  {
146     Map<String, Object> map = new HashMap<>();
147     map.put("key", 3);
148     LOGJSONObject logJObject = new LOGJSONObject(map);
149     String s = "key";
150     assertThat(logJObject.getInt(s), is(3));
151   }
152
153   @Test
154   public void Given_Method_Is_GetJSONArray_And_Value_Is_A_JSONArray_Return_Value()  {
155     JSONArray jA = new JSONArray();
156     Map<String, Object> map = new HashMap<>();
157     map.put("key", jA);
158     LOGJSONObject logJObject = new LOGJSONObject(map);
159     String s = "key";
160     assertThat(logJObject.getJSONArray(s), is(jA));
161   }
162
163   @Test
164   public void Given_Method_Is_GetJSONObject_And_Value_Is_A_JSONObject_Return_Value()  {
165     LOGJSONObject logJObj = new LOGJSONObject();
166     logJObj.put("stub_key", 1);
167     Map<String, Object> map = new HashMap<>();
168     map.put("key", logJObj);
169     LOGJSONObject logJObject = new LOGJSONObject(map);
170     String s = "key";
171     assertThat(logJObject.getJSONObject(s), is(logJObj));
172   }
173
174   @Test
175   public void Given_Method_Is_GetLong_And_Value_Is_A_Long_Return_Value() {
176     long l = 5;
177     Map<String, Object> map = new HashMap<>();
178     map.put("key", l);
179     LOGJSONObject logJObject = new LOGJSONObject(map);
180     String s = "key";
181     assertThat(logJObject.getLong(s), is(5L));
182   }
183
184   @Test
185   public void Given_Method_Is_getNames_And_Value_Is_A_LOGJSONObject_Return_StringArray() {
186     LOGJSONObject logJObj = new LOGJSONObject();
187     logJObj.put("name1", "elyk");
188     String[] sArray = new String[logJObj.length()];
189     sArray[0] = "name1";
190     assertThat(LOGJSONObject.getNames(logJObj), is(sArray));
191   }
192
193   @Test
194   public void Given_Method_Is_GetString_And_Value_Is_A_String_Return_Value() {
195     String val = "value";
196     Map<String, Object> map = new HashMap<>();
197     map.put("key", val);
198     LOGJSONObject logJObject = new LOGJSONObject(map);
199     String s = "key";
200     assertThat(logJObject.getString(s), is("value"));
201   }
202
203   @Test
204   public void Given_Method_Is_Increment_And_Value_Is_Null_Put_Defualt_Value() {
205     Map<String, Object> mapResult = new HashMap<>();
206     mapResult.put("key", 1);
207     LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
208     String val = null;
209     Map<String, Object> map = new HashMap<>();
210     map.put("key", val);
211     LOGJSONObject logJObject = new LOGJSONObject(map);
212     String s = "key";
213     logJObject.increment(s);
214     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
215   }
216
217   @Test
218   public void Given_Method_Is_Increment_And_Value_Is_An_Int_Put_Value_Plus_One() {
219     Map<String, Object> mapResult = new HashMap<>();
220     mapResult.put("key", 3);
221     LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
222     int val = 2;
223     Map<String, Object> map = new HashMap<>();
224     map.put("key", val);
225     LOGJSONObject logJObject = new LOGJSONObject(map);
226     String s = "key";
227     logJObject.increment(s);
228     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
229   }
230
231   @Test
232   public void Given_Method_Is_Increment_And_Value_Is_A_Long_Put_Value_Plus_One() {
233     Map<String, Object> mapResult = new HashMap<>();
234     mapResult.put("key", 4L);
235     LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
236     long val = 3;
237     Map<String, Object> map = new HashMap<>();
238     map.put("key", val);
239     LOGJSONObject logJObject = new LOGJSONObject(map);
240     String s = "key";
241     logJObject.increment(s);
242     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
243   }
244
245   @Test
246   public void Given_Method_Is_Increment_And_Value_Is_A_Double_Put_Value_Plus_One() {
247     Map<String, Object> mapResult = new HashMap<>();
248     mapResult.put("key", 5.0);
249     LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
250     double val = 4.0;
251     Map<String, Object> map = new HashMap<>();
252     map.put("key", val);
253     LOGJSONObject logJObject = new LOGJSONObject(map);
254     String s = "key";
255     logJObject.increment(s);
256     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
257   }
258
259   @Test
260   public void Given_Method_Is_Increment_And_Value_Is_A_Float_Put_Value_Plus_One() {
261     Map<String, Object> mapResult = new HashMap<>();
262     mapResult.put("key", 5.0);
263     LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult);
264     float val = 4.0f;
265     Map<String, Object> map = new HashMap<>();
266     map.put("key", val);
267     LOGJSONObject logJObject = new LOGJSONObject(map);
268     String s = "key";
269     logJObject.increment(s);
270     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
271   }
272
273   @Test
274   public void Given_Method_Is_Names_And_Object_Contains_Keys_Put_Keys_Into_New_JSONArray() {
275     JSONArray ja = new JSONArray();
276     ja.put("key");
277     String val = "value";
278     Map<String, Object> map = new HashMap<>();
279     map.put("key", val);
280     LOGJSONObject logJObject = new LOGJSONObject(map);
281     assertThat(logJObject.names().get(0), is(ja.get(0)));
282   }
283
284   @Test
285   public void Given_Method_Is_NumberToString_And_Number_is_Not_Null_Return_Reformatted_Number_As_String() {
286     Number num = 3.0;
287     String val = "value";
288     Map<String, Object> map = new HashMap<>();
289     map.put("key", val);
290     LOGJSONObject logJObject = new LOGJSONObject(map);
291     String s = "key";
292     assertThat(logJObject.numberToString(num), is("3"));
293   }
294
295   @Test
296   public void Given_Method_Is_OptBoolean_And_Value_is_Boolean_Return_Value() {
297     boolean val = true;
298     Map<String, Object> map = new HashMap<>();
299     map.put("key", val);
300     LOGJSONObject logJObject = new LOGJSONObject(map);
301     String s = "key";
302     assertThat(logJObject.optBoolean(s, false), is(true));
303   }
304
305   @Test
306   public void Given_Method_Is_OptBoolean_And_Value_is_Not_Boolean_Return_Default_Value() {
307     String val = "not_boolean";
308     Map<String, Object> map = new HashMap<>();
309     map.put("key", val);
310     LOGJSONObject logJObject = new LOGJSONObject(map);
311     String s = "key";
312     assertThat(logJObject.optBoolean(s, false), is(false));
313   }
314
315   @Test
316   public void Given_Method_Is_OptDouble_And_Value_is_Double_Return_Value() {
317     double val = 2.0;
318     Map<String, Object> map = new HashMap<>();
319     map.put("key", val);
320     LOGJSONObject logJObject = new LOGJSONObject(map);
321     String s = "key";
322     assertThat(logJObject.optDouble(s, 0.0), is(2.0));
323   }
324
325   @Test
326   public void Given_Method_Is_OptDouble_And_Value_is_Not_Double_Return_Default_Value() {
327     String val = "not_double";
328     Map<String, Object> map = new HashMap<>();
329     map.put("key", val);
330     LOGJSONObject logJObject = new LOGJSONObject(map);
331     String s = "key";
332     assertThat(logJObject.optDouble(s, 0.0), is(0.0));
333   }
334
335   @Test
336   public void Given_Method_Is_OptInt_And_Value_is_Int_Return_Value() {
337     int val = 1;
338     Map<String, Object> map = new HashMap<>();
339     map.put("key", val);
340     LOGJSONObject logJObject = new LOGJSONObject(map);
341     String s = "key";
342     assertThat(logJObject.optInt(s, 0), is(1));
343   }
344
345   @Test
346   public void Given_Method_Is_OptInt_And_Value_Is_Null_Return_Default_Value() {
347     Map<String, Object> map = new HashMap<>();
348     map.put("key", null);
349     LOGJSONObject logJObject = new LOGJSONObject(map);
350     String s = "key";
351     assertThat(logJObject.optInt(s, 0), is(0));
352   }
353
354   @Test
355   public void Given_Method_Is_OptLong_And_Value_is_Long_Return_Value() {
356     long val = 4;
357     Map<String, Object> map = new HashMap<>();
358     map.put("key", val);
359     LOGJSONObject logJObject = new LOGJSONObject(map);
360     String s = "key";
361     assertThat(logJObject.optLong(s, 0), is(4L));
362   }
363
364   @Test
365   public void Given_Method_Is_OptLong_And_Value_is_Not_Long_Return_Default_Value() {
366     Map<String, Object> map = new HashMap<>();
367     map.put("key", null);
368     LOGJSONObject logJObject = new LOGJSONObject(map);
369     String s = "key";
370     assertThat(logJObject.optLong(s, 0), is(0L));
371   }
372
373   @Test
374   public void Given_Method_Is_OptString_And_Value_is_String_Return_Value() {
375     String val = "value";
376     Map<String, Object> map = new HashMap<>();
377     map.put("key", val);
378     LOGJSONObject logJObject = new LOGJSONObject(map);
379     String s = "key";
380     assertThat(logJObject.optString(s, "default_value"), is("value"));
381   }
382
383   @Test
384   public void Given_Method_Is_putOnce_And_KeyValuePair_Does_Not_Exist_In_logJObject_Put_KeyValuePair_Into_logJObject() {
385     String val = "value";
386     Map<String, Object> map = new HashMap<>();
387     LOGJSONObject logJObject = new LOGJSONObject(map);
388     String s = "key";
389     assertThat(logJObject.putOnce(s, val).get("key"), is("value"));
390   }
391
392   @Test
393   public void Given_Method_Is_StringToValue_And_Value_Is_Number_Return_Number() {
394     String val = "312";
395     Map<String, Object> map = new HashMap<>();
396     assertThat(LOGJSONObject.stringToValue(val), is(312));
397   }
398
399   @Test
400   public void Given_Method_Is_ToJSONArray_And_KeyValue_Exists_Return_Value_Array() {
401     JSONArray names = new JSONArray();
402     Map<String, Object> map = new HashMap<>();
403     map.put("name", "value");
404     names.put("name");
405     LOGJSONObject logJObject = new LOGJSONObject(map);
406     assertThat(logJObject.toJSONArray(names).get(0), is("value"));
407   }
408
409   @Test
410   public void Given_Method_Is_ValueToString_And_Value_Is_JSONArray_Return_Value_To_String() {
411     JSONArray val = new JSONArray();
412     val.put("value");
413     assertThat(LOGJSONObject.valueToString(val), is("[\"value\"]"));
414   }
415
416   @Test
417   public void Given_Method_Is_writeValue_And_Value_IS_Not_Null_Return_Writer_With_Value() throws IOException {
418     Writer writer = new CharArrayWriter();
419     String val = "value";
420     assertThat(LOGJSONObject.writeValue(writer, val, 3, 1).toString(), is("\"value\""));
421   }
422
423   @Test
424   public void Given_Method_Is_write_And_Length_Of_logJObject_Is_One_Write_Value_With_Indent() {
425     Writer writer = new CharArrayWriter();
426     Map<String, Object> map = new HashMap<>();
427     map.put("key", "value");
428     LOGJSONObject logJObject = new LOGJSONObject(map);
429     assertThat(logJObject.write(writer, 3, 1).toString(), is("{\"key\": \"value\"}"));
430   }
431
432   @Test
433   public void Given_Method_Is_write_And_Length_Of_logJObject_Is_Not_One_Or_Zero_Write_Value_With_New_Indent() {
434     Writer writer = new CharArrayWriter();
435     Map<String, Object> map = new HashMap<>();
436     map.put("key", "value");
437     map.put("key1", "value1");
438     LOGJSONObject logJObject = new LOGJSONObject(map);
439     assertThat(logJObject.write(writer, 3, 1).toString(), is("{\n    \"key1\": \"value1\",\n    \"key\": \"value\"\n }"));
440   }
441 }