Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / util / MapperUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.aai.util;
22
23 import org.openecomp.aai.exceptions.AAIException;
24 import com.fasterxml.jackson.annotation.JsonInclude;
25 import com.fasterxml.jackson.databind.*;
26 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
27
28 public class MapperUtil {
29
30
31   /**
32    * Read as object of.
33    *
34    * @param <T> the generic type
35    * @param clazz the clazz
36    * @param value the value
37    * @return the t
38    * @throws AAIException the AAI exception
39    */
40   public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException {
41         com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
42     try {        
43         return MAPPER.readValue(value, clazz);
44     } catch (Exception e) {
45         throw new AAIException("AAI_4007", e);
46     }
47   }
48   
49   /**
50    * Read with dashes as object of.
51    *
52    * @param <T> the generic type
53    * @param clazz the clazz
54    * @param value the value
55    * @return the t
56    * @throws AAIException the AAI exception
57    */
58   public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException {
59           com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
60             try {
61                 MAPPER.registerModule(new JaxbAnnotationModule());
62                 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
63                 MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
64                 
65                 return MAPPER.readValue(value, clazz);
66             } catch (Exception e) {
67                 throw new AAIException("AAI_4007", e);
68             }
69           }
70
71   /**
72    * Write as JSON string.
73    *
74    * @param obj the obj
75    * @return the string
76    * @throws AAIException the AAI exception
77    */
78   public static String writeAsJSONString(Object obj) throws AAIException  {
79           com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
80             try {
81              String s =  MAPPER.writeValueAsString(obj);
82              return s;           
83                          //readValue(value, clazz);
84             } catch (Exception e) {
85                 throw new AAIException("AAI_4008", e);
86             }
87           }
88   
89   /**
90    * Write as JSON string with dashes.
91    *
92    * @param obj the obj
93    * @return the string
94    * @throws AAIException the AAI exception
95    */
96   public static String writeAsJSONStringWithDashes(Object obj) throws AAIException  {
97           com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
98             try {
99                 MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
100             
101             MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
102             MAPPER.configure(SerializationFeature.INDENT_OUTPUT, false);
103             MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
104
105             MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
106             MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
107
108             MAPPER.registerModule(new JaxbAnnotationModule());
109             String s =  MAPPER.writeValueAsString(obj);
110             return s;            
111             } catch (Exception e) {
112                 throw new AAIException("AAI_4008", e);
113             }
114           }
115 }