66import io .visual_regression_tracker .sdk_java .response .BuildResponse ;
77import io .visual_regression_tracker .sdk_java .response .TestRunResponse ;
88import lombok .SneakyThrows ;
9+ import okhttp3 .Protocol ;
10+ import okhttp3 .Request ;
11+ import okhttp3 .Response ;
12+ import okhttp3 .ResponseBody ;
913import okhttp3 .mockwebserver .MockResponse ;
1014import okhttp3 .mockwebserver .MockWebServer ;
1115import okhttp3 .mockwebserver .RecordedRequest ;
@@ -48,25 +52,6 @@ public void tearDown() {
4852 server .shutdown ();
4953 }
5054
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-
7055 @ Test
7156 public void shouldStartBuild () throws IOException , InterruptedException {
7257 String buildId = "123123" ;
@@ -86,57 +71,12 @@ public void shouldStartBuild() throws IOException, InterruptedException {
8671 vrt .start ();
8772
8873 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 ()));
9075 MatcherAssert .assertThat (request .getBody ().readUtf8 (), CoreMatchers .is (gson .toJson (buildRequest )));
9176 MatcherAssert .assertThat (vrt .buildId , CoreMatchers .is (buildId ));
9277 MatcherAssert .assertThat (vrt .projectId , CoreMatchers .is (projectId ));
9378 }
9479
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-
14080 @ Test
14181 public void shouldStopBuild () throws IOException , InterruptedException {
14282 String buildId = "123123" ;
@@ -154,7 +94,7 @@ public void shouldStopBuild() throws IOException, InterruptedException {
15494
15595 RecordedRequest request = server .takeRequest ();
15696 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 ()));
15898 MatcherAssert .assertThat (Objects .requireNonNull (request .getRequestUrl ()).encodedPath (), CoreMatchers .containsString (buildId ));
15999 }
160100
@@ -204,13 +144,13 @@ public void shouldSubmitTestRun() throws IOException, InterruptedException {
204144 TestRunResponse result = vrt .submitTestRun (name , imageBase64 , testRunOptions );
205145
206146 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 ()));
208148 MatcherAssert .assertThat (request .getBody ().readUtf8 (), CoreMatchers .is (gson .toJson (testRunRequest )));
209149 MatcherAssert .assertThat (gson .toJson (result ), CoreMatchers .is (gson .toJson (testRunResponse )));
210150 }
211151
212152 @ Test
213- public void shouldNotSubmitTestRunIfNotStarted () throws IOException {
153+ public void submitTestRunShouldThrowIfNotStarted () throws IOException {
214154 VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
215155 Mockito .when (vrtMocked .isStarted ()).thenReturn (false );
216156
@@ -224,8 +164,8 @@ public void shouldNotSubmitTestRunIfNotStarted() throws IOException {
224164 MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Visual Regression Tracker has not been started" ));
225165 }
226166
227- @ DataProvider (name = "shouldTrackThrowExceptionCases " )
228- public Object [][] shouldTrackThrowExceptionCases () {
167+ @ DataProvider (name = "trackShouldThrowExceptionCases " )
168+ public Object [][] trackShouldThrowExceptionCases () {
229169 return new Object [][]{
230170 {
231171 TestRunResponse .builder ()
@@ -244,8 +184,8 @@ public Object[][] shouldTrackThrowExceptionCases() {
244184 };
245185 }
246186
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 {
249189 VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
250190 Mockito .when (vrtMocked .submitTestRun (Mockito .anyString (), Mockito .anyString (), Mockito .any ())).thenReturn (testRunResponse );
251191
@@ -289,4 +229,50 @@ public void shouldTrackOverload() throws IOException {
289229
290230 Mockito .verify (vrtMocked , Mockito .times (1 )).track (Mockito .anyString (), Mockito .anyString (), Mockito .any (TestRunOptions .class ));
291231 }
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+ }
292278}
0 commit comments