11package io .visual_regression_tracker .sdk_java ;
22
33import com .google .gson .Gson ;
4+ import io .visual_regression_tracker .sdk_java .request .BuildRequest ;
5+ import io .visual_regression_tracker .sdk_java .request .TestRunRequest ;
6+ import io .visual_regression_tracker .sdk_java .response .BuildResponse ;
7+ import io .visual_regression_tracker .sdk_java .response .TestRunResponse ;
48import okhttp3 .*;
59
610import java .io .IOException ;
7- import java .util .HashMap ;
8- import java .util .Map ;
911
1012public class VisualRegressionTracker {
1113 static final MediaType JSON = MediaType .get ("application/json; charset=utf-8" );
14+ String apiKeyHeaderName = "apiKey" ;
15+ Gson gson = new Gson ();
1216 VisualRegressionTrackerConfig visualRegressionTrackerConfig ;
1317 String buildId ;
1418 OkHttpClient client ;
@@ -21,61 +25,62 @@ public VisualRegressionTracker(VisualRegressionTrackerConfig visualRegressionTra
2125
2226 void startBuild () throws IOException {
2327 if (this .buildId == null ) {
24- Map <String , String > data = new HashMap <>();
25- data .put ("projectId" , this .visualRegressionTrackerConfig .projectId );
26- data .put ("branchName" , this .visualRegressionTrackerConfig .branchName );
28+ BuildRequest newBuild = BuildRequest .builder ()
29+ .branchName (this .visualRegressionTrackerConfig .branchName )
30+ .projectId (this .visualRegressionTrackerConfig .projectId )
31+ .build ();
2732
28- RequestBody body = RequestBody .create (new Gson () .toJson (data ), JSON );
33+ RequestBody body = RequestBody .create (gson .toJson (newBuild ), JSON );
2934
3035 Request request = new Request .Builder ()
3136 .url (this .visualRegressionTrackerConfig .apiUrl .concat ("/builds" ))
32- .addHeader ("apiKey" , this .visualRegressionTrackerConfig .apiKey )
37+ .addHeader (apiKeyHeaderName , this .visualRegressionTrackerConfig .apiKey )
3338 .post (body )
3439 .build ();
3540
3641 try (ResponseBody responseBody = client .newCall (request ).execute ().body ()) {
37- BuildDTO buildDTO = new Gson ().fromJson (responseBody .string (), BuildDTO .class );
42+ BuildResponse buildDTO = new Gson ().fromJson (responseBody .string (), BuildResponse .class );
3843 this .buildId = buildDTO .getId ();
3944 }
4045 }
4146 }
4247
43- TestResultDTO submitTestRun (String name , String imageBase64 , TestRunOptions testRunOptions ) throws IOException {
44- Map <String , Object > data = new HashMap <>();
45- data .put ("projectId" , this .visualRegressionTrackerConfig .projectId );
46- data .put ("buildId" , this .buildId );
47- data .put ("name" , name );
48- data .put ("imageBase64" , imageBase64 );
49- data .put ("os" , testRunOptions .getOs ());
50- data .put ("browser" , testRunOptions .getBrowser ());
51- data .put ("viewport" , testRunOptions .getViewport ());
52- data .put ("device" , testRunOptions .getDevice ());
53- data .put ("diffTollerancePercent" , testRunOptions .getDiffTollerancePercent ());
48+ TestRunResponse submitTestRun (String name , String imageBase64 , TestRunOptions testRunOptions ) throws IOException {
49+ TestRunRequest newTestRun = TestRunRequest .builder ()
50+ .projectId (this .visualRegressionTrackerConfig .projectId )
51+ .buildId (this .buildId )
52+ .name (name )
53+ .imageBase64 (imageBase64 )
54+ .os (testRunOptions .getOs ())
55+ .browser (testRunOptions .getBrowser ())
56+ .viewport (testRunOptions .getViewport ())
57+ .device (testRunOptions .getDevice ())
58+ .diffTollerancePercent (testRunOptions .getDiffTollerancePercent ())
59+ .build ();
5460
55- RequestBody body = RequestBody .create (new Gson () .toJson (data ), JSON );
61+ RequestBody body = RequestBody .create (gson .toJson (newTestRun ), JSON );
5662
5763 Request request = new Request .Builder ()
5864 .url (this .visualRegressionTrackerConfig .apiUrl .concat ("/test" ))
59- .addHeader ("apiKey" , this .visualRegressionTrackerConfig .apiKey )
65+ .addHeader (apiKeyHeaderName , this .visualRegressionTrackerConfig .apiKey )
6066 .post (body )
6167 .build ();
6268
63-
6469 try (ResponseBody responseBody = client .newCall (request ).execute ().body ()) {
65- return new Gson () .fromJson (responseBody .string (), TestResultDTO .class );
70+ return gson .fromJson (responseBody .string (), TestRunResponse .class );
6671 }
6772 }
6873
6974 public void track (String name , String imageBase64 , TestRunOptions testRunOptions ) throws IOException {
7075 this .startBuild ();
7176
72- TestResultDTO testResultDTO = this .submitTestRun (name , imageBase64 , testRunOptions );
77+ TestRunResponse testResultDTO = this .submitTestRun (name , imageBase64 , testRunOptions );
7378
74- if (testResultDTO .getStatus ().equals ("new" )) {
79+ if (testResultDTO .getStatus ().equals (TestRunStatus . NEW )) {
7580 throw new TestRunException ("No baseline: " .concat (testResultDTO .getUrl ()));
7681 }
7782
78- if (testResultDTO .getStatus ().equals ("unresolved" )) {
83+ if (testResultDTO .getStatus ().equals (TestRunStatus . UNRESOLVED )) {
7984 throw new TestRunException ("Difference found: " .concat (testResultDTO .getUrl ()));
8085 }
8186 }
0 commit comments