11package io .visual_regression_tracker .sdk_java ;
22
3+ import java .io .IOException ;
4+ import java .util .Optional ;
5+
36import com .google .gson .Gson ;
47import io .visual_regression_tracker .sdk_java .request .BuildRequest ;
58import io .visual_regression_tracker .sdk_java .request .TestRunRequest ;
69import io .visual_regression_tracker .sdk_java .response .BuildResponse ;
710import io .visual_regression_tracker .sdk_java .response .TestRunResponse ;
11+ import lombok .extern .slf4j .Slf4j ;
812import okhttp3 .MediaType ;
913import okhttp3 .OkHttpClient ;
1014import okhttp3 .Request ;
1115import okhttp3 .RequestBody ;
1216import okhttp3 .Response ;
13- import org .slf4j .Logger ;
14- import org .slf4j .LoggerFactory ;
15-
16- import java .io .IOException ;
17- import java .util .Optional ;
18-
1917
18+ @ Slf4j
2019public class VisualRegressionTracker {
21- protected static final String apiKeyHeaderName = "apiKey" ;
20+
21+ private static final String TRACKER_NOT_STARTED = "Visual Regression Tracker has not been started" ;
22+ protected static final String API_KEY_HEADER = "apiKey" ;
2223 protected static final MediaType JSON = MediaType .get ("application/json; charset=utf-8" );
23- private static final Logger LOGGER = LoggerFactory .getLogger (VisualRegressionTracker .class );
2424 protected Gson gson ;
25- protected VisualRegressionTrackerConfig visualRegressionTrackerConfig ;
25+ protected VisualRegressionTrackerConfig configuration ;
26+ protected PathProvider paths ;
2627 protected String buildId ;
2728 protected String projectId ;
2829 protected OkHttpClient client ;
2930
30- public VisualRegressionTracker (VisualRegressionTrackerConfig visualRegressionTrackerConfig ) {
31- this . visualRegressionTrackerConfig = visualRegressionTrackerConfig ;
32-
33- this . client = new OkHttpClient ();
34- this . gson = new Gson ();
31+ public VisualRegressionTracker (VisualRegressionTrackerConfig trackerConfig ) {
32+ configuration = trackerConfig ;
33+ paths = new PathProvider ( trackerConfig . getApiUrl ());
34+ client = new OkHttpClient ();
35+ gson = new Gson ();
3536 }
3637
3738 public void start () throws IOException {
39+ String projectName = configuration .getProject ();
40+ String branch = configuration .getBranchName ();
41+
3842 BuildRequest newBuild = BuildRequest .builder ()
39- .branchName (this . visualRegressionTrackerConfig . getBranchName () )
40- .project (this . visualRegressionTrackerConfig . getProject () )
41- .build ();
43+ .branchName (branch )
44+ .project (projectName )
45+ .build ();
4246
4347 RequestBody body = RequestBody .create (JSON , gson .toJson (newBuild ));
4448
4549 Request request = new Request .Builder ()
46- .url (this . visualRegressionTrackerConfig . getApiUrl (). concat ( "/builds" ))
47- .addHeader (apiKeyHeaderName , this . visualRegressionTrackerConfig .getApiKey ())
48- .post (body )
49- .build ();
50+ .url (paths . getBuildPath ( ))
51+ .addHeader (API_KEY_HEADER , configuration .getApiKey ())
52+ .post (body )
53+ .build ();
5054
51- try ( Response response = client . newCall ( request ). execute ()) {
55+ log . info ( "Starting Visual Regression Tracker for project <{}> and branch <{}>" , projectName , branch );
5256
53- BuildResponse buildDTO = handleResponse ( response , BuildResponse . class );
57+ Response response = client . newCall ( request ). execute ( );
5458
55- this .buildId = buildDTO .getId ();
56- this .projectId = buildDTO .getProjectId ();
57- }
59+ BuildResponse buildResponse = handleResponse (response , BuildResponse .class );
60+
61+ buildId = buildResponse .getId ();
62+ projectId = buildResponse .getProjectId ();
63+
64+ log .info ("Visual Regression Tracker is started for project <{}>: buildId <{}>, projectId <{}>" ,
65+ projectName , projectId , buildId );
5866 }
5967
6068 public void stop () throws IOException {
61- if (!this . isStarted ()) {
62- throw new TestRunException ("Visual Regression Tracker has not been started" );
69+ if (!isStarted ()) {
70+ throw new TestRunException (TRACKER_NOT_STARTED );
6371 }
6472
6573 Request request = new Request .Builder ()
66- .url (this . visualRegressionTrackerConfig . getApiUrl (). concat ( "/builds/" ). concat ( this . buildId ))
67- .addHeader (apiKeyHeaderName , this . visualRegressionTrackerConfig .getApiKey ())
68- .patch (RequestBody .create (JSON , "" ))
69- .build ();
74+ .url (paths . getBuildPathForBuild ( buildId ))
75+ .addHeader (API_KEY_HEADER , configuration .getApiKey ())
76+ .patch (RequestBody .create (JSON , "" ))
77+ .build ();
7078
71- try (Response response = client .newCall (request ).execute ()) {
72- handleResponse (response , Object .class );
73- }
79+ log .info ("Stopping Visual Regression Tracker for buildId <{}>" , buildId );
80+
81+ Response response = client .newCall (request ).execute ();
82+ handleResponse (response , Object .class );
83+
84+ log .info ("Visual Regression Tracker is stopped for buildId <{}>" , buildId );
7485 }
7586
76- public void track (String name , String imageBase64 , TestRunOptions testRunOptions ) throws IOException {
77- TestRunResponse testResultDTO = this .submitTestRun (name , imageBase64 , testRunOptions );
87+ public void track (String name , String imageBase64 , TestRunOptions testRunOptions )
88+ throws IOException {
89+ log .info ("Tracking test run <{}> with options <{}> for buildId <{}>" , name , testRunOptions , buildId );
90+ TestRunResponse testResultDTO = submitTestRun (name , imageBase64 , testRunOptions );
7891
7992 String errorMessage ;
8093 switch (testResultDTO .getStatus ()) {
@@ -90,57 +103,57 @@ public void track(String name, String imageBase64, TestRunOptions testRunOptions
90103 }
91104
92105 if (!errorMessage .isEmpty ()) {
93- if (this . visualRegressionTrackerConfig .getEnableSoftAssert ()) {
94- LOGGER .error (errorMessage );
106+ if (configuration .getEnableSoftAssert ()) {
107+ log .error (errorMessage );
95108 } else {
96109 throw new TestRunException (errorMessage );
97110 }
98111 }
99112 }
100113
101114 public void track (String name , String imageBase64 ) throws IOException {
102- this . track (name , imageBase64 , TestRunOptions .builder ().build ());
115+ track (name , imageBase64 , TestRunOptions .builder ().build ());
103116 }
104117
105118 protected boolean isStarted () {
106- return this . buildId != null && this . projectId != null ;
119+ return buildId != null && projectId != null ;
107120 }
108121
109- protected TestRunResponse submitTestRun (String name , String imageBase64 , TestRunOptions testRunOptions ) throws IOException {
110- if (!this .isStarted ()) {
111- throw new TestRunException ("Visual Regression Tracker has not been started" );
122+ protected TestRunResponse submitTestRun (String name , String imageBase64 ,
123+ TestRunOptions testRunOptions ) throws IOException {
124+ if (!isStarted ()) {
125+ throw new TestRunException (TRACKER_NOT_STARTED );
112126 }
113127
114128 TestRunRequest newTestRun = TestRunRequest .builder ()
115- .projectId (this . projectId )
116- .buildId (this . buildId )
117- .branchName (this . visualRegressionTrackerConfig .getBranchName ())
118- .name (name )
119- .imageBase64 (imageBase64 )
120- .os (testRunOptions .getOs ())
121- .browser (testRunOptions .getBrowser ())
122- .viewport (testRunOptions .getViewport ())
123- .device (testRunOptions .getDevice ())
124- .diffTollerancePercent (testRunOptions .getDiffTollerancePercent ())
125- .build ();
129+ .projectId (projectId )
130+ .buildId (buildId )
131+ .branchName (configuration .getBranchName ())
132+ .name (name )
133+ .imageBase64 (imageBase64 )
134+ .os (testRunOptions .getOs ())
135+ .browser (testRunOptions .getBrowser ())
136+ .viewport (testRunOptions .getViewport ())
137+ .device (testRunOptions .getDevice ())
138+ .diffTollerancePercent (testRunOptions .getDiffTollerancePercent ())
139+ .build ();
126140
127141 RequestBody body = RequestBody .create (JSON , gson .toJson (newTestRun ));
128142
129143 Request request = new Request .Builder ()
130- .url (this . visualRegressionTrackerConfig . getApiUrl (). concat ( "/test-runs" ))
131- .addHeader (apiKeyHeaderName , this . visualRegressionTrackerConfig .getApiKey ())
132- .post (body )
133- .build ();
144+ .url (paths . getTestRunPath ( ))
145+ .addHeader (API_KEY_HEADER , configuration .getApiKey ())
146+ .post (body )
147+ .build ();
134148
135- try (Response response = client .newCall (request ).execute ()) {
136- return handleResponse (response , TestRunResponse .class );
137- }
149+ Response response = client .newCall (request ).execute ();
150+ return handleResponse (response , TestRunResponse .class );
138151 }
139152
140153 protected <T > T handleResponse (Response response , Class <T > classOfT ) throws IOException {
141154 String responseBody = Optional .ofNullable (response .body ())
142- .orElseThrow (() -> new TestRunException ("Cannot get response body" ))
143- .string ();
155+ .orElseThrow (() -> new TestRunException ("Cannot get response body" ))
156+ .string ();
144157
145158 if (!response .isSuccessful ()) {
146159 throw new TestRunException (responseBody );
0 commit comments