fcbd86e0ce2bef58b8af6a56974709007f2eda11
[aai/aai-common.git] / aai-els-onap-logging / src / test / java / org / onap / aai / logging / ErrorLogHelperTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *    http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.aai.logging;
23
24 import static java.lang.Thread.sleep;
25 import static org.junit.Assert.*;
26
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;
33 import java.util.Map;
34
35 import javax.ws.rs.core.MediaType;
36
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.aai.domain.errorResponse.ErrorMessage;
41 import org.onap.aai.domain.errorResponse.ExceptionType;
42 import org.onap.aai.domain.errorResponse.Fault;
43 import org.onap.aai.domain.errorResponse.Info;
44 import org.onap.aai.domain.errorResponse.ServiceFault;
45 import org.onap.aai.exceptions.AAIException;
46 import org.onap.aai.util.LogFile;
47 import org.slf4j.MDC;
48
49 import com.fasterxml.jackson.core.JsonProcessingException;
50 import com.fasterxml.jackson.databind.JsonMappingException;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52 import com.fasterxml.jackson.dataformat.xml.XmlMapper;
53
54 @SuppressWarnings("deprecation")
55 public class ErrorLogHelperTest {
56
57     private static final String errorLogFileName = "error.log";
58     private static final ObjectMapper objectMapper = new ObjectMapper();
59     private static final XmlMapper xmlMapper = new XmlMapper();
60
61     @Before
62     public void init() {
63         System.setProperty("AJSC_HOME", ".");
64     }
65
66     @After
67     public void cleanup() throws IOException {
68         MDC.clear();
69         LogFile.deleteContents(errorLogFileName);
70     }
71
72     @Test
73     public void logErrorTest() throws IOException, InterruptedException {
74         // ||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110
75         ErrorLogHelper.logError("AAI_6110");
76         sleep(2000);
77         String logContents = LogFile.getContents(errorLogFileName);
78
79         assertNotNull(logContents);
80
81         String logContentParts[] = logContents.split("\\|");
82
83         assertTrue(logContentParts.length >= 11);
84         assertEquals("ERROR", logContentParts[7]);
85         assertEquals(AaiElsErrorCode.BUSINESS_PROCESS_ERROR, logContentParts[8]);
86         assertTrue(logContentParts[10].startsWith("ERR.5.4.6110"));
87     }
88
89     @Test
90     public void logErrorWithMessageTest() throws IOException, InterruptedException {
91         // ||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110 message
92         String errorMessage = "Object is referenced by additional objects";
93         ErrorLogHelper.logError("AAI_6110", errorMessage);
94         sleep(3000);
95         String logContents = LogFile.getContents(errorLogFileName);
96
97         assertNotNull(logContents);
98
99         String logContentParts[] = logContents.split("\\|");
100
101         assertTrue(logContentParts.length >= 11);
102         assertTrue(logContentParts[9].contains(errorMessage));
103         assertTrue(logContentParts[10].startsWith("ERR.5.4.6110"));
104     }
105
106     @Test
107     public void getRESTAPIPolicyErrorResponseXmlTest() throws AAIException, JsonMappingException, JsonProcessingException {
108         // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
109         List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_XML_TYPE);
110         ArrayList<String> variables = new ArrayList<String>();
111
112         AAIException aaiException = new AAIException("AAI_3102");
113         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
114         assertNotNull(errorResponse);
115         assertTrue(errorResponse.contains("<Fault>"));
116         assertTrue(errorResponse.contains("<policyException>"));
117         assertTrue(errorResponse.contains("<variables>"));
118         assertTrue(errorResponse.contains("<variable>"));
119
120         Fault restResponse = xmlMapper.readValue(errorResponse, Fault.class);
121         assertNotNull(restResponse);
122
123         ErrorMessage policyErrorMessage = restResponse.getRequestError().get(ExceptionType.POLICY);
124         assertEquals("POL3102", policyErrorMessage.getMessageId());
125         assertEquals("Error parsing input performing %1 on %2 (msg=%3) (ec=%4)", policyErrorMessage.getText());
126         assertEquals("null", policyErrorMessage.getVariables().get(0));
127         assertEquals("null", policyErrorMessage.getVariables().get(1));
128         assertEquals("Error parsing input performing %1 on %2", policyErrorMessage.getVariables().get(2));
129         assertEquals("ERR.5.1.3102", policyErrorMessage.getVariables().get(3));
130     }
131
132     @Test
133     public void getRESTAPIServiceErrorResponseXmlTest() throws AAIException, JsonMappingException, JsonProcessingException {
134         // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
135         List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_XML_TYPE);
136         ArrayList<String> variables = new ArrayList<String>();
137
138         AAIException aaiException = new AAIException("AAI_3009");
139         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
140         assertNotNull(errorResponse);
141
142         Fault restResponse = xmlMapper.readValue(errorResponse, Fault.class);
143         assertNotNull(restResponse);
144
145         ErrorMessage serviceErrorMessage = restResponse.getRequestError().get(ExceptionType.SERVICE);
146         assertEquals("SVC3009", serviceErrorMessage.getMessageId());
147         assertEquals("Malformed URL (msg=%1) (ec=%2)", serviceErrorMessage.getText());
148         assertEquals("Malformed URL", serviceErrorMessage.getVariables().get(0));
149         assertEquals("ERR.5.6.3009", serviceErrorMessage.getVariables().get(1));
150     }
151
152     @Test
153     public void getRESTAPIPolicyErrorResponseJsonTest() throws AAIException, JsonMappingException, JsonProcessingException {
154         // AAI_3002=5:1:WARN:3002:400:3002:Error writing output performing %1 on %2:300
155         List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
156         ArrayList<String> args = new ArrayList<String>(Arrays.asList("PUT", "resource"));
157
158         AAIException aaiException = new AAIException("AAI_3002");
159         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, args);
160         assertNotNull(errorResponse);
161         assertTrue(errorResponse.contains("policyException"));
162
163         Fault restResponse = objectMapper.readValue(errorResponse, Fault.class);
164         assertNotNull(restResponse);
165
166         ErrorMessage policyErrorMessage = restResponse.getRequestError().get(ExceptionType.POLICY);
167         assertEquals("POL3002", policyErrorMessage.getMessageId());
168
169         List<String> variables = policyErrorMessage.getVariables();
170         assertEquals("PUT", variables.get(0));
171         assertEquals("resource", variables.get(1));
172         assertEquals("Error writing output performing %1 on %2", variables.get(2));
173         assertEquals("ERR.5.1.3002", variables.get(3));
174     }
175
176     @Test
177     public void getRESTAPIServiceErrorResponseJsonTest() throws AAIException, JsonMappingException, JsonProcessingException {
178         // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
179         List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
180         ArrayList<String> variables = new ArrayList<String>();
181
182         AAIException aaiException = new AAIException("AAI_3009");
183         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
184         assertNotNull(errorResponse);
185         assertTrue(errorResponse.contains("serviceException"));
186
187         org.onap.aai.domain.errorResponse.Fault restResponse =
188             objectMapper.readValue(errorResponse, org.onap.aai.domain.errorResponse.Fault.class);
189                 
190         Map<ExceptionType, ErrorMessage> requestError = restResponse.getRequestError();
191         assertNotNull(requestError);
192         ErrorMessage errorMessage = requestError.get(ExceptionType.SERVICE);
193         assertEquals("SVC3009", errorMessage.getMessageId());
194         assertEquals("Malformed URL (msg=%1) (ec=%2)", errorMessage.getText());
195         assertEquals("Malformed URL", errorMessage.getVariables().get(0));
196         assertEquals("ERR.5.6.3009", errorMessage.getVariables().get(1));
197
198         ServiceFault serviceFault = objectMapper.readValue(errorResponse, ServiceFault.class);
199         assertEquals("SVC3009", serviceFault.getRequestError().getServiceException().getMessageId());
200     }
201
202     @Test
203     public void getRESTAPIServiceErrorResponseWithLoggingTest() throws IOException, InterruptedException {
204         // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
205         List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
206         ArrayList<String> variables = new ArrayList<String>();
207
208         AAIException aaiException = new AAIException("AAI_3009");
209         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaiException, variables);
210         sleep(2000);
211         assertNotNull(errorResponse);
212         String logContents = LogFile.getContents(errorLogFileName);
213
214         assertNotNull(logContents);
215         String logContentParts[] = logContents.split("\\|");
216
217         assertTrue(logContentParts.length >= 11);
218         assertTrue(logContentParts[10].startsWith("ERR.5.6.3009"));
219
220     }
221
222     @Test
223     public void thatErrorObjectCanBeRetrieved() throws ErrorObjectNotFoundException {
224         ErrorObject errorObject = ErrorLogHelper.getErrorObject("AAI_3000");
225         assertEquals("3000", errorObject.getErrorCode());
226         assertEquals("3000", errorObject.getRESTErrorCode());
227         assertEquals("Invalid input performing %1 on %2", errorObject.getErrorText());
228         assertEquals("2", errorObject.getCategory());
229         assertEquals("INFO", errorObject.getSeverity());
230     }
231
232     @Test
233     public void thatInvalidErrorCodeWillReturnDefaultException() throws ErrorObjectNotFoundException {
234         ErrorObject errorObject = ErrorLogHelper.getErrorObject("AAI_1234");
235         assertEquals("4000", errorObject.getErrorCode());
236         assertEquals("3002", errorObject.getRESTErrorCode());
237         assertEquals("Internal Error", errorObject.getErrorText());
238         assertEquals("4", errorObject.getCategory());
239         assertEquals("ERROR", errorObject.getSeverity());
240     }
241
242     @Test
243     public void thatInvalidMediaTypeWillReturnInvalidAcceptHeaderException() throws ErrorObjectNotFoundException, JsonMappingException, JsonProcessingException {
244         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(Collections.singletonList(MediaType.TEXT_PLAIN_TYPE), new AAIException(), new ArrayList<>());
245         
246         Fault restResponse = objectMapper.readValue(errorResponse, Fault.class);
247         assertNotNull(restResponse);
248
249         ErrorMessage serviceErrorMessage = restResponse.getRequestError().get(ExceptionType.SERVICE);
250         List<String> variables = serviceErrorMessage.getVariables();
251         assertEquals("SVC3000", serviceErrorMessage.getMessageId());
252         assertEquals("null", variables.get(0));
253         assertEquals("null", variables.get(1));
254         assertEquals("Invalid Accept header", variables.get(2));
255         assertEquals("4.0.4014", variables.get(3));
256     }
257
258     @Test
259     public void thatRestApiInfoResponseCanBeRetrieved() {
260         Map<AAIException, ArrayList<String>> aaiExceptionsMap = new HashMap<>();
261         aaiExceptionsMap.put(new AAIException("AAI_0002", "OK"), new ArrayList<String>(Arrays.asList("someApp", "someTransactionId")));
262         Info info = ErrorLogHelper.getRestApiInfoResponse(aaiExceptionsMap);
263         ErrorMessage errorMessage = info.getErrorMessages().get(0);
264         assertEquals("INF0001", errorMessage.getMessageId());
265         assertEquals("Internal Error (msg=%1) (ec=%2)", errorMessage.getText());
266         assertEquals("Successful health check:OK", errorMessage.getVariables().get(0));
267         assertEquals("0.0.0002", errorMessage.getVariables().get(1));
268     }
269
270 }