12
12
import org .hamcrest .CoreMatchers ;
13
13
import org .hamcrest .MatcherAssert ;
14
14
import 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 ;
17
17
import org .testng .annotations .DataProvider ;
18
18
import org .testng .annotations .Test ;
19
19
20
20
import java .io .IOException ;
21
21
22
22
public 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 (
27
25
"http://localhost" ,
28
26
"733c148e-ef70-4e6d-9ae5-ab22263697cc" ,
29
- "BAZ0EG0PRH4CRQPH19ZKAVADBP9E1 " ,
27
+ "XHGDZDFD3GMJDNM87JKEMP0JS1G5 " ,
30
28
"develop"
31
29
);
30
+ private MockWebServer server ;
31
+ private VisualRegressionTracker vrt ;
32
32
33
33
@ SneakyThrows
34
- @ BeforeTest
35
- void setup () {
34
+ @ BeforeMethod
35
+ public void setup () {
36
36
server = new MockWebServer ();
37
37
server .start ();
38
38
39
39
// target to mock server
40
- this .config .apiUrl = server .url ("/" ).toString ();
40
+ this .config .setApiUrl ( server .url ("/" ).toString () );
41
41
vrt = new VisualRegressionTracker (config );
42
42
}
43
43
44
44
@ SneakyThrows
45
- @ AfterTest
46
- void tearDown () {
45
+ @ AfterMethod
46
+ public void tearDown () {
47
47
server .shutdown ();
48
48
}
49
49
50
50
@ Test
51
- void shouldStartBuild () throws IOException , InterruptedException {
51
+ public void shouldStartBuild () throws IOException , InterruptedException {
52
52
String buildId = "123123" ;
53
53
String projectId = "projectId" ;
54
54
BuildRequest buildRequest = BuildRequest .builder ()
55
- .branchName (this .config .branchName )
56
- .project (this .config .project )
55
+ .branchName (this .config .getBranchName () )
56
+ .project (this .config .getProject () )
57
57
.build ();
58
58
BuildResponse buildResponse = BuildResponse .builder ()
59
59
.id (buildId )
60
60
.projectId (projectId )
61
61
.build ();
62
- server .enqueue (new MockResponse ().setBody (gson .toJson (buildResponse )));
62
+ server .enqueue (new MockResponse ()
63
+ .setResponseCode (200 )
64
+ .setBody (gson .toJson (buildResponse )));
63
65
64
66
vrt .startBuild ();
65
67
66
68
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 () ));
68
70
MatcherAssert .assertThat (request .getBody ().readUtf8 (), CoreMatchers .is (gson .toJson (buildRequest )));
69
71
MatcherAssert .assertThat (vrt .buildId , CoreMatchers .is (buildId ));
70
72
MatcherAssert .assertThat (vrt .projectId , CoreMatchers .is (projectId ));
71
73
}
72
74
73
75
@ 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 {
75
107
String buildId = "123123" ;
76
108
String projectId = "projectId" ;
77
109
String name = "Test name" ;
@@ -85,7 +117,7 @@ void shouldSubmitTestRun() throws IOException, InterruptedException {
85
117
.build ();
86
118
TestRunRequest testRunRequest = TestRunRequest .builder ()
87
119
.projectId (projectId )
88
- .branchName (config .branchName )
120
+ .branchName (config .getBranchName () )
89
121
.buildId (buildId )
90
122
.name (name )
91
123
.imageBase64 (imageBase64 )
@@ -100,11 +132,12 @@ void shouldSubmitTestRun() throws IOException, InterruptedException {
100
132
.build ();
101
133
server .enqueue (new MockResponse ().setBody (gson .toJson (testRunResponse )));
102
134
vrt .buildId = buildId ;
135
+ vrt .projectId = projectId ;
103
136
104
137
TestRunResponse result = vrt .submitTestRun (name , imageBase64 , testRunOptions );
105
138
106
139
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 () ));
108
141
MatcherAssert .assertThat (request .getBody ().readUtf8 (), CoreMatchers .is (gson .toJson (testRunRequest )));
109
142
MatcherAssert .assertThat (gson .toJson (result ), CoreMatchers .is (gson .toJson (testRunResponse )));
110
143
}
@@ -129,7 +162,6 @@ public Object[][] shouldTrackThrowExceptionCases() {
129
162
};
130
163
}
131
164
132
-
133
165
@ Test (dataProvider = "shouldTrackThrowExceptionCases" )
134
166
public void shouldTrackThrowException (TestRunResponse testRunResponse , String expectedExceptionMessage ) throws IOException {
135
167
VisualRegressionTracker vrtMocked = Mockito .mock (VisualRegressionTracker .class );
0 commit comments