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