6
6
import io .visual_regression_tracker .sdk_java .response .BuildResponse ;
7
7
import io .visual_regression_tracker .sdk_java .response .TestRunResponse ;
8
8
import lombok .SneakyThrows ;
9
+ import okhttp3 .Protocol ;
10
+ import okhttp3 .Request ;
11
+ import okhttp3 .Response ;
12
+ import okhttp3 .ResponseBody ;
9
13
import okhttp3 .mockwebserver .MockResponse ;
10
14
import okhttp3 .mockwebserver .MockWebServer ;
11
15
import okhttp3 .mockwebserver .RecordedRequest ;
@@ -48,25 +52,6 @@ public void tearDown() {
48
52
server .shutdown ();
49
53
}
50
54
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
-
70
55
@ Test
71
56
public void shouldStartBuild () throws IOException , InterruptedException {
72
57
String buildId = "123123" ;
@@ -86,57 +71,12 @@ public void shouldStartBuild() throws IOException, InterruptedException {
86
71
vrt .start ();
87
72
88
73
RecordedRequest request = server .takeRequest ();
89
- MatcherAssert .assertThat (request .getHeader (vrt .apiKeyHeaderName ), CoreMatchers .is (config .getApiKey ()));
74
+ MatcherAssert .assertThat (request .getHeader (VisualRegressionTracker .apiKeyHeaderName ), CoreMatchers .is (config .getApiKey ()));
90
75
MatcherAssert .assertThat (request .getBody ().readUtf8 (), CoreMatchers .is (gson .toJson (buildRequest )));
91
76
MatcherAssert .assertThat (vrt .buildId , CoreMatchers .is (buildId ));
92
77
MatcherAssert .assertThat (vrt .projectId , CoreMatchers .is (projectId ));
93
78
}
94
79
95
- @ Test
96
- public void shouldThrowExceptionIfProjectNotFound () throws IOException {
97
- server .enqueue (new MockResponse ()
98
- .setResponseCode (404 )
99
- .setBody ("{\r \n \" statusCode\" : 404,\r \n \" message\" : \" Project not found\" \r \n }" ));
100
-
101
- String exceptionMessage = "" ;
102
- try {
103
- vrt .start ();
104
- } catch (TestRunException ex ) {
105
- exceptionMessage = ex .getMessage ();
106
- }
107
- MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Project not found" ));
108
- }
109
-
110
- @ Test
111
- public void shouldThrowExceptionIfUnauthorized () throws IOException {
112
- server .enqueue (new MockResponse ()
113
- .setResponseCode (401 )
114
- .setBody ("{\r \n \" statusCode\" : 401,\r \n \" message\" : \" Unauthorized\" \r \n }" ));
115
-
116
- String exceptionMessage = "" ;
117
- try {
118
- vrt .start ();
119
- } catch (TestRunException ex ) {
120
- exceptionMessage = ex .getMessage ();
121
- }
122
- MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Unauthorized" ));
123
- }
124
-
125
- @ Test
126
- public void shouldThrowExceptionIfForbidden () throws IOException {
127
- server .enqueue (new MockResponse ()
128
- .setResponseCode (403 )
129
- .setBody ("{\r \n \" statusCode\" : 403,\r \n \" message\" : \" Forbidden\" \r \n }" ));
130
-
131
- String exceptionMessage = "" ;
132
- try {
133
- vrt .start ();
134
- } catch (TestRunException ex ) {
135
- exceptionMessage = ex .getMessage ();
136
- }
137
- MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Api key not authenticated" ));
138
- }
139
-
140
80
@ Test
141
81
public void shouldStopBuild () throws IOException , InterruptedException {
142
82
String buildId = "123123" ;
@@ -154,7 +94,7 @@ public void shouldStopBuild() throws IOException, InterruptedException {
154
94
155
95
RecordedRequest request = server .takeRequest ();
156
96
MatcherAssert .assertThat (request .getMethod (), CoreMatchers .is ("PATCH" ));
157
- MatcherAssert .assertThat (request .getHeader (vrt .apiKeyHeaderName ), CoreMatchers .is (config .getApiKey ()));
97
+ MatcherAssert .assertThat (request .getHeader (VisualRegressionTracker .apiKeyHeaderName ), CoreMatchers .is (config .getApiKey ()));
158
98
MatcherAssert .assertThat (Objects .requireNonNull (request .getRequestUrl ()).encodedPath (), CoreMatchers .containsString (buildId ));
159
99
}
160
100
@@ -204,13 +144,13 @@ public void shouldSubmitTestRun() throws IOException, InterruptedException {
204
144
TestRunResponse result = vrt .submitTestRun (name , imageBase64 , testRunOptions );
205
145
206
146
RecordedRequest request = server .takeRequest ();
207
- MatcherAssert .assertThat (request .getHeader (vrt .apiKeyHeaderName ), CoreMatchers .is (config .getApiKey ()));
147
+ MatcherAssert .assertThat (request .getHeader (VisualRegressionTracker .apiKeyHeaderName ), CoreMatchers .is (config .getApiKey ()));
208
148
MatcherAssert .assertThat (request .getBody ().readUtf8 (), CoreMatchers .is (gson .toJson (testRunRequest )));
209
149
MatcherAssert .assertThat (gson .toJson (result ), CoreMatchers .is (gson .toJson (testRunResponse )));
210
150
}
211
151
212
152
@ Test
213
- public void shouldNotSubmitTestRunIfNotStarted () throws IOException {
153
+ public void submitTestRunShouldThrowIfNotStarted () throws IOException {
214
154
VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
215
155
Mockito .when (vrtMocked .isStarted ()).thenReturn (false );
216
156
@@ -224,8 +164,8 @@ public void shouldNotSubmitTestRunIfNotStarted() throws IOException {
224
164
MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Visual Regression Tracker has not been started" ));
225
165
}
226
166
227
- @ DataProvider (name = "shouldTrackThrowExceptionCases " )
228
- public Object [][] shouldTrackThrowExceptionCases () {
167
+ @ DataProvider (name = "trackShouldThrowExceptionCases " )
168
+ public Object [][] trackShouldThrowExceptionCases () {
229
169
return new Object [][]{
230
170
{
231
171
TestRunResponse .builder ()
@@ -244,8 +184,8 @@ public Object[][] shouldTrackThrowExceptionCases() {
244
184
};
245
185
}
246
186
247
- @ Test (dataProvider = "shouldTrackThrowExceptionCases " )
248
- public void shouldTrackThrowException (TestRunResponse testRunResponse , String expectedExceptionMessage ) throws IOException {
187
+ @ Test (dataProvider = "trackShouldThrowExceptionCases " )
188
+ public void trackShouldThrowException (TestRunResponse testRunResponse , String expectedExceptionMessage ) throws IOException {
249
189
VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
250
190
Mockito .when (vrtMocked .submitTestRun (Mockito .anyString (), Mockito .anyString (), Mockito .any ())).thenReturn (testRunResponse );
251
191
@@ -289,4 +229,50 @@ public void shouldTrackOverload() throws IOException {
289
229
290
230
Mockito .verify (vrtMocked , Mockito .times (1 )).track (Mockito .anyString (), Mockito .anyString (), Mockito .any (TestRunOptions .class ));
291
231
}
232
+
233
+ @ DataProvider (name = "shouldReturnIsStartedCases" )
234
+ public Object [][] shouldReturnIsStartedCases () {
235
+ return new Object [][]{
236
+ {null , null , false },
237
+ {null , "some" , false },
238
+ {"some" , null , false },
239
+ {"some" , "some" , true },
240
+ };
241
+ }
242
+
243
+ @ Test (dataProvider = "shouldReturnIsStartedCases" )
244
+ public void shouldReturnIsStarted (String buildId , String projectId , boolean expectedResult ) {
245
+ vrt .buildId = buildId ;
246
+ vrt .projectId = projectId ;
247
+
248
+ boolean result = vrt .isStarted ();
249
+
250
+ MatcherAssert .assertThat (result , CoreMatchers .is (expectedResult ));
251
+ }
252
+
253
+ @ Test
254
+ public void handleRequestShouldThrowIfNotSuccess () throws IOException {
255
+ String error = "{\n " +
256
+ " \" statusCode\" : 404,\n " +
257
+ " \" message\" : \" Project not found\" \n " +
258
+ "}" ;
259
+ Request mockRequest = new Request .Builder ()
260
+ .url (config .getApiUrl ())
261
+ .build ();
262
+
263
+ String exceptionMessage = "" ;
264
+ try {
265
+ vrt .handleResponse (new Response .Builder ()
266
+ .request (mockRequest )
267
+ .protocol (Protocol .HTTP_2 )
268
+ .code (401 )
269
+ .message ("Not found" )
270
+ .body (ResponseBody .create (error , VisualRegressionTracker .JSON ))
271
+ .build (), Object .class );
272
+ } catch (TestRunException ex ) {
273
+ exceptionMessage = ex .getMessage ();
274
+ }
275
+
276
+ MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is (error ));
277
+ }
292
278
}
0 commit comments