Merge "Improve unit test coverage of loop/log"
[clamp.git] / src / test / java / org / onap / clamp / clds / model / jsontype / JsonTypeDescriptorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
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
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  */
22
23 package org.onap.clamp.clds.model.jsontype;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26
27 import com.google.gson.JsonObject;
28
29 import org.hibernate.HibernateException;
30 import org.junit.Test;
31 import org.onap.clamp.dao.model.jsontype.JsonTypeDescriptor;
32
33 public class JsonTypeDescriptorTest {
34
35     private JsonTypeDescriptor descriptor = new JsonTypeDescriptor();
36
37     @Test
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);
44
45         JsonObject jsonResult = descriptor.fromString("{\"one\":\"oneValue\",\"child\":{\"two\":\"twoValue\"}}");
46
47         assertThat(jsonResult).isEqualTo(object);
48     }
49
50     @Test
51     public void testUnwrap() {
52         JsonObject res1 = descriptor.unwrap(null, null, null);
53         assertThat(res1).isNull();
54
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\"}}");
63
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\"}}");
68     }
69
70     @Test(expected = HibernateException.class)
71     public void testUnwrapExpectationThrown() {
72         JsonObject object = new JsonObject();
73         object.addProperty("one","oneValue");
74
75         descriptor.unwrap(object, Integer.class, null);
76     }
77
78     @Test
79     public void testWrap() {
80         JsonObject res1 = descriptor.wrap(null, null);
81         assertThat(res1).isNull();
82
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);
90     }
91
92     @Test(expected = HibernateException.class)
93     public void testWrapExpectationThrown() {
94         descriptor.wrap(1, null);
95     }
96 }