Skip to content

Commit d4f41fa

Browse files
committed
404, 401 error handled
1 parent e687bd8 commit d4f41fa

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

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/VisualRegressionTracker.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
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;
13+
import okhttp3.ResponseBody;
914

1015
import java.io.IOException;
1116

@@ -39,13 +44,17 @@ void startBuild() throws IOException {
3944
.post(body)
4045
.build();
4146

42-
try (ResponseBody responseBody = client.newCall(request).execute().body()) {
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+
ResponseBody responseBody = response.body();
4355
BuildResponse buildDTO = new Gson().fromJson(responseBody.string(), BuildResponse.class);
4456
this.buildId = buildDTO.getId();
4557
this.projectId = buildDTO.getProjectId();
46-
} catch(Exception ex){
47-
System.out.println(ex.getMessage());
48-
throw ex;
4958
}
5059
}
5160
}

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class VisualRegressionTrackerTest {
2424
MockWebServer server;
2525
VisualRegressionTracker vrt;
2626
VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig(
27-
"http://localhost",
27+
"http://localhost:4200",
2828
"733c148e-ef70-4e6d-9ae5-ab22263697cc",
29-
"BAZ0EG0PRH4CRQPH19ZKAVADBP9E1",
29+
"XHGDZDFD3GMJDNM87JKEMP0JS1G5",
3030
"develop"
3131
);
3232

@@ -71,18 +71,33 @@ void shouldStartBuild() throws IOException, InterruptedException {
7171
}
7272

7373
@Test
74-
void shouldThrowExceptionIfProjectNotFound() throws IOException, InterruptedException {
75-
String projectId = "non-existing";
76-
BuildRequest buildRequest = BuildRequest.builder()
77-
.branchName(this.config.branchName)
78-
.project(this.config.project)
79-
.build();
74+
void shouldThrowExceptionIfProjectNotFound() throws IOException {
8075
server.enqueue(new MockResponse()
81-
.setHttp2ErrorCode(404)
76+
.setResponseCode(404)
8277
.setBody("{\r\n \"statusCode\": 404,\r\n \"message\": \"Project not found\"\r\n}"));
8378

84-
vrt.startBuild();
79+
String exceptionMessage = "";
80+
try {
81+
vrt.startBuild();
82+
} catch (TestRunException ex) {
83+
exceptionMessage = ex.getMessage();
84+
}
85+
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Project not found"));
86+
}
8587

88+
@Test
89+
void shouldThrowExceptionIfUnauthorized() throws IOException {
90+
server.enqueue(new MockResponse()
91+
.setResponseCode(401)
92+
.setBody("{\r\n \"statusCode\": 401,\r\n \"message\": \"Unauthorized\"\r\n}"));
93+
94+
String exceptionMessage = "";
95+
try {
96+
vrt.startBuild();
97+
} catch (TestRunException ex) {
98+
exceptionMessage = ex.getMessage();
99+
}
100+
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Unauthorized"));
86101
}
87102

88103
@Test
@@ -144,7 +159,6 @@ public Object[][] shouldTrackThrowExceptionCases() {
144159
};
145160
}
146161

147-
148162
@Test(dataProvider = "shouldTrackThrowExceptionCases")
149163
public void shouldTrackThrowException(TestRunResponse testRunResponse, String expectedExceptionMessage) throws IOException {
150164
VisualRegressionTracker vrtMocked = Mockito.mock(VisualRegressionTracker.class);

0 commit comments

Comments
 (0)