Skip to content

Commit f67f11e

Browse files
authored
Issue #48 : Migrated to Java 11 (#54)
* Issue #48 : Migrated to Java 11
1 parent 5e29e68 commit f67f11e

File tree

6 files changed

+317
-192
lines changed

6 files changed

+317
-192
lines changed

.github/workflows/gradle.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v2
1919

20-
- name: Set up JDK 1.8
20+
- name: Set up JDK 11
2121
uses: actions/setup-java@v1
2222
with:
23-
java-version: 1.8
23+
java-version: 11
2424

2525
- name: Grant execute permission for gradlew
2626
run: chmod +x gradlew

build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ buildscript {
2525
}
2626

2727
dependencies {
28-
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
2928
implementation 'com.google.code.gson:gson:2.8.6'
3029
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
3130
testImplementation group: 'org.testng', name: 'testng', version: '7.3.0'

jitpack.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
jdk:
2+
- openjdk11
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,55 @@
11
package io.visual_regression_tracker.sdk_java;
22

3-
import java.io.IOException;
4-
import java.util.Optional;
5-
import java.util.concurrent.TimeUnit;
6-
73
import com.google.gson.Gson;
84
import io.visual_regression_tracker.sdk_java.request.BuildRequest;
95
import io.visual_regression_tracker.sdk_java.request.TestRunRequest;
106
import io.visual_regression_tracker.sdk_java.response.BuildResponse;
117
import io.visual_regression_tracker.sdk_java.response.TestRunResponse;
128
import lombok.extern.slf4j.Slf4j;
13-
import okhttp3.MediaType;
14-
import okhttp3.OkHttpClient;
15-
import okhttp3.Request;
16-
import okhttp3.RequestBody;
17-
import okhttp3.Response;
9+
10+
import java.io.IOException;
11+
import java.net.URI;
12+
import java.net.http.HttpClient;
13+
import java.net.http.HttpRequest;
14+
import java.net.http.HttpResponse;
15+
import java.time.Duration;
16+
17+
enum METHOD {
18+
GET,
19+
POST,
20+
PATCH
21+
}
1822

1923
@Slf4j
2024
public class VisualRegressionTracker {
2125

2226
private static final String TRACKER_NOT_STARTED = "Visual Regression Tracker has not been started";
2327
protected static final String API_KEY_HEADER = "apiKey";
24-
protected static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
2528
protected Gson gson;
2629
protected VisualRegressionTrackerConfig configuration;
2730
protected PathProvider paths;
2831
protected String buildId;
2932
protected String projectId;
30-
protected OkHttpClient client;
3133

3234
public VisualRegressionTracker(VisualRegressionTrackerConfig trackerConfig) {
3335
configuration = trackerConfig;
3436
paths = new PathProvider(trackerConfig.getApiUrl());
35-
client = new OkHttpClient.Builder()
36-
.connectTimeout(configuration.getHttpTimeoutInSeconds(), TimeUnit.SECONDS)
37-
.readTimeout(configuration.getHttpTimeoutInSeconds(), TimeUnit.SECONDS)
38-
.writeTimeout(configuration.getHttpTimeoutInSeconds(), TimeUnit.SECONDS)
39-
.build();
4037
gson = new Gson();
4138
}
4239

43-
public BuildResponse start() throws IOException {
40+
public BuildResponse start() throws IOException, InterruptedException {
4441
String projectName = configuration.getProject();
4542
String branch = configuration.getBranchName();
4643
String ciBuildId = configuration.getCiBuildId();
4744

4845
BuildRequest newBuild = BuildRequest.builder()
49-
.branchName(branch)
50-
.project(projectName)
51-
.ciBuildId(ciBuildId)
52-
.build();
53-
54-
RequestBody body = RequestBody.create(JSON, gson.toJson(newBuild));
55-
56-
Request request = new Request.Builder()
57-
.url(paths.getBuildPath())
58-
.addHeader(API_KEY_HEADER, configuration.getApiKey())
59-
.post(body)
60-
.build();
61-
46+
.branchName(branch)
47+
.project(projectName)
48+
.ciBuildId(ciBuildId)
49+
.build();
6250
log.info("Starting Visual Regression Tracker for project <{}> and branch <{}>", projectName, branch);
63-
64-
Response response = client.newCall(request).execute();
65-
51+
HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofString(gson.toJson(newBuild));
52+
HttpResponse<String> response = getResponse(METHOD.POST, paths.getBuildPath(), body);
6653
BuildResponse buildResponse = handleResponse(response, BuildResponse.class);
6754

6855
buildId = buildResponse.getId();
@@ -73,27 +60,22 @@ public BuildResponse start() throws IOException {
7360
return buildResponse;
7461
}
7562

76-
public void stop() throws IOException {
63+
public void stop() throws IOException, InterruptedException {
7764
if (!isStarted()) {
7865
throw new TestRunException(TRACKER_NOT_STARTED);
7966
}
8067

81-
Request request = new Request.Builder()
82-
.url(paths.getBuildPathForBuild(buildId))
83-
.addHeader(API_KEY_HEADER, configuration.getApiKey())
84-
.patch(RequestBody.create(JSON, ""))
85-
.build();
86-
8768
log.info("Stopping Visual Regression Tracker for buildId <{}>", buildId);
8869

89-
Response response = client.newCall(request).execute();
70+
HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofString("");
71+
HttpResponse<String> response = getResponse(METHOD.PATCH, paths.getBuildPathForBuild(buildId), body);
9072
handleResponse(response, Object.class);
9173

9274
log.info("Visual Regression Tracker is stopped for buildId <{}>", buildId);
9375
}
9476

9577
public TestRunResult track(String name, String imageBase64, TestRunOptions testRunOptions)
96-
throws IOException {
78+
throws IOException, InterruptedException {
9779
log.info("Tracking test run <{}> with options <{}> for buildId <{}>", name, testRunOptions, buildId);
9880
TestRunResponse testResultDTO = submitTestRun(name, imageBase64, testRunOptions);
9981

@@ -121,7 +103,7 @@ public TestRunResult track(String name, String imageBase64, TestRunOptions testR
121103
return new TestRunResult(testResultDTO, this.paths);
122104
}
123105

124-
public TestRunResult track(String name, String imageBase64) throws IOException {
106+
public TestRunResult track(String name, String imageBase64) throws IOException, InterruptedException {
125107
return track(name, imageBase64, TestRunOptions.builder().build());
126108
}
127109

@@ -130,46 +112,61 @@ protected boolean isStarted() {
130112
}
131113

132114
protected TestRunResponse submitTestRun(String name, String imageBase64,
133-
TestRunOptions testRunOptions) throws IOException {
115+
TestRunOptions testRunOptions) throws IOException, InterruptedException {
134116
if (!isStarted()) {
135117
throw new TestRunException(TRACKER_NOT_STARTED);
136118
}
137119

138120
TestRunRequest newTestRun = TestRunRequest.builder()
139-
.projectId(projectId)
140-
.buildId(buildId)
141-
.branchName(configuration.getBranchName())
142-
.name(name)
143-
.imageBase64(imageBase64)
144-
.os(testRunOptions.getOs())
145-
.browser(testRunOptions.getBrowser())
146-
.viewport(testRunOptions.getViewport())
147-
.device(testRunOptions.getDevice())
148-
.diffTollerancePercent(testRunOptions.getDiffTollerancePercent())
149-
.ignoreAreas(testRunOptions.getIgnoreAreas())
150-
.build();
151-
152-
RequestBody body = RequestBody.create(JSON, gson.toJson(newTestRun));
153-
154-
Request request = new Request.Builder()
155-
.url(paths.getTestRunPath())
156-
.addHeader(API_KEY_HEADER, configuration.getApiKey())
157-
.post(body)
158-
.build();
159-
160-
Response response = client.newCall(request).execute();
121+
.projectId(projectId)
122+
.buildId(buildId)
123+
.branchName(configuration.getBranchName())
124+
.name(name)
125+
.imageBase64(imageBase64)
126+
.os(testRunOptions.getOs())
127+
.browser(testRunOptions.getBrowser())
128+
.viewport(testRunOptions.getViewport())
129+
.device(testRunOptions.getDevice())
130+
.diffTollerancePercent(testRunOptions.getDiffTollerancePercent())
131+
.ignoreAreas(testRunOptions.getIgnoreAreas())
132+
.build();
133+
134+
HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofString(gson.toJson(newTestRun));
135+
HttpResponse<String> response = getResponse(METHOD.POST, paths.getTestRunPath(), body);
161136
return handleResponse(response, TestRunResponse.class);
162137
}
163138

164-
protected <T> T handleResponse(Response response, Class<T> classOfT) throws IOException {
165-
String responseBody = Optional.ofNullable(response.body())
166-
.orElseThrow(() -> new TestRunException("Cannot get response body"))
167-
.string();
139+
private HttpResponse<String> getResponse(METHOD method, String url, HttpRequest.BodyPublisher body) throws IOException, InterruptedException {
140+
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
141+
.timeout(Duration.ofSeconds(configuration.getHttpTimeoutInSeconds()))
142+
.header(API_KEY_HEADER, configuration.getApiKey())
143+
.header("Content-Type", "application/json;charset=UTF-8")
144+
.uri(URI.create(url));
145+
HttpRequest request = getRequest(method, body, requestBuilder);
146+
HttpResponse<String> response = HttpClient.newBuilder()
147+
.version(HttpClient.Version.HTTP_1_1)
148+
.connectTimeout(Duration.ofSeconds(configuration.getHttpTimeoutInSeconds()))
149+
.build()
150+
.send(request, HttpResponse.BodyHandlers.ofString());
151+
return response;
152+
}
153+
154+
protected HttpRequest getRequest(METHOD method, HttpRequest.BodyPublisher body, HttpRequest.Builder requestBuilder) {
155+
switch (method) {
156+
case PATCH:
157+
return requestBuilder.method("PATCH", body).build();
158+
case POST:
159+
return requestBuilder.POST(body).build();
160+
default:
161+
throw new UnsupportedOperationException("This method is not yet supported.");
162+
}
163+
}
168164

169-
if (!response.isSuccessful()) {
165+
protected <T> T handleResponse(HttpResponse<String> response, Class<T> classOfT) {
166+
String responseBody = response.body();
167+
if (!String.valueOf(response.statusCode()).startsWith("2")) {
170168
throw new TestRunException(responseBody);
171169
}
172-
173170
return gson.fromJson(responseBody, classOfT);
174171
}
175172
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package io.visual_regression_tracker.sdk_java;
2+
3+
import javax.net.ssl.SSLContext;
4+
import javax.net.ssl.SSLParameters;
5+
import javax.net.ssl.SSLSession;
6+
import java.net.Authenticator;
7+
import java.net.CookieHandler;
8+
import java.net.ProxySelector;
9+
import java.net.URI;
10+
import java.net.http.HttpClient;
11+
import java.net.http.HttpHeaders;
12+
import java.net.http.HttpRequest;
13+
import java.net.http.HttpResponse;
14+
import java.time.Duration;
15+
import java.util.Optional;
16+
import java.util.concurrent.CompletableFuture;
17+
import java.util.concurrent.Executor;
18+
19+
public class MockHttpClient extends HttpClient {
20+
21+
private final String body;
22+
private int statusCode;
23+
24+
public MockHttpClient(int statusCode, String body) {
25+
this.statusCode = statusCode;
26+
this.body = body;
27+
}
28+
29+
@Override
30+
public Optional<CookieHandler> cookieHandler() {
31+
return null;
32+
}
33+
34+
@Override
35+
public Optional<Duration> connectTimeout() {
36+
return null;
37+
}
38+
39+
@Override
40+
public Redirect followRedirects() {
41+
return null;
42+
}
43+
44+
@Override
45+
public Optional<ProxySelector> proxy() {
46+
return null;
47+
}
48+
49+
@Override
50+
public SSLContext sslContext() {
51+
return null;
52+
}
53+
54+
@Override
55+
public SSLParameters sslParameters() {
56+
return null;
57+
}
58+
59+
@Override
60+
public Optional<Authenticator> authenticator() {
61+
return null;
62+
}
63+
64+
@Override
65+
public Version version() {
66+
return null;
67+
}
68+
69+
@Override
70+
public Optional<Executor> executor() {
71+
return null;
72+
}
73+
74+
@Override
75+
public <T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler) {
76+
HttpResponse httpResponse = new HttpResponse() {
77+
@Override
78+
public int statusCode() {
79+
return statusCode;
80+
}
81+
82+
@Override
83+
public HttpRequest request() {
84+
return null;
85+
}
86+
87+
@Override
88+
public Optional<HttpResponse> previousResponse() {
89+
return Optional.empty();
90+
}
91+
92+
@Override
93+
public HttpHeaders headers() {
94+
return null;
95+
}
96+
97+
@Override
98+
public Object body() {
99+
return body;
100+
}
101+
102+
@Override
103+
public Optional<SSLSession> sslSession() {
104+
return Optional.empty();
105+
}
106+
107+
@Override
108+
public URI uri() {
109+
return null;
110+
}
111+
112+
@Override
113+
public Version version() {
114+
return null;
115+
}
116+
};
117+
return httpResponse;
118+
}
119+
120+
@Override
121+
public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler) {
122+
return null;
123+
}
124+
125+
@Override
126+
public <T> CompletableFuture<HttpResponse<T>>
127+
sendAsync(HttpRequest x, HttpResponse.BodyHandler<T> y, HttpResponse.PushPromiseHandler<T> z) {
128+
return null;
129+
}
130+
}

0 commit comments

Comments
 (0)