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