Updating licenses in all files
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / openecomp / appc / listener / util / TestMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.listener.util;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.Serializable;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.json.JSONObject;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.openecomp.appc.listener.util.Mapper;
38
39 import com.fasterxml.jackson.annotation.JsonProperty;
40 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
41
42 public class TestMapper {
43
44     private String dummyJson = "{\"a\":\"%s\"}";
45     private DummyObj dummyObj = new DummyObj();
46
47     @JsonSerialize
48     public static class DummyObj implements Serializable {
49         @JsonProperty("a")
50         public String a;
51
52         public DummyObj() {
53         }
54     }
55
56     @Before
57     public void setup() {
58     }
59
60     @Test
61     public void testGetMapper() {
62         assertNotNull(Mapper.getMapper());
63     }
64
65     @Test
66     public void testToJsonObject() {
67         JSONObject out;
68         out = Mapper.toJsonObject(".");
69         assertNull(out);
70
71         String value = "b";
72         out = Mapper.toJsonObject(String.format(dummyJson, value));
73         assertNotNull(out);
74         assertEquals(value, out.get("a"));
75     }
76
77     @Test
78     public void testConstructor() {
79         // Only here for code coverage
80         Mapper m = new Mapper();
81         assertNotNull(m);
82     }
83
84     @Test
85     public void testMap() {
86         List<String> in = new ArrayList<String>();
87         in.add("");
88         in.add(null);
89
90         List<DummyObj> out = Mapper.mapList(in, DummyObj.class);
91         assertNotNull(out);
92         assertTrue(out.isEmpty());
93
94         in.add(String.format(dummyJson, "1"));
95         in.add("{\"invalid\":\"yes\"}");
96         in.add(String.format(dummyJson, "2"));
97
98         out = Mapper.mapList(in, DummyObj.class);
99         assertNotNull(out);
100         assertEquals(2, out.size());
101         assertEquals("1", out.get(0).a);
102         assertEquals("2", out.get(1).a);
103     }
104
105 }