aboutsummaryrefslogtreecommitdiffstats
path: root/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorLogHelperTest.java
blob: 73890a0a648f23890565f27f6fe1f3d9db9cef69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
 * ============LICENSE_START=======================================================
 * 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.
 * 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.onap.aai.logging;

import static java.lang.Thread.sleep;
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.Ignore;
import org.junit.Test;
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.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 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);
    }

    @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(2000);
        String logContents = LogFile.getContents(errorLogFileName);

        assertNotNull(logContents);

        String logContentParts[] = logContents.split("\\|");

        assertTrue(logContentParts.length >= 11);
        assertEquals("ERROR", logContentParts[7]);
        assertEquals(AaiElsErrorCode.BUSINESS_PROCESS_ERROR, logContentParts[8]);
        assertTrue(logContentParts[10].startsWith("ERR.5.4.6110"));
    }

    // @Test
    // @Ignore("Test is flaky in the pipeline")
    // public void logErrorWithMessageTest() throws IOException, InterruptedException {
    //     // ||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);
    //     // TODO: Add a dynamic wait mechanism here
    //     sleep(5000); // reducing the wait leads to test flakiness in pipeline
    //     String logContents = LogFile.getContents(errorLogFileName);

    //     assertNotNull(logContents);

    //     String logContentParts[] = logContents.split("\\|");

    //     assertTrue(logContentParts.length >= 11);
    //     assertTrue(logContentParts[9].contains(errorMessage));
    //     assertTrue(logContentParts[10].startsWith("ERR.5.4.6110"));
    // }

    @Test
    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
        List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
        ArrayList<String> args = new ArrayList<String>(Arrays.asList("PUT", "resource"));

        AAIException aaiException = new AAIException("AAI_3002");
        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, args);
        assertNotNull(errorResponse);
        assertTrue(errorResponse.contains("policyException"));

        Fault restResponse = objectMapper.readValue(errorResponse, Fault.class);
        assertNotNull(restResponse);

        ErrorMessage policyErrorMessage = restResponse.getRequestError().get(ExceptionType.POLICY);
        assertEquals("POL3002", policyErrorMessage.getMessageId());

        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 getRESTAPIServiceErrorResponseJsonTest() throws AAIException, JsonMappingException, JsonProcessingException {
        // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
        List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
        ArrayList<String> variables = new ArrayList<String>();

        AAIException aaiException = new AAIException("AAI_3009");
        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaiException, variables);
        assertNotNull(errorResponse);
        assertTrue(errorResponse.contains("serviceException"));

        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);
        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
        List<MediaType> headers = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);
        ArrayList<String> variables = new ArrayList<String>();

        AAIException aaiException = new AAIException("AAI_3009");
        String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaiException, variables);
        sleep(2000);
        assertNotNull(errorResponse);
        String logContents = LogFile.getContents(errorLogFileName);

        assertNotNull(logContents);
        String logContentParts[] = logContents.split("\\|");

        assertTrue(logContentParts.length >= 11);
        assertTrue(logContentParts[10].startsWith("ERR.5.6.3009"));

    }

    @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("Success X-FromAppId=%1 X-TransactionId=%2 (msg=%3) (ec=%4)", errorMessage.getText());
        assertEquals("Successful health check:OK", errorMessage.getVariables().get(0));
        assertEquals("0.0.0002", errorMessage.getVariables().get(1));
    }

}