Skip to content

Commit 234dced

Browse files
authored
Merge pull request #21 from Visual-Regression-Tracker/57-build-stop
57 build stop
2 parents 0afda06 + f7d43a3 commit 234dced

File tree

3 files changed

+124
-41
lines changed

3 files changed

+124
-41
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 '3.1.2'
2+
version '4.0.0'
33

44
apply plugin: 'java'
55
apply plugin: 'jacoco'

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

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,64 @@ public VisualRegressionTracker(VisualRegressionTrackerConfig visualRegressionTra
2929
this.client = new OkHttpClient();
3030
}
3131

32-
protected void startBuild() throws IOException {
33-
if (this.buildId == null) {
34-
BuildRequest newBuild = BuildRequest.builder()
35-
.branchName(this.visualRegressionTrackerConfig.getBranchName())
36-
.project(this.visualRegressionTrackerConfig.getProject())
37-
.build();
38-
39-
RequestBody body = RequestBody.create(gson.toJson(newBuild), JSON);
40-
41-
Request request = new Request.Builder()
42-
.url(this.visualRegressionTrackerConfig.getApiUrl().concat("/builds"))
43-
.addHeader(apiKeyHeaderName, this.visualRegressionTrackerConfig.getApiKey())
44-
.post(body)
45-
.build();
46-
47-
try (Response response = client.newCall(request).execute()) {
48-
if (response.code() == 401) {
49-
throw new TestRunException("Unauthorized");
50-
}
51-
if (response.code() == 403) {
52-
throw new TestRunException("Api key not authenticated");
53-
}
54-
if (response.code() == 404) {
55-
throw new TestRunException("Project not found");
56-
}
57-
58-
String responseBody = Optional.ofNullable(response.body())
59-
.orElseThrow(() -> new TestRunException("Cannot get response body"))
60-
.string();
61-
BuildResponse buildDTO = gson.fromJson(responseBody, BuildResponse.class);
62-
this.buildId = Optional.ofNullable(buildDTO.getId())
63-
.orElseThrow(() -> new TestRunException("Build id is null"));
64-
this.projectId = Optional.ofNullable(buildDTO.getProjectId())
65-
.orElseThrow(() -> new TestRunException("Project id is null"));
32+
protected boolean isStarted() {
33+
return this.buildId != null && this.projectId != null;
34+
}
35+
36+
public void start() throws IOException {
37+
BuildRequest newBuild = BuildRequest.builder()
38+
.branchName(this.visualRegressionTrackerConfig.getBranchName())
39+
.project(this.visualRegressionTrackerConfig.getProject())
40+
.build();
41+
42+
RequestBody body = RequestBody.create(gson.toJson(newBuild), JSON);
43+
44+
Request request = new Request.Builder()
45+
.url(this.visualRegressionTrackerConfig.getApiUrl().concat("/builds"))
46+
.addHeader(apiKeyHeaderName, this.visualRegressionTrackerConfig.getApiKey())
47+
.post(body)
48+
.build();
49+
50+
try (Response response = client.newCall(request).execute()) {
51+
if (response.code() == 401) {
52+
throw new TestRunException("Unauthorized");
6653
}
54+
if (response.code() == 403) {
55+
throw new TestRunException("Api key not authenticated");
56+
}
57+
if (response.code() == 404) {
58+
throw new TestRunException("Project not found");
59+
}
60+
61+
String responseBody = Optional.ofNullable(response.body())
62+
.orElseThrow(() -> new TestRunException("Cannot get response body"))
63+
.string();
64+
BuildResponse buildDTO = gson.fromJson(responseBody, BuildResponse.class);
65+
this.buildId = Optional.ofNullable(buildDTO.getId())
66+
.orElseThrow(() -> new TestRunException("Build id is null"));
67+
this.projectId = Optional.ofNullable(buildDTO.getProjectId())
68+
.orElseThrow(() -> new TestRunException("Project id is null"));
6769
}
6870
}
6971

72+
public void stop() throws IOException {
73+
if (!this.isStarted()) {
74+
throw new TestRunException("Visual Regression Tracker has not been started");
75+
}
76+
77+
Request request = new Request.Builder()
78+
.url(this.visualRegressionTrackerConfig.getApiUrl().concat("/builds/").concat(this.buildId))
79+
.addHeader(apiKeyHeaderName, this.visualRegressionTrackerConfig.getApiKey())
80+
.build();
81+
82+
client.newCall(request).execute();
83+
}
84+
7085
protected TestRunResponse submitTestRun(String name, String imageBase64, TestRunOptions testRunOptions) throws IOException {
86+
if (!this.isStarted()) {
87+
throw new TestRunException("Visual Regression Tracker has not been started");
88+
}
89+
7190
TestRunRequest newTestRun = TestRunRequest.builder()
7291
.projectId(this.projectId)
7392
.buildId(this.buildId)
@@ -98,8 +117,6 @@ protected TestRunResponse submitTestRun(String name, String imageBase64, TestRun
98117
}
99118

100119
public void track(String name, String imageBase64, TestRunOptions testRunOptions) throws IOException {
101-
this.startBuild();
102-
103120
TestRunResponse testResultDTO = this.submitTestRun(name, imageBase64, testRunOptions);
104121

105122
TestRunStatus status = Optional.ofNullable(testResultDTO.getStatus())

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

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.testng.annotations.Test;
1919

2020
import java.io.IOException;
21+
import java.util.Objects;
2122

2223
public class VisualRegressionTrackerTest {
2324
private final Gson gson = new Gson();
@@ -47,6 +48,25 @@ public void tearDown() {
4748
server.shutdown();
4849
}
4950

51+
@DataProvider(name = "shouldReturnIsStartedCases")
52+
public Object[][] shouldReturnIsStartedCases() {
53+
return new Object[][]{
54+
{null, null, false},
55+
{null, "some", false},
56+
{"some", null, false},
57+
{"some", "some", true},
58+
};
59+
}
60+
61+
@Test(dataProvider = "shouldReturnIsStartedCases")
62+
public void shouldReturnIsStarted(String buildId, String projectId, boolean expectedResult) {
63+
vrt.buildId = buildId;
64+
vrt.projectId = projectId;
65+
66+
boolean result = vrt.isStarted();
67+
MatcherAssert.assertThat(result, CoreMatchers.is(expectedResult));
68+
}
69+
5070
@Test
5171
public void shouldStartBuild() throws IOException, InterruptedException {
5272
String buildId = "123123";
@@ -63,7 +83,7 @@ public void shouldStartBuild() throws IOException, InterruptedException {
6383
.setResponseCode(200)
6484
.setBody(gson.toJson(buildResponse)));
6585

66-
vrt.startBuild();
86+
vrt.start();
6787

6888
RecordedRequest request = server.takeRequest();
6989
MatcherAssert.assertThat(request.getHeader(vrt.apiKeyHeaderName), CoreMatchers.is(config.getApiKey()));
@@ -80,7 +100,7 @@ public void shouldThrowExceptionIfProjectNotFound() throws IOException {
80100

81101
String exceptionMessage = "";
82102
try {
83-
vrt.startBuild();
103+
vrt.start();
84104
} catch (TestRunException ex) {
85105
exceptionMessage = ex.getMessage();
86106
}
@@ -95,7 +115,7 @@ public void shouldThrowExceptionIfUnauthorized() throws IOException {
95115

96116
String exceptionMessage = "";
97117
try {
98-
vrt.startBuild();
118+
vrt.start();
99119
} catch (TestRunException ex) {
100120
exceptionMessage = ex.getMessage();
101121
}
@@ -110,13 +130,44 @@ public void shouldThrowExceptionIfForbidden() throws IOException {
110130

111131
String exceptionMessage = "";
112132
try {
113-
vrt.startBuild();
133+
vrt.start();
114134
} catch (TestRunException ex) {
115135
exceptionMessage = ex.getMessage();
116136
}
117137
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Api key not authenticated"));
118138
}
119139

140+
@Test
141+
public void shouldStopBuild() throws IOException, InterruptedException {
142+
String buildId = "123123";
143+
String projectId = " someId";
144+
vrt.buildId = buildId;
145+
vrt.projectId = projectId;
146+
BuildResponse buildResponse = BuildResponse.builder()
147+
.id(buildId)
148+
.build();
149+
server.enqueue(new MockResponse()
150+
.setResponseCode(200)
151+
.setBody(gson.toJson(buildResponse)));
152+
153+
vrt.stop();
154+
155+
RecordedRequest request = server.takeRequest();
156+
MatcherAssert.assertThat(request.getHeader(vrt.apiKeyHeaderName), CoreMatchers.is(config.getApiKey()));
157+
MatcherAssert.assertThat(Objects.requireNonNull(request.getRequestUrl()).encodedPath(), CoreMatchers.containsString(buildId));
158+
}
159+
160+
@Test
161+
public void stopShouldThrowExceptionIfNotStarted() throws IOException {
162+
String exceptionMessage = "";
163+
try {
164+
vrt.stop();
165+
} catch (TestRunException ex) {
166+
exceptionMessage = ex.getMessage();
167+
}
168+
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Visual Regression Tracker has not been started"));
169+
}
170+
120171
@Test
121172
public void shouldSubmitTestRun() throws IOException, InterruptedException {
122173
String buildId = "123123";
@@ -157,6 +208,21 @@ public void shouldSubmitTestRun() throws IOException, InterruptedException {
157208
MatcherAssert.assertThat(gson.toJson(result), CoreMatchers.is(gson.toJson(testRunResponse)));
158209
}
159210

211+
@Test
212+
public void shouldNotSubmitTestRunIfNotStarted() throws IOException {
213+
VisualRegressionTracker vrtMocked = Mockito.mock(VisualRegressionTracker.class);
214+
Mockito.when(vrtMocked.isStarted()).thenReturn(false);
215+
216+
Mockito.doCallRealMethod().when(vrtMocked).submitTestRun(Mockito.anyString(), Mockito.any(), Mockito.any());
217+
String exceptionMessage = "";
218+
try {
219+
vrtMocked.submitTestRun("name", null, null);
220+
} catch (TestRunException ex) {
221+
exceptionMessage = ex.getMessage();
222+
}
223+
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Visual Regression Tracker has not been started"));
224+
}
225+
160226
@DataProvider(name = "shouldTrackThrowExceptionCases")
161227
public Object[][] shouldTrackThrowExceptionCases() {
162228
return new Object[][]{

0 commit comments

Comments
 (0)