Refactor ErrorLogHelper
[aai/aai-common.git] / aai-els-onap-logging / src / test / java / org / onap / aai / logging / ErrorLogHelperTest.java
index 9952277..fcbd86e 100644 (file)
@@ -3,6 +3,7 @@
  * org.onap.aai
  * ================================================================================
  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Deutsche Telekom SA.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,44 +27,54 @@ import static org.junit.Assert.*;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.ws.rs.core.MediaType;
 
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.onap.aai.domain.restPolicyException.PolicyException;
-import org.onap.aai.domain.restPolicyException.RESTResponse;
-import org.onap.aai.domain.restPolicyException.RequestError;
-import org.onap.aai.domain.restServiceException.ServiceException;
+import org.onap.aai.domain.errorResponse.ErrorMessage;
+import org.onap.aai.domain.errorResponse.ExceptionType;
+import org.onap.aai.domain.errorResponse.Fault;
+import org.onap.aai.domain.errorResponse.Info;
+import org.onap.aai.domain.errorResponse.ServiceFault;
 import org.onap.aai.exceptions.AAIException;
 import org.onap.aai.util.LogFile;
-import org.onap.aai.util.MapperUtil;
 import org.slf4j.MDC;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
+
+@SuppressWarnings("deprecation")
 public class ErrorLogHelperTest {
 
-    private static final String ErrorLogFileName = "error.log";
+    private static final String errorLogFileName = "error.log";
+    private static final ObjectMapper objectMapper = new ObjectMapper();
+    private static final XmlMapper xmlMapper = new XmlMapper();
 
     @Before
     public void init() {
         System.setProperty("AJSC_HOME", ".");
-
     }
 
     @After
     public void cleanup() throws IOException {
         MDC.clear();
-        LogFile.deleteContents(ErrorLogFileName);
+        LogFile.deleteContents(errorLogFileName);
     }
 
     @Test
     public void logErrorTest() throws IOException, InterruptedException {
         // ||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110
         ErrorLogHelper.logError("AAI_6110");
-        sleep(5000);
-        String logContents = LogFile.getContents(ErrorLogFileName);
+        sleep(2000);
+        String logContents = LogFile.getContents(errorLogFileName);
 
         assertNotNull(logContents);
 
@@ -80,8 +91,8 @@ public class ErrorLogHelperTest {
         // ||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110 message
         String errorMessage = "Object is referenced by additional objects";
         ErrorLogHelper.logError("AAI_6110", errorMessage);
-        sleep(5000);
-        String logContents = LogFile.getContents(ErrorLogFileName);
+        sleep(3000);
+        String logContents = LogFile.getContents(errorLogFileName);
 
         assertNotNull(logContents);
 
@@ -93,58 +104,112 @@ public class ErrorLogHelperTest {
     }
 
     @Test
-    public void getRESTAPIPolicyErrorResponseTest() throws AAIException {
+    public void getRESTAPIPolicyErrorResponseXmlTest() throws AAIException, JsonMappingException, JsonProcessingException {
+        // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
+        List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_XML_TYPE);
+        ArrayList<String> variables = new ArrayList<String>();
+
+        AAIException aaiException = new AAIException("AAI_3102");
+        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
+        assertNotNull(errorResponse);
+        assertTrue(errorResponse.contains("<Fault>"));
+        assertTrue(errorResponse.contains("<policyException>"));
+        assertTrue(errorResponse.contains("<variables>"));
+        assertTrue(errorResponse.contains("<variable>"));
+
+        Fault restResponse = xmlMapper.readValue(errorResponse, Fault.class);
+        assertNotNull(restResponse);
+
+        ErrorMessage policyErrorMessage = restResponse.getRequestError().get(ExceptionType.POLICY);
+        assertEquals("POL3102", policyErrorMessage.getMessageId());
+        assertEquals("Error parsing input performing %1 on %2 (msg=%3) (ec=%4)", policyErrorMessage.getText());
+        assertEquals("null", policyErrorMessage.getVariables().get(0));
+        assertEquals("null", policyErrorMessage.getVariables().get(1));
+        assertEquals("Error parsing input performing %1 on %2", policyErrorMessage.getVariables().get(2));
+        assertEquals("ERR.5.1.3102", policyErrorMessage.getVariables().get(3));
+    }
+
+    @Test
+    public void getRESTAPIServiceErrorResponseXmlTest() throws AAIException, JsonMappingException, JsonProcessingException {
+        // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
+        List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_XML_TYPE);
+        ArrayList<String> variables = new ArrayList<String>();
+
+        AAIException aaiException = new AAIException("AAI_3009");
+        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
+        assertNotNull(errorResponse);
+
+        Fault restResponse = xmlMapper.readValue(errorResponse, Fault.class);
+        assertNotNull(restResponse);
+
+        ErrorMessage serviceErrorMessage = restResponse.getRequestError().get(ExceptionType.SERVICE);
+        assertEquals("SVC3009", serviceErrorMessage.getMessageId());
+        assertEquals("Malformed URL (msg=%1) (ec=%2)", serviceErrorMessage.getText());
+        assertEquals("Malformed URL", serviceErrorMessage.getVariables().get(0));
+        assertEquals("ERR.5.6.3009", serviceErrorMessage.getVariables().get(1));
+    }
+
+    @Test
+    public void getRESTAPIPolicyErrorResponseJsonTest() throws AAIException, JsonMappingException, JsonProcessingException {
         // AAI_3002=5:1:WARN:3002:400:3002:Error writing output performing %1 on %2:300
-        ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
+        List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
         ArrayList<String> args = new ArrayList<String>(Arrays.asList("PUT", "resource"));
 
-        AAIException aaie = new AAIException("AAI_3002");
-        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
+        AAIException aaiException = new AAIException("AAI_3002");
+        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, args);
         assertNotNull(errorResponse);
+        assertTrue(errorResponse.contains("policyException"));
 
-        RESTResponse resp = MapperUtil.readAsObjectOf(RESTResponse.class, errorResponse);
-        RequestError requestError = resp.getRequestError();
-        assertNotNull(requestError);
-        PolicyException policyException = requestError.getPolicyException();
-        assertNotNull(policyException);
-        assertEquals("POL3002", policyException.getMessageId());
+        Fault restResponse = objectMapper.readValue(errorResponse, Fault.class);
+        assertNotNull(restResponse);
+
+        ErrorMessage policyErrorMessage = restResponse.getRequestError().get(ExceptionType.POLICY);
+        assertEquals("POL3002", policyErrorMessage.getMessageId());
 
-        List<String> vars = policyException.getVariables();
-        assertTrue(vars.contains("PUT"));
-        assertTrue(vars.contains("resource"));
+        List<String> variables = policyErrorMessage.getVariables();
+        assertEquals("PUT", variables.get(0));
+        assertEquals("resource", variables.get(1));
+        assertEquals("Error writing output performing %1 on %2", variables.get(2));
+        assertEquals("ERR.5.1.3002", variables.get(3));
     }
 
     @Test
-    public void getRESTAPIServiceErrorResponseTest() throws AAIException {
+    public void getRESTAPIServiceErrorResponseJsonTest() throws AAIException, JsonMappingException, JsonProcessingException {
         // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
-        ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
-        ArrayList<String> args = new ArrayList<String>();
+        List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
+        ArrayList<String> variables = new ArrayList<String>();
 
-        AAIException aaie = new AAIException("AAI_3009");
-        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
+        AAIException aaiException = new AAIException("AAI_3009");
+        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
         assertNotNull(errorResponse);
+        assertTrue(errorResponse.contains("serviceException"));
 
-        org.onap.aai.domain.restServiceException.RESTResponse resp =
-                MapperUtil.readAsObjectOf(org.onap.aai.domain.restServiceException.RESTResponse.class, errorResponse);
-        org.onap.aai.domain.restServiceException.RequestError requestError = resp.getRequestError();
+        org.onap.aai.domain.errorResponse.Fault restResponse =
+            objectMapper.readValue(errorResponse, org.onap.aai.domain.errorResponse.Fault.class);
+                
+        Map<ExceptionType, ErrorMessage> requestError = restResponse.getRequestError();
         assertNotNull(requestError);
-        ServiceException serviceException = requestError.getServiceException();
-        assertNotNull(serviceException);
-        assertEquals("SVC3009", serviceException.getMessageId());
-
+        ErrorMessage errorMessage = requestError.get(ExceptionType.SERVICE);
+        assertEquals("SVC3009", errorMessage.getMessageId());
+        assertEquals("Malformed URL (msg=%1) (ec=%2)", errorMessage.getText());
+        assertEquals("Malformed URL", errorMessage.getVariables().get(0));
+        assertEquals("ERR.5.6.3009", errorMessage.getVariables().get(1));
+
+        ServiceFault serviceFault = objectMapper.readValue(errorResponse, ServiceFault.class);
+        assertEquals("SVC3009", serviceFault.getRequestError().getServiceException().getMessageId());
     }
 
     @Test
     public void getRESTAPIServiceErrorResponseWithLoggingTest() throws IOException, InterruptedException {
         // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
-        ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
-        ArrayList<String> args = new ArrayList<String>();
+        List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
+        ArrayList<String> variables = new ArrayList<String>();
 
-        AAIException aaie = new AAIException("AAI_3009");
-        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaie, args);
-        sleep(5000);
+        AAIException aaiException = new AAIException("AAI_3009");
+        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaiException, variables);
+        sleep(2000);
         assertNotNull(errorResponse);
-        String logContents = LogFile.getContents(ErrorLogFileName);
+        String logContents = LogFile.getContents(errorLogFileName);
 
         assertNotNull(logContents);
         String logContentParts[] = logContents.split("\\|");
@@ -154,4 +219,52 @@ public class ErrorLogHelperTest {
 
     }
 
+    @Test
+    public void thatErrorObjectCanBeRetrieved() throws ErrorObjectNotFoundException {
+        ErrorObject errorObject = ErrorLogHelper.getErrorObject("AAI_3000");
+        assertEquals("3000", errorObject.getErrorCode());
+        assertEquals("3000", errorObject.getRESTErrorCode());
+        assertEquals("Invalid input performing %1 on %2", errorObject.getErrorText());
+        assertEquals("2", errorObject.getCategory());
+        assertEquals("INFO", errorObject.getSeverity());
+    }
+
+    @Test
+    public void thatInvalidErrorCodeWillReturnDefaultException() throws ErrorObjectNotFoundException {
+        ErrorObject errorObject = ErrorLogHelper.getErrorObject("AAI_1234");
+        assertEquals("4000", errorObject.getErrorCode());
+        assertEquals("3002", errorObject.getRESTErrorCode());
+        assertEquals("Internal Error", errorObject.getErrorText());
+        assertEquals("4", errorObject.getCategory());
+        assertEquals("ERROR", errorObject.getSeverity());
+    }
+
+    @Test
+    public void thatInvalidMediaTypeWillReturnInvalidAcceptHeaderException() throws ErrorObjectNotFoundException, JsonMappingException, JsonProcessingException {
+        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(Collections.singletonList(MediaType.TEXT_PLAIN_TYPE), new AAIException(), new ArrayList<>());
+        
+        Fault restResponse = objectMapper.readValue(errorResponse, Fault.class);
+        assertNotNull(restResponse);
+
+        ErrorMessage serviceErrorMessage = restResponse.getRequestError().get(ExceptionType.SERVICE);
+        List<String> variables = serviceErrorMessage.getVariables();
+        assertEquals("SVC3000", serviceErrorMessage.getMessageId());
+        assertEquals("null", variables.get(0));
+        assertEquals("null", variables.get(1));
+        assertEquals("Invalid Accept header", variables.get(2));
+        assertEquals("4.0.4014", variables.get(3));
+    }
+
+    @Test
+    public void thatRestApiInfoResponseCanBeRetrieved() {
+        Map<AAIException, ArrayList<String>> aaiExceptionsMap = new HashMap<>();
+        aaiExceptionsMap.put(new AAIException("AAI_0002", "OK"), new ArrayList<String>(Arrays.asList("someApp", "someTransactionId")));
+        Info info = ErrorLogHelper.getRestApiInfoResponse(aaiExceptionsMap);
+        ErrorMessage errorMessage = info.getErrorMessages().get(0);
+        assertEquals("INF0001", errorMessage.getMessageId());
+        assertEquals("Internal Error (msg=%1) (ec=%2)", errorMessage.getText());
+        assertEquals("Successful health check:OK", errorMessage.getVariables().get(0));
+        assertEquals("0.0.0002", errorMessage.getVariables().get(1));
+    }
+
 }