11package io .visual_regression_tracker .sdk_java ;
22
3+ import java .io .IOException ;
4+ import java .util .Objects ;
5+
36import com .google .gson .Gson ;
47import io .visual_regression_tracker .sdk_java .request .BuildRequest ;
58import io .visual_regression_tracker .sdk_java .request .TestRunRequest ;
1316import okhttp3 .mockwebserver .MockResponse ;
1417import okhttp3 .mockwebserver .MockWebServer ;
1518import okhttp3 .mockwebserver .RecordedRequest ;
16- import org .hamcrest .CoreMatchers ;
17- import org .hamcrest .MatcherAssert ;
1819import org .mockito .Mockito ;
1920import org .simplify4u .sjf4jmock .LoggerMock ;
2021import org .slf4j .Logger ;
2324import org .testng .annotations .DataProvider ;
2425import org .testng .annotations .Test ;
2526
26- import java .io .IOException ;
27- import java .util .Objects ;
27+ import static org .hamcrest .CoreMatchers .containsString ;
28+ import static org .hamcrest .CoreMatchers .is ;
29+ import static org .hamcrest .MatcherAssert .assertThat ;
30+ import static org .mockito .ArgumentMatchers .any ;
31+ import static org .mockito .ArgumentMatchers .anyString ;
32+ import static org .mockito .Mockito .doCallRealMethod ;
33+ import static org .mockito .Mockito .mock ;
34+ import static org .mockito .Mockito .reset ;
35+ import static org .mockito .Mockito .verify ;
36+ import static org .mockito .Mockito .when ;
2837
2938public class VisualRegressionTrackerTest {
39+
40+ private final static String BUILD_ID = "123123" ;
41+ private final static String PROJECT_ID = "projectId" ;
42+ private final static String NAME = "Test name" ;
43+ private final static String IMAGE_BASE_64 = "image" ;
44+
3045 private final Gson gson = new Gson ();
3146 private final VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig (
3247 "http://localhost" ,
@@ -35,8 +50,10 @@ public class VisualRegressionTrackerTest {
3550 "develop" ,
3651 false
3752 );
53+
3854 private MockWebServer server ;
3955 private VisualRegressionTracker vrt ;
56+ private VisualRegressionTracker vrtMocked ;
4057
4158 @ SneakyThrows
4259 @ BeforeMethod
@@ -49,209 +66,183 @@ public void setup() {
4966 // target to mock server
5067 this .config .setApiUrl (server .url ("/" ).toString ());
5168 vrt = new VisualRegressionTracker (config );
69+ vrtMocked = mock (VisualRegressionTracker .class );
5270 }
5371
5472 @ SneakyThrows
5573 @ AfterMethod
5674 public void tearDown () {
5775 server .shutdown ();
76+ reset (vrtMocked );
5877 }
5978
6079 @ Test
6180 public void shouldStartBuild () throws IOException , InterruptedException {
62- String buildId = "123123" ;
63- String projectId = "projectId" ;
6481 BuildRequest buildRequest = BuildRequest .builder ()
65- .branchName (this .config .getBranchName ())
66- .project (this .config .getProject ())
67- .build ();
82+ .branchName (this .config .getBranchName ())
83+ .project (this .config .getProject ())
84+ .build ();
6885 BuildResponse buildResponse = BuildResponse .builder ()
69- .id (buildId )
70- .projectId (projectId )
71- .build ();
86+ .id (BUILD_ID )
87+ .projectId (PROJECT_ID )
88+ .build ();
7289 server .enqueue (new MockResponse ()
73- .setResponseCode (200 )
74- .setBody (gson .toJson (buildResponse )));
90+ .setResponseCode (200 )
91+ .setBody (gson .toJson (buildResponse )));
7592
7693 vrt .start ();
7794
78- RecordedRequest request = server .takeRequest ();
79- MatcherAssert . assertThat (request .getHeader (VisualRegressionTracker .apiKeyHeaderName ), CoreMatchers . is (config .getApiKey ()));
80- MatcherAssert . assertThat (request .getBody ().readUtf8 (), CoreMatchers . is (gson .toJson (buildRequest )));
81- MatcherAssert . assertThat (vrt .buildId , CoreMatchers . is (buildId ));
82- MatcherAssert . assertThat (vrt .projectId , CoreMatchers . is (projectId ));
95+ RecordedRequest recordedRequest = server .takeRequest ();
96+ assertThat (recordedRequest .getHeader (VisualRegressionTracker .apiKeyHeaderName ), is (config .getApiKey ()));
97+ assertThat (recordedRequest .getBody ().readUtf8 (), is (gson .toJson (buildRequest )));
98+ assertThat (vrt .buildId , is (BUILD_ID ));
99+ assertThat (vrt .projectId , is (PROJECT_ID ));
83100 }
84101
85102 @ Test
86103 public void shouldStopBuild () throws IOException , InterruptedException {
87- String buildId = "123123" ;
88- String projectId = " someId" ;
89- vrt .buildId = buildId ;
90- vrt .projectId = projectId ;
104+ vrt .buildId = BUILD_ID ;
105+ vrt .projectId = PROJECT_ID ;
91106 BuildResponse buildResponse = BuildResponse .builder ()
92- .id (buildId )
93- .build ();
107+ .id (BUILD_ID )
108+ .build ();
94109 server .enqueue (new MockResponse ()
95- .setResponseCode (200 )
96- .setBody (gson .toJson (buildResponse )));
110+ .setResponseCode (200 )
111+ .setBody (gson .toJson (buildResponse )));
97112
98113 vrt .stop ();
99114
100- RecordedRequest request = server .takeRequest ();
101- MatcherAssert . assertThat (request .getMethod (), CoreMatchers . is ("PATCH" ));
102- MatcherAssert . assertThat (request .getHeader (VisualRegressionTracker .apiKeyHeaderName ), CoreMatchers . is (config .getApiKey ()));
103- MatcherAssert . assertThat (Objects .requireNonNull (request .getRequestUrl ()).encodedPath (), CoreMatchers . containsString (buildId ));
115+ RecordedRequest recordedRequest = server .takeRequest ();
116+ assertThat (recordedRequest .getMethod (), is ("PATCH" ));
117+ assertThat (recordedRequest .getHeader (VisualRegressionTracker .apiKeyHeaderName ), is (config .getApiKey ()));
118+ assertThat (Objects .requireNonNull (recordedRequest .getRequestUrl ()).encodedPath (), containsString (BUILD_ID ));
104119 }
105120
106- @ Test
121+ @ Test (expectedExceptions = TestRunException .class ,
122+ expectedExceptionsMessageRegExp = "Visual Regression Tracker has not been started" )
107123 public void stopShouldThrowExceptionIfNotStarted () throws IOException {
108- String exceptionMessage = "" ;
109- try {
110- vrt .stop ();
111- } catch (TestRunException ex ) {
112- exceptionMessage = ex .getMessage ();
113- }
114- MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Visual Regression Tracker has not been started" ));
124+ vrt .stop ();
115125 }
116126
117127 @ Test
118128 public void shouldSubmitTestRun () throws IOException , InterruptedException {
119- String buildId = "123123" ;
120- String projectId = "projectId" ;
121- String name = "Test name" ;
122- String imageBase64 = "image" ;
123129 TestRunOptions testRunOptions = TestRunOptions .builder ()
124- .device ("Device" )
125- .os ("OS" )
126- .browser ("Browser" )
127- .viewport ("Viewport" )
128- .diffTollerancePercent (0.5f )
129- .build ();
130+ .device ("Device" )
131+ .os ("OS" )
132+ .browser ("Browser" )
133+ .viewport ("Viewport" )
134+ .diffTollerancePercent (0.5f )
135+ .build ();
130136 TestRunRequest testRunRequest = TestRunRequest .builder ()
131- .projectId (projectId )
132- .branchName (config .getBranchName ())
133- .buildId (buildId )
134- .name (name )
135- .imageBase64 (imageBase64 )
136- .os (testRunOptions .getOs ())
137- .browser (testRunOptions .getBrowser ())
138- .viewport (testRunOptions .getViewport ())
139- .device (testRunOptions .getDevice ())
140- .diffTollerancePercent (testRunOptions .getDiffTollerancePercent ())
141- .build ();
137+ .projectId (PROJECT_ID )
138+ .branchName (config .getBranchName ())
139+ .buildId (BUILD_ID )
140+ .name (NAME )
141+ .imageBase64 (IMAGE_BASE_64 )
142+ .os (testRunOptions .getOs ())
143+ .browser (testRunOptions .getBrowser ())
144+ .viewport (testRunOptions .getViewport ())
145+ .device (testRunOptions .getDevice ())
146+ .diffTollerancePercent (testRunOptions .getDiffTollerancePercent ())
147+ .build ();
142148 TestRunResponse testRunResponse = TestRunResponse .builder ()
143- .status (TestRunStatus .UNRESOLVED )
144- .build ();
149+ .status (TestRunStatus .UNRESOLVED )
150+ .build ();
145151 server .enqueue (new MockResponse ().setBody (gson .toJson (testRunResponse )));
146- vrt .buildId = buildId ;
147- vrt .projectId = projectId ;
152+ vrt .buildId = BUILD_ID ;
153+ vrt .projectId = PROJECT_ID ;
148154
149- TestRunResponse result = vrt .submitTestRun (name , imageBase64 , testRunOptions );
155+ TestRunResponse result = vrt .submitTestRun (NAME , IMAGE_BASE_64 , testRunOptions );
150156
151157 RecordedRequest request = server .takeRequest ();
152- MatcherAssert . assertThat (request .getHeader (VisualRegressionTracker .apiKeyHeaderName ), CoreMatchers . is (config .getApiKey ()));
153- MatcherAssert . assertThat (request .getBody ().readUtf8 (), CoreMatchers . is (gson .toJson (testRunRequest )));
154- MatcherAssert . assertThat (gson .toJson (result ), CoreMatchers . is (gson .toJson (testRunResponse )));
158+ assertThat (request .getHeader (VisualRegressionTracker .apiKeyHeaderName ), is (config .getApiKey ()));
159+ assertThat (request .getBody ().readUtf8 (), is (gson .toJson (testRunRequest )));
160+ assertThat (gson .toJson (result ), is (gson .toJson (testRunResponse )));
155161 }
156162
157- @ Test
163+ @ Test (expectedExceptions = TestRunException .class ,
164+ expectedExceptionsMessageRegExp = "Visual Regression Tracker has not been started" )
158165 public void submitTestRunShouldThrowIfNotStarted () throws IOException {
159- VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
160- Mockito .when (vrtMocked .isStarted ()).thenReturn (false );
166+ when (vrtMocked .isStarted ()).thenReturn (false );
161167
162- Mockito .doCallRealMethod ().when (vrtMocked ).submitTestRun (Mockito .anyString (), Mockito .any (), Mockito .any ());
163- String exceptionMessage = "" ;
164- try {
165- vrtMocked .submitTestRun ("name" , null , null );
166- } catch (TestRunException ex ) {
167- exceptionMessage = ex .getMessage ();
168- }
169- MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Visual Regression Tracker has not been started" ));
168+ doCallRealMethod ().when (vrtMocked ).submitTestRun (anyString (), any (), any ());
169+ vrtMocked .submitTestRun ("name" , null , null );
170170 }
171171
172172 @ DataProvider (name = "trackErrorCases" )
173173 public Object [][] trackErrorCases () {
174- return new Object [][]{
174+ return new Object [][] {
175175 {
176176 TestRunResponse .builder ()
177- .url ("https://someurl.com/test/123123" )
178- .status (TestRunStatus .UNRESOLVED )
177+ .url ("https://someurl.com/test/123123" )
178+ .status (TestRunStatus .UNRESOLVED )
179179 .build (),
180180 "Difference found: https://someurl.com/test/123123"
181181 },
182182 {
183183 TestRunResponse .builder ()
184- .url ("https://someurl.com/test/123123" )
185- .status (TestRunStatus .NEW )
184+ .url ("https://someurl.com/test/123123" )
185+ .status (TestRunStatus .NEW )
186186 .build (),
187187 "No baseline: https://someurl.com/test/123123"
188188 }
189189 };
190190 }
191191
192- @ Test (dataProvider = "trackErrorCases" )
192+ @ Test (dataProvider = "trackErrorCases" ,
193+ expectedExceptions = TestRunException .class ,
194+ expectedExceptionsMessageRegExp = "^(Difference found: https://someurl.com/test/123123|No baseline: https://someurl.com/test/123123)$" )
193195 public void trackShouldThrowException (TestRunResponse testRunResponse , String expectedExceptionMessage ) throws IOException {
194- VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
195196 vrtMocked .visualRegressionTrackerConfig = new VisualRegressionTrackerConfig ("" , "" , "" , "" , false );
196- Mockito . when (vrtMocked .submitTestRun (Mockito . anyString (), Mockito . anyString (), Mockito . any ())).thenReturn (testRunResponse );
197+ when (vrtMocked .submitTestRun (anyString (), anyString (), any ())).thenReturn (testRunResponse );
197198
198- Mockito .doCallRealMethod ().when (vrtMocked ).track (Mockito .anyString (), Mockito .anyString (), Mockito .any ());
199- String exceptionMessage = "" ;
200- try {
201- vrtMocked .track ("name" , "image" , TestRunOptions .builder ().build ());
202- } catch (TestRunException ex ) {
203- exceptionMessage = ex .getMessage ();
204- }
205- MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is (expectedExceptionMessage ));
199+ doCallRealMethod ().when (vrtMocked ).track (anyString (), anyString (), any ());
200+ vrtMocked .track ("name" , "image" , TestRunOptions .builder ().build ());
206201 }
207202
208203 @ Test (dataProvider = "trackErrorCases" )
209204 public void trackShouldLogSevere (TestRunResponse testRunResponse , String expectedExceptionMessage ) throws IOException {
210205 Logger loggerMock = LoggerMock .getLoggerMock (VisualRegressionTracker .class );
211- VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
212206 vrtMocked .visualRegressionTrackerConfig = new VisualRegressionTrackerConfig ("" , "" , "" , "" , true );
213- Mockito . when (vrtMocked .submitTestRun (Mockito . anyString (), Mockito . anyString (), Mockito . any ())).thenReturn (testRunResponse );
207+ when (vrtMocked .submitTestRun (anyString (), anyString (), any ())).thenReturn (testRunResponse );
214208
215- Mockito . doCallRealMethod ().when (vrtMocked ).track (Mockito . anyString (), Mockito . anyString (), Mockito . any ());
209+ doCallRealMethod ().when (vrtMocked ).track (anyString (), anyString (), any ());
216210 vrtMocked .track ("name" , "image" , TestRunOptions .builder ().build ());
217211
218- Mockito . verify (loggerMock ).error (expectedExceptionMessage );
212+ verify (loggerMock ).error (expectedExceptionMessage );
219213 }
220214
221215 @ DataProvider (name = "shouldTrackPassCases" )
222216 public Object [][] shouldTrackPassCases () {
223- return new Object [][]{
217+ return new Object [][] {
224218 {
225219 TestRunResponse .builder ()
226- .url ("https://someurl.com/test/123123" )
227- .status (TestRunStatus .OK )
220+ .url ("https://someurl.com/test/123123" )
221+ .status (TestRunStatus .OK )
228222 .build (),
229223 }
230224 };
231225 }
232226
233227 @ Test (dataProvider = "shouldTrackPassCases" )
234228 public void shouldTrackPass (TestRunResponse testRunResponse ) throws IOException {
235- VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
236- Mockito .when (vrtMocked .submitTestRun (Mockito .anyString (), Mockito .anyString (), Mockito .any ())).thenReturn (testRunResponse );
229+ when (vrtMocked .submitTestRun (anyString (), anyString (), any ())).thenReturn (testRunResponse );
237230
238- Mockito . doCallRealMethod ().when (vrtMocked ).track (Mockito . anyString (), Mockito . anyString (), Mockito . any ());
231+ doCallRealMethod ().when (vrtMocked ).track (anyString (), anyString (), any ());
239232 vrtMocked .track ("name" , "image" , TestRunOptions .builder ().build ());
240233 }
241234
242235 @ Test ()
243236 public void shouldTrackOverload () throws IOException {
244- VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
245-
246- Mockito .doCallRealMethod ().when (vrtMocked ).track (Mockito .anyString (), Mockito .anyString ());
237+ doCallRealMethod ().when (vrtMocked ).track (anyString (), anyString ());
247238 vrtMocked .track ("name" , "image" );
248239
249- Mockito . verify (vrtMocked , Mockito .times (1 )).track (Mockito . anyString (), Mockito . anyString (), Mockito . any (TestRunOptions .class ));
240+ verify (vrtMocked , Mockito .times (1 )).track (anyString (), anyString (), any (TestRunOptions .class ));
250241 }
251242
252243 @ DataProvider (name = "shouldReturnIsStartedCases" )
253244 public Object [][] shouldReturnIsStartedCases () {
254- return new Object [][]{
245+ return new Object [][] {
255246 {null , null , false },
256247 {null , "some" , false },
257248 {"some" , null , false },
@@ -266,32 +257,32 @@ public void shouldReturnIsStarted(String buildId, String projectId, boolean expe
266257
267258 boolean result = vrt .isStarted ();
268259
269- MatcherAssert . assertThat (result , CoreMatchers . is (expectedResult ));
260+ assertThat (result , is (expectedResult ));
270261 }
271262
272263 @ Test
273264 public void handleRequestShouldThrowIfNotSuccess () throws IOException {
274265 String error = "{\n " +
275- " \" statusCode\" : 404,\n " +
276- " \" message\" : \" Project not found\" \n " +
277- "}" ;
266+ " \" statusCode\" : 404,\n " +
267+ " \" message\" : \" Project not found\" \n " +
268+ "}" ;
278269 Request mockRequest = new Request .Builder ()
279- .url (config .getApiUrl ())
280- .build ();
270+ .url (config .getApiUrl ())
271+ .build ();
281272
282273 String exceptionMessage = "" ;
283274 try {
284275 vrt .handleResponse (new Response .Builder ()
285- .request (mockRequest )
286- .protocol (Protocol .HTTP_2 )
287- .code (401 )
288- .message ("Not found" )
289- .body (ResponseBody .create (error , VisualRegressionTracker .JSON ))
290- .build (), Object .class );
276+ .request (mockRequest )
277+ .protocol (Protocol .HTTP_2 )
278+ .code (404 )
279+ .message ("Not found" )
280+ .body (ResponseBody .create (error , VisualRegressionTracker .JSON ))
281+ .build (), Object .class );
291282 } catch (TestRunException ex ) {
292283 exceptionMessage = ex .getMessage ();
293284 }
294285
295- MatcherAssert . assertThat (exceptionMessage , CoreMatchers . is (error ));
286+ assertThat (exceptionMessage , is (error ));
296287 }
297288}
0 commit comments