6021b75c403c5b245c8c497c85b5d27a51b136cc
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / util / JsonUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. 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
21 package org.openecomp.sdc.common.util;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonNull;
25 import com.google.gson.JsonObject;
26 import org.junit.Test;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertTrue;
32
33 public class JsonUtilsTest {
34
35     private String testProperty01 = "testProperty01";
36     private String testValue01 = "testValue01";
37     private String testProperty02 = "testProperty02";
38     private String testValue02 = "testValue02";
39     private String expectedJsonObject = "" +
40             "{" +
41             "\""+testProperty01+"\":\""+testValue01+"\"," +
42             "\""+testProperty02+"\":\""+testValue02+"\"" +
43             "}";
44
45     @Test
46     public void validateToStringConvertsJsonObjectToValidString() {
47         String result = JsonUtils.toString(generateJsonObject());
48
49         assertEquals(result, expectedJsonObject);
50     }
51
52     @Test
53     public void validateToStringReturnsNullIfJsonElementIsNull() {
54         String result = JsonUtils.toString(null);
55
56         assertNull(result);
57     }
58
59     @Test(expected = UnsupportedOperationException.class)
60     public void validateToStringFromJsonArrayThrowsUnsupportedOperationException() {
61        JsonUtils.toString(generateJsonArray());
62     }
63
64     @Test
65     public void validateToStringReturnsNullIfJsonElementIsJsonNull() {
66         String result = JsonUtils.toString(new JsonNull());
67
68         assertNull(result);
69     }
70
71     @Test
72     public void validateContainsEntryReturnsTrueIfKeyIsPresentInJsonObject() {
73         boolean result = JsonUtils.containsEntry(generateJsonObject(),testProperty01);
74
75         assertTrue(result);
76     }
77
78     @Test
79     public void validateContainsEntryReturnsFalseIfKeyIsNotPresentInJsonObject() {
80         boolean result = JsonUtils.containsEntry(new JsonObject(),testProperty01);
81
82         assertFalse(result);
83     }
84
85     @Test
86     public void validateIsEmptyJsonReturnsTrueIfInputIsEmpty() {
87         boolean result = JsonUtils.isEmptyJson(new JsonObject());
88
89         assertTrue(result);
90     }
91
92     @Test
93     public void validateIsEmptyJsonReturnsFalseIfInputIsNotEmpty() {
94         boolean result = JsonUtils.isEmptyJson(generateJsonArray().get(0));
95
96         assertFalse(result);
97     }
98
99     @Test
100     public void validateIsNullOrEmptyReturnsTrueIfInputIsEmpty() {
101         boolean result = JsonUtils.isJsonNullOrEmpty(new JsonObject());
102
103         assertTrue(result);
104     }
105
106     @Test
107     public void validateIsNullOrEmptyReturnsFalseIfInputIsNotNull() {
108         boolean result = JsonUtils.isJsonNullOrEmpty(generateJsonObject());
109
110         assertFalse(result);
111     }
112
113     private JsonObject generateJsonObject() {
114         final JsonObject testObject = new JsonObject();
115         testObject.addProperty(testProperty01,testValue01);
116         testObject.addProperty(testProperty02,testValue02);
117
118         return testObject;
119     }
120
121     private JsonArray generateJsonArray() {
122         final JsonArray testArray = new JsonArray();
123         testArray.add(generateJsonObject());
124
125         return testArray;
126     }
127
128 }