More unit test coverage and code cleanup
[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.modules.junit4.PowerMockRunner;
35 import java.util.HashMap;
36 import java.util.Map;
37 import static org.hamcrest.Matchers.is;
38 import static org.junit.Assert.assertThat;
39
40
41 @RunWith(PowerMockRunner.class)
42 public class LOGJSONObjectTest {
43
44   private static LOGJSONObject logJO;
45
46   @Before
47   public void setUp() throws Exception {
48     Map<String, Object> map = new HashMap<>();
49     map.put("key", null);
50     logJO = new LOGJSONObject(map);
51   }
52
53   @Test
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\"}"));
63   }
64
65   @Test
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\"}"));
70   }
71
72   @Test
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}"));
80   }
81
82   @Test
83   public void Given_Method_Is_Accumulate_And_Value_Is_Valid_Put_Value_Into_New_JSONArray()  {
84     Map<String, Object> map = new HashMap<>();
85     map.put("key", 3);
86     LOGJSONObject logJObject = new LOGJSONObject(map);
87     String s = "key";
88     logJObject.accumulate(s, null);
89     assertThat(logJObject.get("key").toString(), is("[3,null]"));
90   }
91
92   @Test
93   public void Given_Method_Is_Accumulate_And_Value_Is_Null_Dont_Add_Key_Value_Pair()  {
94     String s = "key";
95     logJO.accumulate(s, null);
96     assertThat(logJO.has("key"), is(false));
97   }
98
99   @Test
100   public void Given_Method_Is_Append_And_Value_Is_Null_Append_New_Value()  {
101     String s = "key";
102     double d = 2.0;
103     logJO.append(s, d);
104     assertThat(logJO.getJSONArray("key").get(0), is(2.0));
105   }
106
107
108   @Test
109   public void Given_Method_Is_DoubleToString_And_Value_Is_NaN_Return_Null()  {
110     double d = 2.0;
111     assertThat(LOGJSONObject.doubleToString(d), is("2"));
112   }
113
114
115   @Test
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);
120     String s = "key";
121     assertThat(logJObject.getBoolean(s), is(false));
122   }
123
124   @Test
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);
129     String s = "key";
130     assertThat(logJObject.getBoolean(s), is(true));
131   }
132
133   @Test
134   public void Given_Method_Is_GetDouble_And_Value_Is_A_Double_Return_Value()  {
135     Map<String, Object> map = new HashMap<>();
136     map.put("key", 2.0);
137     LOGJSONObject logJObject = new LOGJSONObject(map);
138     String s = "key";
139     assertThat(logJObject.getDouble(s), is(2.0));
140   }
141
142   @Test
143   public void Given_Method_Is_GetInt_And_Value_Is_An_Int_Return_Value()  {
144     Map<String, Object> map = new HashMap<>();
145     map.put("key", 3);
146     LOGJSONObject logJObject = new LOGJSONObject(map);
147     String s = "key";
148     assertThat(logJObject.getInt(s), is(3));
149   }
150
151   @Test
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<>();
155     map.put("key", jA);
156     LOGJSONObject logJObject = new LOGJSONObject(map);
157     String s = "key";
158     assertThat(logJObject.getJSONArray(s), is(jA));
159   }
160
161   @Test
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);
168     String s = "key";
169     assertThat(logJObject.getJSONObject(s), is(logJObj));
170   }
171
172   @Test
173   public void Given_Method_Is_GetLong_And_Value_Is_A_Long_Return_Value() {
174     long l = 5;
175     Map<String, Object> map = new HashMap<>();
176     map.put("key", l);
177     LOGJSONObject logJObject = new LOGJSONObject(map);
178     String s = "key";
179     assertThat(logJObject.getLong(s), is(5L));
180   }
181
182   @Test
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()];
187     sArray[0] = "name1";
188     assertThat(LOGJSONObject.getNames(logJObj), is(sArray));
189   }
190
191   @Test
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<>();
195     map.put("key", val);
196     LOGJSONObject logJObject = new LOGJSONObject(map);
197     String s = "key";
198     assertThat(logJObject.getString(s), is("value"));
199   }
200
201   @Test
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);
206     String val = null;
207     Map<String, Object> map = new HashMap<>();
208     map.put("key", val);
209     LOGJSONObject logJObject = new LOGJSONObject(map);
210     String s = "key";
211     logJObject.increment(s);
212     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
213   }
214
215   @Test
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);
220     int val = 2;
221     Map<String, Object> map = new HashMap<>();
222     map.put("key", val);
223     LOGJSONObject logJObject = new LOGJSONObject(map);
224     String s = "key";
225     logJObject.increment(s);
226     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
227   }
228
229   @Test
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);
234     long val = 3;
235     Map<String, Object> map = new HashMap<>();
236     map.put("key", val);
237     LOGJSONObject logJObject = new LOGJSONObject(map);
238     String s = "key";
239     logJObject.increment(s);
240     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
241   }
242
243   @Test
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);
248     double val = 4.0;
249     Map<String, Object> map = new HashMap<>();
250     map.put("key", val);
251     LOGJSONObject logJObject = new LOGJSONObject(map);
252     String s = "key";
253     logJObject.increment(s);
254     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
255   }
256
257   @Test
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);
262     float val = 4.0f;
263     Map<String, Object> map = new HashMap<>();
264     map.put("key", val);
265     LOGJSONObject logJObject = new LOGJSONObject(map);
266     String s = "key";
267     logJObject.increment(s);
268     assertThat(logJObject.get("key"), is(logJObjectResult.get("key")));
269   }
270
271   @Test
272   public void Given_Method_Is_Names_And_Object_Contains_Keys_Put_Keys_Into_New_JSONArray() {
273     JSONArray ja = new JSONArray();
274     ja.put("key");
275     String val = "value";
276     Map<String, Object> map = new HashMap<>();
277     map.put("key", val);
278     LOGJSONObject logJObject = new LOGJSONObject(map);
279     assertThat(logJObject.names().get(0), is(ja.get(0)));
280   }
281
282   @Test
283   public void Given_Method_Is_NumberToString_And_Number_is_Not_Null_Return_Reformatted_Number_As_String() {
284     Number num = 3.0;
285     String val = "value";
286     Map<String, Object> map = new HashMap<>();
287     map.put("key", val);
288     LOGJSONObject logJObject = new LOGJSONObject(map);
289     String s = "key";
290     assertThat(logJObject.numberToString(num), is("3"));
291   }
292
293   @Test
294   public void Given_Method_Is_OptBoolean_And_Value_is_Boolean_Return_Value() {
295     boolean val = true;
296     Map<String, Object> map = new HashMap<>();
297     map.put("key", val);
298     LOGJSONObject logJObject = new LOGJSONObject(map);
299     String s = "key";
300     assertThat(logJObject.optBoolean(s, false), is(true));
301   }
302
303   @Test
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<>();
307     map.put("key", val);
308     LOGJSONObject logJObject = new LOGJSONObject(map);
309     String s = "key";
310     assertThat(logJObject.optBoolean(s, false), is(false));
311   }
312
313   @Test
314   public void Given_Method_Is_OptDouble_And_Value_is_Double_Return_Value() {
315     double val = 2.0;
316     Map<String, Object> map = new HashMap<>();
317     map.put("key", val);
318     LOGJSONObject logJObject = new LOGJSONObject(map);
319     String s = "key";
320     assertThat(logJObject.optDouble(s, 0.0), is(2.0));
321   }
322
323   @Test
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<>();
327     map.put("key", val);
328     LOGJSONObject logJObject = new LOGJSONObject(map);
329     String s = "key";
330     assertThat(logJObject.optDouble(s, 0.0), is(0.0));
331   }
332
333   @Test
334   public void Given_Method_Is_OptInt_And_Value_is_Int_Return_Value() {
335     int val = 1;
336     Map<String, Object> map = new HashMap<>();
337     map.put("key", val);
338     LOGJSONObject logJObject = new LOGJSONObject(map);
339     String s = "key";
340     assertThat(logJObject.optInt(s, 0), is(1));
341   }
342
343   @Test
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);
348     String s = "key";
349     assertThat(logJObject.optInt(s, 0), is(0));
350   }
351
352   @Test
353   public void Given_Method_Is_OptLong_And_Value_is_Long_Return_Value() {
354     long val = 4;
355     Map<String, Object> map = new HashMap<>();
356     map.put("key", val);
357     LOGJSONObject logJObject = new LOGJSONObject(map);
358     String s = "key";
359     assertThat(logJObject.optLong(s, 0), is(4L));
360   }
361
362   @Test
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);
367     String s = "key";
368     assertThat(logJObject.optLong(s, 0), is(0L));
369   }
370
371   @Test
372   public void Given_Method_Is_OptString_And_Value_is_String_Return_Value() {
373     String val = "value";
374     Map<String, Object> map = new HashMap<>();
375     map.put("key", val);
376     LOGJSONObject logJObject = new LOGJSONObject(map);
377     String s = "key";
378     assertThat(logJObject.optString(s, "default_value"), is("value"));
379   }
380
381   @Test
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);
386     String s = "key";
387     assertThat(logJObject.putOnce(s, val).get("key"), is("value"));
388   }
389
390   @Test
391   public void Given_Method_Is_StringToValue_And_Value_Is_Number_Return_Number() {
392     String val = "312";
393     Map<String, Object> map = new HashMap<>();
394     assertThat(LOGJSONObject.stringToValue(val), is(312));
395   }
396
397   @Test
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");
402     names.put("name");
403     LOGJSONObject logJObject = new LOGJSONObject(map);
404     assertThat(logJObject.toJSONArray(names).get(0), is("value"));
405   }
406
407   @Test
408   public void Given_Method_Is_ValueToString_And_Value_Is_JSONArray_Return_Value_To_String() {
409     JSONArray val = new JSONArray();
410     val.put("value");
411     assertThat(LOGJSONObject.valueToString(val), is("[\"value\"]"));
412   }
413
414   @Test
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\""));
419   }
420
421   @Test
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\"}"));
428   }
429
430   @Test
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 }"));
438   }
439 }