Add junit coverage to TimedOutException class
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / util / Mapper.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.onap.appc.listener.util;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import com.fasterxml.jackson.databind.JsonNode;
32 import org.json.JSONObject;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 public class Mapper {
39
40     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(Mapper.class);
41
42     private static ObjectMapper mapper = new ObjectMapper();
43
44     /**
45      * @return The object mapper that we are using.
46      */
47     public static ObjectMapper getMapper() {
48         return mapper;
49     }
50
51     /**
52      * Convert a String to a DcaeMessage
53      * 
54      * @param data
55      *            The json string to try and parse
56      * @return A DcaeMessage from the json string or null if it could not
57      */
58     public static <T> T mapOne(String data, Class<T> cls) {
59         try {
60             return mapper.readValue(data, cls);
61         } catch (Exception e) {
62             LOG.warn(String.format("Could not map [ %s ] to %s", data, cls.getName()), e);
63             return null;
64         }
65     }
66
67     public static <T> List<T> mapList(List<String> data, Class<T> cls) {
68         List<T> out = new ArrayList<T>();
69         for (String s : data) {
70             T tmp = Mapper.mapOne(s, cls);
71             if (tmp != null) {
72                 out.add(tmp);
73             }
74         }
75         return out;
76     }
77
78     /**
79      * Convenience method to try and convert objects to json String
80      * 
81      * @param obj
82      *            The object to try and convert
83      * @return A json string representing the object or null if it could not be converted
84      */
85     public static String toJsonString(Object obj) {
86         String jsonStr;
87         try {
88             if (obj instanceof JSONObject) {
89                 jsonStr = obj.toString();
90             }else {
91                 jsonStr = mapper.writeValueAsString(obj);
92             }
93             return jsonStr;
94         } catch (Exception e) {
95             LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
96             return null;
97         }
98     }
99
100     public static JSONObject toJsonObject(Object obj) {
101         String jsonStr;
102         try {
103             if (obj.getClass().equals(String.class)) {
104                 jsonStr = (String) obj;
105             } else {
106                 jsonStr = mapper.writeValueAsString(obj);
107             }
108             return new JSONObject(jsonStr);
109         } catch (Exception e) {
110             LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
111             return null;
112         }
113     }
114     public static JsonNode toJsonNodeFromJsonString(String jsonStr) {
115         JsonNode jsonNode = null;
116         if(jsonStr != null) {
117             try {
118                 jsonNode = mapper.readTree(jsonStr);
119             } catch (IOException e) {
120                 LOG.warn(String.format("Could not map %s to jsonNode.", jsonStr), e);
121             }
122         }
123         return jsonNode;
124     }
125     public static JsonNode toJsonNode(Object obj) {
126         JsonNode jsonNode = null;
127         String jsonStr = toJsonString(obj);
128         if(jsonStr != null) {
129             try {
130                 jsonNode = mapper.readTree(jsonStr);
131             } catch (IOException e) {
132                 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
133             }
134         }
135         return jsonNode;
136     }
137 }