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 static java.lang.Thread.sleep;
24 import static org.junit.Assert.*;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
31 import javax.ws.rs.core.MediaType;
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;
45 public class ErrorLogHelperTest {
47 private static final String ErrorLogFileName = "error.log";
51 System.setProperty("AJSC_HOME", ".");
56 public void cleanup() throws IOException {
58 LogFile.deleteContents(ErrorLogFileName);
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");
66 String logContents = LogFile.getContents(ErrorLogFileName);
68 assertNotNull(logContents);
70 String logContentParts[] = logContents.split("\\|");
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"));
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);
84 String logContents = LogFile.getContents(ErrorLogFileName);
86 assertNotNull(logContents);
88 String logContentParts[] = logContents.split("\\|");
90 assertTrue(logContentParts.length >= 11);
91 assertTrue(logContentParts[9].contains(errorMessage));
92 assertTrue(logContentParts[10].startsWith("ERR.5.4.6110"));
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"));
101 AAIException aaie = new AAIException("AAI_3002");
102 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
103 assertNotNull(errorResponse);
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());
112 List<String> vars = policyException.getVariables();
113 assertTrue(vars.contains("PUT"));
114 assertTrue(vars.contains("resource"));
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>();
123 AAIException aaie = new AAIException("AAI_3009");
124 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args);
125 assertNotNull(errorResponse);
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());
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>();
143 AAIException aaie = new AAIException("AAI_3009");
144 String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaie, args);
146 assertNotNull(errorResponse);
147 String logContents = LogFile.getContents(ErrorLogFileName);
149 assertNotNull(logContents);
150 String logContentParts[] = logContents.split("\\|");
152 assertTrue(logContentParts.length >= 11);
153 assertTrue(logContentParts[10].startsWith("ERR.5.6.3009"));