Minor text fixes
[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
22 package org.onap.aai.babel.logging;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.Comparator;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Optional;
35 import java.util.concurrent.TimeUnit;
36
37 import org.apache.commons.lang3.time.StopWatch;
38 import org.junit.Assert;
39
40 public class LogReader {
41
42     private Map<String, Path> cachedLogMap = new HashMap<>();
43     private Map<String, BufferedReader> readersMap = new HashMap<>();
44     private BufferedReader cachedReader;
45
46     public LogReader(String logDirectory, String logFilePrefix) throws IOException {
47         cachedReader = getReader(logDirectory, logFilePrefix);
48     }
49
50     private BufferedReader getReader(String logDirectory, String logFilePrefix) throws IOException {
51         BufferedReader reader = readersMap.get(logFilePrefix);
52         if (reader == null) {
53             reader = new BufferedReader(new FileReader(getLogFile(logDirectory, logFilePrefix)));
54             while (reader.readLine() != null) {
55                 // Consume all lines
56             }
57             readersMap.put(logFilePrefix, reader);
58         }
59         return reader;
60     }
61
62     /**
63      * Find the most recently modified log file with a name starting with the specified prefix.
64      *
65      * @param logDirectory
66      *            the root folder storing log files
67      * @param filenamePrefix
68      *            the log file name prefix
69      * @return the most recently created log file
70      * @throws IOException
71      *             if an I/O error occurs
72      */
73     public File getLogFile(String logDirectory, String filenamePrefix) throws IOException {
74         Path cachedLog = cachedLogMap.get(filenamePrefix);
75
76         if (cachedLog == null) {
77             Optional<Path> latestFilePath = Files.list(Paths.get(logDirectory))
78                     .filter(f -> Files.isDirectory(f) == false && f.getFileName().toString().startsWith(filenamePrefix))
79                     .max(Comparator.comparingLong(f -> f.toFile().lastModified()));
80             if (latestFilePath.isPresent()) {
81                 cachedLog = latestFilePath.get();
82             } else {
83                 throw new IOException("No validation log files were found!");
84             }
85         }
86         return cachedLog.toFile();
87     }
88
89     /**
90      * Wait for and read new log entries.
91      *
92      * @return new lines appended to the log file
93      * @throws IOException
94      *             If an I/O error occurs
95      */
96     public String getNewLines() throws IOException {
97         StopWatch stopwatch = new StopWatch();
98         stopwatch.start();
99
100         while (!cachedReader.ready()) {
101             if (stopwatch.getTime() > TimeUnit.SECONDS.toMillis(30)) {
102                 Assert.fail("Test took too long");
103             }
104             // else keep waiting
105         }
106
107         StringBuilder lines = new StringBuilder();
108         String line;
109         while ((line = cachedReader.readLine()) != null) {
110             lines.append(line).append(System.lineSeparator());
111         }
112         return lines.toString();
113     }
114 }