2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.ccadaptor;
27 import static junit.framework.TestCase.assertTrue;
29 import java.io.BufferedReader;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
39 public class DebugLogTest {
42 public static void createEmptyLogFile() throws IOException {
43 Path logPath = Paths.get(buildTestResourcePath("rt.log"));
44 Files.createFile(logPath);
48 public static void removeLog() throws IOException {
49 Path existingLogPath = Paths.get(buildTestResourcePath("rt.log"));
50 Files.delete(existingLogPath);
54 public void printRTAriDebug_shouldNotDoAnything_whenLogFileDoesNotExist() {
56 Path nonExistingLogPath = Paths.get(buildTestResourcePath("nonExisting.log"));
59 DebugLog debugLog = new DebugLog(nonExistingLogPath);
60 debugLog.printRTAriDebug("testMethod", "Custom Debug Message");
63 assertTrue(Files.notExists(nonExistingLogPath));
67 public void printRTAriDebug_shouldWriteMessageToLogWithDate_whenLogFileExists() throws IOException {
69 Path existingLogPath = Paths.get(buildTestResourcePath("rt.log"));
72 DebugLog debugLog = new DebugLog(existingLogPath);
73 debugLog.printRTAriDebug("testMethod", "Custom Debug Message");
76 String logEntry = readLogEntry(existingLogPath);
77 assertTrue(logEntry.matches("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2} testMethod Custom Debug Message"));
80 private static String buildTestResourcePath(String resourceName) {
81 String path = DebugLogTest.class.getClassLoader().getResource("./").getPath();
82 return path + resourceName;
85 private String readLogEntry(Path path) throws IOException {
86 try (BufferedReader br = new BufferedReader(new FileReader(path.toFile()))) {