|
| 1 | +package io.github.rsshekhawat; |
| 2 | + |
| 3 | +import org.apache.maven.plugin.AbstractMojo; |
| 4 | +import org.apache.maven.plugin.MojoExecutionException; |
| 5 | +import org.apache.maven.plugin.MojoExecution; |
| 6 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 7 | +import org.apache.maven.plugins.annotations.Mojo; |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.util.*; |
| 12 | +import net.masterthought.cucumber.Configuration; |
| 13 | +import net.masterthought.cucumber.ReportBuilder; |
| 14 | +import net.masterthought.cucumber.Reportable; |
| 15 | +import net.masterthought.cucumber.json.support.Status; |
| 16 | +import net.masterthought.cucumber.presentation.PresentationMode; |
| 17 | +import org.apache.maven.plugins.annotations.Parameter; |
| 18 | + |
| 19 | +@Mojo(name="xbrowser-reports", defaultPhase = LifecyclePhase.POST_INTEGRATION_TEST) |
| 20 | +public class CustomCucumberReports extends AbstractMojo { |
| 21 | + |
| 22 | + @Parameter( defaultValue = "${mojoExecution}", readonly = true ) |
| 23 | + private MojoExecution mojo; |
| 24 | + |
| 25 | + @Parameter( property = "Qualifiers", required = true) |
| 26 | + private String Qualifiers; |
| 27 | + |
| 28 | + String propertiesDirectoryPath = System.getProperty("user.dir")+ File.separator+"target"+File.separator+"parallel-xbrowser"+File.separator+"properties"; |
| 29 | + |
| 30 | + @Override |
| 31 | + public void execute() throws MojoExecutionException { |
| 32 | + getLog().info("------------------------------------------------------------------------"); |
| 33 | + getLog().info("Plugin Name : "+mojo.getPlugin().getArtifactId()); |
| 34 | + getLog().info("Plugin Version : "+mojo.getPlugin().getVersion()); |
| 35 | + getLog().info("Developer : Rahul Shekhawat"); |
| 36 | + getLog().info("------------------------------------------------------------------------"); |
| 37 | + getLog().info("Creating customized cucumber reports..."); |
| 38 | + getLog().info("------------------------------------------------------------------------"); |
| 39 | + createCucumberReports(); |
| 40 | + getLog().info("------------------------------------------------------------------------"); |
| 41 | + getLog().info("Successfully generated customized cucumber reports. Good Bye !!!"); |
| 42 | + getLog().info("------------------------------------------------------------------------"); |
| 43 | + } |
| 44 | + |
| 45 | + public void createCucumberReports() { |
| 46 | + |
| 47 | + File reportOutputDirectory = new File("target"); |
| 48 | + List<String> jsonFiles = new ArrayList<>(); |
| 49 | + String cucumberReportDirectory = System.getProperty("user.dir")+File.separator+"target"+File.separator+"parallel-xbrowser"+File.separator+"cucumber-report"; |
| 50 | + File folder = new File(cucumberReportDirectory); |
| 51 | + File[] listOfFiles = folder.listFiles(); |
| 52 | + assert listOfFiles != null; |
| 53 | + for (File file : listOfFiles) { |
| 54 | + if (file.isFile()) { |
| 55 | + jsonFiles.add(file.getAbsolutePath()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + String buildNumber = System.getenv("BUILD_NUMBER"); |
| 60 | + String projectName = System.getenv("JOB_BASE_NAME"); |
| 61 | + |
| 62 | + Configuration configuration = new Configuration(reportOutputDirectory, projectName); |
| 63 | + |
| 64 | + // optional configuration - check javadoc for details |
| 65 | + configuration.addPresentationModes(PresentationMode.RUN_WITH_JENKINS); |
| 66 | + |
| 67 | + // do not make scenario failed when step has status SKIPPED |
| 68 | + configuration.setNotFailingStatuses(Collections.singleton(Status.SKIPPED)); |
| 69 | + configuration.setBuildNumber(buildNumber); |
| 70 | + |
| 71 | + // optionally specify qualifiers for each of the report json files |
| 72 | + configuration.addPresentationModes(PresentationMode.PARALLEL_TESTING); |
| 73 | + |
| 74 | + int count = 0; |
| 75 | + for (Map.Entry<String, String> mapElement : getQualifierMap(Qualifiers).entrySet()) { |
| 76 | + count+=1; |
| 77 | + configuration.addClassifications("Target "+count, mapElement.getValue()); |
| 78 | + configuration.setQualifier(mapElement.getKey(), mapElement.getValue()); |
| 79 | + } |
| 80 | + |
| 81 | + ReportBuilder reportBuilder = new ReportBuilder(jsonFiles, configuration); |
| 82 | + Reportable result = reportBuilder.generateReports(); |
| 83 | + |
| 84 | + // and here validate 'result' to decide what to do if report has failed |
| 85 | + } |
| 86 | + |
| 87 | + public Map<String, String> getQualifierMap(String list){ |
| 88 | + |
| 89 | + Map<String, String> map = new HashMap<>(); |
| 90 | + File folder = new File(propertiesDirectoryPath); |
| 91 | + File[] listOfFiles = folder.listFiles(); |
| 92 | + assert listOfFiles != null; |
| 93 | + for (File file : listOfFiles) { |
| 94 | + if (file.isFile()) { |
| 95 | + String key = file.getName().split("\\.")[0]; |
| 96 | + String value = getQualifierValue(file.getName(),list); |
| 97 | + map.put(key, value); |
| 98 | + } |
| 99 | + } |
| 100 | + return map; |
| 101 | + } |
| 102 | + |
| 103 | + public String getQualifierValue(String fileName, String list){ |
| 104 | + |
| 105 | + List<String> res = new ArrayList<>(); |
| 106 | + Map<String, String> map = new HashMap<>(); |
| 107 | + try { |
| 108 | + File file = new File(propertiesDirectoryPath+File.separator+fileName); |
| 109 | + Properties prop = new Properties(); |
| 110 | + InputStream is = new FileInputStream(file); |
| 111 | + prop.load(is); |
| 112 | + for (String key : prop.stringPropertyNames()){ |
| 113 | + String value = prop.getProperty(key); |
| 114 | + map.put(key, value); |
| 115 | + } |
| 116 | + is.close(); |
| 117 | + String[] arr = list.split("\\s+"); |
| 118 | + |
| 119 | + for(String item: arr){ |
| 120 | + if(map.get(item)==null) |
| 121 | + continue; |
| 122 | + res.add(map.get(item)); |
| 123 | + } |
| 124 | + |
| 125 | + }catch (Exception exc){ |
| 126 | + getLog().info("Exception : "+exc.getMessage()); |
| 127 | + } |
| 128 | + return String.join(", ", res); |
| 129 | + } |
| 130 | +} |
0 commit comments