Add junit coverage to RequestInfoBuilder class
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / LCM / conv / Converter.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.LCM.conv;
26
27 import com.fasterxml.jackson.annotation.JsonInclude;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import com.fasterxml.jackson.databind.MapperFeature;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.ObjectWriter;
33 import com.fasterxml.jackson.databind.SerializationFeature;
34 import org.apache.commons.lang3.StringUtils;
35 import org.json.JSONObject;
36 import org.onap.appc.listener.LCM.model.DmaapMessage;
37 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
38 import org.onap.appc.listener.util.Mapper;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40
41 public class Converter {
42
43     private Converter() {
44     }
45
46     public static DmaapOutgoingMessage convertJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj){
47
48         if (event == null || inObj == null) {
49             throw new IllegalArgumentException("One of given arguments is null");
50         }
51
52         DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
53         outObj.setBody(inObj);
54         outObj.setRpcName(event.getRpcName());
55         outObj.setVersion(event.getVersion());
56         outObj.setType("response");
57         outObj.setCorrelationID(event.getCorrelationID());
58         return outObj;
59     }
60
61     public static String convertDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
62
63         if (inObj == null)
64             throw new IllegalArgumentException("Input message is null");
65
66
67         ObjectMapper objectMapper = new ObjectMapper();
68         ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
69             .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
70             .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
71         return writer.writeValueAsString(inObj);
72
73     }
74
75     public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,
76         Exception inputException) {
77
78         if (event == null || inputException == null) {
79             throw new IllegalArgumentException("One of given arguments is null");
80         }
81
82         DmaapOutgoingMessage dmaapOutgoingMessage;
83         String errMsg =
84             StringUtils.isEmpty(inputException.getMessage()) ? inputException.toString() : inputException.getMessage();
85         JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header"));
86         JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject)
87             .accumulate("status", new JSONObject().accumulate("code", 200).accumulate("value", errMsg));
88         dmaapOutgoingMessage = new DmaapOutgoingMessage();
89         dmaapOutgoingMessage.setRpcName(event.getRpcName());
90         dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID());
91         dmaapOutgoingMessage.setType("error");
92         dmaapOutgoingMessage.setVersion(event.getVersion());
93         JSONObject jsonObjectBody = new JSONObject().accumulate("output", jsonObjectOutput);
94         JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
95         dmaapOutgoingMessage.setBody(jsonNodeBody);
96         return dmaapOutgoingMessage;
97     }
98
99     public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
100
101         if (dmaapBody == null) {
102             throw new IllegalArgumentException("Dmaap body is null");
103         }
104
105         JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
106         String requestId = getValue(commonHeaderJsonNode, "request-id", "");
107         String subRequestId = getValue(commonHeaderJsonNode, "sub-request-id", "");
108         if (!StringUtils.isEmpty(subRequestId)) {
109             requestId = requestId + "-" + subRequestId;
110         }
111         return requestId;
112     }
113
114     public static Integer extractStatusCode(JsonNode event) {
115
116         if (event == null){
117             throw new IllegalArgumentException("Input event is null");
118         }
119
120         Integer statusCode;
121         statusCode = event.get("output").get("status").get("code").asInt();
122         return statusCode;
123     }
124
125     private static String getValue(JsonNode jsonNode, String name, String defaultValue) {
126         if (jsonNode == null) {
127             return defaultValue;
128         }
129         JsonNode childJsonNode = jsonNode.get(name);
130         if (childJsonNode == null) {
131             return defaultValue;
132         }
133         String value = childJsonNode.asText();
134         if (value == null) {
135             return defaultValue;
136         }
137         return value;
138     }
139
140 }