1212import org .hamcrest .CoreMatchers ;
1313import org .hamcrest .MatcherAssert ;
1414import org .mockito .Mockito ;
15- import org .testng .annotations .AfterTest ;
16- import org .testng .annotations .BeforeTest ;
15+ import org .testng .annotations .AfterMethod ;
16+ import org .testng .annotations .BeforeMethod ;
1717import org .testng .annotations .DataProvider ;
1818import org .testng .annotations .Test ;
1919
2020import java .io .IOException ;
2121
2222public class VisualRegressionTrackerTest {
23- Gson gson = new Gson ();
24- MockWebServer server ;
25- VisualRegressionTracker vrt ;
26- VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig (
23+ private final Gson gson = new Gson ();
24+ private final VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig (
2725 "http://localhost" ,
2826 "733c148e-ef70-4e6d-9ae5-ab22263697cc" ,
29- "BAZ0EG0PRH4CRQPH19ZKAVADBP9E1 " ,
27+ "XHGDZDFD3GMJDNM87JKEMP0JS1G5 " ,
3028 "develop"
3129 );
30+ private MockWebServer server ;
31+ private VisualRegressionTracker vrt ;
3232
3333 @ SneakyThrows
34- @ BeforeTest
35- void setup () {
34+ @ BeforeMethod
35+ public void setup () {
3636 server = new MockWebServer ();
3737 server .start ();
3838
3939 // target to mock server
40- this .config .apiUrl = server .url ("/" ).toString ();
40+ this .config .setApiUrl ( server .url ("/" ).toString () );
4141 vrt = new VisualRegressionTracker (config );
4242 }
4343
4444 @ SneakyThrows
45- @ AfterTest
46- void tearDown () {
45+ @ AfterMethod
46+ public void tearDown () {
4747 server .shutdown ();
4848 }
4949
5050 @ Test
51- void shouldStartBuild () throws IOException , InterruptedException {
51+ public void shouldStartBuild () throws IOException , InterruptedException {
5252 String buildId = "123123" ;
5353 String projectId = "projectId" ;
5454 BuildRequest buildRequest = BuildRequest .builder ()
55- .branchName (this .config .branchName )
56- .project (this .config .project )
55+ .branchName (this .config .getBranchName () )
56+ .project (this .config .getProject () )
5757 .build ();
5858 BuildResponse buildResponse = BuildResponse .builder ()
5959 .id (buildId )
6060 .projectId (projectId )
6161 .build ();
62- server .enqueue (new MockResponse ().setBody (gson .toJson (buildResponse )));
62+ server .enqueue (new MockResponse ()
63+ .setResponseCode (200 )
64+ .setBody (gson .toJson (buildResponse )));
6365
6466 vrt .startBuild ();
6567
6668 RecordedRequest request = server .takeRequest ();
67- MatcherAssert .assertThat (request .getHeader (vrt .apiKeyHeaderName ), CoreMatchers .is (config .apiKey ));
69+ MatcherAssert .assertThat (request .getHeader (vrt .apiKeyHeaderName ), CoreMatchers .is (config .getApiKey () ));
6870 MatcherAssert .assertThat (request .getBody ().readUtf8 (), CoreMatchers .is (gson .toJson (buildRequest )));
6971 MatcherAssert .assertThat (vrt .buildId , CoreMatchers .is (buildId ));
7072 MatcherAssert .assertThat (vrt .projectId , CoreMatchers .is (projectId ));
7173 }
7274
7375 @ Test
74- void shouldSubmitTestRun () throws IOException , InterruptedException {
76+ public void shouldThrowExceptionIfProjectNotFound () throws IOException {
77+ server .enqueue (new MockResponse ()
78+ .setResponseCode (404 )
79+ .setBody ("{\r \n \" statusCode\" : 404,\r \n \" message\" : \" Project not found\" \r \n }" ));
80+
81+ String exceptionMessage = "" ;
82+ try {
83+ vrt .startBuild ();
84+ } catch (TestRunException ex ) {
85+ exceptionMessage = ex .getMessage ();
86+ }
87+ MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Project not found" ));
88+ }
89+
90+ @ Test
91+ public void shouldThrowExceptionIfUnauthorized () throws IOException {
92+ server .enqueue (new MockResponse ()
93+ .setResponseCode (401 )
94+ .setBody ("{\r \n \" statusCode\" : 401,\r \n \" message\" : \" Unauthorized\" \r \n }" ));
95+
96+ String exceptionMessage = "" ;
97+ try {
98+ vrt .startBuild ();
99+ } catch (TestRunException ex ) {
100+ exceptionMessage = ex .getMessage ();
101+ }
102+ MatcherAssert .assertThat (exceptionMessage , CoreMatchers .is ("Unauthorized" ));
103+ }
104+
105+ @ Test
106+ public void shouldSubmitTestRun () throws IOException , InterruptedException {
75107 String buildId = "123123" ;
76108 String projectId = "projectId" ;
77109 String name = "Test name" ;
@@ -85,7 +117,7 @@ void shouldSubmitTestRun() throws IOException, InterruptedException {
85117 .build ();
86118 TestRunRequest testRunRequest = TestRunRequest .builder ()
87119 .projectId (projectId )
88- .branchName (config .branchName )
120+ .branchName (config .getBranchName () )
89121 .buildId (buildId )
90122 .name (name )
91123 .imageBase64 (imageBase64 )
@@ -100,11 +132,12 @@ void shouldSubmitTestRun() throws IOException, InterruptedException {
100132 .build ();
101133 server .enqueue (new MockResponse ().setBody (gson .toJson (testRunResponse )));
102134 vrt .buildId = buildId ;
135+ vrt .projectId = projectId ;
103136
104137 TestRunResponse result = vrt .submitTestRun (name , imageBase64 , testRunOptions );
105138
106139 RecordedRequest request = server .takeRequest ();
107- MatcherAssert .assertThat (request .getHeader (vrt .apiKeyHeaderName ), CoreMatchers .is (config .apiKey ));
140+ MatcherAssert .assertThat (request .getHeader (vrt .apiKeyHeaderName ), CoreMatchers .is (config .getApiKey () ));
108141 MatcherAssert .assertThat (request .getBody ().readUtf8 (), CoreMatchers .is (gson .toJson (testRunRequest )));
109142 MatcherAssert .assertThat (gson .toJson (result ), CoreMatchers .is (gson .toJson (testRunResponse )));
110143 }
@@ -129,7 +162,6 @@ public Object[][] shouldTrackThrowExceptionCases() {
129162 };
130163 }
131164
132-
133165 @ Test (dataProvider = "shouldTrackThrowExceptionCases" )
134166 public void shouldTrackThrowException (TestRunResponse testRunResponse , String expectedExceptionMessage ) throws IOException {
135167 VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
0 commit comments