test coverage for private method/constuctor
[appc.git] / appc-config / appc-config-adaptor / provider / src / main / java / org / onap / appc / ccadaptor / DebugLog.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24 package org.onap.appc.ccadaptor;
25
26 import java.io.BufferedWriter;
27 import java.io.File;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.io.StringWriter;
32 import java.nio.file.Path;
33 import java.text.DateFormat;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36
37
38 public final class DebugLog {
39
40     public static final String LOG_FILE = "/tmp/rt.log";
41     private final Path path;
42
43     public DebugLog(Path path) {
44         this.path = path;
45     }
46
47     public void printRTAriDebug(String methodName, String message) {
48         writeToLogFile(methodName, message);
49     }
50
51     private String currentDateTime() {
52         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
53         return dateFormat.format(new Date());
54     }
55
56     private void appendToFile(File logPath, String dataToWrite) {
57         try (BufferedWriter out = new BufferedWriter(new FileWriter(logPath, true))) {
58             out.write(dataToWrite);
59         } catch (IOException e) {
60             e.printStackTrace();
61         }
62     }
63
64     public void outputStackTrace(Exception e) {
65         String fn = "DebugLog.outputStackTrace";
66         StringWriter sw = new StringWriter();
67         e.printStackTrace(new PrintWriter(sw));
68         String stackTrace = sw.toString();
69         writeToLogFile(fn, "Stack trace::: " + stackTrace);
70     }
71
72     private void writeToLogFile(String methodName, String message) {
73         File logPath = path.toFile();
74         if (logPath.exists()) {
75             StringBuilder logMessageBuilder = createLogMessage(methodName, message);
76             appendToFile(logPath, logMessageBuilder.toString());
77         }
78     }
79
80     private StringBuilder createLogMessage(String methodName, String message) {
81         StringBuilder logMessageBuilder = new StringBuilder();
82         logMessageBuilder
83             .append(currentDateTime())
84             .append(" ")
85             .append(methodName)
86             .append(" ")
87             .append(message)
88             .append('\n');
89         return logMessageBuilder;
90     }
91 }