test coverage for private method/constuctor
[appc.git] / appc-config / appc-config-adaptor / provider / src / test / java / org / onap / appc / ccadaptor / DebugLogTest.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
25 package org.onap.appc.ccadaptor;
26
27 import static junit.framework.TestCase.assertTrue;
28
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;
38
39 public class DebugLogTest {
40
41     @BeforeClass
42     public static void createEmptyLogFile() throws IOException {
43         Path logPath = Paths.get(buildTestResourcePath("rt.log"));
44         Files.createFile(logPath);
45     }
46
47     @AfterClass
48     public static void removeLog() throws IOException {
49         Path existingLogPath = Paths.get(buildTestResourcePath("rt.log"));
50         Files.delete(existingLogPath);
51     }
52
53     @Test
54     public void printRTAriDebug_shouldNotDoAnything_whenLogFileDoesNotExist() {
55         // GIVEN
56         Path nonExistingLogPath = Paths.get(buildTestResourcePath("nonExisting.log"));
57
58         // WHEN
59         DebugLog debugLog = new DebugLog(nonExistingLogPath);
60         debugLog.printRTAriDebug("testMethod", "Custom Debug Message");
61
62         // THEN
63         assertTrue(Files.notExists(nonExistingLogPath));
64     }
65
66     @Test
67     public void printRTAriDebug_shouldWriteMessageToLogWithDate_whenLogFileExists() throws IOException {
68         // GIVEN
69         Path existingLogPath = Paths.get(buildTestResourcePath("rt.log"));
70
71         // WHEN
72         DebugLog debugLog = new DebugLog(existingLogPath);
73         debugLog.printRTAriDebug("testMethod", "Custom Debug Message");
74
75         // THEN
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"));
78     }
79
80     private static String buildTestResourcePath(String resourceName) {
81         String path = DebugLogTest.class.getClassLoader().getResource("./").getPath();
82         return path + resourceName;
83     }
84
85     private String readLogEntry(Path path) throws IOException {
86         try (BufferedReader br = new BufferedReader(new FileReader(path.toFile()))) {
87             return br.readLine();
88         }
89     }
90 }