Skip to content

Commit 51c9398

Browse files
committed
java 8 migrated
1 parent a6d6a2d commit 51c9398

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ buildscript {
2424
}
2525

2626
dependencies {
27+
implementation 'com.squareup.okhttp3:okhttp:4.6.0'
2728
implementation 'com.google.code.gson:gson:2.8.6'
2829
testImplementation group: 'org.testng', name: 'testng', version: '7.1.0'
2930
testImplementation 'commons-io:commons-io:2.6'

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

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
11
package io.visual_regression_tracker.sdk_java;
22

33
import com.google.gson.Gson;
4+
import okhttp3.*;
45

56
import java.io.IOException;
6-
import java.net.URI;
7-
import java.net.http.HttpClient;
8-
import java.net.http.HttpRequest;
9-
import java.net.http.HttpResponse;
107
import java.util.HashMap;
118
import java.util.Map;
129

1310
public class VisualRegressionTracker {
11+
static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
1412
Config config;
1513
String buildId;
16-
HttpClient client;
14+
OkHttpClient client;
1715

1816
public VisualRegressionTracker(Config config) {
1917
this.config = config;
2018

21-
this.client = HttpClient.newHttpClient();
19+
this.client = new OkHttpClient();
2220
}
2321

24-
void startBuild() throws IOException, InterruptedException {
22+
void startBuild() throws IOException {
2523
if (this.buildId == null) {
2624
Map<String, String> data = new HashMap<>();
2725
data.put("projectId", this.config.projectId);
2826
data.put("branchName", this.config.branchName);
2927

30-
HttpRequest request = HttpRequest.newBuilder()
31-
.uri(URI.create(this.config.apiUrl.concat("/builds")))
32-
.header("apiKey", this.config.token)
33-
.header("Content-Type", "application/json")
34-
.POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(data)))
35-
.build();
28+
RequestBody body = RequestBody.create(new Gson().toJson(data), JSON);
3629

37-
HttpResponse<String> response = this.client.send(request, HttpResponse.BodyHandlers.ofString());
38-
BuildDTO buildDTO = new Gson().fromJson(response.body(), BuildDTO.class);
30+
Request request = new Request.Builder()
31+
.url(this.config.apiUrl.concat("/builds"))
32+
.addHeader("apiKey", this.config.token)
33+
.post(body)
34+
.build();
3935

40-
this.buildId = buildDTO.getId();
36+
try (ResponseBody responseBody = client.newCall(request).execute().body()) {
37+
BuildDTO buildDTO = new Gson().fromJson(responseBody.string(), BuildDTO.class);
38+
this.buildId = buildDTO.getId();
39+
}
4140
}
4241
}
4342

44-
TestResultDTO submitTestRun(TestRun testRun) throws IOException, InterruptedException {
43+
TestResultDTO submitTestRun(TestRun testRun) throws IOException {
4544
Map<String, Object> data = new HashMap<>();
4645
data.put("projectId", this.config.projectId);
4746
data.put("buildId", this.buildId);
@@ -53,30 +52,30 @@ TestResultDTO submitTestRun(TestRun testRun) throws IOException, InterruptedExce
5352
data.put("device", testRun.getDevice());
5453
data.put("diffTollerancePercent", testRun.getDiffTollerancePercent());
5554

55+
RequestBody body = RequestBody.create(new Gson().toJson(data), JSON);
5656

57-
HttpRequest request = HttpRequest.newBuilder()
58-
.uri(URI.create(this.config.apiUrl.concat("/test")))
59-
.header("apiKey", this.config.token)
60-
.header("Content-Type", "application/json")
61-
.POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(data)))
57+
Request request = new Request.Builder()
58+
.url(this.config.apiUrl.concat("/test"))
59+
.addHeader("apiKey", this.config.token)
60+
.post(body)
6261
.build();
6362

64-
HttpResponse<String> response = this.client.send(request, HttpResponse.BodyHandlers.ofString());
6563

66-
System.out.println(response.body());
67-
return new Gson().fromJson(response.body(), TestResultDTO.class);
64+
try (ResponseBody responseBody = client.newCall(request).execute().body()) {
65+
return new Gson().fromJson(responseBody.string(), TestResultDTO.class);
66+
}
6867
}
6968

70-
public void track(TestRun testRun) throws IOException, InterruptedException {
69+
public void track(TestRun testRun) throws IOException {
7170
this.startBuild();
7271

7372
TestResultDTO testResultDTO = this.submitTestRun(testRun);
7473

75-
if(testResultDTO.getStatus().equals("new")) {
74+
if (testResultDTO.getStatus().equals("new")) {
7675
throw new TestRunException("No baseline: ".concat(testResultDTO.getUrl()));
7776
}
7877

79-
if(testResultDTO.getStatus().equals("unresolved")) {
78+
if (testResultDTO.getStatus().equals("unresolved")) {
8079
throw new TestRunException("Difference found: ".concat(testResultDTO.getUrl()));
8180
}
8281
}

src/test/java/io/visual_regression_tracker/sdk_java/Example.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public class Example {
1111
Config config = Config.builder()
1212
.apiUrl("http://localhost:4200")
1313
.branchName("develop")
14-
.projectId("76f0c443-9811-4f4f-b1c2-7c01c5775d9a")
14+
.projectId("003f5fcf-6c5f-4f1f-a99f-82a697711382")
1515
.token("F5Z2H0H2SNMXZVHX0EA4YQM1MGDD")
1616
.build();
1717

1818
@Test
19-
public void asdas() throws IOException, InterruptedException {
19+
public void asdas() throws IOException {
2020
VisualRegressionTracker visualRegressionTracker = new VisualRegressionTracker(config);
2121

2222

0 commit comments

Comments
 (0)