18
18
import org .testng .annotations .Test ;
19
19
20
20
import java .io .IOException ;
21
+ import java .util .Objects ;
21
22
22
23
public class VisualRegressionTrackerTest {
23
24
private final Gson gson = new Gson ();
@@ -47,6 +48,25 @@ public void tearDown() {
47
48
server .shutdown ();
48
49
}
49
50
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
+
50
70
@ Test
51
71
public void shouldStartBuild () throws IOException , InterruptedException {
52
72
String buildId = "123123" ;
@@ -63,7 +83,7 @@ public void shouldStartBuild() throws IOException, InterruptedException {
63
83
.setResponseCode (200 )
64
84
.setBody (gson .toJson (buildResponse )));
65
85
66
- vrt .startBuild ();
86
+ vrt .start ();
67
87
68
88
RecordedRequest request = server .takeRequest ();
69
89
MatcherAssert .assertThat (request .getHeader (vrt .apiKeyHeaderName ), CoreMatchers .is (config .getApiKey ()));
@@ -80,7 +100,7 @@ public void shouldThrowExceptionIfProjectNotFound() throws IOException {
80
100
81
101
String exceptionMessage = "" ;
82
102
try {
83
- vrt .startBuild ();
103
+ vrt .start ();
84
104
} catch (TestRunException ex ) {
85
105
exceptionMessage = ex .getMessage ();
86
106
}
@@ -95,7 +115,7 @@ public void shouldThrowExceptionIfUnauthorized() throws IOException {
95
115
96
116
String exceptionMessage = "" ;
97
117
try {
98
- vrt .startBuild ();
118
+ vrt .start ();
99
119
} catch (TestRunException ex ) {
100
120
exceptionMessage = ex .getMessage ();
101
121
}
@@ -110,13 +130,44 @@ public void shouldThrowExceptionIfForbidden() throws IOException {
110
130
111
131
String exceptionMessage = "" ;
112
132
try {
113
- vrt .startBuild ();
133
+ vrt .start ();
114
134
} catch (TestRunException ex ) {
115
135
exceptionMessage = ex .getMessage ();
116
136
}
117
137
MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Api key not authenticated" ));
118
138
}
119
139
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
+
120
171
@ Test
121
172
public void shouldSubmitTestRun () throws IOException , InterruptedException {
122
173
String buildId = "123123" ;
@@ -157,6 +208,21 @@ public void shouldSubmitTestRun() throws IOException, InterruptedException {
157
208
MatcherAssert .assertThat (gson .toJson (result ), CoreMatchers .is (gson .toJson (testRunResponse )));
158
209
}
159
210
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
+
160
226
@ DataProvider (name = "shouldTrackThrowExceptionCases" )
161
227
public Object [][] shouldTrackThrowExceptionCases () {
162
228
return new Object [][]{
0 commit comments