1010import okhttp3 .Request ;
1111import okhttp3 .RequestBody ;
1212import okhttp3 .Response ;
13- import okhttp3 .ResponseBody ;
1413
1514import java .io .IOException ;
1615import java .util .Optional ;
@@ -56,9 +55,11 @@ void startBuild() throws IOException {
5655 String responseBody = Optional .ofNullable (response .body ())
5756 .orElseThrow (() -> new TestRunException ("Cannot get response body" ))
5857 .string ();
59- BuildResponse buildDTO = new Gson ().fromJson (responseBody , BuildResponse .class );
60- this .buildId = buildDTO .getId ();
61- this .projectId = buildDTO .getProjectId ();
58+ BuildResponse buildDTO = gson .fromJson (responseBody , BuildResponse .class );
59+ this .buildId = Optional .ofNullable (buildDTO .getId ())
60+ .orElseThrow (() -> new TestRunException ("Build id is null" ));
61+ this .projectId = Optional .ofNullable (buildDTO .getProjectId ())
62+ .orElseThrow (() -> new TestRunException ("Project id is null" ));
6263 }
6364 }
6465 }
@@ -85,8 +86,11 @@ TestRunResponse submitTestRun(String name, String imageBase64, TestRunOptions te
8586 .post (body )
8687 .build ();
8788
88- try (ResponseBody responseBody = client .newCall (request ).execute ().body ()) {
89- return gson .fromJson (responseBody .string (), TestRunResponse .class );
89+ try (Response response = client .newCall (request ).execute ()) {
90+ String responseBody = Optional .ofNullable (response .body ())
91+ .orElseThrow (() -> new TestRunException ("Cannot get response body" ))
92+ .string ();
93+ return gson .fromJson (responseBody , TestRunResponse .class );
9094 }
9195 }
9296
@@ -95,11 +99,14 @@ public void track(String name, String imageBase64, TestRunOptions testRunOptions
9599
96100 TestRunResponse testResultDTO = this .submitTestRun (name , imageBase64 , testRunOptions );
97101
98- if (testResultDTO .getStatus ().equals (TestRunStatus .NEW )) {
102+ TestRunStatus status = Optional .ofNullable (testResultDTO .getStatus ())
103+ .orElseThrow (() -> new TestRunException ("Status is null" ));
104+
105+ if (status .equals (TestRunStatus .NEW )) {
99106 throw new TestRunException ("No baseline: " .concat (testResultDTO .getUrl ()));
100107 }
101108
102- if (testResultDTO . getStatus () .equals (TestRunStatus .UNRESOLVED )) {
109+ if (status .equals (TestRunStatus .UNRESOLVED )) {
103110 throw new TestRunException ("Difference found: " .concat (testResultDTO .getUrl ()));
104111 }
105112 }
0 commit comments