Restoring to previous known working version
[appc.git] / appc-config / appc-config-adaptor / provider / src / test / java / org / onap / appc / ccadaptor / DebugLogTest.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP : APPC\r
4  * ================================================================================\r
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Copyright (C) 2017 Amdocs\r
8  * =============================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *      http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  *\r
21  * ============LICENSE_END=========================================================\r
22  */\r
23 \r
24 package org.onap.appc.ccadaptor;\r
25 \r
26 import static junit.framework.TestCase.assertTrue;\r
27 \r
28 import java.io.BufferedReader;\r
29 import java.io.FileReader;\r
30 import java.io.IOException;\r
31 import java.nio.file.Files;\r
32 import java.nio.file.Path;\r
33 import java.nio.file.Paths;\r
34 import org.junit.AfterClass;\r
35 import org.junit.BeforeClass;\r
36 import org.junit.Test;\r
37 \r
38 public class DebugLogTest {\r
39 \r
40     @BeforeClass\r
41     public static void createEmptyLogFile() throws IOException {\r
42         Path logPath = Paths.get(buildTestResourcePath("rt.log"));\r
43         Files.createFile(logPath);\r
44     }\r
45 \r
46     @AfterClass\r
47     public static void removeLog() throws IOException {\r
48         Path existingLogPath = Paths.get(buildTestResourcePath("rt.log"));\r
49         Files.delete(existingLogPath);\r
50     }\r
51 \r
52     //@Test\r
53     public void printRTAriDebug_shouldNotDoAnything_whenLogFileDoesNotExist() {\r
54         // GIVEN\r
55         Path nonExistingLogPath = Paths.get(buildTestResourcePath("nonExisting.log"));\r
56 \r
57         // WHEN\r
58        // DebugLog debugLog = new DebugLog(nonExistingLogPath);\r
59         DebugLog debugLog = new DebugLog();\r
60         debugLog.printRTAriDebug("testMethod", "Custom Debug Message");\r
61 \r
62         // THEN\r
63         assertTrue(Files.notExists(nonExistingLogPath));\r
64     }\r
65 \r
66     //@Test\r
67     public void printRTAriDebug_shouldWriteMessageToLogWithDate_whenLogFileExists() throws IOException {\r
68         // GIVEN\r
69         Path existingLogPath = Paths.get(buildTestResourcePath("rt.log"));\r
70 \r
71         // WHEN\r
72         //DebugLog debugLog = new DebugLog(existingLogPath);\r
73         DebugLog debugLog = new DebugLog();\r
74         debugLog.printRTAriDebug("testMethod", "Custom Debug Message");\r
75 \r
76         // THEN\r
77         String logEntry = readLogEntry(existingLogPath);\r
78         assertTrue(logEntry.matches("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2} testMethod Custom Debug Message"));\r
79     }\r
80 \r
81     private static String buildTestResourcePath(String resourceName) {\r
82         String path = DebugLogTest.class.getClassLoader().getResource("./").getPath();\r
83         return path + resourceName;\r
84     }\r
85 \r
86     private String readLogEntry(Path path) throws IOException {\r
87         try (BufferedReader br = new BufferedReader(new FileReader(path.toFile()))) {\r
88             return br.readLine();\r
89         }\r
90     }\r
91 }\r