Improve ActionUtil coverage and improve Sonar score 51/109351/4
authorJulienBe <julien.bertozzi@intl.att.com>
Fri, 19 Jun 2020 14:54:22 +0000 (16:54 +0200)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Sun, 21 Jun 2020 07:14:49 +0000 (07:14 +0000)
Issue-ID: SDC-3133
Signed-off-by: JulienBe <julien.bertozzi@intl.att.com>
Change-Id: I5e6f927599520efa5bfa5856863ca173ecdfb553
Signed-off-by: JulienBe <julien.bertozzi@intl.att.com>
openecomp-be/lib/openecomp-sdc-action-lib/openecomp-sdc-action-api/src/main/java/org/openecomp/sdc/action/util/ActionUtil.java
openecomp-be/lib/openecomp-sdc-action-lib/openecomp-sdc-action-api/src/test/java/org/openecomp/sdc/action/types/ActionUtilTest.java [new file with mode: 0644]

index 2fba053..28195b3 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -29,8 +29,8 @@ import org.slf4j.MDC;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.time.ZoneOffset;
-import java.util.Date;
-import java.util.TimeZone;
+import java.util.*;
+import java.util.function.LongSupplier;
 
 import static org.openecomp.sdc.action.ActionConstants.*;
 import static org.openecomp.sdc.action.errors.ActionErrorConstants.*;
@@ -38,254 +38,198 @@ import static org.openecomp.sdc.action.types.ActionLogResponseCode.*;
 
 public class ActionUtil {
 
-  private static final String UTC_DATE_FORMAT = "dd MMM yyyy kk:mm:ss z";
-  private static final String LOG_UTC_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
+    private static final String UTC_DATE_FORMAT = "dd MMM yyyy kk:mm:ss z";
+    private static final String LOG_UTC_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
+    private static final ActionLogResponseCode defaultResponseCode = INTERNAL_SERVER_ERROR;
+    private static final Map<String, ActionLogResponseCode> errorCodeMap = initErrorCodeMap();
+    private static final EnumMap<CategoryLogLevel, String> errorTypeMap = initErrorTypeMap();
+
+    private static Map<String, ActionLogResponseCode> initErrorCodeMap() {
+        Map<String, ActionLogResponseCode> map = new HashMap<>();
+        map.put(ACTION_REQUEST_INVALID_GENERIC_CODE, INVALID_REQUEST_PARAM);
+        map.put(ACTION_AUTHENTICATION_ERR_CODE, INTERNAL_SERVER_ERROR);
+        map.put(ACTION_AUTHORIZATION_ERR_CODE, MISSING_AUTHORIZATION);
+        map.put(ACTION_INVALID_INSTANCE_ID_CODE, MISSING_INSTANCE_ID_HEADER);
+        map.put(ACTION_INVALID_REQUEST_ID_CODE, MISSING_REQUEST_ID_HEADER);
+        map.put(ACTION_INVALID_PARAM_CODE, INVALID_REQUEST_PARAM);
+        map.put(ACTION_INVALID_REQUEST_BODY_CODE, MISSING_REQUEST_BODY);
+        map.put(ACTION_UPDATE_NOT_ALLOWED_CODE_NAME, ACTION_NAME_UPDATE_NOT_ALLOWED);
+        map.put(ACTION_CHECKOUT_ON_LOCKED_ENTITY, CHECKOUT_ON_LOCKED_ENTITY);
+        map.put(ACTION_ENTITY_UNIQUE_VALUE_ERROR, ACTION_NAME_ALREADY_EXISTS);
+        map.put(ACTION_INVALID_SEARCH_CRITERIA, INVALID_SEARCH_FILTER_CRITERIA);
+        map.put(ACTION_MULT_SEARCH_CRITERIA, MULTIPLE_FILTER_CRITERIA_NOT_SUPPORTED);
+        map.put(ACTION_UPDATE_ON_UNLOCKED_ENTITY, UPDATE_ON_UNLOCKED_ENTITY);
+        map.put(ACTION_UPDATE_INVALID_VERSION, INVALID_REQUESTED_VERSION);
+        map.put(ACTION_UPDATE_NOT_ALLOWED_CODE, UPDATE_NOT_ALLOWED);
+        map.put(ACTION_CHECKIN_ON_UNLOCKED_ENTITY, CHECKIN_ON_UNLOCKED_ENTITY);
+        map.put(ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED, SUBMIT_ON_FINAL_ENTITY);
+        map.put(ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED, SUBMIT_ON_LOCKED_ENTITY_OTHER_USER);
+        map.put(ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY, UNDO_CHECKOUT_ON_UNLOCKED_ENTITY);
+        map.put(ACTION_NOT_LOCKED_CODE, ACTION_NOT_LOCKED);
+        map.put(ACTION_ARTIFACT_CHECKSUM_ERROR_CODE, CHECKSUM_ERROR);
+        map.put(ACTION_ARTIFACT_TOO_BIG_ERROR_CODE, ARTIFACT_TOO_BIG);
+        map.put(ACTION_ARTIFACT_ALREADY_EXISTS_CODE, ARTIFACT_ALREADY_EXISTS);
+        map.put(ACTION_ARTIFACT_UPDATE_READ_ONLY, ARTIFACT_UPDATE_READ_ONLY);
+        map.put(ACTION_ARTIFACT_DELETE_READ_ONLY, ARTIFACT_DELETE_READ_ONLY);
+        map.put(ACTION_ARTIFACT_INVALID_PROTECTION_CODE, ARTIFACT_PROTECTION_INVALID);
+        map.put(ACTION_ARTIFACT_INVALID_NAME_CODE, ARTIFACT_NAME_INVALID);
+        map.put(ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER, UPDATE_ON_LOCKED_ENTITY);
+        map.put(ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER, CHECKIN_ON_LOCKED_ENTITY_OTHER_USER);
+        map.put(ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER, CHECKOUT_ON_LOCKED_ENTITY);
+        map.put(ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER, UNDO_CHECKOUT_ON_LOCKED_ENTITY);
+        map.put(ACTION_ENTITY_NOT_EXIST_CODE, ACTION_NOT_FOUND);
+        map.put(ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE, ARTIFACT_NOT_FOUND);
+        map.put(ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE, DELETE_ARTIFACT_ON_LOCKED_ENTITY);
+        map.put(ACTION_DELETE_ON_LOCKED_ENTITY_CODE, DELETE_ON_LOCKED_ENTITY_OTHER_USER);
+        map.put(ACTION_INTERNAL_SERVER_ERR_CODE, INTERNAL_SERVER_ERROR);
+        map.put(ACTION_QUERY_FAILURE_CODE, QUERY_FAILURE);
+        return map;
+    }
 
-  /**
-   * Get Current Timestamp in UTC format.
-   *
-   * @return Current Timestamp in UTC format
-   */
-  public static Date getCurrentTimeStampUtc() {
-    return Date.from(java.time.ZonedDateTime.now(ZoneOffset.UTC).toInstant());
-  }
+    private static EnumMap<CategoryLogLevel, String> initErrorTypeMap() {
+        EnumMap<CategoryLogLevel, String> map = new EnumMap<>(CategoryLogLevel.class);
+        map.put(CategoryLogLevel.WARN, "W");
+        map.put(CategoryLogLevel.ERROR, "E");
+        map.put(CategoryLogLevel.FATAL, "F");
+        return map;
+    }
 
-  /**
-   * Convert timestamp to UTC format date string.
-   *
-   * @param timeStamp UTC timestamp to be converted to the UTC Date format
-   * @return UTC formatted Date string from timestamp
-   */
-  public static String getUtcDateStringFromTimestamp(Date timeStamp) {
-    DateFormat df = new SimpleDateFormat(UTC_DATE_FORMAT);
-    df.setTimeZone(TimeZone.getTimeZone("GMT"));
-    return df.format(timeStamp);
-  }
+    private ActionUtil() {
+    }
 
-  /**
-   * Convert timestamp to UTC format date string.
-   *
-   * @param timeStamp UTC timestamp to be converted to the UTC Date format
-   * @return UTC formatted Date string from timestamp
-   */
-  public static String getLogUtcDateStringFromTimestamp(Date timeStamp) {
-    DateFormat df = new SimpleDateFormat(LOG_UTC_DATE_FORMAT);
-    df.setTimeZone(TimeZone.getTimeZone("GMT"));
-    return df.format(timeStamp);
-  }
+    /**
+     * Get Current Timestamp in UTC format.
+     *
+     * @return Current Timestamp in UTC format
+     */
+    public static Date getCurrentTimeStampUtc() {
+        return Date.from(java.time.ZonedDateTime.now(ZoneOffset.UTC).toInstant());
+    }
 
-  /**
-   * Method to set up specific attributes MDC for the current logging operation.
-   *
-   * @param subOperation Request Name
-   */
-  public static void actionLogPreProcessor(ActionSubOperation subOperation, String targetEntity) {
-    MDC.put(BEGIN_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
-    if (subOperation != null) {
-      MDC.put(TARGET_SERVICE_NAME, subOperation.name());
+    /**
+     * Convert timestamp to UTC format date string.
+     *
+     * @param timeStamp UTC timestamp to be converted to the UTC Date format
+     * @return UTC formatted Date string from timestamp
+     */
+    public static String getUtcDateStringFromTimestamp(Date timeStamp) {
+        DateFormat df = new SimpleDateFormat(UTC_DATE_FORMAT);
+        df.setTimeZone(TimeZone.getTimeZone("GMT"));
+        return df.format(timeStamp);
     }
 
-    MDC.put(TARGET_ENTITY, targetEntity);
-  }
+    /**
+     * Convert timestamp to UTC format date string.
+     *
+     * @param timeStamp UTC timestamp to be converted to the UTC Date format
+     * @return UTC formatted Date string from timestamp
+     */
+    public static String getLogUtcDateStringFromTimestamp(Date timeStamp) {
+        DateFormat df = new SimpleDateFormat(LOG_UTC_DATE_FORMAT);
+        df.setTimeZone(TimeZone.getTimeZone("GMT"));
+        return df.format(timeStamp);
+    }
 
-  /**
-   * Method to enhance the MDC after the logging operation for Metrics and Audit logs.
-   *
-   * @param statusCode Response code for the current operation
-   */
-  public static void actionLogPostProcessor(ResponseStatus statusCode) {
-    actionLogPostProcessor(statusCode, false);
-  }
+    /**
+     * Method to set up specific attributes MDC for the current logging operation.
+     *
+     * @param subOperation Request Name
+     */
+    public static void actionLogPreProcessor(ActionSubOperation subOperation, String targetEntity) {
+        MDC.put(BEGIN_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
+        if (subOperation != null) {
+            MDC.put(TARGET_SERVICE_NAME, subOperation.name());
+        }
+
+        MDC.put(TARGET_ENTITY, targetEntity);
+    }
 
-  public static void actionLogPostProcessor(ResponseStatus statusCode, boolean isServiceMetricLog) {
-    actionLogPostProcessor(statusCode, null, isServiceMetricLog);
-  }
+    /**
+     * Method to enhance the MDC after the logging operation for Metrics and Audit logs.
+     *
+     * @param statusCode Response code for the current operation
+     */
+    public static void actionLogPostProcessor(ResponseStatus statusCode) {
+        actionLogPostProcessor(statusCode, false);
+    }
 
-  public static void actionLogPostProcessor(ResponseStatus statusCode, String responseCode,
-                                            boolean isServiceMetricLog) {
-    actionLogPostProcessor(statusCode, responseCode, null, isServiceMetricLog);
-  }
+    public static void actionLogPostProcessor(ResponseStatus statusCode, boolean isServiceMetricLog) {
+        actionLogPostProcessor(statusCode, null, isServiceMetricLog);
+    }
 
-  /**
-   * Action log post processor.
-   *
-   * @param statusCode          the status code
-   * @param responseCode        the response code
-   * @param responseDescription the response description
-   * @param isServiceMetricLog  the is service metric log
-   */
-  public static void actionLogPostProcessor(ResponseStatus statusCode, String responseCode,
-                                            String responseDescription,
-                                            boolean isServiceMetricLog) {
-    MDC.put(STATUS_CODE, statusCode.name());
-    if (responseCode != null) {
-      int logResponseCode = getLogResponseCode(responseCode);
-      MDC.put(RESPONSE_CODE, Integer.toString(logResponseCode));
+    public static void actionLogPostProcessor(ResponseStatus statusCode, String responseCode,
+                                              boolean isServiceMetricLog) {
+        actionLogPostProcessor(statusCode, responseCode, null, isServiceMetricLog);
     }
-    MDC.put(RESPONSE_DESCRIPTION, responseDescription);
-    long beginTimestamp;
-    if (isServiceMetricLog) {
-      beginTimestamp = Long.valueOf(MDC.get(SERVICE_METRIC_BEGIN_TIMESTAMP));
-    } else {
-      beginTimestamp = Long.valueOf(MDC.get(BEGIN_TIMESTAMP));
+
+    /**
+     * Action log post processor.
+     *
+     * @param statusCode          the status code
+     * @param responseCode        the response code
+     * @param responseDescription the response description
+     * @param isServiceMetricLog  the is service metric log
+     */
+    public static void actionLogPostProcessor(ResponseStatus statusCode, String responseCode,
+                                              String responseDescription,
+                                              boolean isServiceMetricLog) {
+        actionLogPostProcessor(statusCode, responseCode, responseDescription, isServiceMetricLog, System::currentTimeMillis);
+    }
+
+    /**
+     * Action log post processor.
+     *
+     * @param statusCode          the status code
+     * @param responseCode        the response code
+     * @param responseDescription the response description
+     * @param isServiceMetricLog  the is service metric log
+     */
+    public static void actionLogPostProcessor(ResponseStatus statusCode, String responseCode,
+                                              String responseDescription,
+                                              boolean isServiceMetricLog,
+                                              LongSupplier getCurrentTime) {
+        MDC.put(STATUS_CODE, statusCode.name());
+        if (responseCode != null) {
+            int logResponseCode = getLogResponseCode(responseCode);
+            MDC.put(RESPONSE_CODE, Integer.toString(logResponseCode));
+        }
+        MDC.put(RESPONSE_DESCRIPTION, responseDescription);
+        long beginTimestamp;
+        if (isServiceMetricLog) {
+            beginTimestamp = Long.parseLong(MDC.get(SERVICE_METRIC_BEGIN_TIMESTAMP));
+        } else {
+            beginTimestamp = Long.parseLong(MDC.get(BEGIN_TIMESTAMP));
+        }
+        long endTimestamp = getCurrentTime.getAsLong();
+        MDC.put(BEGIN_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(beginTimestamp)));
+        MDC.put(END_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(endTimestamp)));
+        MDC.put(ELAPSED_TIME, String.valueOf(endTimestamp - beginTimestamp));
     }
-    long endTimestamp = System.currentTimeMillis();
-    MDC.put(BEGIN_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(beginTimestamp)));
-    MDC.put(END_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(endTimestamp)));
-    MDC.put(ELAPSED_TIME, String.valueOf(endTimestamp - beginTimestamp));
-  }
 
-  /**
-   * Action Library Error logging Helper.
-   *
-   * @param errorCategory    WARN or ERROR
-   * @param errorCode        Action Library exception code
-   * @param errorDescription Description of the error
-   */
-  public static void actionErrorLogProcessor(CategoryLogLevel errorCategory, String errorCode,
-                                             String errorDescription) {
-    MDC.put(ERROR_CATEGORY, errorCategory.name());
-    if (errorCode != null) {
-      String errorType = "";
-      switch (errorCategory) {
-        case WARN:
-          errorType = "W";
-          break;
-        case ERROR:
-          errorType = "E";
-          break;
-        case FATAL:
-          errorType = "F";
-          break;
-        default:
-      }
-      MDC.put(ERROR_CODE, getLogResponseCode(errorCode) + errorType);
+    /**
+     * Action Library Error logging Helper.
+     *
+     * @param errorCategory    WARN or ERROR
+     * @param errorCode        Action Library exception code
+     * @param errorDescription Description of the error
+     */
+    public static void actionErrorLogProcessor(CategoryLogLevel errorCategory, String errorCode,
+                                               String errorDescription) {
+        MDC.put(ERROR_CATEGORY, errorCategory.name());
+        if (errorCode != null) {
+            MDC.put(ERROR_CODE, getLogResponseCode(errorCode) + (errorTypeMap.getOrDefault(errorCategory, "")));
+        }
+        MDC.put(ERROR_DESCRIPTION, errorDescription);
     }
-    MDC.put(ERROR_DESCRIPTION, errorDescription);
-  }
 
-  /**
-   * Method to convert Action Library exception codes to OPENECOMP Audit codes in {@link
-   * ActionLogResponseCode} e.g: ACT1060 --> 201
-   *
-   * @param errorCode Action library exception code
-   * @return Audit log code corresponding to the Action Library exception
-   */
-  public static int getLogResponseCode(String errorCode) {
-    ActionLogResponseCode responseCode = INTERNAL_SERVER_ERROR;
-    switch (errorCode) {
-      case ACTION_REQUEST_INVALID_GENERIC_CODE:
-        responseCode = INVALID_REQUEST_PARAM;
-        break;
-      case ACTION_AUTHENTICATION_ERR_CODE:
-        break;
-      case ACTION_AUTHORIZATION_ERR_CODE:
-        responseCode = MISSING_AUTHORIZATION;
-        break;
-      case ACTION_INVALID_INSTANCE_ID_CODE:
-        responseCode = MISSING_INSTANCE_ID_HEADER;
-        break;
-      case ACTION_INVALID_REQUEST_ID_CODE:
-        responseCode = MISSING_REQUEST_ID_HEADER;
-        break;
-      case ACTION_INVALID_PARAM_CODE:
-        responseCode = INVALID_REQUEST_PARAM;
-        break;
-      case ACTION_INVALID_REQUEST_BODY_CODE:
-        responseCode = MISSING_REQUEST_BODY;
-        break;
-      case ACTION_UPDATE_NOT_ALLOWED_CODE_NAME:
-        responseCode = ACTION_NAME_UPDATE_NOT_ALLOWED;
-        break;
-      case ACTION_CHECKOUT_ON_LOCKED_ENTITY:
-        responseCode = CHECKOUT_ON_LOCKED_ENTITY;
-        break;
-      case ACTION_ENTITY_UNIQUE_VALUE_ERROR:
-        responseCode = ACTION_NAME_ALREADY_EXISTS;
-        break;
-      case ACTION_INVALID_SEARCH_CRITERIA:
-        responseCode = INVALID_SEARCH_FILTER_CRITERIA;
-        break;
-      case ACTION_MULT_SEARCH_CRITERIA:
-        responseCode = MULTIPLE_FILTER_CRITERIA_NOT_SUPPORTED;
-        break;
-      case ACTION_UPDATE_ON_UNLOCKED_ENTITY:
-        responseCode = UPDATE_ON_UNLOCKED_ENTITY;
-        break;
-      case ACTION_UPDATE_INVALID_VERSION:
-        responseCode = INVALID_REQUESTED_VERSION;
-        break;
-      case ACTION_UPDATE_NOT_ALLOWED_CODE:
-        responseCode = UPDATE_NOT_ALLOWED;
-        break;
-      case ACTION_CHECKIN_ON_UNLOCKED_ENTITY:
-        responseCode = CHECKIN_ON_UNLOCKED_ENTITY;
-        break;
-      case ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED:
-        responseCode = SUBMIT_ON_FINAL_ENTITY;
-        break;
-      case ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED:
-        responseCode = SUBMIT_ON_LOCKED_ENTITY_OTHER_USER;
-        break;
-      case ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY:
-        responseCode = UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
-        break;
-      case ACTION_NOT_LOCKED_CODE:
-        responseCode = ACTION_NOT_LOCKED;
-        break;
-      case ACTION_ARTIFACT_CHECKSUM_ERROR_CODE:
-        responseCode = CHECKSUM_ERROR;
-        break;
-      case ACTION_ARTIFACT_TOO_BIG_ERROR_CODE:
-        responseCode = ARTIFACT_TOO_BIG;
-        break;
-      case ACTION_ARTIFACT_ALREADY_EXISTS_CODE:
-        responseCode = ARTIFACT_ALREADY_EXISTS;
-        break;
-      case ACTION_ARTIFACT_UPDATE_READ_ONLY:
-        responseCode = ARTIFACT_UPDATE_READ_ONLY;
-        break;
-      case ACTION_ARTIFACT_DELETE_READ_ONLY:
-        responseCode = ARTIFACT_DELETE_READ_ONLY;
-        break;
-      case ACTION_ARTIFACT_INVALID_PROTECTION_CODE:
-        responseCode = ARTIFACT_PROTECTION_INVALID;
-        break;
-      case ACTION_ARTIFACT_INVALID_NAME_CODE:
-        responseCode = ARTIFACT_NAME_INVALID;
-        break;
-      case ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER:
-        responseCode = UPDATE_ON_LOCKED_ENTITY;
-        break;
-      case ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER:
-        responseCode = CHECKIN_ON_LOCKED_ENTITY_OTHER_USER;
-        break;
-      case ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER:
-        responseCode = CHECKOUT_ON_LOCKED_ENTITY;
-        break;
-      case ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER:
-        responseCode = UNDO_CHECKOUT_ON_LOCKED_ENTITY;
-        break;
-      case ACTION_ENTITY_NOT_EXIST_CODE:
-        responseCode = ACTION_NOT_FOUND;
-        break;
-      case ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE:
-        responseCode = ARTIFACT_NOT_FOUND;
-        break;
-      case ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE:
-        responseCode = DELETE_ARTIFACT_ON_LOCKED_ENTITY;
-        break;
-      case ACTION_DELETE_ON_LOCKED_ENTITY_CODE:
-        responseCode = DELETE_ON_LOCKED_ENTITY_OTHER_USER;
-        break;
-      case ACTION_INTERNAL_SERVER_ERR_CODE:
-        responseCode = INTERNAL_SERVER_ERROR;
-        break;
-      case ACTION_QUERY_FAILURE_CODE:
-        responseCode = QUERY_FAILURE;
-        break;
-      default:
+    /**
+     * Method to convert Action Library exception codes to OPENECOMP Audit codes in {@link
+     * ActionLogResponseCode} e.g: ACT1060 --> 201
+     *
+     * @param errorCode Action library exception code
+     * @return Audit log code corresponding to the Action Library exception
+     */
+    public static int getLogResponseCode(String errorCode) {
+        return errorCodeMap.getOrDefault(errorCode, defaultResponseCode).getValue();
     }
-    return responseCode.getValue();
-  }
 }
diff --git a/openecomp-be/lib/openecomp-sdc-action-lib/openecomp-sdc-action-api/src/test/java/org/openecomp/sdc/action/types/ActionUtilTest.java b/openecomp-be/lib/openecomp-sdc-action-lib/openecomp-sdc-action-api/src/test/java/org/openecomp/sdc/action/types/ActionUtilTest.java
new file mode 100644 (file)
index 0000000..9457eec
--- /dev/null
@@ -0,0 +1,135 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.action.types;
+
+import org.junit.jupiter.api.Test;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
+import org.openecomp.sdc.action.logging.CategoryLogLevel;
+import org.openecomp.sdc.action.util.ActionUtil;
+import org.slf4j.MDC;
+
+import java.time.Clock;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.util.Date;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.openecomp.sdc.action.ActionConstants.*;
+import static org.openecomp.sdc.action.errors.ActionErrorConstants.*;
+import static org.openecomp.sdc.action.types.ActionLogResponseCode.*;
+
+public class ActionUtilTest {
+
+    @Test
+    public void getLogResponseCodeTest() {
+        assertEquals(INVALID_REQUEST_PARAM.getValue(), ActionUtil.getLogResponseCode(ACTION_REQUEST_INVALID_GENERIC_CODE));
+        assertEquals(INTERNAL_SERVER_ERROR.getValue(), ActionUtil.getLogResponseCode(ACTION_AUTHENTICATION_ERR_CODE));
+        assertEquals(MISSING_AUTHORIZATION.getValue(), ActionUtil.getLogResponseCode(ACTION_AUTHORIZATION_ERR_CODE));
+        assertEquals(MISSING_INSTANCE_ID_HEADER.getValue(), ActionUtil.getLogResponseCode(ACTION_INVALID_INSTANCE_ID_CODE));
+        assertEquals(MISSING_REQUEST_ID_HEADER.getValue(), ActionUtil.getLogResponseCode(ACTION_INVALID_REQUEST_ID_CODE));
+        assertEquals(INVALID_REQUEST_PARAM.getValue(), ActionUtil.getLogResponseCode(ACTION_INVALID_PARAM_CODE));
+        assertEquals(MISSING_REQUEST_BODY.getValue(), ActionUtil.getLogResponseCode(ACTION_INVALID_REQUEST_BODY_CODE));
+        assertEquals(ACTION_NAME_UPDATE_NOT_ALLOWED.getValue(), ActionUtil.getLogResponseCode(ACTION_UPDATE_NOT_ALLOWED_CODE_NAME));
+        assertEquals(CHECKOUT_ON_LOCKED_ENTITY.getValue(), ActionUtil.getLogResponseCode(ACTION_CHECKOUT_ON_LOCKED_ENTITY));
+        assertEquals(ACTION_NAME_ALREADY_EXISTS.getValue(), ActionUtil.getLogResponseCode(ACTION_ENTITY_UNIQUE_VALUE_ERROR));
+        assertEquals(INVALID_SEARCH_FILTER_CRITERIA.getValue(), ActionUtil.getLogResponseCode(ACTION_INVALID_SEARCH_CRITERIA));
+        assertEquals(MULTIPLE_FILTER_CRITERIA_NOT_SUPPORTED.getValue(), ActionUtil.getLogResponseCode(ACTION_MULT_SEARCH_CRITERIA));
+        assertEquals(UPDATE_ON_UNLOCKED_ENTITY.getValue(), ActionUtil.getLogResponseCode(ACTION_UPDATE_ON_UNLOCKED_ENTITY));
+        assertEquals(INVALID_REQUESTED_VERSION.getValue(), ActionUtil.getLogResponseCode(ACTION_UPDATE_INVALID_VERSION));
+        assertEquals(UPDATE_NOT_ALLOWED.getValue(), ActionUtil.getLogResponseCode(ACTION_UPDATE_NOT_ALLOWED_CODE));
+        assertEquals(CHECKIN_ON_UNLOCKED_ENTITY.getValue(), ActionUtil.getLogResponseCode(ACTION_CHECKIN_ON_UNLOCKED_ENTITY));
+        assertEquals(SUBMIT_ON_FINAL_ENTITY.getValue(), ActionUtil.getLogResponseCode(ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED));
+        assertEquals(SUBMIT_ON_LOCKED_ENTITY_OTHER_USER.getValue(), ActionUtil.getLogResponseCode(ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED));
+        assertEquals(UNDO_CHECKOUT_ON_UNLOCKED_ENTITY.getValue(), ActionUtil.getLogResponseCode(ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY));
+        assertEquals(ACTION_NOT_LOCKED.getValue(), ActionUtil.getLogResponseCode(ACTION_NOT_LOCKED_CODE));
+        assertEquals(CHECKSUM_ERROR.getValue(), ActionUtil.getLogResponseCode(ACTION_ARTIFACT_CHECKSUM_ERROR_CODE));
+        assertEquals(ARTIFACT_TOO_BIG.getValue(), ActionUtil.getLogResponseCode(ACTION_ARTIFACT_TOO_BIG_ERROR_CODE));
+        assertEquals(ARTIFACT_ALREADY_EXISTS.getValue(), ActionUtil.getLogResponseCode(ACTION_ARTIFACT_ALREADY_EXISTS_CODE));
+        assertEquals(ARTIFACT_UPDATE_READ_ONLY.getValue(), ActionUtil.getLogResponseCode(ACTION_ARTIFACT_UPDATE_READ_ONLY));
+        assertEquals(ARTIFACT_DELETE_READ_ONLY.getValue(), ActionUtil.getLogResponseCode(ACTION_ARTIFACT_DELETE_READ_ONLY));
+        assertEquals(ARTIFACT_PROTECTION_INVALID.getValue(), ActionUtil.getLogResponseCode(ACTION_ARTIFACT_INVALID_PROTECTION_CODE));
+        assertEquals(ARTIFACT_NAME_INVALID.getValue(), ActionUtil.getLogResponseCode(ACTION_ARTIFACT_INVALID_NAME_CODE));
+        assertEquals(UPDATE_ON_LOCKED_ENTITY.getValue(), ActionUtil.getLogResponseCode(ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER));
+        assertEquals(CHECKIN_ON_LOCKED_ENTITY_OTHER_USER.getValue(), ActionUtil.getLogResponseCode(ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER));
+        assertEquals(CHECKOUT_ON_LOCKED_ENTITY.getValue(), ActionUtil.getLogResponseCode(ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER));
+        assertEquals(UNDO_CHECKOUT_ON_LOCKED_ENTITY.getValue(), ActionUtil.getLogResponseCode(ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER));
+        assertEquals(ACTION_NOT_FOUND.getValue(), ActionUtil.getLogResponseCode(ACTION_ENTITY_NOT_EXIST_CODE));
+        assertEquals(ARTIFACT_NOT_FOUND.getValue(), ActionUtil.getLogResponseCode(ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE));
+        assertEquals(DELETE_ARTIFACT_ON_LOCKED_ENTITY.getValue(), ActionUtil.getLogResponseCode(ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE));
+        assertEquals(DELETE_ON_LOCKED_ENTITY_OTHER_USER.getValue(), ActionUtil.getLogResponseCode(ACTION_DELETE_ON_LOCKED_ENTITY_CODE));
+        assertEquals(INTERNAL_SERVER_ERROR.getValue(), ActionUtil.getLogResponseCode(ACTION_INTERNAL_SERVER_ERR_CODE));
+        assertEquals(QUERY_FAILURE.getValue(), ActionUtil.getLogResponseCode(ACTION_QUERY_FAILURE_CODE));
+        assertEquals(INTERNAL_SERVER_ERROR.getValue(), ActionUtil.getLogResponseCode("plomplomoyo"));
+    }
+
+    @Test
+    public void actionErrorLogProcessorTest() {
+        ActionUtil.actionErrorLogProcessor(CategoryLogLevel.DEBUG, "", "description");
+        assertEquals("201", MDC.get(ERROR_CODE));
+        assertEquals("DEBUG", MDC.get(ERROR_CATEGORY));
+        assertEquals("description", MDC.get(ERROR_DESCRIPTION));
+
+        ActionUtil.actionErrorLogProcessor(CategoryLogLevel.WARN, "222", "description?");
+        assertEquals("201W", MDC.get(ERROR_CODE));
+        assertEquals("WARN", MDC.get(ERROR_CATEGORY));
+        assertEquals("description?", MDC.get(ERROR_DESCRIPTION));
+
+        ActionUtil.actionErrorLogProcessor(CategoryLogLevel.ERROR, ACTION_ARTIFACT_INVALID_NAME_CODE, "noitpircsed?!");
+        assertEquals("516E", MDC.get(ERROR_CODE));
+        assertEquals("ERROR", MDC.get(ERROR_CATEGORY));
+        assertEquals("noitpircsed?!", MDC.get(ERROR_DESCRIPTION));
+
+        ActionUtil.actionErrorLogProcessor(CategoryLogLevel.FATAL, "400", "sloubi1");
+        assertEquals("201F", MDC.get(ERROR_CODE));
+        assertEquals("FATAL", MDC.get(ERROR_CATEGORY));
+        assertEquals("sloubi1", MDC.get(ERROR_DESCRIPTION));
+
+        ActionUtil.actionErrorLogProcessor(CategoryLogLevel.INFO, null, "sloubi2");
+        assertEquals("201F", MDC.get(ERROR_CODE));
+        assertEquals("INFO", MDC.get(ERROR_CATEGORY));
+        assertEquals("sloubi2", MDC.get(ERROR_DESCRIPTION));
+    }
+
+    @Test
+    public void actionLogPostProcessorTest() {
+        MDC.put(SERVICE_METRIC_BEGIN_TIMESTAMP, "486");
+        MDC.put(BEGIN_TIMESTAMP, "133");
+
+        ActionUtil.actionLogPostProcessor(ONAPLogConstants.ResponseStatus.COMPLETE, "randomString", "anotherString", true);
+        assertEquals("COMPLETE", MDC.get(STATUS_CODE));
+        assertEquals("201", MDC.get(RESPONSE_CODE));
+        assertEquals("anotherString", MDC.get(RESPONSE_DESCRIPTION));
+
+        ActionUtil.actionLogPostProcessor(ONAPLogConstants.ResponseStatus.COMPLETE, "randomString", "anotherString", true, () -> 68000L);
+        assertEquals("1970-01-01T00:01:08.000", MDC.get(END_TIMESTAMP));
+        assertEquals("67514", MDC.get(ELAPSED_TIME));
+        assertEquals("1970-01-01T00:00:00.486", MDC.get(BEGIN_TIMESTAMP));
+
+        ActionUtil.actionLogPreProcessor(ActionSubOperation.CHECKOUT_ACTION, "targetEntity");
+        assertEquals("targetEntity", MDC.get(TARGET_ENTITY));
+        assertEquals("CHECKOUT_ACTION", MDC.get(TARGET_SERVICE_NAME));
+    }
+
+    @Test
+    public void getLogUtcDateStringFromTimestampTest() {
+        assertEquals("2020-01-23T12:34:56.000", ActionUtil.getLogUtcDateStringFromTimestamp(Date.from(Instant.now(Clock.fixed(Instant.parse("2020-01-23T12:34:56Z"), ZoneOffset.UTC)))));
+    }
+}