2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.logging;
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;
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;
41 import static java.lang.Thread.sleep;
42 import static org.junit.Assert.*;
44 public class ErrorLogHelperTest {
46 private static final String ErrorLogFileName = "error.log";
50 System.setProperty("AJSC_HOME", ".");
54 public void cleanup() throws IOException{
56 LogFile.deleteContents(ErrorLogFileName);
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");
63 String logContents = LogFile.getContents(ErrorLogFileName);
65 assertNotNull(logContents);
67 String logContentParts[] = logContents.split("\\|");
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"));
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);
81 String logContents = LogFile.getContents(ErrorLogFileName);
83 assertNotNull(logContents);
85 String logContentParts[] = logContents.split("\\|");
87 assertTrue(logContentParts.length >= 11 );
88 assertTrue (logContentParts[9].contains(errorMessage));
89 assertTrue (logContentParts[10].startsWith("ERR.5.4.6110"));
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"));
98 AAIException aaie = new AAIException("AAI_3002");
99 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
100 assertNotNull(errorResponse);
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());
109 List<String> vars = policyException.getVariables();
110 assertTrue(vars.contains("PUT"));
111 assertTrue(vars.contains("resource"));
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>();
119 AAIException aaie = new AAIException("AAI_3009");
120 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
121 assertNotNull(errorResponse);
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());
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>();
137 AAIException aaie = new AAIException("AAI_3009");
138 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaie, args);
140 assertNotNull(errorResponse);
141 String logContents = LogFile.getContents(ErrorLogFileName);
143 assertNotNull(logContents);
144 String logContentParts[] = logContents.split("\\|");
146 assertTrue(logContentParts.length >= 11 );
147 assertTrue (logContentParts[10].startsWith("ERR.5.6.3009"));