AAI-common sonar fixes
[aai/aai-common.git] / aai-els-onap-logging / src / test / java / org / onap / aai / util / MapperUtilTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.util;
24
25 import static org.junit.Assert.assertEquals;
26
27 import org.json.JSONObject;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 public class MapperUtilTest {
33
34     private JSONObject expectedJson;
35     private JSONObject sampleJson;
36
37     @Before
38     public void setup() {
39         expectedJson = new JSONObject();
40         sampleJson = new JSONObject();
41     }
42
43     @Test
44     public void writeAsJSONStringTest() throws Exception {
45         expectedJson.put("color", "black");
46         expectedJson.put("shape", "box");
47         SampleClass sample = new SampleClass("black", "box");
48         Assert.assertEquals(expectedJson.toString(), MapperUtil.writeAsJSONString(sample));
49     }
50
51     @Test
52     public void readAsObjectOfTest() throws Exception {
53         sampleJson.put("color", "black");
54         sampleJson.put("shape", "box");
55         SampleClass expectedObject = new SampleClass("black", "box");
56         SampleClass actualObject = MapperUtil.readAsObjectOf(SampleClass.class, sampleJson.toString());
57         assertEquals(expectedObject.getColor(), actualObject.getColor());
58         assertEquals(expectedObject.getShape(), actualObject.getShape());
59     }
60 }
61
62
63 class SampleClass {
64     private String color;
65     private String shape;
66
67     public SampleClass() {
68
69     }
70
71     public SampleClass(String c, String s) {
72         color = c;
73         shape = s;
74     }
75
76     public String getColor() {
77         return color;
78     }
79
80     public void setColor(String color) {
81         this.color = color;
82     }
83
84     public String getShape() {
85         return shape;
86     }
87
88     public void setShape(String shape) {
89         this.shape = shape;
90     }
91 }