All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ats.tools.report.general.HtmlReportExecution Maven / Gradle / Ivy

The newest version!
package com.ats.tools.report.general;

import com.ats.generator.ATS;
import com.ats.tools.ResourceContent;
import com.ats.tools.report.models.Project;
import com.ats.tools.report.utils.FileUtils;

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

public class HtmlReportExecution {

    private static final String REPORT_TYPE = "${reportType}";
    private static final String PROJECT_NAME = "${projectNameFormatted}";
    private static final String SUITS_LAUNCHED = "${suitsLaunched}";
    private static final String CREATED_AT = "${createdAt}";
    private static final String LOGO = "${logoImage}";
    private static final String PDF_REPORT_LINK = "${pdfReportLink}";
    private static final String ATS_TEST_SCRIPT_VERSION = "${atsTestScriptVersion}";
    public static final String PROJECT_NAME_HTML_TEMPLATE = "${projectNameUri}";
    private final String mainTemplate;
    private final Project project;
    private final String outputPath;
    private final int devReportLevel;
    private final String projectLogo;
    private OutputStream fileWriter;
    private OutputStream validationFileWriter;
    private final boolean isValidationReport;

    private static final String styles = ResourceContent.getHtmlReportCss();
    private static final String scripts = ResourceContent.getHtmlReportJavascript();
    private static final String STYLES_PLACEHOLDER = "${styles}";
    private static final String SCRIPTS_PLACEHOLDER = "${scripts}";

    public HtmlReportExecution(String mainTemplate, Project project, String outputPath, int devReportLevel, boolean isValidationReport) {
        this.mainTemplate = mainTemplate;
        this.project = project;
        this.outputPath = outputPath;
        this.devReportLevel = devReportLevel;
        this.projectLogo = FileUtils.getBase64LogoProperty(project, outputPath);
        this.isValidationReport = isValidationReport;
    }

    public String processMainExecutionsFile() {
        String result = mainTemplate.replace(REPORT_TYPE, getReportType(devReportLevel));
        result = result.replace(STYLES_PLACEHOLDER, styles);
        result = result.replace(SCRIPTS_PLACEHOLDER, scripts);
        result = result.replace(LOGO, projectLogo);
        result = result.replace(PROJECT_NAME, buildProjectName(project));
        result = result.replace(SUITS_LAUNCHED, String.valueOf(project.getSuites().size()));
        result = result.replace(PDF_REPORT_LINK, "summary.pdf");
        result = result.replace(CREATED_AT, String.valueOf(project.getStartedFormatted()));
        result = result.replace(ATS_TEST_SCRIPT_VERSION, ATS.getAtsVersion());

        File executionFile = new File(outputPath + File.separator +"ats-report.html");
        writeFile(result, executionFile);

        if (isValidationReport) {
            File validationFile = new File(outputPath + File.separator +"validation-report.html");
            writeValidationFile(result, validationFile);
        }

        return executionFile.getPath();
    }

    private void writeValidationFile(String result, File validationFile) {
        try {
            validationFileWriter = Files.newOutputStream(Path.of(validationFile.getPath()));
            validationFileWriter.write(result.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private CharSequence buildProjectName(Project project) {
        return PROJECT_NAME_HTML_TEMPLATE.replace("${projectUri}", project.getProjectUuid()).replace("${projectNameUri}", project.getProjectName() + " " + project.getProjectVersion());
    }

    public void writeFile(String executionReport, File suiteFile) {
        try {
            fileWriter = Files.newOutputStream(Path.of(suiteFile.getPath()));
            fileWriter.write(executionReport.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private String getReportType(int devReportLevel) {
        switch (devReportLevel) {
            case 1:
                return "Execution report";
            case 2:
                return "Detailed execution report";
            case 3:
                return "Detailed execution report with screenshots";
            default:
                return "Not available";
        }
    }

    public void closeWriter() {
        try {
            fileWriter.write("
".getBytes()); fileWriter.flush(); fileWriter.close(); if (isValidationReport) { validationFileWriter.write("".getBytes()); validationFileWriter.flush(); validationFileWriter.close(); } } catch (IOException e) { throw new RuntimeException(e); } } public OutputStream getFileWriter() { return fileWriter; } public OutputStream getValidationFileWriter() { return validationFileWriter; } }



© 2015 - 2024 Weber Informatics LLC | Privacy Policy