Skip to content

Commit 970b6f9

Browse files
authored
Merge pull request #2 from Visual-Regression-Tracker/53-tests
tests are added
2 parents ef6e9a6 + d9a308c commit 970b6f9

File tree

13 files changed

+279
-98
lines changed

13 files changed

+279
-98
lines changed

.github/workflows/gradle.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,3 @@ jobs:
2424
run: chmod +x gradlew
2525
- name: Build with Gradle
2626
run: ./gradlew build
27-
- name: Codecov
28-
uses: codecov/[email protected]
29-

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ dependencies {
2828
implementation 'com.google.code.gson:gson:2.8.6'
2929
testImplementation group: 'org.testng', name: 'testng', version: '7.1.0'
3030
testImplementation 'commons-io:commons-io:2.6'
31+
testImplementation 'org.mockito:mockito-core:3.3.3'
32+
testImplementation "com.squareup.okhttp3:mockwebserver:4.7.2"
3133
}
3234

33-
test{
35+
test {
3436
useTestNG()
3537
}

lombok.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# This file is generated by the 'io.freefair.lombok' Gradle plugin
22
config.stopBubbling = true
3+
lombok.addLombokGeneratedAnnotation = true

src/main/java/io/visual_regression_tracker/sdk_java/BuildDTO.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/java/io/visual_regression_tracker/sdk_java/TestResultDTO.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.visual_regression_tracker.sdk_java;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
public enum TestRunStatus {
10+
@SerializedName("ok")
11+
OK,
12+
@SerializedName("approved")
13+
APPROVED,
14+
@SerializedName("failed")
15+
FAILED,
16+
@SerializedName("new")
17+
NEW,
18+
@SerializedName("unresolved")
19+
UNRESOLVED
20+
}

src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTracker.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package io.visual_regression_tracker.sdk_java;
22

33
import com.google.gson.Gson;
4+
import io.visual_regression_tracker.sdk_java.request.BuildRequest;
5+
import io.visual_regression_tracker.sdk_java.request.TestRunRequest;
6+
import io.visual_regression_tracker.sdk_java.response.BuildResponse;
7+
import io.visual_regression_tracker.sdk_java.response.TestRunResponse;
48
import okhttp3.*;
59

610
import java.io.IOException;
7-
import java.util.HashMap;
8-
import java.util.Map;
911

1012
public class VisualRegressionTracker {
1113
static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
14+
String apiKeyHeaderName = "apiKey";
15+
Gson gson = new Gson();
1216
VisualRegressionTrackerConfig visualRegressionTrackerConfig;
1317
String buildId;
1418
OkHttpClient client;
@@ -21,61 +25,62 @@ public VisualRegressionTracker(VisualRegressionTrackerConfig visualRegressionTra
2125

2226
void startBuild() throws IOException {
2327
if (this.buildId == null) {
24-
Map<String, String> data = new HashMap<>();
25-
data.put("projectId", this.visualRegressionTrackerConfig.projectId);
26-
data.put("branchName", this.visualRegressionTrackerConfig.branchName);
28+
BuildRequest newBuild = BuildRequest.builder()
29+
.branchName(this.visualRegressionTrackerConfig.branchName)
30+
.projectId(this.visualRegressionTrackerConfig.projectId)
31+
.build();
2732

28-
RequestBody body = RequestBody.create(new Gson().toJson(data), JSON);
33+
RequestBody body = RequestBody.create(gson.toJson(newBuild), JSON);
2934

3035
Request request = new Request.Builder()
3136
.url(this.visualRegressionTrackerConfig.apiUrl.concat("/builds"))
32-
.addHeader("apiKey", this.visualRegressionTrackerConfig.apiKey)
37+
.addHeader(apiKeyHeaderName, this.visualRegressionTrackerConfig.apiKey)
3338
.post(body)
3439
.build();
3540

3641
try (ResponseBody responseBody = client.newCall(request).execute().body()) {
37-
BuildDTO buildDTO = new Gson().fromJson(responseBody.string(), BuildDTO.class);
42+
BuildResponse buildDTO = new Gson().fromJson(responseBody.string(), BuildResponse.class);
3843
this.buildId = buildDTO.getId();
3944
}
4045
}
4146
}
4247

43-
TestResultDTO submitTestRun(String name, String imageBase64, TestRunOptions testRunOptions) throws IOException {
44-
Map<String, Object> data = new HashMap<>();
45-
data.put("projectId", this.visualRegressionTrackerConfig.projectId);
46-
data.put("buildId", this.buildId);
47-
data.put("name", name);
48-
data.put("imageBase64", imageBase64);
49-
data.put("os", testRunOptions.getOs());
50-
data.put("browser", testRunOptions.getBrowser());
51-
data.put("viewport", testRunOptions.getViewport());
52-
data.put("device", testRunOptions.getDevice());
53-
data.put("diffTollerancePercent", testRunOptions.getDiffTollerancePercent());
48+
TestRunResponse submitTestRun(String name, String imageBase64, TestRunOptions testRunOptions) throws IOException {
49+
TestRunRequest newTestRun = TestRunRequest.builder()
50+
.projectId(this.visualRegressionTrackerConfig.projectId)
51+
.buildId(this.buildId)
52+
.name(name)
53+
.imageBase64(imageBase64)
54+
.os(testRunOptions.getOs())
55+
.browser(testRunOptions.getBrowser())
56+
.viewport(testRunOptions.getViewport())
57+
.device(testRunOptions.getDevice())
58+
.diffTollerancePercent(testRunOptions.getDiffTollerancePercent())
59+
.build();
5460

55-
RequestBody body = RequestBody.create(new Gson().toJson(data), JSON);
61+
RequestBody body = RequestBody.create(gson.toJson(newTestRun), JSON);
5662

5763
Request request = new Request.Builder()
5864
.url(this.visualRegressionTrackerConfig.apiUrl.concat("/test"))
59-
.addHeader("apiKey", this.visualRegressionTrackerConfig.apiKey)
65+
.addHeader(apiKeyHeaderName, this.visualRegressionTrackerConfig.apiKey)
6066
.post(body)
6167
.build();
6268

63-
6469
try (ResponseBody responseBody = client.newCall(request).execute().body()) {
65-
return new Gson().fromJson(responseBody.string(), TestResultDTO.class);
70+
return gson.fromJson(responseBody.string(), TestRunResponse.class);
6671
}
6772
}
6873

6974
public void track(String name, String imageBase64, TestRunOptions testRunOptions) throws IOException {
7075
this.startBuild();
7176

72-
TestResultDTO testResultDTO = this.submitTestRun(name, imageBase64, testRunOptions);
77+
TestRunResponse testResultDTO = this.submitTestRun(name, imageBase64, testRunOptions);
7378

74-
if (testResultDTO.getStatus().equals("new")) {
79+
if (testResultDTO.getStatus().equals(TestRunStatus.NEW)) {
7580
throw new TestRunException("No baseline: ".concat(testResultDTO.getUrl()));
7681
}
7782

78-
if (testResultDTO.getStatus().equals("unresolved")) {
83+
if (testResultDTO.getStatus().equals(TestRunStatus.UNRESOLVED)) {
7984
throw new TestRunException("Difference found: ".concat(testResultDTO.getUrl()));
8085
}
8186
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.visual_regression_tracker.sdk_java.request;
2+
3+
import lombok.Builder;
4+
5+
@Builder
6+
public class BuildRequest {
7+
String projectId;
8+
String branchName;
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.visual_regression_tracker.sdk_java.request;
2+
3+
import lombok.Builder;
4+
5+
@Builder
6+
public class TestRunRequest {
7+
String projectId;
8+
String buildId;
9+
String name;
10+
String imageBase64;
11+
String os;
12+
String browser;
13+
String viewport;
14+
String device;
15+
Integer diffTollerancePercent;
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.visual_regression_tracker.sdk_java.response;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
@Builder
7+
@Getter
8+
public class BuildResponse {
9+
String id;
10+
}

0 commit comments

Comments
 (0)