From 4272e1d1913f228f3d7d5e5ca76e989e5f2d2d2e Mon Sep 17 00:00:00 2001 From: Rakesh Panigrahi Date: Wed, 23 Apr 2025 23:52:56 +0530 Subject: [PATCH 1/5] # 687 - Persist report to external repo after all tests executions --- core/pom.xml | 4 + .../constants/ZeroCodeReportConstants.java | 2 + .../core/di/main/ApplicationMainModule.java | 3 + .../engine/listener/TestUtilityListener.java | 11 +- .../core/reportsupload/ReportUploader.java | 5 + .../reportsupload/ReportUploaderImpl.java | 176 ++++++++++++++++++ .../reportsupload/ReportUploaderImplTest.java | 98 ++++++++++ pom.xml | 6 + 8 files changed, 304 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploader.java create mode 100644 core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java create mode 100644 core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java diff --git a/core/pom.xml b/core/pom.xml index 61377ed4e..f80084cd4 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -201,6 +201,10 @@ com.google.protobuf protobuf-java-util + + org.eclipse.jgit + org.eclipse.jgit + diff --git a/core/src/main/java/org/jsmart/zerocode/core/constants/ZeroCodeReportConstants.java b/core/src/main/java/org/jsmart/zerocode/core/constants/ZeroCodeReportConstants.java index c296ddc21..8545e65d8 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/constants/ZeroCodeReportConstants.java +++ b/core/src/main/java/org/jsmart/zerocode/core/constants/ZeroCodeReportConstants.java @@ -8,7 +8,9 @@ public interface ZeroCodeReportConstants { String TARGET_REPORT_DIR = "target/zerocode-test-reports/"; String TARGET_FULL_REPORT_CSV_FILE_NAME = "zerocode-junit-granular-report.csv"; String TARGET_FILE_NAME = "target/zerocode-junit-interactive-fuzzy-search.html"; + String SUREFIRE_REPORT_DIR = "target/surefire-reports/"; String HIGH_CHART_HTML_FILE_NAME = "zerocode_results_chart"; + String REPORT_UPLOAD_DIR = "target/upload-reports/"; String AUTHOR_MARKER_OLD = "@@"; //Deprecated String AUTHOR_MARKER_NEW = "@"; String CATEGORY_MARKER = "#"; diff --git a/core/src/main/java/org/jsmart/zerocode/core/di/main/ApplicationMainModule.java b/core/src/main/java/org/jsmart/zerocode/core/di/main/ApplicationMainModule.java index 0e0cab725..1d5c6ebca 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/di/main/ApplicationMainModule.java +++ b/core/src/main/java/org/jsmart/zerocode/core/di/main/ApplicationMainModule.java @@ -27,6 +27,8 @@ import org.jsmart.zerocode.core.engine.validators.ZeroCodeValidatorImpl; import org.jsmart.zerocode.core.report.ZeroCodeReportGenerator; import org.jsmart.zerocode.core.report.ZeroCodeReportGeneratorImpl; +import org.jsmart.zerocode.core.reportsupload.ReportUploader; +import org.jsmart.zerocode.core.reportsupload.ReportUploaderImpl; import org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunner; import org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl; @@ -68,6 +70,7 @@ public void configure() { bind(ZeroCodeExternalFileProcessor.class).to(ZeroCodeExternalFileProcessorImpl.class); bind(ZeroCodeParameterizedProcessor.class).to(ZeroCodeParameterizedProcessorImpl.class); bind(ZeroCodeSorter.class).to(ZeroCodeSorterImpl.class); + bind(ReportUploader.class).to(ReportUploaderImpl.class); // ------------------------------------------------ // Bind properties for localhost, CI, DIT, SIT etc diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/listener/TestUtilityListener.java b/core/src/main/java/org/jsmart/zerocode/core/engine/listener/TestUtilityListener.java index 6dd689331..1de95f150 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/engine/listener/TestUtilityListener.java +++ b/core/src/main/java/org/jsmart/zerocode/core/engine/listener/TestUtilityListener.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.inject.Inject; import org.jsmart.zerocode.core.report.ZeroCodeReportGenerator; +import org.jsmart.zerocode.core.reportsupload.ReportUploader; import org.junit.runner.Description; import org.junit.runner.Result; import org.junit.runner.notification.RunListener; @@ -16,10 +17,13 @@ public class TestUtilityListener extends RunListener { private final ZeroCodeReportGenerator reportGenerator; + private final ReportUploader reportUploader; + @Inject - public TestUtilityListener(ObjectMapper mapper, ZeroCodeReportGenerator injectedReportGenerator) { + public TestUtilityListener(ObjectMapper mapper, ZeroCodeReportGenerator injectedReportGenerator, ReportUploader injectedReportUploader) { this.mapper = mapper; this.reportGenerator = injectedReportGenerator; + this.reportUploader = injectedReportUploader; } @Override @@ -37,6 +41,7 @@ public void testRunFinished(Result result) { */ printTestCompleted(); generateChartsAndReports(); + uploadReports(); runPostFinished(); } @@ -71,4 +76,8 @@ private void generateChartsAndReports() { reportGenerator.generateExtentReport(); } + + private void uploadReports(){ + reportUploader.uploadReport(); + } } \ No newline at end of file diff --git a/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploader.java b/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploader.java new file mode 100644 index 000000000..19e838062 --- /dev/null +++ b/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploader.java @@ -0,0 +1,5 @@ +package org.jsmart.zerocode.core.reportsupload; + +public interface ReportUploader { + void uploadReport(); +} diff --git a/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java b/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java new file mode 100644 index 000000000..77d5659ff --- /dev/null +++ b/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java @@ -0,0 +1,176 @@ +package org.jsmart.zerocode.core.reportsupload; + +import com.google.inject.Inject; +import com.google.inject.name.Named; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.transport.URIish; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; +import org.jsmart.zerocode.core.constants.ZeroCodeReportConstants; +import org.jsmart.zerocode.core.report.ZeroCodeReportGeneratorImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.*; + +public class ReportUploaderImpl implements ReportUploader { + + private static final Logger LOGGER = LoggerFactory.getLogger(ZeroCodeReportGeneratorImpl.class); + + @Inject(optional = true) + @Named("reports.repo") + private String reportsRepo; + + @Inject(optional = true) + @Named("reports.repo.username") + private String reportsRepoUsername; + + @Inject(optional = true) + @Named("reports.repo.token") + private String reportsRepoToken; + + @Inject(optional = true) + @Named("reports.repo.max.upload.limit.mb") + private Integer reportsRepoMaxUploadLimitMb; + + public void uploadReport() { + if (!isAllRequiredVariablesSet()) { + LOGGER.warn("One or more required variables are not set. Skipping report upload."); + return; + } + + setDefaultUploadLimit(); + createParentDirectoryIfNotExists(new File(ZeroCodeReportConstants.REPORT_UPLOAD_DIR)); + + try (Git git = initializeOrOpenGitRepository(ZeroCodeReportConstants.REPORT_UPLOAD_DIR)) { + addRemoteRepositoryIfMissing(git); + + //Copy files to the repository + copyFile(ZeroCodeReportConstants.TARGET_FILE_NAME,ZeroCodeReportConstants.REPORT_UPLOAD_DIR); + copyFile( + ZeroCodeReportConstants.TARGET_FULL_REPORT_DIR + ZeroCodeReportConstants.TARGET_FULL_REPORT_CSV_FILE_NAME, + ZeroCodeReportConstants.REPORT_UPLOAD_DIR); + + /* + copyFirstFileUnder2MBMatchingPattern( + ZeroCodeReportConstants.SUREFIRE_REPORT_DIR, + "*.xml", + ZeroCodeReportConstants.REPORT_UPLOAD_DIR + ); + */ + copyFirstFileUnder2MBMatchingPattern( + ZeroCodeReportConstants.TARGET_FULL_REPORT_DIR + "/logs", + "*.log", + ZeroCodeReportConstants.REPORT_UPLOAD_DIR + ); + addAndCommitChanges(git); + pushToRemoteRepository(git); + } catch (Exception e) { + LOGGER.warn("Report upload failed: {}", e.getMessage()); + } + } + + void addRemoteRepositoryIfMissing(Git git) throws URISyntaxException, GitAPIException { + if (git.remoteList().call().isEmpty()) { + LOGGER.debug("Adding remote repository: {}", reportsRepo); + git.remoteAdd().setName("origin").setUri(new URIish(reportsRepo)).call(); + } else { + LOGGER.debug("Remote repository already exists."); + } + } + + void addAndCommitChanges(Git git) throws GitAPIException { + git.add().addFilepattern(".").call(); + LOGGER.debug("Added all files to the Git index."); + + git.commit() + .setMessage("Updated files") + .setAuthor(reportsRepoUsername, reportsRepoUsername) + .call(); + LOGGER.debug("Committed changes."); + } + + void pushToRemoteRepository(Git git) throws GitAPIException { + git.push() + .setCredentialsProvider(new UsernamePasswordCredentialsProvider(reportsRepoUsername, reportsRepoToken)) + .call(); + LOGGER.debug("Pushed changes to remote repository!"); + } + + boolean isAllRequiredVariablesSet() { + return reportsRepo != null && !reportsRepo.isEmpty() && + reportsRepoUsername != null && !reportsRepoUsername.isEmpty() && + reportsRepoToken != null && !reportsRepoToken.isEmpty(); + } + + void setDefaultUploadLimit() { + if (reportsRepoMaxUploadLimitMb == null) { + reportsRepoMaxUploadLimitMb = 2; + LOGGER.debug("reportsRepoMaxUploadLimitMb is not set. Defaulting to 2 MB."); + } + } + + void createParentDirectoryIfNotExists(File repoDir) { + File parentDir = repoDir.getParentFile(); + if (!parentDir.exists() && parentDir.mkdirs()) { + LOGGER.debug("Directory created: {}", parentDir.getAbsolutePath()); + } else if (!parentDir.exists()) { + LOGGER.warn("Failed to create directory: {}", parentDir.getAbsolutePath()); + } + } + + Git initializeOrOpenGitRepository(String repoUploadDir) throws IOException, GitAPIException { + if (new File(repoUploadDir, ".git").exists()) { + LOGGER.debug("Existing Git repository found."); + Git git = Git.open(new File(repoUploadDir)); + LOGGER.debug("Pulling latest changes from remote repository..."); + git.pull() + .setCredentialsProvider(new UsernamePasswordCredentialsProvider(reportsRepoUsername, reportsRepoToken)) + .call(); + return git; + } else { + LOGGER.debug("Initializing a new Git repository..."); + return Git.cloneRepository() + .setURI(reportsRepo) + .setDirectory(new File(repoUploadDir)) + .setCredentialsProvider(new UsernamePasswordCredentialsProvider(reportsRepoUsername, reportsRepoToken)) + .call(); + } + } + + void copyFile(String sourcePath, String targetDirPath) throws IOException { + File sourceFile = new File(sourcePath); + if (!sourceFile.exists()) { + LOGGER.warn("File not found: {}", sourcePath); + return; + } + if (sourceFile.length() <= reportsRepoMaxUploadLimitMb * 1024 * 1024) { + Files.copy(sourceFile.toPath(), Paths.get(targetDirPath, sourceFile.getName()), StandardCopyOption.REPLACE_EXISTING); + LOGGER.debug("File copied: {}", sourceFile.getName()); + } else { + LOGGER.warn("File size exceeds {} MB. Skipping copy: {}", reportsRepoMaxUploadLimitMb,sourceFile.getName()); + } + } + + protected void copyFirstFileUnder2MBMatchingPattern(String dirPath, String globPattern, String targetDirPath) throws IOException { + Path dir = Paths.get(dirPath); + final long maxSizeInBytes = reportsRepoMaxUploadLimitMb * 1024 * 1024; + + try (DirectoryStream stream = Files.newDirectoryStream(dir, globPattern)) { + for (Path entry : stream) { + if (Files.isRegularFile(entry) && Files.size(entry) <= maxSizeInBytes) { + Path target = Paths.get(targetDirPath, entry.getFileName().toString()); + Files.copy(entry, target, StandardCopyOption.REPLACE_EXISTING); + LOGGER.info("Copied first matched file: {}", entry.getFileName()); + return; + } + } + LOGGER.warn("No file under 2MB found matching pattern: {}", globPattern); + } + } + + +} diff --git a/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java b/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java new file mode 100644 index 000000000..260c6219e --- /dev/null +++ b/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java @@ -0,0 +1,98 @@ +package org.jsmart.zerocode.core.reportsupload; + +import com.google.inject.Inject; +import com.google.inject.name.Named; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.Assert.*; + +public class ReportUploaderImplTest { + + + @Inject(optional = true) + @Named("reports.repo") + private String reportsRepo; + + @Inject(optional = true) + @Named("reports.repo.username") + private String reportsRepoUsername; + + @Inject(optional = true) + @Named("reports.repo.token") + private String reportsRepoToken; + + @Inject(optional = true) + @Named("reports.repo.max.upload.limit.mb") + private Integer reportsRepoMaxUploadLimitMb; + + private ReportUploaderImpl reportUploader; + + private Path tempDir; + + @Before + public void setUp() throws IOException { + reportUploader = new ReportUploaderImpl(); + reportUploader.setDefaultUploadLimit(); + tempDir = Files.createTempDirectory("testRepo"); + } + + @After + public void tearDown() throws IOException { + if (tempDir != null) { + Files.walk(tempDir) + .map(Path::toFile) + .forEach(File::delete); + } + } + + /* + @Test + public void initializeOrOpenGitRepository_existingRepository_pullsLatestChanges() throws IOException, GitAPIException { + File gitDir = new File(tempDir.toFile(), ".git"); + Git.init().setDirectory(tempDir.toFile()).call(); + + Git git = reportUploader.initializeOrOpenGitRepository(tempDir.toString()); + + assertNotNull(git); + assertTrue(gitDir.exists()); + } + + */ + + @Test + public void copyFile_fileExistsAndWithinSizeLimit_copiesFile() throws IOException { + System.out.println(reportsRepoUsername); + + File sourceFile = new File(tempDir.toFile(), "source.txt"); + Files.write(sourceFile.toPath(), "test content".getBytes()); + File targetDir = new File(tempDir.toFile(), "target"); + targetDir.mkdir(); + + reportUploader.copyFile(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); + + File copiedFile = new File(targetDir, sourceFile.getName()); + assertTrue(copiedFile.exists()); + } + + @Test + public void copyFile_fileExceedsSizeLimit_logsWarning() throws IOException { + File sourceFile = new File(tempDir.toFile(), "largeFile.txt"); + Files.write(sourceFile.toPath(), new byte[3 * 1024 * 1024]); // 3 MB file + File targetDir = new File(tempDir.toFile(), "target"); + targetDir.mkdir(); + reportUploader.copyFile(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); + + File copiedFile = new File(targetDir, sourceFile.getName()); + assertFalse(copiedFile.exists()); + } + +} diff --git a/pom.xml b/pom.xml index 0e162aa94..d2cafbd1b 100644 --- a/pom.xml +++ b/pom.xml @@ -110,6 +110,7 @@ false 3.24.4 + 6.8.0.202311291450-r @@ -326,6 +327,11 @@ protobuf-java-util ${google.protobuf.version} + + org.eclipse.jgit + org.eclipse.jgit + ${eclipse.jgit.version} + From 4163c7cca3eb817513baf04c4fda153e0deb414e Mon Sep 17 00:00:00 2001 From: Rakesh Panigrahi Date: Sun, 20 Jul 2025 21:26:14 +0530 Subject: [PATCH 2/5] Issue - 687 # Added Unit test and Integration test --- .../reportsupload/ReportUploaderImpl.java | 41 ++++++++--- .../reportsupload/ReportUploaderImplTest.java | 71 +++++++++++++------ .../test/resources/report_uploader.properties | 4 ++ .../reportuploader/ReportUploaderTest.java | 19 +++++ .../resources/report_uploader_test.properties | 10 +++ 5 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 core/src/test/resources/report_uploader.properties create mode 100644 http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java create mode 100644 http-testing/src/test/resources/report_uploader_test.properties diff --git a/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java b/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java index 77d5659ff..555863095 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java +++ b/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java @@ -36,6 +36,7 @@ public class ReportUploaderImpl implements ReportUploader { @Named("reports.repo.max.upload.limit.mb") private Integer reportsRepoMaxUploadLimitMb; + public void uploadReport() { if (!isAllRequiredVariablesSet()) { LOGGER.warn("One or more required variables are not set. Skipping report upload."); @@ -49,7 +50,7 @@ public void uploadReport() { addRemoteRepositoryIfMissing(git); //Copy files to the repository - copyFile(ZeroCodeReportConstants.TARGET_FILE_NAME,ZeroCodeReportConstants.REPORT_UPLOAD_DIR); + copyFile(ZeroCodeReportConstants.TARGET_FILE_NAME, ZeroCodeReportConstants.REPORT_UPLOAD_DIR); copyFile( ZeroCodeReportConstants.TARGET_FULL_REPORT_DIR + ZeroCodeReportConstants.TARGET_FULL_REPORT_CSV_FILE_NAME, ZeroCodeReportConstants.REPORT_UPLOAD_DIR); @@ -73,7 +74,7 @@ public void uploadReport() { } } - void addRemoteRepositoryIfMissing(Git git) throws URISyntaxException, GitAPIException { + protected void addRemoteRepositoryIfMissing(Git git) throws URISyntaxException, GitAPIException { if (git.remoteList().call().isEmpty()) { LOGGER.debug("Adding remote repository: {}", reportsRepo); git.remoteAdd().setName("origin").setUri(new URIish(reportsRepo)).call(); @@ -82,7 +83,7 @@ void addRemoteRepositoryIfMissing(Git git) throws URISyntaxException, GitAPIExce } } - void addAndCommitChanges(Git git) throws GitAPIException { + protected void addAndCommitChanges(Git git) throws GitAPIException { git.add().addFilepattern(".").call(); LOGGER.debug("Added all files to the Git index."); @@ -93,27 +94,27 @@ void addAndCommitChanges(Git git) throws GitAPIException { LOGGER.debug("Committed changes."); } - void pushToRemoteRepository(Git git) throws GitAPIException { + protected void pushToRemoteRepository(Git git) throws GitAPIException { git.push() .setCredentialsProvider(new UsernamePasswordCredentialsProvider(reportsRepoUsername, reportsRepoToken)) .call(); LOGGER.debug("Pushed changes to remote repository!"); } - boolean isAllRequiredVariablesSet() { + protected boolean isAllRequiredVariablesSet() { return reportsRepo != null && !reportsRepo.isEmpty() && reportsRepoUsername != null && !reportsRepoUsername.isEmpty() && reportsRepoToken != null && !reportsRepoToken.isEmpty(); } - void setDefaultUploadLimit() { + protected void setDefaultUploadLimit() { if (reportsRepoMaxUploadLimitMb == null) { reportsRepoMaxUploadLimitMb = 2; LOGGER.debug("reportsRepoMaxUploadLimitMb is not set. Defaulting to 2 MB."); } } - void createParentDirectoryIfNotExists(File repoDir) { + protected void createParentDirectoryIfNotExists(File repoDir) { File parentDir = repoDir.getParentFile(); if (!parentDir.exists() && parentDir.mkdirs()) { LOGGER.debug("Directory created: {}", parentDir.getAbsolutePath()); @@ -122,7 +123,7 @@ void createParentDirectoryIfNotExists(File repoDir) { } } - Git initializeOrOpenGitRepository(String repoUploadDir) throws IOException, GitAPIException { + protected Git initializeOrOpenGitRepository(String repoUploadDir) throws IOException, GitAPIException { if (new File(repoUploadDir, ".git").exists()) { LOGGER.debug("Existing Git repository found."); Git git = Git.open(new File(repoUploadDir)); @@ -141,7 +142,7 @@ Git initializeOrOpenGitRepository(String repoUploadDir) throws IOException, GitA } } - void copyFile(String sourcePath, String targetDirPath) throws IOException { + protected void copyFile(String sourcePath, String targetDirPath) throws IOException { File sourceFile = new File(sourcePath); if (!sourceFile.exists()) { LOGGER.warn("File not found: {}", sourcePath); @@ -151,7 +152,7 @@ void copyFile(String sourcePath, String targetDirPath) throws IOException { Files.copy(sourceFile.toPath(), Paths.get(targetDirPath, sourceFile.getName()), StandardCopyOption.REPLACE_EXISTING); LOGGER.debug("File copied: {}", sourceFile.getName()); } else { - LOGGER.warn("File size exceeds {} MB. Skipping copy: {}", reportsRepoMaxUploadLimitMb,sourceFile.getName()); + LOGGER.warn("File size exceeds {} MB. Skipping copy: {}", reportsRepoMaxUploadLimitMb, sourceFile.getName()); } } @@ -173,4 +174,24 @@ protected void copyFirstFileUnder2MBMatchingPattern(String dirPath, String globP } + public void setReportsRepo(String reportsRepo) { + this.reportsRepo = reportsRepo; + } + + public void setReportsRepoUsername(String reportsRepoUsername) { + this.reportsRepoUsername = reportsRepoUsername; + } + + public void setReportsRepoToken(String reportsRepoToken) { + this.reportsRepoToken = reportsRepoToken; + } + + public void setReportsRepoMaxUploadLimitMb(Integer reportsRepoMaxUploadLimitMb) { + if (reportsRepoMaxUploadLimitMb == null) { + this.reportsRepoMaxUploadLimitMb = 2; + }else { + this.reportsRepoMaxUploadLimitMb = reportsRepoMaxUploadLimitMb; + } + + } } diff --git a/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java b/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java index 260c6219e..d73b80407 100644 --- a/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java +++ b/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java @@ -4,9 +4,13 @@ import com.google.inject.name.Named; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; +import org.jsmart.zerocode.core.di.main.ApplicationMainModule; +import org.jukito.JukitoRunner; +import org.jukito.TestModule; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import java.io.File; import java.io.IOException; @@ -15,8 +19,10 @@ import static org.junit.Assert.*; +@RunWith(JukitoRunner.class) public class ReportUploaderImplTest { - + private ReportUploaderImpl reportUploader; + private Path tempDir; @Inject(optional = true) @Named("reports.repo") @@ -34,44 +40,54 @@ public class ReportUploaderImplTest { @Named("reports.repo.max.upload.limit.mb") private Integer reportsRepoMaxUploadLimitMb; - private ReportUploaderImpl reportUploader; - - private Path tempDir; + public static class JukitoModule extends TestModule { + @Override + protected void configureTest() { + ApplicationMainModule applicationMainModule = new ApplicationMainModule("report_uploader.properties"); + install(applicationMainModule); + } + } @Before public void setUp() throws IOException { reportUploader = new ReportUploaderImpl(); reportUploader.setDefaultUploadLimit(); + reportUploader.setReportsRepo(reportsRepo); + reportUploader.setReportsRepoUsername(reportsRepoUsername); + reportUploader.setReportsRepoToken(reportsRepoToken); + reportUploader.setReportsRepoMaxUploadLimitMb(reportsRepoMaxUploadLimitMb); tempDir = Files.createTempDirectory("testRepo"); } - @After - public void tearDown() throws IOException { - if (tempDir != null) { - Files.walk(tempDir) - .map(Path::toFile) - .forEach(File::delete); + @Test + public void testInitializeOrOpenGitRepository_cloneRepo() throws Exception { + if(reportUploader.isAllRequiredVariablesSet()){ + File targetDir = new File(tempDir.toFile(),"clonerepo"); + Git git = reportUploader.initializeOrOpenGitRepository(targetDir.getAbsolutePath()); + assertTrue(new File(targetDir, ".git").exists()); + git.close(); } } - /* @Test - public void initializeOrOpenGitRepository_existingRepository_pullsLatestChanges() throws IOException, GitAPIException { - File gitDir = new File(tempDir.toFile(), ".git"); - Git.init().setDirectory(tempDir.toFile()).call(); - - Git git = reportUploader.initializeOrOpenGitRepository(tempDir.toString()); - - assertNotNull(git); - assertTrue(gitDir.exists()); + public void testAddAndCommitChanges() throws Exception { + if(reportUploader.isAllRequiredVariablesSet()){ + File targetDir = new File(tempDir.toFile(),"commitrepo"); + Git git = Git.init().setDirectory(targetDir).call(); + + // Create a dummy file to commit + File file = new File(targetDir, "dummy.txt"); + Files.write(file.toPath(), "data".getBytes()); + + reportUploader.addAndCommitChanges(git); + assertTrue(git.log().call().iterator().hasNext()); + git.close(); + } } - */ - @Test public void copyFile_fileExistsAndWithinSizeLimit_copiesFile() throws IOException { - System.out.println(reportsRepoUsername); - + System.out.println(reportsRepo); File sourceFile = new File(tempDir.toFile(), "source.txt"); Files.write(sourceFile.toPath(), "test content".getBytes()); File targetDir = new File(tempDir.toFile(), "target"); @@ -95,4 +111,13 @@ public void copyFile_fileExceedsSizeLimit_logsWarning() throws IOException { assertFalse(copiedFile.exists()); } + @After + public void tearDown() throws IOException { + if (tempDir != null) { + Files.walk(tempDir) + .map(Path::toFile) + .forEach(File::delete); + } + } + } diff --git a/core/src/test/resources/report_uploader.properties b/core/src/test/resources/report_uploader.properties new file mode 100644 index 000000000..654e12cb1 --- /dev/null +++ b/core/src/test/resources/report_uploader.properties @@ -0,0 +1,4 @@ +reports.repo= +reports.repo.username= +reports.repo.token= +reports.repo.max.upload.limit.mb= \ No newline at end of file diff --git a/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java b/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java new file mode 100644 index 000000000..d84b25e1a --- /dev/null +++ b/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java @@ -0,0 +1,19 @@ +package org.jsmart.zerocode.testhelp.tests.reportuploader; + +import org.jsmart.zerocode.core.domain.Scenario; +import org.jsmart.zerocode.core.domain.TargetEnv; +import org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertTrue; + +@TargetEnv("report_uploader_test.properties") +@RunWith(ZeroCodeUnitRunner.class) +public class ReportUploaderTest { + + @Test + @Scenario("helloworld/hello_world_status_ok_assertions.json") + public void testGet() throws Exception { + } +} diff --git a/http-testing/src/test/resources/report_uploader_test.properties b/http-testing/src/test/resources/report_uploader_test.properties new file mode 100644 index 000000000..04bbc1fe1 --- /dev/null +++ b/http-testing/src/test/resources/report_uploader_test.properties @@ -0,0 +1,10 @@ +# Web Server host and port +web.application.endpoint.host=https://api.github.com +# Web Service Port; Leave it blank in case it is default port i.e. 80 or 443 etc +web.application.endpoint.port= +# Web Service context; Leave it blank in case you do not have a common context +web.application.endpoint.context= +reports.repo= +reports.repo.username= +reports.repo.token= +reports.repo.max.upload.limit.mb= \ No newline at end of file From b577d254cf1eb5dda1c27c30045fd4d55cfc34e5 Mon Sep 17 00:00:00 2001 From: Rakesh Panigrahi Date: Mon, 21 Jul 2025 21:27:36 +0530 Subject: [PATCH 3/5] Issue - 687 # Added check for file not found --- .../zerocode/core/reportsupload/ReportUploaderImpl.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java b/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java index 555863095..ecba2605b 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java +++ b/core/src/main/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImpl.java @@ -55,13 +55,11 @@ public void uploadReport() { ZeroCodeReportConstants.TARGET_FULL_REPORT_DIR + ZeroCodeReportConstants.TARGET_FULL_REPORT_CSV_FILE_NAME, ZeroCodeReportConstants.REPORT_UPLOAD_DIR); - /* copyFirstFileUnder2MBMatchingPattern( ZeroCodeReportConstants.SUREFIRE_REPORT_DIR, "*.xml", ZeroCodeReportConstants.REPORT_UPLOAD_DIR ); - */ copyFirstFileUnder2MBMatchingPattern( ZeroCodeReportConstants.TARGET_FULL_REPORT_DIR + "/logs", "*.log", @@ -158,6 +156,11 @@ protected void copyFile(String sourcePath, String targetDirPath) throws IOExcept protected void copyFirstFileUnder2MBMatchingPattern(String dirPath, String globPattern, String targetDirPath) throws IOException { Path dir = Paths.get(dirPath); + + if (!Files.exists(dir) || !Files.isDirectory(dir)) { + LOGGER.warn("Directory does not exist or is not a valid directory: {}", dirPath); + return; + } final long maxSizeInBytes = reportsRepoMaxUploadLimitMb * 1024 * 1024; try (DirectoryStream stream = Files.newDirectoryStream(dir, globPattern)) { From 24aabee3ccc22899d28643bd45310c967149b81b Mon Sep 17 00:00:00 2001 From: Rakesh Panigrahi Date: Tue, 22 Jul 2025 21:41:55 +0530 Subject: [PATCH 4/5] Issue - 687 # Refactored config and test --- .../testhelp/tests/reportuploader/ReportUploaderTest.java | 2 +- .../src/test/resources/report_uploader_test.properties | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java b/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java index d84b25e1a..988cd687f 100644 --- a/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java +++ b/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java @@ -13,7 +13,7 @@ public class ReportUploaderTest { @Test - @Scenario("helloworld/hello_world_status_ok_assertions.json") public void testGet() throws Exception { + assertTrue(true); } } diff --git a/http-testing/src/test/resources/report_uploader_test.properties b/http-testing/src/test/resources/report_uploader_test.properties index 04bbc1fe1..9169322a9 100644 --- a/http-testing/src/test/resources/report_uploader_test.properties +++ b/http-testing/src/test/resources/report_uploader_test.properties @@ -1,10 +1,4 @@ -# Web Server host and port -web.application.endpoint.host=https://api.github.com -# Web Service Port; Leave it blank in case it is default port i.e. 80 or 443 etc -web.application.endpoint.port= -# Web Service context; Leave it blank in case you do not have a common context -web.application.endpoint.context= reports.repo= reports.repo.username= reports.repo.token= -reports.repo.max.upload.limit.mb= \ No newline at end of file +reports.repo.max.upload.limit.mb=2 \ No newline at end of file From 912a0935e21859d43170dc20e9387cabfdd4435f Mon Sep 17 00:00:00 2001 From: Rakesh Panigrahi Date: Thu, 24 Jul 2025 22:38:40 +0530 Subject: [PATCH 5/5] Fixed Build Issue --- .../engine/listener/TestUtilityListener.java | 5 +---- .../reportsupload/ReportUploaderImplTest.java | 19 +++++++++++-------- .../reportuploader/ReportUploaderTest.java | 0 .../resources/report_uploader_test.properties | 2 +- pom.xml | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) rename {http-testing => http-testing-examples}/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java (100%) rename {http-testing => http-testing-examples}/src/test/resources/report_uploader_test.properties (62%) diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/listener/TestUtilityListener.java b/core/src/main/java/org/jsmart/zerocode/core/engine/listener/TestUtilityListener.java index 1de95f150..f9083f039 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/engine/listener/TestUtilityListener.java +++ b/core/src/main/java/org/jsmart/zerocode/core/engine/listener/TestUtilityListener.java @@ -41,7 +41,6 @@ public void testRunFinished(Result result) { */ printTestCompleted(); generateChartsAndReports(); - uploadReports(); runPostFinished(); } @@ -55,9 +54,7 @@ private void printTestCompleted() { * Override this to handle post-finished tasks */ public void runPostFinished() { - /* - * Do nothing for now - */ + uploadReports(); } private void generateChartsAndReports() { diff --git a/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java b/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java index d73b80407..0d19b39e3 100644 --- a/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java +++ b/core/src/test/java/org/jsmart/zerocode/core/reportsupload/ReportUploaderImplTest.java @@ -1,25 +1,24 @@ package org.jsmart.zerocode.core.reportsupload; +import com.google.inject.AbstractModule; import com.google.inject.Inject; import com.google.inject.name.Named; import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.api.errors.GitAPIException; import org.jsmart.zerocode.core.di.main.ApplicationMainModule; -import org.jukito.JukitoRunner; -import org.jukito.TestModule; +import org.jsmart.zerocode.core.guice.ZeroCodeGuiceTestRule; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; -@RunWith(JukitoRunner.class) public class ReportUploaderImplTest { private ReportUploaderImpl reportUploader; private Path tempDir; @@ -40,14 +39,18 @@ public class ReportUploaderImplTest { @Named("reports.repo.max.upload.limit.mb") private Integer reportsRepoMaxUploadLimitMb; - public static class JukitoModule extends TestModule { + @Rule + public ZeroCodeGuiceTestRule guiceRule = new ZeroCodeGuiceTestRule(this, ReportUploaderImplTest.ZeroCodeTestModule.class); + + public static class ZeroCodeTestModule extends AbstractModule { @Override - protected void configureTest() { + protected void configure() { ApplicationMainModule applicationMainModule = new ApplicationMainModule("report_uploader.properties"); install(applicationMainModule); } } + @Before public void setUp() throws IOException { reportUploader = new ReportUploaderImpl(); diff --git a/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java b/http-testing-examples/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java similarity index 100% rename from http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java rename to http-testing-examples/src/test/java/org/jsmart/zerocode/testhelp/tests/reportuploader/ReportUploaderTest.java diff --git a/http-testing/src/test/resources/report_uploader_test.properties b/http-testing-examples/src/test/resources/report_uploader_test.properties similarity index 62% rename from http-testing/src/test/resources/report_uploader_test.properties rename to http-testing-examples/src/test/resources/report_uploader_test.properties index 9169322a9..654e12cb1 100644 --- a/http-testing/src/test/resources/report_uploader_test.properties +++ b/http-testing-examples/src/test/resources/report_uploader_test.properties @@ -1,4 +1,4 @@ reports.repo= reports.repo.username= reports.repo.token= -reports.repo.max.upload.limit.mb=2 \ No newline at end of file +reports.repo.max.upload.limit.mb= \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5d5e8cb33..e984b4659 100644 --- a/pom.xml +++ b/pom.xml @@ -110,7 +110,7 @@ false 3.24.4 - 6.8.0.202311291450-r + 5.13.0.202109080827-r