Incorporate the ECOMP SDC Artefact Generator code
[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.HashMap;
31 import java.util.Map;
32 import java.util.Optional;
33 import java.util.concurrent.TimeUnit;
34 import org.apache.commons.lang.time.StopWatch;
35 import org.junit.Assert;
36
37 public class LogReader {
38
39     private Map<String, Path> cachedLogMap = new HashMap<>();
40     private Map<String, BufferedReader> readersMap = new HashMap<>();
41     private BufferedReader cachedReader;
42
43     public LogReader(String logDirectory, String logFilePrefix) throws IOException {
44         cachedReader = getReader(logDirectory, logFilePrefix);
45     }
46
47     private BufferedReader getReader(String logDirectory, String logFilePrefix) throws IOException {
48         BufferedReader reader = readersMap.get(logFilePrefix);
49         if (reader == null) {
50             reader = new BufferedReader(new FileReader(getLogFile(logDirectory, logFilePrefix)));
51             while (reader.readLine() != null) {
52                 // Consume all lines
53             }
54             readersMap.put(logFilePrefix, reader);
55         }
56         return reader;
57     }
58
59     /**
60      * @param logDirectory
61      * @return the most recently created log file.
62      * @throws IOException
63      */
64     public File getLogFile(String logDirectory, String filenamePrefix) throws IOException {
65         Path cachedLog = cachedLogMap.get(filenamePrefix);
66
67         if (cachedLog == null) {
68             Optional<Path> latestFilePath = Files.list(Paths.get(logDirectory))
69                     .filter(f -> Files.isDirectory(f) == false && f.getFileName().toString().startsWith(filenamePrefix))
70                     .max((f1, f2) -> (int) (f1.toFile().lastModified() - f2.toFile().lastModified()));
71             if (latestFilePath.isPresent()) {
72                 cachedLog = latestFilePath.get();
73             } else {
74                 throw new IOException("No validation log files were found!");
75             }
76         }
77         return cachedLog.toFile();
78     }
79
80     /**
81      * @return new lines appended to the log file
82      * @throws IOException
83      */
84     public String getNewLines() throws IOException {
85         StopWatch stopwatch = new StopWatch();
86         stopwatch.start();
87
88         while (!cachedReader.ready()) {
89             if (stopwatch.getTime() > TimeUnit.SECONDS.toMillis(30)) {
90                 Assert.fail("Test took too long");
91             }
92             // else keep waiting
93         }
94
95         StringBuilder lines = new StringBuilder();
96         String line;
97         while ((line = cachedReader.readLine()) != null) {
98             lines.append(line).append(System.lineSeparator());
99         }
100         return lines.toString();
101     }
102 }