Fix 'Fail to import service with get_property of map-of-string'-bug
[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 static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonNull;
31 import com.google.gson.JsonObject;
32 import org.junit.jupiter.api.Test;
33
34 class JsonUtilsTest {
35
36     private String testProperty01 = "testProperty01";
37     private String testValue01 = "testValue01";
38     private String testProperty02 = "testProperty02";
39     private String testValue02 = "testValue02";
40     private String expectedJsonObject = "" +
41         "{" +
42         "\"" + testProperty01 + "\":\"" + testValue01 + "\"," +
43         "\"" + testProperty02 + "\":\"" + testValue02 + "\"" +
44         "}";
45
46     @Test
47     void validateToStringConvertsJsonObjectToValidString() {
48         String result = JsonUtils.toString(generateJsonObject());
49         assertEquals(result, expectedJsonObject);
50     }
51
52     @Test
53     void validateToStringReturnsNullIfJsonElementIsNull() {
54         String result = JsonUtils.toString(null);
55         assertNull(result);
56     }
57
58     @Test
59     void validateToStringFromJsonArrayDoesNotThrowsUnsupportedOperationException() {
60         String result = JsonUtils.toString(generateJsonArray());
61         assertNotNull(result);
62     }
63
64     @Test
65     void validateToStringReturnsNullIfJsonElementIsJsonNull() {
66         String result = JsonUtils.toString(new JsonNull());
67         assertNull(result);
68     }
69
70     @Test
71     void validateContainsEntryReturnsTrueIfKeyIsPresentInJsonObject() {
72         boolean result = JsonUtils.containsEntry(generateJsonObject(), testProperty01);
73         assertTrue(result);
74     }
75
76     @Test
77     void validateContainsEntryReturnsFalseIfKeyIsNotPresentInJsonObject() {
78         boolean result = JsonUtils.containsEntry(new JsonObject(), testProperty01);
79         assertFalse(result);
80     }
81
82     @Test
83     void validateIsEmptyJsonReturnsTrueIfInputIsEmpty() {
84         boolean result = JsonUtils.isEmptyJson(new JsonObject());
85         assertTrue(result);
86     }
87
88     @Test
89     void validateIsEmptyJsonReturnsFalseIfInputIsNotEmpty() {
90         boolean result = JsonUtils.isEmptyJson(generateJsonArray().get(0));
91         assertFalse(result);
92     }
93
94     @Test
95     void validateIsNullOrEmptyReturnsTrueIfInputIsEmpty() {
96         boolean result = JsonUtils.isJsonNullOrEmpty(new JsonObject());
97         assertTrue(result);
98     }
99
100     @Test
101     void validateIsNullOrEmptyReturnsFalseIfInputIsNotNull() {
102         boolean result = JsonUtils.isJsonNullOrEmpty(generateJsonObject());
103         assertFalse(result);
104     }
105
106     private JsonObject generateJsonObject() {
107         final JsonObject testObject = new JsonObject();
108         testObject.addProperty(testProperty01, testValue01);
109         testObject.addProperty(testProperty02, testValue02);
110         return testObject;
111     }
112
113     private JsonArray generateJsonArray() {
114         final JsonArray testArray = new JsonArray();
115         testArray.add(generateJsonObject());
116         return testArray;
117     }
118
119 }