2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.sdc.common.util;
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;
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;
36 private String testProperty01 = "testProperty01";
37 private String testValue01 = "testValue01";
38 private String testProperty02 = "testProperty02";
39 private String testValue02 = "testValue02";
40 private String expectedJsonObject = "" +
42 "\"" + testProperty01 + "\":\"" + testValue01 + "\"," +
43 "\"" + testProperty02 + "\":\"" + testValue02 + "\"" +
47 void validateToStringConvertsJsonObjectToValidString() {
48 String result = JsonUtils.toString(generateJsonObject());
49 assertEquals(result, expectedJsonObject);
53 void validateToStringReturnsNullIfJsonElementIsNull() {
54 String result = JsonUtils.toString(null);
59 void validateToStringFromJsonArrayDoesNotThrowsUnsupportedOperationException() {
60 String result = JsonUtils.toString(generateJsonArray());
61 assertNotNull(result);
65 void validateToStringReturnsNullIfJsonElementIsJsonNull() {
66 String result = JsonUtils.toString(new JsonNull());
71 void validateContainsEntryReturnsTrueIfKeyIsPresentInJsonObject() {
72 boolean result = JsonUtils.containsEntry(generateJsonObject(), testProperty01);
77 void validateContainsEntryReturnsFalseIfKeyIsNotPresentInJsonObject() {
78 boolean result = JsonUtils.containsEntry(new JsonObject(), testProperty01);
83 void validateIsEmptyJsonReturnsTrueIfInputIsEmpty() {
84 boolean result = JsonUtils.isEmptyJson(new JsonObject());
89 void validateIsEmptyJsonReturnsFalseIfInputIsNotEmpty() {
90 boolean result = JsonUtils.isEmptyJson(generateJsonArray().get(0));
95 void validateIsNullOrEmptyReturnsTrueIfInputIsEmpty() {
96 boolean result = JsonUtils.isJsonNullOrEmpty(new JsonObject());
101 void validateIsNullOrEmptyReturnsFalseIfInputIsNotNull() {
102 boolean result = JsonUtils.isJsonNullOrEmpty(generateJsonObject());
106 private JsonObject generateJsonObject() {
107 final JsonObject testObject = new JsonObject();
108 testObject.addProperty(testProperty01, testValue01);
109 testObject.addProperty(testProperty02, testValue02);
113 private JsonArray generateJsonArray() {
114 final JsonArray testArray = new JsonArray();
115 testArray.add(generateJsonObject());