diff options
author | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2023-12-19 11:55:44 +0100 |
---|---|---|
committer | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2023-12-22 15:11:57 +0100 |
commit | 03c062dc0d583fff34c17801e1d46a72dfe4c831 (patch) | |
tree | 8b7a6f78f5cbcd2f4cb4b064f076f021ef9fbd67 /aai-els-onap-logging/src/test | |
parent | d5a889f9661e89289344ed736600bc7222095379 (diff) |
Refactor ErrorLogHelper
- change is designed to not make any changes to the serialised objects [1]
- introduce common error response object that is used for both service and policy, xml + json format
- separate creation of error response and the marshalling of it
- increase test coverage
- use jackson object mappers for marshalling json and xml objects instead of custom MapperUtil
- make code more functional
- use more descriptive variable names
[1] only the internal representation changes to account for both
Service- and PolicyException
Issue-ID: AAI-3695
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Change-Id: I1e82bf4f0706704679d14aac98969fa00beed758
Diffstat (limited to 'aai-els-onap-logging/src/test')
3 files changed, 156 insertions, 161 deletions
diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/domain/restResponseInfo/InfoTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/domain/restResponseInfo/InfoTest.java deleted file mode 100644 index 6e1b0ea4..00000000 --- a/aai-els-onap-logging/src/test/java/org/onap/aai/domain/restResponseInfo/InfoTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 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========================================================= - */ -// -// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 -// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> -// Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2015.10.28 at 05:53:17 PM EDT -// - -package org.onap.aai.domain.restResponseInfo; - -import static org.junit.Assert.assertTrue; - -import java.util.List; - -import org.junit.Test; - -public class InfoTest { - private Info info = new Info(); - - @Test - public void infoTest() { - Info.ResponseMessages responseMessages = info.getResponseMessages(); - if (responseMessages == null) - responseMessages = new Info.ResponseMessages(); - - List<Info.ResponseMessages.ResponseMessage> list = responseMessages.getResponseMessage(); - Info.ResponseMessages.ResponseMessage respMsg = new Info.ResponseMessages.ResponseMessage(); - - respMsg.setMessageId("1"); - respMsg.setText("textValue"); - - Info.ResponseMessages.ResponseMessage.Variables vars = new Info.ResponseMessages.ResponseMessage.Variables(); - vars.getVariable().add("var1"); - vars.getVariable().add("var2"); - - respMsg.setVariables(vars); - list.add(respMsg); - info.setResponseMessages(responseMessages); - - Info.ResponseMessages responseMessages1 = info.getResponseMessages(); - List<Info.ResponseMessages.ResponseMessage> list1 = responseMessages1.getResponseMessage(); - Info.ResponseMessages.ResponseMessage respMsg1 = list1.get(0); - assertTrue(respMsg1.getMessageId().equals("1")); - assertTrue(respMsg1.getText().equals("textValue")); - assertTrue(respMsg1.getVariables().getVariable().contains("var1")); - assertTrue(respMsg1.getVariables().getVariable().contains("var2")); - } - -} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/domain/restResponseInfo/ObjectFactoryTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/domain/restResponseInfo/ObjectFactoryTest.java deleted file mode 100644 index fe95d183..00000000 --- a/aai-els-onap-logging/src/test/java/org/onap/aai/domain/restResponseInfo/ObjectFactoryTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 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========================================================= - */ -// -// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 -// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> -// Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2015.10.28 at 05:53:17 PM EDT -// - -package org.onap.aai.domain.restResponseInfo; - -import static org.junit.Assert.assertNotNull; - -import org.junit.Test; - -public class ObjectFactoryTest { - private ObjectFactory objectFactory = new ObjectFactory(); - - @Test - public void objectFactoryTest() { - Info.ResponseMessages.ResponseMessage.Variables variables = - objectFactory.createInfoResponseMessagesResponseMessageVariables(); - Info.ResponseMessages.ResponseMessage responseMessage = - objectFactory.createInfoResponseMessagesResponseMessage(); - Info.ResponseMessages responseMessages = objectFactory.createInfoResponseMessages(); - Info info = objectFactory.createInfo(); - - assertNotNull(variables); - assertNotNull(responseMessage); - assertNotNull(responseMessages); - assertNotNull(info); - } - -} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorLogHelperTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorLogHelperTest.java index 99522771..fcbd86e0 100644 --- a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorLogHelperTest.java +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorLogHelperTest.java @@ -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)); + } + } |