Skip to content

Commit d912bbc

Browse files
authored
Merge pull request #11 from Visual-Regression-Tracker/77-exceptions-handle
77 exceptions handle
2 parents e341d92 + f8c79fc commit d912bbc

File tree

7 files changed

+109
-57
lines changed

7 files changed

+109
-57
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group 'io.visual-regression-tracker.sdk-java'
2-
version '2.0.0'
2+
version '3.1.0'
33

44
apply plugin: 'java'
55
apply plugin: "io.freefair.lombok"

lombok.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
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/TestRunOptions.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
@Builder
77
@Getter
88
public class TestRunOptions {
9-
String os;
10-
String browser;
11-
String viewport;
12-
String device;
9+
private final String os;
10+
private final String browser;
11+
private final String viewport;
12+
private final String device;
1313
@Builder.Default
14-
Integer diffTollerancePercent = 1;
14+
private final Integer diffTollerancePercent = 1;
1515
}

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

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
import io.visual_regression_tracker.sdk_java.request.TestRunRequest;
66
import io.visual_regression_tracker.sdk_java.response.BuildResponse;
77
import io.visual_regression_tracker.sdk_java.response.TestRunResponse;
8-
import okhttp3.*;
8+
import okhttp3.MediaType;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.Request;
11+
import okhttp3.RequestBody;
12+
import okhttp3.Response;
913

1014
import java.io.IOException;
15+
import java.util.Optional;
1116

1217
public class VisualRegressionTracker {
13-
static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
18+
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
1419
String apiKeyHeaderName = "apiKey";
1520
Gson gson = new Gson();
1621
VisualRegressionTrackerConfig visualRegressionTrackerConfig;
@@ -24,34 +29,46 @@ public VisualRegressionTracker(VisualRegressionTrackerConfig visualRegressionTra
2429
this.client = new OkHttpClient();
2530
}
2631

27-
void startBuild() throws IOException {
32+
protected void startBuild() throws IOException {
2833
if (this.buildId == null) {
2934
BuildRequest newBuild = BuildRequest.builder()
30-
.branchName(this.visualRegressionTrackerConfig.branchName)
31-
.project(this.visualRegressionTrackerConfig.project)
35+
.branchName(this.visualRegressionTrackerConfig.getBranchName())
36+
.project(this.visualRegressionTrackerConfig.getProject())
3237
.build();
3338

3439
RequestBody body = RequestBody.create(gson.toJson(newBuild), JSON);
3540

3641
Request request = new Request.Builder()
37-
.url(this.visualRegressionTrackerConfig.apiUrl.concat("/builds"))
38-
.addHeader(apiKeyHeaderName, this.visualRegressionTrackerConfig.apiKey)
42+
.url(this.visualRegressionTrackerConfig.getApiUrl().concat("/builds"))
43+
.addHeader(apiKeyHeaderName, this.visualRegressionTrackerConfig.getApiKey())
3944
.post(body)
4045
.build();
4146

42-
try (ResponseBody responseBody = client.newCall(request).execute().body()) {
43-
BuildResponse buildDTO = new Gson().fromJson(responseBody.string(), BuildResponse.class);
44-
this.buildId = buildDTO.getId();
45-
this.projectId = buildDTO.getProjectId();
47+
try (Response response = client.newCall(request).execute()) {
48+
if (response.code() == 404) {
49+
throw new TestRunException("Project not found");
50+
}
51+
if (response.code() == 401) {
52+
throw new TestRunException("Unauthorized");
53+
}
54+
55+
String responseBody = Optional.ofNullable(response.body())
56+
.orElseThrow(() -> new TestRunException("Cannot get response body"))
57+
.string();
58+
BuildResponse buildDTO = gson.fromJson(responseBody, BuildResponse.class);
59+
this.buildId = Optional.ofNullable(buildDTO.getId())
60+
.orElseThrow(() -> new TestRunException("Build id is null"));
61+
this.projectId = Optional.ofNullable(buildDTO.getProjectId())
62+
.orElseThrow(() -> new TestRunException("Project id is null"));
4663
}
4764
}
4865
}
4966

50-
TestRunResponse submitTestRun(String name, String imageBase64, TestRunOptions testRunOptions) throws IOException {
67+
protected TestRunResponse submitTestRun(String name, String imageBase64, TestRunOptions testRunOptions) throws IOException {
5168
TestRunRequest newTestRun = TestRunRequest.builder()
5269
.projectId(this.projectId)
5370
.buildId(this.buildId)
54-
.branchName(this.visualRegressionTrackerConfig.branchName)
71+
.branchName(this.visualRegressionTrackerConfig.getBranchName())
5572
.name(name)
5673
.imageBase64(imageBase64)
5774
.os(testRunOptions.getOs())
@@ -64,13 +81,16 @@ TestRunResponse submitTestRun(String name, String imageBase64, TestRunOptions te
6481
RequestBody body = RequestBody.create(gson.toJson(newTestRun), JSON);
6582

6683
Request request = new Request.Builder()
67-
.url(this.visualRegressionTrackerConfig.apiUrl.concat("/test-runs"))
68-
.addHeader(apiKeyHeaderName, this.visualRegressionTrackerConfig.apiKey)
84+
.url(this.visualRegressionTrackerConfig.getApiUrl().concat("/test-runs"))
85+
.addHeader(apiKeyHeaderName, this.visualRegressionTrackerConfig.getApiKey())
6986
.post(body)
7087
.build();
7188

72-
try (ResponseBody responseBody = client.newCall(request).execute().body()) {
73-
return gson.fromJson(responseBody.string(), TestRunResponse.class);
89+
try (Response response = client.newCall(request).execute()) {
90+
String responseBody = Optional.ofNullable(response.body())
91+
.orElseThrow(() -> new TestRunException("Cannot get response body"))
92+
.string();
93+
return gson.fromJson(responseBody, TestRunResponse.class);
7494
}
7595
}
7696

@@ -79,11 +99,14 @@ public void track(String name, String imageBase64, TestRunOptions testRunOptions
7999

80100
TestRunResponse testResultDTO = this.submitTestRun(name, imageBase64, testRunOptions);
81101

82-
if (testResultDTO.getStatus().equals(TestRunStatus.NEW)) {
102+
TestRunStatus status = Optional.ofNullable(testResultDTO.getStatus())
103+
.orElseThrow(() -> new TestRunException("Status is null"));
104+
105+
if (status.equals(TestRunStatus.NEW)) {
83106
throw new TestRunException("No baseline: ".concat(testResultDTO.getUrl()));
84107
}
85108

86-
if (testResultDTO.getStatus().equals(TestRunStatus.UNRESOLVED)) {
109+
if (status.equals(TestRunStatus.UNRESOLVED)) {
87110
throw new TestRunException("Difference found: ".concat(testResultDTO.getUrl()));
88111
}
89112
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package io.visual_regression_tracker.sdk_java;
22

33
import lombok.AllArgsConstructor;
4+
import lombok.Data;
45

6+
@Data
57
@AllArgsConstructor
68
public class VisualRegressionTrackerConfig {
7-
String apiUrl;
8-
String project;
9-
String apiKey;
10-
String branchName;
9+
private String apiUrl;
10+
private String project;
11+
private String apiKey;
12+
private String branchName;
1113
}

src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
@Builder
88
@Getter
99
public class TestRunResponse {
10-
String url;
11-
TestRunStatus status;
12-
// not used for now
13-
// int pixelMisMatchCount;
14-
// float diffPercent;
15-
// float diffTollerancePercent;
10+
private final String url;
11+
private final TestRunStatus status;
1612
}

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

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,98 @@
1212
import org.hamcrest.CoreMatchers;
1313
import org.hamcrest.MatcherAssert;
1414
import org.mockito.Mockito;
15-
import org.testng.annotations.AfterTest;
16-
import org.testng.annotations.BeforeTest;
15+
import org.testng.annotations.AfterMethod;
16+
import org.testng.annotations.BeforeMethod;
1717
import org.testng.annotations.DataProvider;
1818
import org.testng.annotations.Test;
1919

2020
import java.io.IOException;
2121

2222
public class VisualRegressionTrackerTest {
23-
Gson gson = new Gson();
24-
MockWebServer server;
25-
VisualRegressionTracker vrt;
26-
VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig(
23+
private final Gson gson = new Gson();
24+
private final VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig(
2725
"http://localhost",
2826
"733c148e-ef70-4e6d-9ae5-ab22263697cc",
29-
"BAZ0EG0PRH4CRQPH19ZKAVADBP9E1",
27+
"XHGDZDFD3GMJDNM87JKEMP0JS1G5",
3028
"develop"
3129
);
30+
private MockWebServer server;
31+
private VisualRegressionTracker vrt;
3232

3333
@SneakyThrows
34-
@BeforeTest
35-
void setup() {
34+
@BeforeMethod
35+
public void setup() {
3636
server = new MockWebServer();
3737
server.start();
3838

3939
// target to mock server
40-
this.config.apiUrl = server.url("/").toString();
40+
this.config.setApiUrl(server.url("/").toString());
4141
vrt = new VisualRegressionTracker(config);
4242
}
4343

4444
@SneakyThrows
45-
@AfterTest
46-
void tearDown() {
45+
@AfterMethod
46+
public void tearDown() {
4747
server.shutdown();
4848
}
4949

5050
@Test
51-
void shouldStartBuild() throws IOException, InterruptedException {
51+
public void shouldStartBuild() throws IOException, InterruptedException {
5252
String buildId = "123123";
5353
String projectId = "projectId";
5454
BuildRequest buildRequest = BuildRequest.builder()
55-
.branchName(this.config.branchName)
56-
.project(this.config.project)
55+
.branchName(this.config.getBranchName())
56+
.project(this.config.getProject())
5757
.build();
5858
BuildResponse buildResponse = BuildResponse.builder()
5959
.id(buildId)
6060
.projectId(projectId)
6161
.build();
62-
server.enqueue(new MockResponse().setBody(gson.toJson(buildResponse)));
62+
server.enqueue(new MockResponse()
63+
.setResponseCode(200)
64+
.setBody(gson.toJson(buildResponse)));
6365

6466
vrt.startBuild();
6567

6668
RecordedRequest request = server.takeRequest();
67-
MatcherAssert.assertThat(request.getHeader(vrt.apiKeyHeaderName), CoreMatchers.is(config.apiKey));
69+
MatcherAssert.assertThat(request.getHeader(vrt.apiKeyHeaderName), CoreMatchers.is(config.getApiKey()));
6870
MatcherAssert.assertThat(request.getBody().readUtf8(), CoreMatchers.is(gson.toJson(buildRequest)));
6971
MatcherAssert.assertThat(vrt.buildId, CoreMatchers.is(buildId));
7072
MatcherAssert.assertThat(vrt.projectId, CoreMatchers.is(projectId));
7173
}
7274

7375
@Test
74-
void shouldSubmitTestRun() throws IOException, InterruptedException {
76+
public void shouldThrowExceptionIfProjectNotFound() throws IOException {
77+
server.enqueue(new MockResponse()
78+
.setResponseCode(404)
79+
.setBody("{\r\n \"statusCode\": 404,\r\n \"message\": \"Project not found\"\r\n}"));
80+
81+
String exceptionMessage = "";
82+
try {
83+
vrt.startBuild();
84+
} catch (TestRunException ex) {
85+
exceptionMessage = ex.getMessage();
86+
}
87+
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Project not found"));
88+
}
89+
90+
@Test
91+
public void shouldThrowExceptionIfUnauthorized() throws IOException {
92+
server.enqueue(new MockResponse()
93+
.setResponseCode(401)
94+
.setBody("{\r\n \"statusCode\": 401,\r\n \"message\": \"Unauthorized\"\r\n}"));
95+
96+
String exceptionMessage = "";
97+
try {
98+
vrt.startBuild();
99+
} catch (TestRunException ex) {
100+
exceptionMessage = ex.getMessage();
101+
}
102+
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Unauthorized"));
103+
}
104+
105+
@Test
106+
public void shouldSubmitTestRun() throws IOException, InterruptedException {
75107
String buildId = "123123";
76108
String projectId = "projectId";
77109
String name = "Test name";
@@ -85,7 +117,7 @@ void shouldSubmitTestRun() throws IOException, InterruptedException {
85117
.build();
86118
TestRunRequest testRunRequest = TestRunRequest.builder()
87119
.projectId(projectId)
88-
.branchName(config.branchName)
120+
.branchName(config.getBranchName())
89121
.buildId(buildId)
90122
.name(name)
91123
.imageBase64(imageBase64)
@@ -100,11 +132,12 @@ void shouldSubmitTestRun() throws IOException, InterruptedException {
100132
.build();
101133
server.enqueue(new MockResponse().setBody(gson.toJson(testRunResponse)));
102134
vrt.buildId = buildId;
135+
vrt.projectId = projectId;
103136

104137
TestRunResponse result = vrt.submitTestRun(name, imageBase64, testRunOptions);
105138

106139
RecordedRequest request = server.takeRequest();
107-
MatcherAssert.assertThat(request.getHeader(vrt.apiKeyHeaderName), CoreMatchers.is(config.apiKey));
140+
MatcherAssert.assertThat(request.getHeader(vrt.apiKeyHeaderName), CoreMatchers.is(config.getApiKey()));
108141
MatcherAssert.assertThat(request.getBody().readUtf8(), CoreMatchers.is(gson.toJson(testRunRequest)));
109142
MatcherAssert.assertThat(gson.toJson(result), CoreMatchers.is(gson.toJson(testRunResponse)));
110143
}
@@ -129,7 +162,6 @@ public Object[][] shouldTrackThrowExceptionCases() {
129162
};
130163
}
131164

132-
133165
@Test(dataProvider = "shouldTrackThrowExceptionCases")
134166
public void shouldTrackThrowException(TestRunResponse testRunResponse, String expectedExceptionMessage) throws IOException {
135167
VisualRegressionTracker vrtMocked = Mockito.mock(VisualRegressionTracker.class);

0 commit comments

Comments
 (0)