diff options
author | 2018-03-15 15:49:36 +0100 | |
---|---|---|
committer | 2018-03-15 16:13:35 +0000 | |
commit | c540e8b138ff06f9ad1e5ee9ff3a70ac9bc99edf (patch) | |
tree | 89dee3616c0ac8690bb8acf4c35c9540611f0d04 /appc-event-listener/appc-event-listener-bundle/src/main/java | |
parent | 286ef6e7dc5baea219a29bd5f81ea3eabf96e7b4 (diff) |
Converter unit tests
Improved code coverage.
Change-Id: Ieff8e92f907a9c45e4c810754d6e3d86e2c5bd0c
Issue-ID: APPC-745
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
Diffstat (limited to 'appc-event-listener/appc-event-listener-bundle/src/main/java')
2 files changed, 59 insertions, 24 deletions
diff --git a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/conv/Converter.java b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/conv/Converter.java index 4cef18c87..6e303a5ff 100644 --- a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/conv/Converter.java +++ b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/conv/Converter.java @@ -26,17 +26,30 @@ package org.onap.appc.listener.LCM.conv; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.SerializationFeature; import org.apache.commons.lang3.StringUtils; import org.json.JSONObject; import org.onap.appc.listener.LCM.model.DmaapMessage; import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage; import org.onap.appc.listener.util.Mapper; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; public class Converter { - - public static DmaapOutgoingMessage convJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj) { - DmaapOutgoingMessage outObj = new DmaapOutgoingMessage(); + + private Converter() { + } + + public static DmaapOutgoingMessage convertJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj){ + + if (event == null || inObj == null) { + throw new IllegalArgumentException("One of given arguments is null"); + } + + DmaapOutgoingMessage outObj = new DmaapOutgoingMessage(); outObj.setBody(inObj); outObj.setRpcName(event.getRpcName()); outObj.setVersion(event.getVersion()); @@ -45,58 +58,80 @@ public class Converter { return outObj; } - public static String convDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException { -// return Mapper.toJsonString(inObj); + public static String convertDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException { + + if (inObj == null) + throw new IllegalArgumentException("Input message is null"); + + ObjectMapper objectMapper = new ObjectMapper(); - ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY,true) - .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS); + ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) + .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true) + .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS); return writer.writeValueAsString(inObj); } - public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,Exception inputException) { - DmaapOutgoingMessage dmaapOutgoingMessage = null; - String errMsg = StringUtils.isEmpty(inputException.getMessage())? inputException.toString() : inputException.getMessage(); + public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event, + Exception inputException) { + + if (event == null || inputException == null) { + throw new IllegalArgumentException("One of given arguments is null"); + } + + DmaapOutgoingMessage dmaapOutgoingMessage; + String errMsg = + StringUtils.isEmpty(inputException.getMessage()) ? inputException.toString() : inputException.getMessage(); JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header")); - JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject).accumulate("status", new JSONObject().accumulate("code",200).accumulate("value",errMsg)); + JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject) + .accumulate("status", new JSONObject().accumulate("code", 200).accumulate("value", errMsg)); dmaapOutgoingMessage = new DmaapOutgoingMessage(); dmaapOutgoingMessage.setRpcName(event.getRpcName()); dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID()); dmaapOutgoingMessage.setType("error"); dmaapOutgoingMessage.setVersion(event.getVersion()); - JSONObject jsonObjectBody = new JSONObject().accumulate("output",jsonObjectOutput); + JSONObject jsonObjectBody = new JSONObject().accumulate("output", jsonObjectOutput); JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString()); dmaapOutgoingMessage.setBody(jsonNodeBody); return dmaapOutgoingMessage; } public static String extractRequestIdWithSubId(JsonNode dmaapBody) { - //TODO: null pointer exception if dmaapBody is null, check if null or ensure is not null before calling + + if (dmaapBody == null) { + throw new IllegalArgumentException("Dmaap body is null"); + } + JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header"); - String requestId = getValue(commonHeaderJsonNode,"request-id",""); - String subRequestId = getValue(commonHeaderJsonNode,"sub-request-id",""); - if(!StringUtils.isEmpty(subRequestId)){ - requestId = requestId +"-"+subRequestId; + String requestId = getValue(commonHeaderJsonNode, "request-id", ""); + String subRequestId = getValue(commonHeaderJsonNode, "sub-request-id", ""); + if (!StringUtils.isEmpty(subRequestId)) { + requestId = requestId + "-" + subRequestId; } return requestId; } public static Integer extractStatusCode(JsonNode event) { + + if (event == null){ + throw new IllegalArgumentException("Input event is null"); + } + Integer statusCode; statusCode = event.get("output").get("status").get("code").asInt(); return statusCode; } - private static String getValue(JsonNode jsonNode,String name,String defaultValue){ - if(jsonNode == null){ + private static String getValue(JsonNode jsonNode, String name, String defaultValue) { + if (jsonNode == null) { return defaultValue; } JsonNode childJsonNode = jsonNode.get(name); - if(childJsonNode == null){ + if (childJsonNode == null) { return defaultValue; } String value = childJsonNode.asText(); - if(value == null){ + if (value == null) { return defaultValue; } return value; diff --git a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/impl/WorkerImpl.java b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/impl/WorkerImpl.java index 9334a8fad..8ede2ba99 100644 --- a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/impl/WorkerImpl.java +++ b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/impl/WorkerImpl.java @@ -66,7 +66,7 @@ public class WorkerImpl implements Runnable { // message at the end try { JsonNode outputJsonNode = doDG(event.getRpcName(), event.getBody()); - DmaapOutgoingMessage dmaapOutgoingMessage= Converter.convJsonNodeToDmaapOutgoingMessage(event, outputJsonNode); + DmaapOutgoingMessage dmaapOutgoingMessage= Converter.convertJsonNodeToDmaapOutgoingMessage(event, outputJsonNode); postMessageToDMaaP(dmaapOutgoingMessage,requestIdWithSubId); Integer statusCode = extractStatusCode(dmaapOutgoingMessage.getBody()); if (ProviderOperations.isSucceeded(statusCode)) { @@ -114,7 +114,7 @@ public class WorkerImpl implements Runnable { private void postMessageToDMaaP(DmaapOutgoingMessage dmaapOutgoingMessage,String requestIdWithSubId) { String dmaapOutgoingMessageJsonString; try { - dmaapOutgoingMessageJsonString = Converter.convDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage); + dmaapOutgoingMessageJsonString = Converter.convertDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage); dmaap.postStatus(dmaapOutgoingMessage.getCambriaPartition(),dmaapOutgoingMessageJsonString); } catch (JsonProcessingException e) { LOG.error("failed to postMessageToDMaaP requestIdWithSubId: "+requestIdWithSubId+" dmaapOutgoingMessage: "+dmaapOutgoingMessage, e); |