2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Samsung. 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============================================
19 * ===================================================================
23 package org.onap.clamp.clds.model.jsontype;
25 import static org.assertj.core.api.Assertions.assertThat;
27 import com.google.gson.JsonObject;
29 import org.hibernate.HibernateException;
30 import org.junit.Test;
31 import org.onap.clamp.dao.model.jsontype.JsonTypeDescriptor;
33 public class JsonTypeDescriptorTest {
35 private JsonTypeDescriptor descriptor = new JsonTypeDescriptor();
38 public void testFromString() {
39 JsonObject object = new JsonObject();
40 object.addProperty("one","oneValue");
41 JsonObject child = new JsonObject();
42 child.addProperty("two","twoValue");
43 object.add("child",child);
45 JsonObject jsonResult = descriptor.fromString("{\"one\":\"oneValue\",\"child\":{\"two\":\"twoValue\"}}");
47 assertThat(jsonResult).isEqualTo(object);
51 public void testUnwrap() {
52 JsonObject res1 = descriptor.unwrap(null, null, null);
53 assertThat(res1).isNull();
55 JsonObject object = new JsonObject();
56 object.addProperty("one","oneValue");
57 JsonObject child = new JsonObject();
58 child.addProperty("two","twoValue");
59 object.add("child",child);
60 String res2 = descriptor.unwrap(object, String.class, null);
61 assertThat(res2.replace("\n", "").replace(" ", ""))
62 .isEqualTo("{\"one\":\"oneValue\",\"child\":{\"two\":\"twoValue\"}}");
64 Object res3 = descriptor.unwrap(object, JsonObject.class, null);
65 String res3Str = ((String) res3).replace(" ", "").replace("\\n", "").replace("\\", "")
66 .replace("\"{", "{").replace("}\"", "}");
67 assertThat(res3Str).isEqualTo("{\"one\":\"oneValue\",\"child\":{\"two\":\"twoValue\"}}");
70 @Test(expected = HibernateException.class)
71 public void testUnwrapExpectationThrown() {
72 JsonObject object = new JsonObject();
73 object.addProperty("one","oneValue");
75 descriptor.unwrap(object, Integer.class, null);
79 public void testWrap() {
80 JsonObject res1 = descriptor.wrap(null, null);
81 assertThat(res1).isNull();
83 JsonObject object = new JsonObject();
84 object.addProperty("one","oneValue");
85 JsonObject child = new JsonObject();
86 child.addProperty("two","twoValue");
87 object.add("child",child);
88 JsonObject res2 = descriptor.wrap("{\"one\":\"oneValue\",\"child\":{\"two\":\"twoValue\"}}", null);
89 assertThat(res2).isEqualTo(object);
92 @Test(expected = HibernateException.class)
93 public void testWrapExpectationThrown() {
94 descriptor.wrap(1, null);