Skip to content

Commit 655d7ed

Browse files
authored
VRT will return build statistics on stop. (#59)
* VRT willl return build statistics on stop.
1 parent f67f11e commit 655d7ed

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public BuildResponse start() throws IOException, InterruptedException {
6060
return buildResponse;
6161
}
6262

63-
public void stop() throws IOException, InterruptedException {
63+
public BuildResponse stop() throws IOException, InterruptedException {
6464
if (!isStarted()) {
6565
throw new TestRunException(TRACKER_NOT_STARTED);
6666
}
@@ -69,9 +69,10 @@ public void stop() throws IOException, InterruptedException {
6969

7070
HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofString("");
7171
HttpResponse<String> response = getResponse(METHOD.PATCH, paths.getBuildPathForBuild(buildId), body);
72-
handleResponse(response, Object.class);
72+
BuildResponse vrtStopResponse = handleResponse(response, BuildResponse.class);
7373

7474
log.info("Visual Regression Tracker is stopped for buildId <{}>", buildId);
75+
return vrtStopResponse;
7576
}
7677

7778
public TestRunResult track(String name, String imageBase64, TestRunOptions testRunOptions)

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

+9
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ public class BuildResponse {
99
private final String id;
1010
private final String projectId;
1111
private final String ciBuildId;
12+
private final int number;
13+
private final String branchName;
14+
private final String status;
15+
private final String userId;
16+
private final int passedCount;
17+
private final int failedCount;
18+
private final int unresolvedCount;
19+
private final boolean merge;
20+
private final boolean isRunning;
1221
}

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

+28-19
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,30 @@ public class VisualRegressionTrackerTest {
4646
private final static String CI_BUILD_ID = "123456789";
4747
private final static String NAME = "Test name";
4848
private final static String IMAGE_BASE_64 = "image";
49-
private final static int HTTP_TIMOUT = 1;
49+
private final static int HTTP_TIMEOUT = 1;
5050

5151
private final Gson gson = new Gson();
5252

5353
private VisualRegressionTrackerConfig config;
54-
private MockWebServer server;
54+
private MockWebServer mockWebServer;
5555
private VisualRegressionTracker vrt;
5656
private VisualRegressionTracker vrtMocked;
5757

5858
@SneakyThrows
5959
@BeforeMethod
6060
public void setup() {
61-
server = new MockWebServer();
62-
server.start();
61+
mockWebServer = new MockWebServer();
62+
mockWebServer.start();
6363

6464
// target to mock server
6565
config = new VisualRegressionTrackerConfig(
66-
server.url("/").toString(),
66+
mockWebServer.url("/").toString(),
6767
"733c148e-ef70-4e6d-9ae5-ab22263697cc",
6868
"XHGDZDFD3GMJDNM87JKEMP0JS1G5",
6969
"develop",
7070
false,
7171
CI_BUILD_ID,
72-
HTTP_TIMOUT);
72+
HTTP_TIMEOUT);
7373
vrt = new VisualRegressionTracker(config);
7474
vrtMocked = mock(VisualRegressionTracker.class);
7575
vrtMocked.paths = new PathProvider("baseApiUrl");
@@ -78,7 +78,7 @@ public void setup() {
7878
@SneakyThrows
7979
@AfterMethod
8080
public void tearDown() {
81-
server.shutdown();
81+
mockWebServer.shutdown();
8282
reset(vrtMocked);
8383
}
8484

@@ -94,13 +94,13 @@ public void shouldStartBuild() throws IOException, InterruptedException {
9494
.projectId(PROJECT_ID)
9595
.ciBuildId(CI_BUILD_ID)
9696
.build();
97-
server.enqueue(new MockResponse()
97+
mockWebServer.enqueue(new MockResponse()
9898
.setResponseCode(200)
9999
.setBody(gson.toJson(buildResponse)));
100100

101101
BuildResponse result = vrt.start();
102102

103-
RecordedRequest recordedRequest = server.takeRequest();
103+
RecordedRequest recordedRequest = mockWebServer.takeRequest();
104104
assertThat(recordedRequest.getHeader(VisualRegressionTracker.API_KEY_HEADER), is(config.getApiKey()));
105105
assertThat(recordedRequest.getBody().readUtf8(), is(gson.toJson(buildRequest)));
106106
assertThat(vrt.buildId, is(BUILD_ID));
@@ -116,17 +116,25 @@ public void shouldStopBuild() throws IOException, InterruptedException {
116116
vrt.projectId = PROJECT_ID;
117117
BuildResponse buildResponse = BuildResponse.builder()
118118
.id(BUILD_ID)
119+
.passedCount(1)
120+
.unresolvedCount(2)
121+
.isRunning(false)
122+
.status("unresolved")
119123
.build();
120-
server.enqueue(new MockResponse()
124+
mockWebServer.enqueue(new MockResponse()
121125
.setResponseCode(200)
122126
.setBody(gson.toJson(buildResponse)));
123127

124-
vrt.stop();
128+
BuildResponse actualBuildResponse = vrt.stop();
125129

126-
RecordedRequest recordedRequest = server.takeRequest();
130+
RecordedRequest recordedRequest = mockWebServer.takeRequest();
127131
assertThat(recordedRequest.getMethod(), is("PATCH"));
128132
assertThat(recordedRequest.getHeader(VisualRegressionTracker.API_KEY_HEADER), is(config.getApiKey()));
129133
assertThat(Objects.requireNonNull(recordedRequest.getRequestUrl()).encodedPath(), containsString(BUILD_ID));
134+
assertThat(actualBuildResponse.isRunning(), is(false));
135+
assertThat(actualBuildResponse.getStatus(), containsString("unresolved"));
136+
assertThat(actualBuildResponse.getPassedCount(), is(1));
137+
assertThat(actualBuildResponse.getUnresolvedCount(), is(2));
130138
}
131139

132140
@Test(expectedExceptions = TestRunException.class,
@@ -166,13 +174,13 @@ public void shouldSubmitTestRun() throws IOException, InterruptedException {
166174
TestRunResponse testRunResponse = TestRunResponse.builder()
167175
.status(TestRunStatus.UNRESOLVED)
168176
.build();
169-
server.enqueue(new MockResponse().setBody(gson.toJson(testRunResponse)));
177+
mockWebServer.enqueue(new MockResponse().setBody(gson.toJson(testRunResponse)));
170178
vrt.buildId = BUILD_ID;
171179
vrt.projectId = PROJECT_ID;
172180

173181
TestRunResponse result = vrt.submitTestRun(NAME, IMAGE_BASE_64, testRunOptions);
174182

175-
RecordedRequest request = server.takeRequest();
183+
RecordedRequest request = mockWebServer.takeRequest();
176184
assertThat(request.getHeader(VisualRegressionTracker.API_KEY_HEADER), is(config.getApiKey()));
177185
assertThat(request.getBody().readUtf8(), is(gson.toJson(testRunRequest)));
178186
assertThat(gson.toJson(result), is(gson.toJson(testRunResponse)));
@@ -348,13 +356,13 @@ public void httpTimeoutWorks() throws IOException, InterruptedException {
348356
.build();
349357
String json = gson.toJson(buildResponse);
350358
//body size is 97 bytes, http timeout is 1s, set body read delay to 0.5s, wait that vrt get all values correctly
351-
server.enqueue(new MockResponse().throttleBody(json.length(), HTTP_TIMOUT * 1000 - 500, TimeUnit.MILLISECONDS)
359+
mockWebServer.enqueue(new MockResponse().throttleBody(json.length(), HTTP_TIMEOUT * 1000 - 500, TimeUnit.MILLISECONDS)
352360
.setResponseCode(200)
353361
.setBody(json));
354362

355363
vrt.start();
356364

357-
server.takeRequest();
365+
mockWebServer.takeRequest();
358366

359367
assertThat(vrt.buildId, is(BUILD_ID));
360368
assertThat(vrt.projectId, is(PROJECT_ID));
@@ -374,13 +382,14 @@ public void httpTimeoutElapsed() throws IOException, InterruptedException {
374382
.ciBuildId(CI_BUILD_ID)
375383
.build();
376384
String json = gson.toJson(buildResponse);
377-
//body size is 97 bytes, http timeout is 1s, set body read delay to 1s, wait for error
378-
server.enqueue(new MockResponse().throttleBody(json.length(), HTTP_TIMOUT, TimeUnit.SECONDS)
385+
//Send part of the body after every timeout, http timeout is 1s, set body read delay to 1s, wait for error
386+
//For some reason double HTTP_TIMEOUT does not throw exception.
387+
mockWebServer.enqueue(new MockResponse().throttleBody((json.length() / 2), HTTP_TIMEOUT, TimeUnit.SECONDS)
379388
.setResponseCode(200)
380389
.setBody(json));
381390

382391
vrt.start();
383392

384-
server.takeRequest();
393+
mockWebServer.takeRequest();
385394
}
386395
}

0 commit comments

Comments
 (0)