99522771f452a08b8e54e8d971ecc50d9c0f6ec5
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.logging;
22
23 import static java.lang.Thread.sleep;
24 import static org.junit.Assert.*;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30
31 import javax.ws.rs.core.MediaType;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.aai.domain.restPolicyException.PolicyException;
37 import org.onap.aai.domain.restPolicyException.RESTResponse;
38 import org.onap.aai.domain.restPolicyException.RequestError;
39 import org.onap.aai.domain.restServiceException.ServiceException;
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.util.LogFile;
42 import org.onap.aai.util.MapperUtil;
43 import org.slf4j.MDC;
44
45 public class ErrorLogHelperTest {
46
47     private static final String ErrorLogFileName = "error.log";
48
49     @Before
50     public void init() {
51         System.setProperty("AJSC_HOME", ".");
52
53     }
54
55     @After
56     public void cleanup() throws IOException {
57         MDC.clear();
58         LogFile.deleteContents(ErrorLogFileName);
59     }
60
61     @Test
62     public void logErrorTest() throws IOException, InterruptedException {
63         // ||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110
64         ErrorLogHelper.logError("AAI_6110");
65         sleep(5000);
66         String logContents = LogFile.getContents(ErrorLogFileName);
67
68         assertNotNull(logContents);
69
70         String logContentParts[] = logContents.split("\\|");
71
72         assertTrue(logContentParts.length >= 11);
73         assertEquals("ERROR", logContentParts[7]);
74         assertEquals(AaiElsErrorCode.BUSINESS_PROCESS_ERROR, logContentParts[8]);
75         assertTrue(logContentParts[10].startsWith("ERR.5.4.6110"));
76     }
77
78     @Test
79     public void logErrorWithMessageTest() throws IOException, InterruptedException {
80         // ||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110 message
81         String errorMessage = "Object is referenced by additional objects";
82         ErrorLogHelper.logError("AAI_6110", errorMessage);
83         sleep(5000);
84         String logContents = LogFile.getContents(ErrorLogFileName);
85
86         assertNotNull(logContents);
87
88         String logContentParts[] = logContents.split("\\|");
89
90         assertTrue(logContentParts.length >= 11);
91         assertTrue(logContentParts[9].contains(errorMessage));
92         assertTrue(logContentParts[10].startsWith("ERR.5.4.6110"));
93     }
94
95     @Test
96     public void getRESTAPIPolicyErrorResponseTest() throws AAIException {
97         // AAI_3002=5:1:WARN:3002:400:3002:Error writing output performing %1 on %2:300
98         ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
99         ArrayList<String> args = new ArrayList<String>(Arrays.asList("PUT", "resource"));
100
101         AAIException aaie = new AAIException("AAI_3002");
102         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
103         assertNotNull(errorResponse);
104
105         RESTResponse resp = MapperUtil.readAsObjectOf(RESTResponse.class, errorResponse);
106         RequestError requestError = resp.getRequestError();
107         assertNotNull(requestError);
108         PolicyException policyException = requestError.getPolicyException();
109         assertNotNull(policyException);
110         assertEquals("POL3002", policyException.getMessageId());
111
112         List<String> vars = policyException.getVariables();
113         assertTrue(vars.contains("PUT"));
114         assertTrue(vars.contains("resource"));
115     }
116
117     @Test
118     public void getRESTAPIServiceErrorResponseTest() throws AAIException {
119         // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
120         ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
121         ArrayList<String> args = new ArrayList<String>();
122
123         AAIException aaie = new AAIException("AAI_3009");
124         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
125         assertNotNull(errorResponse);
126
127         org.onap.aai.domain.restServiceException.RESTResponse resp =
128                 MapperUtil.readAsObjectOf(org.onap.aai.domain.restServiceException.RESTResponse.class, errorResponse);
129         org.onap.aai.domain.restServiceException.RequestError requestError = resp.getRequestError();
130         assertNotNull(requestError);
131         ServiceException serviceException = requestError.getServiceException();
132         assertNotNull(serviceException);
133         assertEquals("SVC3009", serviceException.getMessageId());
134
135     }
136
137     @Test
138     public void getRESTAPIServiceErrorResponseWithLoggingTest() throws IOException, InterruptedException {
139         // AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
140         ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
141         ArrayList<String> args = new ArrayList<String>();
142
143         AAIException aaie = new AAIException("AAI_3009");
144         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaie, args);
145         sleep(5000);
146         assertNotNull(errorResponse);
147         String logContents = LogFile.getContents(ErrorLogFileName);
148
149         assertNotNull(logContents);
150         String logContentParts[] = logContents.split("\\|");
151
152         assertTrue(logContentParts.length >= 11);
153         assertTrue(logContentParts[10].startsWith("ERR.5.6.3009"));
154
155     }
156
157 }