4d568d9b7dc5ce936c747340a619b7a6e719cab5
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / util / TestMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.listener.util;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.io.Serializable;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.json.JSONObject;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.appc.listener.util.Mapper;
39
40 import com.fasterxml.jackson.annotation.JsonProperty;
41 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
42
43 public class TestMapper {
44
45     private String dummyJson = "{\"a\":\"%s\"}";
46     private DummyObj dummyObj = new DummyObj();
47
48     @JsonSerialize
49     public static class DummyObj implements Serializable {
50         @JsonProperty("a")
51         public String a;
52
53         public DummyObj() {
54         }
55     }
56
57     @Before
58     public void setup() {
59     }
60
61     @Test
62     public void testGetMapper() {
63         assertNotNull(Mapper.getMapper());
64     }
65
66     @Test
67     public void testToJsonObject() {
68         JSONObject out;
69         out = Mapper.toJsonObject(".");
70         assertNull(out);
71
72         String value = "b";
73         out = Mapper.toJsonObject(String.format(dummyJson, value));
74         assertNotNull(out);
75         assertEquals(value, out.get("a"));
76     }
77
78     @Test
79     public void testConstructor() {
80         // Only here for code coverage
81         Mapper m = new Mapper();
82         assertNotNull(m);
83     }
84
85     @Test
86     public void testMap() {
87         List<String> in = new ArrayList<String>();
88         in.add("");
89         in.add(null);
90
91         List<DummyObj> out = Mapper.mapList(in, DummyObj.class);
92         assertNotNull(out);
93         assertTrue(out.isEmpty());
94
95         in.add(String.format(dummyJson, "1"));
96         in.add("{\"invalid\":\"yes\"}");
97         in.add(String.format(dummyJson, "2"));
98
99         out = Mapper.mapList(in, DummyObj.class);
100         assertNotNull(out);
101         assertEquals(2, out.size());
102         assertEquals("1", out.get(0).a);
103         assertEquals("2", out.get(1).a);
104     }
105
106 }