Increase code coverage on aai-common: aai-els-onap-logging library
[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 org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.onap.aai.domain.restPolicyException.PolicyException;
27 import org.onap.aai.domain.restPolicyException.RESTResponse;
28 import org.onap.aai.domain.restPolicyException.RequestError;
29 import org.onap.aai.domain.restServiceException.ServiceException;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.util.LogFile;
32 import org.onap.aai.util.MapperUtil;
33 import org.slf4j.MDC;
34
35 import javax.ws.rs.core.MediaType;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40
41 import static java.lang.Thread.sleep;
42 import static org.junit.Assert.*;
43
44 public class ErrorLogHelperTest {
45
46     private static final String ErrorLogFileName = "error.log";
47
48     @Before
49     public void init() {
50         System.setProperty("AJSC_HOME", ".");
51
52     }
53     @After
54     public void cleanup() throws IOException{
55         MDC.clear();
56         LogFile.deleteContents(ErrorLogFileName);
57     }
58     @Test
59     public void logErrorTest() throws IOException, InterruptedException {
60         //||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110
61         ErrorLogHelper.logError("AAI_6110");
62         sleep(5000);
63         String logContents = LogFile.getContents(ErrorLogFileName);
64
65         assertNotNull(logContents);
66
67         String logContentParts[] = logContents.split("\\|");
68
69         assertTrue(logContentParts.length >= 11 );
70         assertEquals ("ERROR", logContentParts[7]);
71         assertEquals (AaiElsErrorCode.BUSINESS_PROCESS_ERROR, logContentParts[8]);
72         assertTrue (logContentParts[10].startsWith("ERR.5.4.6110"));
73     }
74
75     @Test
76     public void logErrorWithMessageTest() throws IOException, InterruptedException {
77         //||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110 message
78         String errorMessage = "Object is referenced by additional objects";
79         ErrorLogHelper.logError("AAI_6110", errorMessage);
80         sleep(5000);
81         String logContents = LogFile.getContents(ErrorLogFileName);
82
83         assertNotNull(logContents);
84
85         String logContentParts[] = logContents.split("\\|");
86
87         assertTrue(logContentParts.length >= 11 );
88         assertTrue (logContentParts[9].contains(errorMessage));
89         assertTrue (logContentParts[10].startsWith("ERR.5.4.6110"));
90     }
91
92     @Test
93     public void getRESTAPIPolicyErrorResponseTest() throws AAIException{
94         //AAI_3002=5:1:WARN:3002:400:3002:Error writing output performing %1 on %2:300
95         ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
96         ArrayList<String> args = new ArrayList<String>(Arrays.asList("PUT", "resource"));
97
98         AAIException aaie = new AAIException("AAI_3002");
99         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
100         assertNotNull(errorResponse);
101
102         RESTResponse resp = MapperUtil.readAsObjectOf(RESTResponse.class, errorResponse);
103         RequestError requestError = resp.getRequestError();
104         assertNotNull(requestError);
105         PolicyException policyException = requestError.getPolicyException();
106         assertNotNull(policyException);
107         assertEquals("POL3002", policyException.getMessageId());
108
109         List<String> vars = policyException.getVariables();
110         assertTrue(vars.contains("PUT"));
111         assertTrue(vars.contains("resource"));
112     }
113     @Test
114     public void getRESTAPIServiceErrorResponseTest() throws AAIException{
115         //AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
116         ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
117         ArrayList<String> args = new ArrayList<String>();
118
119         AAIException aaie = new AAIException("AAI_3009");
120         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
121         assertNotNull(errorResponse);
122
123         org.onap.aai.domain.restServiceException.RESTResponse resp = MapperUtil.readAsObjectOf(org.onap.aai.domain.restServiceException.RESTResponse.class, errorResponse);
124         org.onap.aai.domain.restServiceException.RequestError requestError = resp.getRequestError();
125         assertNotNull(requestError);
126         ServiceException serviceException = requestError.getServiceException();
127         assertNotNull(serviceException);
128         assertEquals("SVC3009", serviceException.getMessageId());
129
130     }
131     @Test
132     public void getRESTAPIServiceErrorResponseWithLoggingTest() throws IOException, InterruptedException{
133         //AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300
134         ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
135         ArrayList<String> args = new ArrayList<String>();
136
137         AAIException aaie = new AAIException("AAI_3009");
138         String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaie, args);
139         sleep(5000);
140         assertNotNull(errorResponse);
141         String logContents = LogFile.getContents(ErrorLogFileName);
142
143         assertNotNull(logContents);
144         String logContentParts[] = logContents.split("\\|");
145
146         assertTrue(logContentParts.length >= 11 );
147         assertTrue (logContentParts[10].startsWith("ERR.5.6.3009"));
148
149     }
150
151 }