Skip to content

Commit

Permalink
Merge pull request #468 from telekom/fix/file-report-handling
Browse files Browse the repository at this point in the history
Migrate DefaultReport from File to Path
  • Loading branch information
martingrossmann authored Feb 7, 2025
2 parents 99d58db + 0edd406 commit 63ebe57
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,73 +24,79 @@
import eu.tsystems.mms.tic.testframework.report.model.context.Screenshot;
import eu.tsystems.mms.tic.testframework.report.model.context.Video;
import eu.tsystems.mms.tic.testframework.utils.FileUtils;
import org.apache.commons.io.file.PathUtils;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultReport implements Report, Loggable {

private File currentReportDirectory;
private Path currentReportDirectory;
private final String baseDir = Properties.BASE_DIR.asString();
private final File finalReportDirectory = new File(baseDir);
private final File tempReportDirectory;
private final Path finalReportPath = Path.of(baseDir);
private final Path tempReportDirectory;
private final ConcurrentHashMap<Class<? extends Annotation>, AnnotationConverter> annotationConverters = new ConcurrentHashMap<>();

public DefaultReport() {
FileUtils fileUtils = new FileUtils();
tempReportDirectory = fileUtils.createTempDir(baseDir);
log().debug("Prepare report in " + tempReportDirectory.getAbsolutePath());

tempReportDirectory = fileUtils.createTempDir("test-report");
log().debug("Prepare report in " + tempReportDirectory.toAbsolutePath());
currentReportDirectory = tempReportDirectory;
}

private File addFile(File sourceFile, File directory, FileMode fileMode) {
private Path addFile(Path sourceFile, Path directory, FileMode fileMode) {
try {
switch (fileMode) {
case COPY:
FileUtils.copyFileToDirectory(sourceFile, directory, true);
break;
default:
case MOVE:
FileUtils.moveFileToDirectory(sourceFile, directory, true);
break;
PathUtils.copyFileToDirectory(sourceFile, directory);
if (fileMode == FileMode.MOVE) {
Files.delete(sourceFile);
}
} catch (IOException e) {
log().error("Could not add file", e);
}
return new File(directory, sourceFile.getName());
return directory.resolve(sourceFile.getFileName());
}

public File finalizeReport() {
public Path finalizeReport() {
try {
if (finalReportDirectory.exists()) {
FileUtils.deleteDirectory(finalReportDirectory);
if (Files.exists(finalReportPath)) {
PathUtils.deleteDirectory(finalReportPath);
}

if (tempReportDirectory.exists()) {
if (Files.exists(tempReportDirectory)) {
log().debug("Temporary directory is {}", tempReportDirectory);
FileUtils.moveDirectory(tempReportDirectory, finalReportDirectory);
currentReportDirectory = finalReportDirectory;
log().info("Report written to " + finalReportDirectory.getAbsolutePath());
Files.createDirectories(finalReportPath);
PathUtils.copyDirectory(tempReportDirectory, finalReportPath);
currentReportDirectory = finalReportPath;
log().info("Report written to " + finalReportPath.toAbsolutePath());
}

try {
PathUtils.deleteDirectory(tempReportDirectory);
} catch (IOException e) {
log().warn("Could not delete temporary directory {}", tempReportDirectory);
}
} catch (IOException e) {
throw new RuntimeException("Could not move report dir: " + e.getMessage(), e);
throw new RuntimeException("Could not copy report dir: " + e.getMessage(), e);
}
return finalReportDirectory;
return finalReportPath;
}

private void addScreenshotFiles(Screenshot screenshot, FileMode fileMode) {
File screenshotsDirectory = getReportDirectory(SCREENSHOTS_FOLDER_NAME);
Path screenshotsDirectory = getReportDirectory(SCREENSHOTS_FOLDER_NAME);
if (screenshot.getScreenshotFile() != null) {
screenshot.setFile(addFile(screenshot.getScreenshotFile(), screenshotsDirectory, fileMode));
Path path = addFile(screenshot.getScreenshotFile().toPath(), screenshotsDirectory, fileMode);
screenshot.setFile(path.toFile());
}

screenshot.getPageSourceFile().ifPresent(file -> {
screenshot.setPageSourceFile(addFile(file, screenshotsDirectory, fileMode));
Path path = addFile(file.toPath(), screenshotsDirectory, fileMode);
screenshot.setPageSourceFile(path.toFile());
});
}

Expand All @@ -109,8 +115,9 @@ public Screenshot provideScreenshot(File file, FileMode fileMode) {

@Override
public Report addVideo(Video video, FileMode fileMode) {
File videoDirectory = getReportDirectory(VIDEO_FOLDER_NAME);
video.setFile(addFile(video.getVideoFile(), videoDirectory, fileMode));
Path videoDirectory = getReportDirectory(VIDEO_FOLDER_NAME);
Path path = addFile(video.getVideoFile().toPath(), videoDirectory, fileMode);
video.setFile(path.toFile());
return this;
}

Expand All @@ -124,23 +131,23 @@ public Video provideVideo(File file, FileMode fileMode) {
/**
* @return Final report directory defined by the user
*/
public File getReportDirectory() {
public Path getReportDirectory() {
return currentReportDirectory;
}

/**
* @return Final report directory defined by the user
*/
public File getFinalReportDirectory() {
return finalReportDirectory;
public Path getFinalReportDirectory() {
return finalReportPath;
}

@Override
public String getRelativePath(File file) {
String absFilePath = file.getAbsolutePath();

for (File dir : Arrays.asList(tempReportDirectory, finalReportDirectory)) {
String absDirPath = dir.getAbsolutePath();
for (Path path : Arrays.asList(tempReportDirectory, finalReportPath)) {
String absDirPath = path.toAbsolutePath().toString();
if (absFilePath.startsWith(absDirPath)) {
return absFilePath.replace(absDirPath, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
import eu.tsystems.mms.tic.testframework.common.IProperties;
import eu.tsystems.mms.tic.testframework.report.model.context.Screenshot;
import eu.tsystems.mms.tic.testframework.report.model.context.Video;
import eu.tsystems.mms.tic.testframework.utils.FileUtils;

import java.io.File;
import java.lang.annotation.Annotation;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

public interface Report {
Expand Down Expand Up @@ -83,32 +87,35 @@ enum Mode {

Report addVideo(Video video, FileMode fileMode);
Video provideVideo(File file, FileMode fileMode);
File finalizeReport();
Path finalizeReport();

File getReportDirectory();
Path getReportDirectory();

/**
* @param childName Child directory or file name
* @return Final report sub directory defined by the user
*/
default File getReportDirectory(String childName) {
File dir = new File(getReportDirectory(), childName);
if (!dir.exists()) {
dir.mkdirs();
default Path getReportDirectory(String childName) {
Path path = getReportDirectory().resolve(childName);
if (!Files.exists(path)) {
new FileUtils().createDirectoriesSafely(path);
}
return dir;
return path;
}
default File getReportFile(String filePath) {
File file = new File(getReportDirectory(), filePath);
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs();

default Path getReportFile(String filePath) {
Path path = getReportDirectory().resolve(Path.of(filePath));
Path dir = path.getParent();
if (!Files.exists(dir)) {
new FileUtils().createDirectoriesSafely(dir);
}
return file;
return path;
}
File getFinalReportDirectory();
default File getFinalReportDirectory(String childName) {
return new File(getFinalReportDirectory(), childName);

Path getFinalReportDirectory();

default Path getFinalReportDirectory(String childName) {
return getFinalReportDirectory().resolve(Path.of(childName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import eu.tsystems.mms.tic.testframework.exceptions.SystemException;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import org.apache.commons.io.FilenameUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -37,6 +38,9 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.UUID;

Expand Down Expand Up @@ -261,9 +265,26 @@ public File createTempFileName(String fileName) {
(extension.length() > 0 ? "." + extension : ""));
}

public File createTempDir(String dirName) {
File dir = new File(System.getProperty("java.io.tmpdir") + "/" + dirName + "-" + UUID.randomUUID());
dir.mkdirs();
return dir;
public Path createTempDir(String dirName) {
try {
// Path dir = Path.of(System.getProperty("java.io.tmpdir"), dirName + "-" + UUID.randomUUID());

return Files.createTempDirectory(dirName + "_");
} catch (IOException e) {
throw new SystemException("Cannot create temporary folder " + dirName, e);
}
}

public boolean createDirectoriesSafely(Path path) {
try {
Files.createDirectories(path);
return true;
} catch (FileAlreadyExistsException e) {
log().error("Folder already exists: {}", path.toAbsolutePath());
return false;
} catch (IOException e) {
log().error("Cannot create folder {}: {}", path.toAbsolutePath(), e.getMessage());
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -280,9 +281,8 @@ private static String getParsedDocumentPath(String fileName) {
}

FileUtils utils = new FileUtils();
File tempDir = utils.createTempDir(fileName);

return tempDir.getAbsolutePath() + File.separator + fileName;
Path tempDir = utils.createTempDir(fileName);
return tempDir.resolve(fileName).toAbsolutePath().toString();
}

private static void closeDocument(PDDocument pdDoc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,33 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Tests the responsive page factory for correct instantiated classes.
*/
public class PageFactoryTest extends AbstractTestSitesTest implements PageFactoryProvider, PropertyManagerProvider {

@Test
public void testT08_CheckPage_ScreenshotOnLoad() {
public void testT08_CheckPage_ScreenshotOnLoad() throws IOException {

final File reportScreenshotDirectory = Testerra.getInjector().getInstance(Report.class).getReportDirectory(Report.SCREENSHOTS_FOLDER_NAME);
final Path reportScreenshotDirectory = Testerra.getInjector().getInstance(Report.class).getReportDirectory(Report.SCREENSHOTS_FOLDER_NAME);
Assert.assertNotNull(reportScreenshotDirectory);

final WebDriver driver = getWebDriver();

final int fileCountBeforeAction = getNumFiles(reportScreenshotDirectory);
final long fileCountBeforeAction = getNumFiles(reportScreenshotDirectory);
PROPERTY_MANAGER.setTestLocalProperty(Testerra.Properties.SCREENSHOT_ON_PAGELOAD, false);
PAGE_FACTORY.createPage(PageWithExistingElement.class, driver);

final int fileCountAfterCheckPageWithoutScreenshot = getNumFiles(reportScreenshotDirectory);
final long fileCountAfterCheckPageWithoutScreenshot = getNumFiles(reportScreenshotDirectory);
Assert.assertEquals(fileCountBeforeAction, fileCountAfterCheckPageWithoutScreenshot, "Record Screenshot count not altered.");

PROPERTY_MANAGER.setTestLocalProperty(Testerra.Properties.SCREENSHOT_ON_PAGELOAD, true);
PAGE_FACTORY.createPage(PageWithExistingElement.class, driver);
final int fileCountAfterCheckPageWithScreenshot = getNumFiles(reportScreenshotDirectory);
final long fileCountAfterCheckPageWithScreenshot = getNumFiles(reportScreenshotDirectory);

Assert.assertNotEquals(fileCountAfterCheckPageWithoutScreenshot, fileCountAfterCheckPageWithScreenshot, "Record Screenshot count altered.");
}
Expand Down Expand Up @@ -112,13 +114,8 @@ public void testT12_LoopDetectionTest_DataProvider_ParallelFalse(String loop) {
}
}

private int getNumFiles(File directory) {
File[] files = directory.listFiles();
if (files == null) {
return 0;
} else {
return files.length;
}
private long getNumFiles(Path directory) throws IOException {
return Files.list(directory).count();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ protected void generateReport(ITestContext context) {

document.pop();

Utils.writeUtf8File(report.getReportDirectory(Report.XML_FOLDER_NAME).getAbsolutePath(), XML_RESULT_FILENAME, document.toXML());
Utils.writeUtf8File(report.getReportDirectory(Report.XML_FOLDER_NAME).toAbsolutePath().toString(), XML_RESULT_FILENAME, document.toXML());
}

static String formattedTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import eu.tsystems.mms.tic.testframework.listeners.GenerateReportNgModelListener;
import eu.tsystems.mms.tic.testframework.report.Report;

import java.nio.file.Path;

public class ReportNgHook extends AbstractModule implements ModuleHook {

@Override
Expand All @@ -39,7 +41,7 @@ public void init() {
Report report = Testerra.getInjector().getInstance(Report.class);

eventBus.register(new CopyReportAppListener(report.getReportDirectory()));
eventBus.register(new GenerateReportNgModelListener(report.getReportDirectory("report-ng/model")));
eventBus.register(new GenerateReportNgModelListener(report.getReportDirectory().resolve(Path.of("report-ng/model")).toFile()));
}

@Override
Expand Down
Loading

0 comments on commit 63ebe57

Please sign in to comment.