2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Deutsche Telekom SA.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.aai.logging;
24 import static java.lang.Thread.sleep;
25 import static org.junit.Assert.*;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
35 import javax.ws.rs.core.MediaType;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.onap.aai.domain.errorResponse.ErrorMessage;
42 import org.onap.aai.domain.errorResponse.ExceptionType;
43 import org.onap.aai.domain.errorResponse.Fault;
44 import org.onap.aai.domain.errorResponse.Info;
45 import org.onap.aai.domain.errorResponse.ServiceFault;
46 import org.onap.aai.exceptions.AAIException;
47 import org.onap.aai.util.LogFile;
50 import com.fasterxml.jackson.core.JsonProcessingException;
51 import com.fasterxml.jackson.databind.JsonMappingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.fasterxml.jackson.dataformat.xml.XmlMapper;
55 @SuppressWarnings("deprecation")
56 public class ErrorLogHelperTest {
58 private static final String errorLogFileName = "error.log";
59 private static final ObjectMapper objectMapper = new ObjectMapper();
60 private static final XmlMapper xmlMapper = new XmlMapper();
64 System.setProperty("AJSC_HOME", ".");
68 public void cleanup() throws IOException {
70 LogFile.deleteContents(errorLogFileName);
74 public void logErrorTest() throws IOException, InterruptedException {
75 // ||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110
76 ErrorLogHelper.logError("AAI_6110");
78 String logContents = LogFile.getContents(errorLogFileName);
80 assertNotNull(logContents);
82 String logContentParts[] = logContents.split("\\|");
84 assertTrue(logContentParts.length >= 11);
85 assertEquals("ERROR", logContentParts[7]);
86 assertEquals(AaiElsErrorCode.BUSINESS_PROCESS_ERROR, logContentParts[8]);
87 assertTrue(logContentParts[10].startsWith("ERR.5.4.6110"));
91 // @Ignore("Test is flaky in the pipeline")
92 // public void logErrorWithMessageTest() throws IOException, InterruptedException {
93 // // ||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110 message
94 // String errorMessage = "Object is referenced by additional objects";
95 // ErrorLogHelper.logError("AAI_6110", errorMessage);
96 // // TODO: Add a dynamic wait mechanism here
97 // sleep(5000); // reducing the wait leads to test flakiness in pipeline
98 // String logContents = LogFile.getContents(errorLogFileName);
100 // assertNotNull(logContents);
102 // String logContentParts[] = logContents.split("\\|");
104 // assertTrue(logContentParts.length >= 11);
105 // assertTrue(logContentParts[9].contains(errorMessage));
106 // assertTrue(logContentParts[10].startsWith("ERR.5.4.6110"));
110 public void getRESTAPIPolicyErrorResponseXmlTest() throws AAIException, JsonMappingException, JsonProcessingException {
111 // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
112 List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_XML_TYPE);
113 ArrayList<String> variables = new ArrayList<String>();
115 AAIException aaiException = new AAIException("AAI_3102");
116 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
117 assertNotNull(errorResponse);
118 assertTrue(errorResponse.contains("<Fault>"));
119 assertTrue(errorResponse.contains("<policyException>"));
120 assertTrue(errorResponse.contains("<variables>"));
121 assertTrue(errorResponse.contains("<variable>"));
123 Fault restResponse = xmlMapper.readValue(errorResponse, Fault.class);
124 assertNotNull(restResponse);
126 ErrorMessage policyErrorMessage = restResponse.getRequestError().get(ExceptionType.POLICY);
127 assertEquals("POL3102", policyErrorMessage.getMessageId());
128 assertEquals("Error parsing input performing %1 on %2 (msg=%3) (ec=%4)", policyErrorMessage.getText());
129 assertEquals("null", policyErrorMessage.getVariables().get(0));
130 assertEquals("null", policyErrorMessage.getVariables().get(1));
131 assertEquals("Error parsing input performing %1 on %2", policyErrorMessage.getVariables().get(2));
132 assertEquals("ERR.5.1.3102", policyErrorMessage.getVariables().get(3));
136 public void getRESTAPIServiceErrorResponseXmlTest() throws AAIException, JsonMappingException, JsonProcessingException {
137 // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
138 List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_XML_TYPE);
139 ArrayList<String> variables = new ArrayList<String>();
141 AAIException aaiException = new AAIException("AAI_3009");
142 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
143 assertNotNull(errorResponse);
145 Fault restResponse = xmlMapper.readValue(errorResponse, Fault.class);
146 assertNotNull(restResponse);
148 ErrorMessage serviceErrorMessage = restResponse.getRequestError().get(ExceptionType.SERVICE);
149 assertEquals("SVC3009", serviceErrorMessage.getMessageId());
150 assertEquals("Malformed URL (msg=%1) (ec=%2)", serviceErrorMessage.getText());
151 assertEquals("Malformed URL", serviceErrorMessage.getVariables().get(0));
152 assertEquals("ERR.5.6.3009", serviceErrorMessage.getVariables().get(1));
156 public void getRESTAPIPolicyErrorResponseJsonTest() throws AAIException, JsonMappingException, JsonProcessingException {
157 // AAI_3002=5:1:WARN:3002:400:3002:Error writing output performing %1 on %2:300
158 List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
159 ArrayList<String> args = new ArrayList<String>(Arrays.asList("PUT", "resource"));
161 AAIException aaiException = new AAIException("AAI_3002");
162 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, args);
163 assertNotNull(errorResponse);
164 assertTrue(errorResponse.contains("policyException"));
166 Fault restResponse = objectMapper.readValue(errorResponse, Fault.class);
167 assertNotNull(restResponse);
169 ErrorMessage policyErrorMessage = restResponse.getRequestError().get(ExceptionType.POLICY);
170 assertEquals("POL3002", policyErrorMessage.getMessageId());
172 List<String> variables = policyErrorMessage.getVariables();
173 assertEquals("PUT", variables.get(0));
174 assertEquals("resource", variables.get(1));
175 assertEquals("Error writing output performing %1 on %2", variables.get(2));
176 assertEquals("ERR.5.1.3002", variables.get(3));
180 public void getRESTAPIServiceErrorResponseJsonTest() throws AAIException, JsonMappingException, JsonProcessingException {
181 // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
182 List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
183 ArrayList<String> variables = new ArrayList<String>();
185 AAIException aaiException = new AAIException("AAI_3009");
186 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
187 assertNotNull(errorResponse);
188 assertTrue(errorResponse.contains("serviceException"));
190 org.onap.aai.domain.errorResponse.Fault restResponse =
191 objectMapper.readValue(errorResponse, org.onap.aai.domain.errorResponse.Fault.class);
193 Map<ExceptionType, ErrorMessage> requestError = restResponse.getRequestError();
194 assertNotNull(requestError);
195 ErrorMessage errorMessage = requestError.get(ExceptionType.SERVICE);
196 assertEquals("SVC3009", errorMessage.getMessageId());
197 assertEquals("Malformed URL (msg=%1) (ec=%2)", errorMessage.getText());
198 assertEquals("Malformed URL", errorMessage.getVariables().get(0));
199 assertEquals("ERR.5.6.3009", errorMessage.getVariables().get(1));
201 ServiceFault serviceFault = objectMapper.readValue(errorResponse, ServiceFault.class);
202 assertEquals("SVC3009", serviceFault.getRequestError().getServiceException().getMessageId());
206 public void getRESTAPIServiceErrorResponseWithLoggingTest() throws IOException, InterruptedException {
207 // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
208 List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
209 ArrayList<String> variables = new ArrayList<String>();
211 AAIException aaiException = new AAIException("AAI_3009");
212 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaiException, variables);
214 assertNotNull(errorResponse);
215 String logContents = LogFile.getContents(errorLogFileName);
217 assertNotNull(logContents);
218 String logContentParts[] = logContents.split("\\|");
220 assertTrue(logContentParts.length >= 11);
221 assertTrue(logContentParts[10].startsWith("ERR.5.6.3009"));
226 public void thatErrorObjectCanBeRetrieved() throws ErrorObjectNotFoundException {
227 ErrorObject errorObject = ErrorLogHelper.getErrorObject("AAI_3000");
228 assertEquals("3000", errorObject.getErrorCode());
229 assertEquals("3000", errorObject.getRESTErrorCode());
230 assertEquals("Invalid input performing %1 on %2", errorObject.getErrorText());
231 assertEquals("2", errorObject.getCategory());
232 assertEquals("INFO", errorObject.getSeverity());
236 public void thatInvalidErrorCodeWillReturnDefaultException() throws ErrorObjectNotFoundException {
237 ErrorObject errorObject = ErrorLogHelper.getErrorObject("AAI_1234");
238 assertEquals("4000", errorObject.getErrorCode());
239 assertEquals("3002", errorObject.getRESTErrorCode());
240 assertEquals("Internal Error", errorObject.getErrorText());
241 assertEquals("4", errorObject.getCategory());
242 assertEquals("ERROR", errorObject.getSeverity());
246 public void thatInvalidMediaTypeWillReturnInvalidAcceptHeaderException() throws ErrorObjectNotFoundException, JsonMappingException, JsonProcessingException {
247 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(Collections.singletonList(MediaType.TEXT_PLAIN_TYPE), new AAIException(), new ArrayList<>());
249 Fault restResponse = objectMapper.readValue(errorResponse, Fault.class);
250 assertNotNull(restResponse);
252 ErrorMessage serviceErrorMessage = restResponse.getRequestError().get(ExceptionType.SERVICE);
253 List<String> variables = serviceErrorMessage.getVariables();
254 assertEquals("SVC3000", serviceErrorMessage.getMessageId());
255 assertEquals("null", variables.get(0));
256 assertEquals("null", variables.get(1));
257 assertEquals("Invalid Accept header", variables.get(2));
258 assertEquals("4.0.4014", variables.get(3));
262 public void thatRestApiInfoResponseCanBeRetrieved() {
263 Map<AAIException, ArrayList<String>> aaiExceptionsMap = new HashMap<>();
264 aaiExceptionsMap.put(new AAIException("AAI_0002", "OK"), new ArrayList<String>(Arrays.asList("someApp", "someTransactionId")));
265 Info info = ErrorLogHelper.getRestApiInfoResponse(aaiExceptionsMap);
266 ErrorMessage errorMessage = info.getErrorMessages().get(0);
267 assertEquals("INF0001", errorMessage.getMessageId());
268 assertEquals("Internal Error (msg=%1) (ec=%2)", errorMessage.getText());
269 assertEquals("Successful health check:OK", errorMessage.getVariables().get(0));
270 assertEquals("0.0.0002", errorMessage.getVariables().get(1));