1818import org .testng .annotations .Test ;
1919
2020import java .io .IOException ;
21+ import java .util .Objects ;
2122
2223public 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