Skip to content

Commit

Permalink
ReportBuilder Java
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatthareja committed Mar 12, 2018
0 parents commit b0f356a
Show file tree
Hide file tree
Showing 34 changed files with 5,947 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.idea/
*.iml
target/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Rajat Thareja

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ReportBuilderJava
130 changes: 130 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.rajatthareja</groupId>
<artifactId>reportbuilder</artifactId>
<version>1.0.0</version>

<name>Report Builder</name>
<description>Merge Cucumber JSON reports and build HTML Test Report</description>
<url>/~https://github.com/rajatthareja/ReportBuilderJava</url>

<licenses>
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
</license>
</licenses>

<developers>
<developer>
<name>Rajat Thareja</name>
<email>rajat.thareja.1990@gmail.com</email>
<url>/~https://github.com/rajatthareja</url>
</developer>
</developers>

<scm>
<connection>scm:git:git://github.com/rajatthareja/ReportBuilderJava.git</connection>
<developerConnection>scm:git:ssh://github.com/rajatthareja/ReportBuilderJava.git</developerConnection>
<url>/~https://github.com/rajatthareja/ReportBuilderJava/tree/master</url>
</scm>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<json.version>20180130</json.version>
<jackson-databind.version>2.9.4</jackson-databind.version>
<freemarker.version>2.3.23</freemarker.version>
<junit.version>4.12</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</project>
206 changes: 206 additions & 0 deletions src/main/java/com/rajatthareja/reportbuilder/Report.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package com.rajatthareja.reportbuilder;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.rajatthareja.reportbuilder.report.Example;
import com.rajatthareja.reportbuilder.report.Feature;
import com.rajatthareja.reportbuilder.report.Row;
import com.rajatthareja.reportbuilder.report.Scenario;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Process the JSON reports and build the consolidated list of features
*/
public class Report {

private List<Feature> features = new ArrayList<>();
private List<String> errors = new ArrayList<>();

/**
* Build consolidated list of features from given cucumber json reports
*
* @param cucumberJsonReports list of json files or directory containing json files or json file urls or json strings or json objects
*/
public Report(List<Object> cucumberJsonReports) {

List<Object> jsonReports = new ArrayList<>();
jsonReports.addAll(cucumberJsonReports);
List<Object> reportDirs = jsonReports.stream().filter(r -> r instanceof File && ((File) r).isDirectory()).collect(Collectors.toList());

jsonReports.removeAll(reportDirs);
reportDirs.forEach(reportDir -> {
try {
jsonReports.addAll(Files.walk(Paths.get(((File) reportDir).getPath())).filter(p -> p.toString().endsWith(".json")).map(p -> new File(p.toString())).collect(Collectors.toList()));
} catch (Exception e) {
e.printStackTrace();
}
});

ObjectMapper mapper = new ObjectMapper();
List<Feature[]> builds = new ArrayList<>();

for (Object jsonReport : jsonReports) {
try {
if (jsonReport instanceof File) {
try {
builds.add(mapper.readValue((File) jsonReport, Feature[].class));
} catch (Exception e) {
builds.add(mapper.readValue(decode(new String(Files.readAllBytes(((File) jsonReport).toPath()))), Feature[].class));
}
} else if (jsonReport instanceof URL) {
try {
builds.add(mapper.readValue((URL) jsonReport, Feature[].class));
} catch (Exception e) {
builds.add(mapper.readValue(decode(readUrl((URL) jsonReport)), Feature[].class));
}
} else if (jsonReport instanceof String) {
try {
builds.add(mapper.readValue((String) jsonReport, Feature[].class));
} catch (Exception e) {
builds.add(mapper.readValue(decode((String) jsonReport), Feature[].class));
}
} else if (jsonReport instanceof JSONObject) {
try {
builds.add(mapper.readValue(jsonReport.toString(), Feature[].class));
} catch (Exception e) {
builds.add(mapper.readValue(decode(jsonReport.toString()), Feature[].class));
}
} else {
throw new Exception("Invalid jsonReport!");
}
} catch (Exception e) {
System.out.println("Error:: While mapping jsonReport");
e.printStackTrace();
}
}
process(builds);
}

/**
* Process the list of array of features to generate list of features and errors
*
* @param builds list of array of features
*/
private void process(List<Feature[]> builds) {
for (Feature[] build : builds) {
features.addAll(Arrays.asList(build));
}

Map<String, Feature> featuresMap = new HashMap<>();
for (Feature feature : features) {
if (featuresMap.get(feature.getId()) == null)
featuresMap.put(feature.getId(), feature);
else
featuresMap.get(feature.getId()).getScenarios().addAll(feature.getScenarios());
}

features = new ArrayList<>(featuresMap.values());
for (Feature feature : features) {
Map<String, Scenario> scenariosMap = new HashMap<>();
List<Row> examples = new ArrayList<>();
for (Scenario scenario : feature.getScenarios()) {
if (scenario.getExamples() != null) {
for (Example e : scenario.getExamples()) {
examples.addAll(e.getRows());
}
continue;
}
if ((scenariosMap.get(scenario.getId()) == null || scenario.getStatus().equals("passed"))) {
if (scenario.getKeyword().equals("Scenario Outline")) {
examples.stream().filter(e -> e.getId().equals(scenario.getId())).findFirst()
.ifPresent(row -> scenario.setName(scenario.getName() + " " + row.getCells()));
}
String error = scenario.getError();
if (error != null && !errors.contains(error)) {
errors.add(error);
}
scenariosMap.put(scenario.getId(), scenario);
}
}
feature.setScenarios(new ArrayList<>((scenariosMap.values())));
}
}

/**
* Try to decode the given string to "UTF-8"
*
* @param json string for decoding
*
* @return decode string if successful else original string
*/
private String decode(String json) {
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
ByteBuffer data = ByteBuffer.wrap(json.getBytes());
try {
return decoder.decode(data).toString();
} catch (CharacterCodingException e) {
System.out.println("Error:: While decoding JSON report");
e.printStackTrace();
return json;
}
}

/**
* Read the file from the given url and return the contents
*
* @param url of the file
*
* @return contents of the file
*/
private String readUrl(URL url) {
StringBuilder content = new StringBuilder();
try {
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line).append("\n");
}
bufferedReader.close();
} catch (IOException e) {
System.out.println("Error:: While reading JSON report from URL");
e.printStackTrace();
}
return content.toString();
}

/**
* Returns list of feature for building report
*
* @return List of feature
*/
public List<Feature> getFeatures() {
return features;
}

/**
* Returns list of error for building report
*
* @return List of errors
*/
public List<String> getErrors() {
return errors;
}
}
Loading

0 comments on commit b0f356a

Please sign in to comment.