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 com.google.gson.JsonArray;
24 import com.google.gson.JsonNull;
25 import com.google.gson.JsonObject;
26 import org.junit.Test;
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;
33 public class JsonUtilsTest {
35 private String testProperty01 = "testProperty01";
36 private String testValue01 = "testValue01";
37 private String testProperty02 = "testProperty02";
38 private String testValue02 = "testValue02";
39 private String expectedJsonObject = "" +
41 "\""+testProperty01+"\":\""+testValue01+"\"," +
42 "\""+testProperty02+"\":\""+testValue02+"\"" +
46 public void validateToStringConvertsJsonObjectToValidString() {
47 String result = JsonUtils.toString(generateJsonObject());
49 assertEquals(result, expectedJsonObject);
53 public void validateToStringReturnsNullIfJsonElementIsNull() {
54 String result = JsonUtils.toString(null);
59 @Test(expected = UnsupportedOperationException.class)
60 public void validateToStringFromJsonArrayThrowsUnsupportedOperationException() {
61 JsonUtils.toString(generateJsonArray());
65 public void validateToStringReturnsNullIfJsonElementIsJsonNull() {
66 String result = JsonUtils.toString(new JsonNull());
72 public void validateContainsEntryReturnsTrueIfKeyIsPresentInJsonObject() {
73 boolean result = JsonUtils.containsEntry(generateJsonObject(),testProperty01);
79 public void validateContainsEntryReturnsFalseIfKeyIsNotPresentInJsonObject() {
80 boolean result = JsonUtils.containsEntry(new JsonObject(),testProperty01);
86 public void validateIsEmptyJsonReturnsTrueIfInputIsEmpty() {
87 boolean result = JsonUtils.isEmptyJson(new JsonObject());
93 public void validateIsEmptyJsonReturnsFalseIfInputIsNotEmpty() {
94 boolean result = JsonUtils.isEmptyJson(generateJsonArray().get(0));
100 public void validateIsNullOrEmptyReturnsTrueIfInputIsEmpty() {
101 boolean result = JsonUtils.isJsonNullOrEmpty(new JsonObject());
107 public void validateIsNullOrEmptyReturnsFalseIfInputIsNotNull() {
108 boolean result = JsonUtils.isJsonNullOrEmpty(generateJsonObject());
113 private JsonObject generateJsonObject() {
114 final JsonObject testObject = new JsonObject();
115 testObject.addProperty(testProperty01,testValue01);
116 testObject.addProperty(testProperty02,testValue02);
121 private JsonArray generateJsonArray() {
122 final JsonArray testArray = new JsonArray();
123 testArray.add(generateJsonObject());