Skip to content

Instantly share code, notes, and snippets.

@messaoudi-mounir
Created December 28, 2024 10:24
Show Gist options
  • Save messaoudi-mounir/30272c7acfdbaf76c546c99c58ded726 to your computer and use it in GitHub Desktop.
Save messaoudi-mounir/30272c7acfdbaf76c546c99c58ded726 to your computer and use it in GitHub Desktop.
Cucumber Json Report Merger
package com.org.qa.utils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
public class CucumberJsonMerger {
public static void main(String[] args) throws Exception {
System.out.println("Cucumber JSON Merger is running.");
// Read system properties for file paths
String jsonFilesProperty = System.getProperty("cucumber.json.files", "target/cucumber-reports/*.json");
String outputFileProperty = System.getProperty("merged.json.file", "target/cucumber-reports/merged-cucumber.json");
if (jsonFilesProperty == null || outputFileProperty == null) {
throw new IllegalArgumentException("System properties 'cucumber.json.files' and 'merged.json.file' must be set.");
}
// Resolve input files from the property
List<File> inputFiles = resolveFiles(jsonFilesProperty);
if (inputFiles.isEmpty()) {
throw new IllegalArgumentException("No valid files found for: " + jsonFilesProperty);
}
String outputFile = outputFileProperty;
System.out.println("Files to merge: " + inputFiles);
// Merge JSON files
mergeJsonReports(inputFiles, outputFile);
System.out.println("Merged JSON file created: " + outputFile);
}
private static void mergeJsonReports(List<File> inputFiles, String outputFile) throws Exception {
ObjectMapper mapper = new ObjectMapper();
List<Object> mergedJson = new ArrayList<>();
// Read and merge JSON arrays from input files
for (File file : inputFiles) {
// Skip empty files
if (file.length() == 0) {
System.out.println("Skipping empty file: " + file.getName());
continue;
}
// Read JSON content into a list of objects
try {
List<Object> jsonArray = mapper.readValue(file, new TypeReference<List<Object>>() {});
mergedJson.addAll(jsonArray);
System.out.println("Merged JSON file: " + file.getName());
} catch (IOException e) {
System.out.println("Error reading file: " + file.getName());
e.printStackTrace();
}
}
// Write merged JSON to the output file
mapper.writerWithDefaultPrettyPrinter().writeValue(new File(outputFile), mergedJson);
}
private static List<File> resolveFiles(String input) throws IOException {
List<File> files = new ArrayList<>();
// Check if input is a file pattern or a list of files
if (input.contains("*")) {
// Treat as file pattern
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + input);
Path directory = Paths.get(input).getParent();
if (directory == null) {
throw new IllegalArgumentException("Invalid pattern: " + input);
}
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
for (Path path : directoryStream) {
if (matcher.matches(path)) {
files.add(path.toFile());
}
}
}
} else {
// Treat as comma-separated list of files
String[] paths = input.split(",");
for (String path : paths) {
File file = new File(path.trim());
if (file.exists()) {
files.add(file);
} else {
System.err.println("File not found: " + path);
}
}
}
return files;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment