Fix remaining Sonar code smells
[aai/babel.git] / src / test / java / org / onap / aai / babel / logging / LogReader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.babel.logging;
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Comparator;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Optional;
34 import java.util.concurrent.TimeUnit;
35 import org.apache.commons.lang3.time.StopWatch;
36 import org.junit.Assert;
37
38 public class LogReader {
39
40     private Map<String, Path> cachedLogMap = new HashMap<>();
41     private Map<String, BufferedReader> readersMap = new HashMap<>();
42     private BufferedReader cachedReader;
43
44     public LogReader(String logDirectory, String logFilePrefix) throws IOException {
45         cachedReader = getReader(logDirectory, logFilePrefix);
46     }
47
48     private BufferedReader getReader(String logDirectory, String logFilePrefix) throws IOException {
49         BufferedReader reader = readersMap.get(logFilePrefix);
50         if (reader == null) {
51             reader = new BufferedReader(new FileReader(getLogFile(logDirectory, logFilePrefix)));
52             while (reader.readLine() != null) {
53                 // Consume all lines
54             }
55             readersMap.put(logFilePrefix, reader);
56         }
57         return reader;
58     }
59
60     /**
61      * @param logDirectory
62      * @return the most recently created log file.
63      * @throws IOException
64      */
65     public File getLogFile(String logDirectory, String filenamePrefix) throws IOException {
66         Path cachedLog = cachedLogMap.get(filenamePrefix);
67
68         if (cachedLog == null) {
69             Optional<Path> latestFilePath = Files.list(Paths.get(logDirectory))
70                     .filter(f -> Files.isDirectory(f) == false && f.getFileName().toString().startsWith(filenamePrefix))
71                     .max(Comparator.comparingLong(f -> f.toFile().lastModified()));
72             if (latestFilePath.isPresent()) {
73                 cachedLog = latestFilePath.get();
74             } else {
75                 throw new IOException("No validation log files were found!");
76             }
77         }
78         return cachedLog.toFile();
79     }
80
81     /**
82      * @return new lines appended to the log file
83      * @throws IOException
84      */
85     public String getNewLines() throws IOException {
86         StopWatch stopwatch = new StopWatch();
87         stopwatch.start();
88
89         while (!cachedReader.ready()) {
90             if (stopwatch.getTime() > TimeUnit.SECONDS.toMillis(30)) {
91                 Assert.fail("Test took too long");
92             }
93             // else keep waiting
94         }
95
96         StringBuilder lines = new StringBuilder();
97         String line;
98         while ((line = cachedReader.readLine()) != null) {
99             lines.append(line).append(System.lineSeparator());
100         }
101         return lines.toString();
102     }
103 }