Skip to content

Commit

Permalink
fixes #45 ; adds also better error logging (#46)
Browse files Browse the repository at this point in the history
* fixes #45 ; adds also better error logging

* bump version to 0.6.0

Co-authored-by: Sergio Freire <[email protected]>
  • Loading branch information
bitcoder and Sergio Freire authored Jun 8, 2022
1 parent 1693570 commit b3d6660
Show file tree
Hide file tree
Showing 15 changed files with 491 additions and 19 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

## [0.6.0] - 2022-06-08

### Added

- better error logging (i.e., checking file existence)

### Changed

### Fixed

- handling testInfoJson (which wasn't imlemented at all)

## [0.5.0] - 2022-06-03

### Added
Expand Down Expand Up @@ -62,7 +74,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

[unreleased]: https://github.com/Xray-App/xray-maven-plugin/compare/0.5.0...HEAD
[unreleased]: https://github.com/Xray-App/xray-maven-plugin/compare/0.6.0...HEAD
[0.6.0]: https://github.com/Xray-App/xray-maven-plugin/compare/0.5.0...0.6.0
[0.5.0]: https://github.com/Xray-App/xray-maven-plugin/compare/0.4.0...0.5.0
[0.4.0]: https://github.com/Xray-App/xray-maven-plugin/compare/0.3.0...0.4.0
[0.3.0]: https://github.com/Xray-App/xray-maven-plugin/compare/0.2.0...0.3.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Add the following dependency to your pom.xml, where the `<configuration>` is opt
<dependency>
<groupId>app.getxray</groupId>
<artifactId>xray-maven-plugin</artifactId>
<version>0.5.0</version>
<version>0.6.0</version>
<scope>test</scope>
<configuration>
<clientId>215FFD69FE46447280000000000</clientId>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>app.getxray</groupId>
<artifactId>xray-maven-plugin</artifactId>
<version>0.6.0-SNAPSHOT</version>
<version>0.6.0</version>
<packaging>maven-plugin</packaging>
<name>xray-maven-plugin</name>
<description>Maven plugin for interacting with Xray (server/datacenter and cloud), used in CI/CD for assisting in test automation flows, such as reporting test results back to Xray and, by consequence, Jira.</description>
<url>https://raw.githubusercontent.com/Xray-App/xray-maven-plugin</url>
<url>https://github.com/Xray-App/xray-maven-plugin</url>

<licenses>
<license>
Expand Down
41 changes: 33 additions & 8 deletions src/main/java/app/getxray/maven/plugin/xray/ImportResultsMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,21 @@ public class ImportResultsMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", required = true, readonly = true)
MavenProject project;

private void abortWithError(String message) {
getLog().error(message);
System.err.println(message);
System.exit(1);
}

public void execute() throws MojoExecutionException, MojoFailureException {
XrayResultsImporter xrayImporter;
JSONObject testExecInfo;
JSONObject testInfo;
JSONObject testExecInfo = null;
JSONObject testInfo = null;

//
String[] reportFiles;
File tempReportFile = new File(reportFile);
if (tempReportFile.isFile()){
if (tempReportFile.isFile()) {
// common case: import a given report file
reportFiles = new String[] { reportFile };
} else if (tempReportFile.isDirectory()){
Expand Down Expand Up @@ -142,7 +148,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getLog().debug(reportFiles[i]);
}
}


if (reportFiles.length == 0) {
abortWithError("no test report file(s) found: " + reportFile);
}
String response = null;

// submit one or more report files
Expand Down Expand Up @@ -181,12 +190,20 @@ public void execute() throws MojoExecutionException, MojoFailureException {
response = xrayImporter.submit(reportFormat, reportFile);
} else {
if (testInfoJson != null) {
testInfo = new JSONObject(new String(Files.readAllBytes(Paths.get(testInfoJson))));
if ((new File(testInfoJson)).isFile()) {
testInfo = new JSONObject(new String(Files.readAllBytes(Paths.get(testInfoJson))));
} else {
abortWithError("file doesnt exist: " + testInfoJson);
}
} else {
testInfo = new JSONObject();
}
if (testExecInfoJson != null) {
testExecInfo = new JSONObject(new String(Files.readAllBytes(Paths.get(testExecInfoJson))));
if ((new File(testExecInfoJson)).isFile()) {
testExecInfo = new JSONObject(new String(Files.readAllBytes(Paths.get(testExecInfoJson))));
} else {
abortWithError("file doesnt exist: " + testExecInfoJson);
}
} else {
testExecInfo = new JSONObject();
}
Expand Down Expand Up @@ -226,12 +243,20 @@ public void execute() throws MojoExecutionException, MojoFailureException {
response = xrayImporter.submit(reportFormat, reportFile);
} else {
if (testInfoJson != null) {
testInfo = new JSONObject(new String(Files.readAllBytes(Paths.get(testInfoJson))));
if ((new File(testInfoJson)).isFile()) {
testInfo = new JSONObject(new String(Files.readAllBytes(Paths.get(testInfoJson))));
} else {
abortWithError("file doesnt exist: " + testInfoJson);
}
} else {
testInfo = new JSONObject();
}
if (testExecInfoJson != null) {
testExecInfo = new JSONObject(new String(Files.readAllBytes(Paths.get(testExecInfoJson))));
if ((new File(testExecInfoJson)).isFile()) {
testExecInfo = new JSONObject(new String(Files.readAllBytes(Paths.get(testExecInfoJson))));
} else {
abortWithError("file doesnt exist: " + testExecInfoJson);
}
} else {
testExecInfo = new JSONObject();
}
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/app/getxray/xray/XrayResultsImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,14 @@ public String submitMultipartServerDC(String format, String reportFile, JSONObje
partName = "file";
}
try {
requestBody = new MultipartBody.Builder()
okhttp3.MultipartBody.Builder requestBodyBuilder = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(partName, reportFile, RequestBody.create(new File(reportFile), mediaType))
.addFormDataPart("info", "info.json", RequestBody.create(testExecInfo.toString(), MEDIA_TYPE_JSON))
.build();
.addFormDataPart("info", "info.json", RequestBody.create(testExecInfo.toString(), MEDIA_TYPE_JSON));
if (testInfo != null) {
requestBodyBuilder.addFormDataPart("testInfo", "testInfo.json", RequestBody.create(testInfo.toString(), MEDIA_TYPE_JSON));
}
requestBody = requestBodyBuilder.build();
} catch (Exception e1) {
e1.printStackTrace();
throw e1;
Expand Down Expand Up @@ -353,11 +356,14 @@ public String submitMultipartCloud(String format, String reportFile, JSONObject
HttpUrl.Builder builder = url.newBuilder();
MultipartBody requestBody = null;
try {
requestBody = new MultipartBody.Builder()
okhttp3.MultipartBody.Builder requestBodyBuilder = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("results", reportFile, RequestBody.create(new File(reportFile), mediaType))
.addFormDataPart("info", "info.json", RequestBody.create(testExecInfo.toString(), MEDIA_TYPE_JSON))
.build();
.addFormDataPart("info", "info.json", RequestBody.create(testExecInfo.toString(), MEDIA_TYPE_JSON));
if (testInfo != null) {
requestBodyBuilder.addFormDataPart("testInfo", "testInfo.json", RequestBody.create(testInfo.toString(), MEDIA_TYPE_JSON));
}
requestBody = requestBodyBuilder.build();
} catch (Exception e1) {
e1.printStackTrace();
throw e1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,40 @@ void junit_multipart(MavenExecutionResult result) throws IOException {
String testExecInfo = CommonUtils.readResourceFileForImportResults("XrayCloudIT/junit_multipart/testExecInfo.json");
String report = CommonUtils.readResourceFileForImportResults("XrayCloudIT/junit_multipart/junit.xml");

wm.verify(
postRequestedFor(urlPathEqualTo("/api/v2/import/execution/junit/multipart"))
.withHeader("Authorization", equalTo("Bearer " + TOKEN))
.withHeader("Content-Type", containing("multipart/form-data;"))
.withAnyRequestBodyPart(
aMultipart()
.withName("results")
.withHeader("Content-Type", containing("application/xml"))
.withBody(equalToXml(report)))
.withAnyRequestBodyPart(
aMultipart()
.withName("info")
.withHeader("Content-Type", containing("application/json"))
.withBody(equalToJson(testExecInfo)))

);
assertThat(result).isSuccessful();
}

@MavenTest
@MavenGoal("xray:import-results")
@SystemProperty(value = "xray.cloud", content = "true")
@SystemProperty(value = "xray.clientId", content = CLIENT_ID)
@SystemProperty(value = "xray.clientSecret", content = CLIENT_SECRET)
@SystemProperty(value = "xray.reportFormat", content = "junit")
@SystemProperty(value = "xray.reportFile", content = "junit.xml")
@SystemProperty(value = "xray.testExecInfoJson", content = "testExecInfo.json")
@SystemProperty(value = "xray.testInfoJson", content = "testInfo.json")
@SystemProperty(value = "xray.useInternalTestProxy", content = "true")
void junit_multipart_customize_test_issues(MavenExecutionResult result) throws IOException {
String testExecInfo = CommonUtils.readResourceFileForImportResults("XrayCloudIT/junit_multipart_customize_test_issues/testExecInfo.json");
String testInfo = CommonUtils.readResourceFileForImportResults("XrayCloudIT/junit_multipart_customize_test_issues/testInfo.json");
String report = CommonUtils.readResourceFileForImportResults("XrayCloudIT/junit_multipart_customize_test_issues/junit.xml");

wm.verify(
postRequestedFor(urlPathEqualTo("/api/v2/import/execution/junit/multipart"))
.withHeader("Authorization", equalTo("Bearer " + TOKEN))
Expand All @@ -224,12 +258,17 @@ void junit_multipart(MavenExecutionResult result) throws IOException {
.withHeader("Content-Type", containing("application/json"))
.withBody(equalToJson(testExecInfo))
)
.withAnyRequestBodyPart(
aMultipart()
.withName("testInfo")
.withHeader("Content-Type", containing("application/json"))
.withBody(equalToJson(testInfo))
)

);
assertThat(result).isSuccessful();
}


@MavenTest
@MavenGoal("xray:import-results")
@SystemProperty(value = "xray.cloud", content = "true")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,45 @@ void junit_multipart(MavenExecutionResult result) throws IOException {
assertThat(result).isSuccessful();
}

@MavenTest
@MavenGoal("xray:import-results")
@SystemProperty(value = "xray.cloud", content = "false")
@SystemProperty(value = "xray.jiraBaseUrl", content = "http://127.0.0.1:18080")
@SystemProperty(value = "xray.jiraUsername", content = "username")
@SystemProperty(value = "xray.jiraPassword", content = "password")
@SystemProperty(value = "xray.reportFormat", content = "junit")
@SystemProperty(value = "xray.reportFile", content = "junit.xml")
@SystemProperty(value = "xray.testExecInfoJson", content = "testExecInfo.json")
@SystemProperty(value = "xray.testInfoJson", content = "testInfo.json")
void junit_multipart_customize_test_issues(MavenExecutionResult result) throws IOException {
String testExecInfo = CommonUtils.readResourceFileForImportResults("XrayDatacenterIT/junit_multipart_customize_test_issues/testExecInfo.json");
String testInfo = CommonUtils.readResourceFileForImportResults("XrayDatacenterIT/junit_multipart_customize_test_issues/testInfo.json");
String report = CommonUtils.readResourceFileForImportResults("XrayDatacenterIT/junit_multipart/junit.xml");

wm.verify(
postRequestedFor(urlPathEqualTo("/rest/raven/2.0/import/execution/junit/multipart"))
.withBasicAuth(new BasicCredentials("username", "password"))
.withHeader("Content-Type", containing("multipart/form-data;"))
.withAnyRequestBodyPart(
aMultipart()
.withName("file")
.withHeader("Content-Type", containing("application/xml"))
.withBody(equalToXml(report)))
.withAnyRequestBodyPart(
aMultipart()
.withName("info")
.withHeader("Content-Type", containing("application/json"))
.withBody(equalToJson(testExecInfo)))
.withAnyRequestBodyPart(
aMultipart()
.withName("testInfo")
.withHeader("Content-Type", containing("application/json"))
.withBody(equalToJson(testInfo)))

);
assertThat(result).isSuccessful();
}

@MavenTest
@MavenGoal("xray:import-results")
@SystemProperty(value = "xray.cloud", content = "false")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="com.xpand.java.CalcTest" time="0.043" tests="4" errors="0" skipped="0" failures="0">
<properties>
<property name="java.runtime.name" value="Java(TM) SE Runtime Environment"/>
<property name="sun.boot.library.path" value="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib"/>
<property name="java.vm.version" value="25.121-b13"/>
<property name="user.country.format" value="PT"/>
<property name="gopherProxySet" value="false"/>
<property name="java.vm.vendor" value="Oracle Corporation"/>
<property name="maven.multiModuleProjectDirectory" value="/Users/smsf/exps/tutorial-java-junit-calc"/>
<property name="java.vendor.url" value="http://java.oracle.com/"/>
<property name="path.separator" value=":"/>
<property name="guice.disable.misplaced.annotation.check" value="true"/>
<property name="java.vm.name" value="Java HotSpot(TM) 64-Bit Server VM"/>
<property name="file.encoding.pkg" value="sun.io"/>
<property name="user.country" value="GB"/>
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="sun.os.patch.level" value="unknown"/>
<property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
<property name="user.dir" value="/Users/smsf/exps/tutorial-java-junit-calc"/>
<property name="java.runtime.version" value="1.8.0_121-b13"/>
<property name="java.awt.graphicsenv" value="sun.awt.CGraphicsEnvironment"/>
<property name="java.endorsed.dirs" value="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/endorsed"/>
<property name="os.arch" value="x86_64"/>
<property name="java.io.tmpdir" value="/var/folders/kp/jly0j3r16l31jvmj8l2cj1qm0000gn/T/"/>
<property name="line.separator" value="&#10;"/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="os.name" value="Mac OS X"/>
<property name="classworlds.conf" value="/usr/local/Cellar/maven/3.8.4/libexec/bin/m2.conf"/>
<property name="sun.jnu.encoding" value="UTF-8"/>
<property name="java.library.path" value="/Users/smsf/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:."/>
<property name="maven.conf" value="/usr/local/Cellar/maven/3.8.4/libexec/conf"/>
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.class.version" value="52.0"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="os.version" value="10.16"/>
<property name="library.jansi.path" value="/usr/local/Cellar/maven/3.8.4/libexec/lib/jansi-native"/>
<property name="http.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="user.home" value="/Users/smsf"/>
<property name="user.timezone" value="Europe/Lisbon"/>
<property name="java.awt.printerjob" value="sun.lwawt.macosx.CPrinterJob"/>
<property name="java.specification.version" value="1.8"/>
<property name="file.encoding" value="UTF-8"/>
<property name="user.name" value="smsf"/>
<property name="java.class.path" value="/usr/local/Cellar/maven/3.8.4/libexec/boot/plexus-classworlds-2.6.0.jar"/>
<property name="java.vm.specification.version" value="1.8"/>
<property name="sun.arch.data.model" value="64"/>
<property name="java.home" value="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre"/>
<property name="sun.java.command" value="org.codehaus.plexus.classworlds.launcher.Launcher test"/>
<property name="java.specification.vendor" value="Oracle Corporation"/>
<property name="user.language" value="en"/>
<property name="awt.toolkit" value="sun.lwawt.macosx.LWCToolkit"/>
<property name="java.vm.info" value="mixed mode"/>
<property name="java.version" value="1.8.0_121"/>
<property name="java.ext.dirs" value="/Users/smsf/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java"/>
<property name="sun.boot.class.path" value="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/classes"/>
<property name="java.vendor" value="Oracle Corporation"/>
<property name="maven.home" value="/usr/local/Cellar/maven/3.8.4/libexec"/>
<property name="file.separator" value="/"/>
<property name="java.vendor.url.bug" value="http://bugreport.sun.com/bugreport/"/>
<property name="sun.cpu.endian" value="little"/>
<property name="sun.io.unicode.encoding" value="UnicodeBig"/>
<property name="socksNonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="ftp.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="sun.cpu.isalist" value=""/>
</properties>
<testcase name="CanDoStuff" classname="com.xpand.java.CalcTest" time="0.005"/>
<testcase name="CanAddNumbers" classname="com.xpand.java.CalcTest" time="0"/>
<testcase name="CanSubtract" classname="com.xpand.java.CalcTest" time="0"/>
<testcase name="CanMultiply" classname="com.xpand.java.CalcTest" time="0"/>
</testsuite>
Loading

0 comments on commit b3d6660

Please sign in to comment.