Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.lambdatest</groupId>
<artifactId>lambdatest-java-sdk</artifactId>
<version>1.0.24-beta.1</version>
<version>1.0.25-beta.1</version>
<name>lambdatest-java-sdk</name>
<description>LambdaTest SDK in Java</description>
<url>https://www.lambdatest.com</url>
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/github/lambdatest/SmartUIConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.github.lambdatest;

import io.github.lambdatest.models.PdfThreshold;

import java.util.Map;

/**
* Configuration class for SmartUI operations
* Includes essential SmartUI CLI configuration options
Expand All @@ -16,6 +20,9 @@ public class SmartUIConfig {
private String configFile;
private String testType;
private String[] pdfNames;
private Double approvalThreshold;
private Double rejectionThreshold;
private Map<String, PdfThreshold> pdfThresholds;

public String getTestType() {
return testType;
Expand Down Expand Up @@ -76,6 +83,23 @@ public SmartUIConfig withPdfNames(String[] pdfNames) {
this.pdfNames = pdfNames;
return this;
}

// Number so int, long, float and double literals are all accepted; null leaves the project default
public SmartUIConfig withApprovalThreshold(Number approvalThreshold) {
this.approvalThreshold = approvalThreshold == null ? null : approvalThreshold.doubleValue();
return this;
}

public SmartUIConfig withRejectionThreshold(Number rejectionThreshold) {
this.rejectionThreshold = rejectionThreshold == null ? null : rejectionThreshold.doubleValue();
return this;
}

// keys are the PDF names as uploaded (file name, or the matching pdfNames entry)
public SmartUIConfig withPdfThresholds(Map<String, PdfThreshold> pdfThresholds) {
this.pdfThresholds = pdfThresholds;
return this;
}

public int getPort() {
return port;
Expand Down Expand Up @@ -108,6 +132,18 @@ public String getConfigFile() {
public String[] getPdfNames() {
return pdfNames;
}

public Double getApprovalThreshold() {
return approvalThreshold;
}

public Double getRejectionThreshold() {
return rejectionThreshold;
}

public Map<String, PdfThreshold> getPdfThresholds() {
return pdfThresholds;
}

/**
* Validate the configuration
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/io/github/lambdatest/SmartUIPdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.lambdatest.models.FormattedResults;
import io.github.lambdatest.models.PdfResult;
import io.github.lambdatest.models.PdfPage;
import io.github.lambdatest.models.PdfThreshold;
import io.github.lambdatest.models.Screenshot;
import io.github.lambdatest.utils.LoggerUtil;
import io.github.lambdatest.utils.SmartUIUtil;
Expand All @@ -25,13 +26,19 @@ public class SmartUIPdf {
public final boolean fetchResults;
public final String projectToken;
public final String[] pdfNames;
public final Double approvalThreshold;
public final Double rejectionThreshold;
public final Map<String, PdfThreshold> pdfThresholds;
private static final Logger log = LoggerUtil.createLogger("lambdatest-java-sdk");

public SmartUIPdf(SmartUIConfig config) {
this.smartUIUtils = new SmartUIUtil();
this.buildName = config.getBuildName();
this.fetchResults = config.getFetchResults();
this.pdfNames = config.getPdfNames();
this.approvalThreshold = config.getApprovalThreshold();
this.rejectionThreshold = config.getRejectionThreshold();
this.pdfThresholds = config.getPdfThresholds();

if (config.getProjectToken() == null || config.getProjectToken().trim().isEmpty()) {
throw new IllegalArgumentException("Project token is required");
Expand Down Expand Up @@ -71,7 +78,8 @@ public FormattedResults uploadPDF(String path) throws Exception {
}

try {
UploadPDFResponse response = smartUIUtils.postPDFToSmartUI(pdfFiles, this.projectToken, this.buildName, this.pdfNames);
UploadPDFResponse response = smartUIUtils.postPDFToSmartUI(pdfFiles, this.projectToken, this.buildName, this.pdfNames,
this.approvalThreshold, this.rejectionThreshold, this.pdfThresholds);

if (this.fetchResults) {
BuildScreenshotsResponse screenshotsResponse = smartUIUtils.getBuildScreenshots(response.getProjectId(), response.getBuildId(), this.projectToken);
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/io/github/lambdatest/models/PdfThreshold.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.lambdatest.models;

public class PdfThreshold {
// boxed so an unset side stays absent from the request and falls back server-side; 0 is a real value
private Double approval;
private Double rejection;

public PdfThreshold() {}

// Number so callers can pass int, long, float or double literals alike
public PdfThreshold(Number approval, Number rejection) {
this.approval = toDouble(approval);
this.rejection = toDouble(rejection);
}

public static PdfThreshold approval(Number approval) {
return new PdfThreshold(approval, null);
}

public static PdfThreshold rejection(Number rejection) {
return new PdfThreshold(null, rejection);
}

public Double getApproval() {
return approval;
}

public void setApproval(Number approval) {
this.approval = toDouble(approval);
}

public Double getRejection() {
return rejection;
}

public void setRejection(Number rejection) {
this.rejection = toDouble(rejection);
}

static Double toDouble(Number value) {
return value == null ? null : value.doubleValue();
}
}
14 changes: 14 additions & 0 deletions src/main/java/io/github/lambdatest/utils/HttpClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ private boolean isValidNumber(String value) {
}

public String uploadPDFs(String url, List<File> pdfFiles, String projectToken, String buildName, String[] pdfNames) throws IOException {
return uploadPDFs(url, pdfFiles, projectToken, buildName, pdfNames, null, null, null);
}

public String uploadPDFs(String url, List<File> pdfFiles, String projectToken, String buildName, String[] pdfNames,
Double approvalThreshold, Double rejectionThreshold, String thresholdsJson) throws IOException {
HttpPost uploadRequest = new HttpPost(url);
uploadRequest.setHeader("Authorization", "Basic " + projectToken);

Expand All @@ -441,6 +446,15 @@ public String uploadPDFs(String url, List<File> pdfFiles, String projectToken, S
if (buildName != null && !buildName.isEmpty() && !buildName.trim().isEmpty()) {
builder.addTextBody("buildName", buildName);
}
if (approvalThreshold != null) {
builder.addTextBody("approvalThreshold", approvalThreshold.toString());
}
if (rejectionThreshold != null) {
builder.addTextBody("rejectionThreshold", rejectionThreshold.toString());
}
if (thresholdsJson != null && !thresholdsJson.isEmpty()) {
builder.addTextBody("thresholds", thresholdsJson, ContentType.APPLICATION_JSON);
}

for (File pdfFile : pdfFiles) {
log.info("Adding PDF file: " + pdfFile.getName());
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/io/github/lambdatest/utils/SmartUIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public String postSnapshot(Object snapshotDOM, Map<String, Object> options, Stri
}

public UploadPDFResponse postPDFToSmartUI(List<File> pdfFiles, String projectToken, String buildName, String[] pdfNames) throws Exception {
return postPDFToSmartUI(pdfFiles, projectToken, buildName, pdfNames, null, null, null);
}

public UploadPDFResponse postPDFToSmartUI(List<File> pdfFiles, String projectToken, String buildName, String[] pdfNames,
Double approvalThreshold, Double rejectionThreshold,
Map<String, PdfThreshold> pdfThresholds) throws Exception {
UploadPDFResponse uploadResponse;
try {
if (pdfFiles == null || pdfFiles.isEmpty()) {
Expand All @@ -93,7 +99,10 @@ public UploadPDFResponse postPDFToSmartUI(List<File> pdfFiles, String projectTok

log.info("Uploading PDFs to SmartUI. Count: " + pdfFiles.size());

String responseString = httpClient.uploadPDFs(url, pdfFiles, projectToken, buildName, pdfNames);
// gson drops null sides, so an unset approval/rejection never reaches the server as 0
String thresholdsJson = (pdfThresholds == null || pdfThresholds.isEmpty()) ? null : gson.toJson(pdfThresholds);
String responseString = httpClient.uploadPDFs(url, pdfFiles, projectToken, buildName, pdfNames,
approvalThreshold, rejectionThreshold, thresholdsJson);
uploadResponse = gson.fromJson(responseString, UploadPDFResponse.class);

if (uploadResponse == null) {
Expand Down