5
5
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketBuildStatus .Status ;
6
6
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketRepository ;
7
7
import com .cloudbees .jenkins .plugins .bitbucket .client .BitbucketIntegrationClientFactory ;
8
- import com .cloudbees .jenkins .plugins .bitbucket .client .BitbucketIntegrationClientFactory .BitbucketServerIntegrationClient ;
9
8
import com .cloudbees .jenkins .plugins .bitbucket .client .BitbucketIntegrationClientFactory .IRequestAudit ;
10
9
import io .jenkins .cli .shaded .org .apache .commons .lang .RandomStringUtils ;
11
10
import java .io .InputStream ;
12
11
import java .nio .charset .StandardCharsets ;
13
12
import java .util .List ;
14
- import java .util .logging .Level ;
15
13
import org .apache .commons .io .IOUtils ;
16
14
import org .apache .http .client .methods .HttpHead ;
17
15
import org .apache .http .client .methods .HttpPost ;
18
16
import org .apache .http .client .methods .HttpRequestBase ;
19
- import org .apache .http .impl .client .CloseableHttpClient ;
20
17
import org .apache .http .impl .client .HttpClientBuilder ;
21
- import org .junit .ClassRule ;
22
- import org .junit .Rule ;
23
- import org .junit .Test ;
18
+ import org .junit .jupiter .api .Test ;
24
19
import org .jvnet .hudson .test .JenkinsRule ;
25
- import org .jvnet .hudson .test .LoggerRule ;
26
- import org .jvnet .hudson .test .WithoutJenkins ;
20
+ import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
27
21
import org .mockito .ArgumentCaptor ;
28
22
import org .mockito .MockedStatic ;
29
23
37
31
import static org .mockito .Mockito .mock ;
38
32
import static org .mockito .Mockito .mockStatic ;
39
33
import static org .mockito .Mockito .verify ;
40
- import static org .mockito .Mockito .when ;
41
34
42
- public class BitbucketServerAPIClientTest {
43
-
44
- @ ClassRule
45
- public static JenkinsRule r = new JenkinsRule ();
46
- @ Rule
47
- public LoggerRule logger = new LoggerRule ().record (BitbucketServerIntegrationClient .class , Level .FINE );
35
+ @ WithJenkins
36
+ class BitbucketServerAPIClientTest {
48
37
49
38
@ Test
50
- @ WithoutJenkins
51
- public void verify_status_notitication_name_max_length () throws Exception {
39
+ void verify_status_notitication_name_max_length (JenkinsRule j ) throws Exception {
52
40
BitbucketApi client = BitbucketIntegrationClientFactory .getApiMockClient ("https://acme.bitbucket.org" );
53
41
BitbucketBuildStatus status = new BitbucketBuildStatus ();
54
42
status .setName (RandomStringUtils .randomAlphanumeric (300 ));
@@ -57,8 +45,7 @@ public void verify_status_notitication_name_max_length() throws Exception {
57
45
58
46
client .postBuildStatus (status );
59
47
60
- IRequestAudit clientAudit = ((IRequestAudit ) client ).getAudit ();
61
- HttpRequestBase request = extractRequest (clientAudit );
48
+ HttpRequestBase request = extractRequest (client );
62
49
assertThat (request ).isNotNull ()
63
50
.isInstanceOf (HttpPost .class );
64
51
try (InputStream content = ((HttpPost ) request ).getEntity ().getContent ()) {
@@ -67,20 +54,21 @@ public void verify_status_notitication_name_max_length() throws Exception {
67
54
}
68
55
}
69
56
70
- private HttpRequestBase extractRequest (IRequestAudit clientAudit ) {
57
+ private HttpRequestBase extractRequest (BitbucketApi client ) {
58
+ assertThat (client ).isInstanceOf (IRequestAudit .class );
59
+ IRequestAudit clientAudit = ((IRequestAudit ) client ).getAudit ();
60
+
71
61
ArgumentCaptor <HttpRequestBase > captor = ArgumentCaptor .forClass (HttpRequestBase .class );
72
62
verify (clientAudit ).request (captor .capture ());
73
63
return captor .getValue ();
74
64
}
75
65
76
66
@ Test
77
- @ WithoutJenkins
78
- public void verify_checkPathExists_given_a_path () throws Exception {
67
+ void verify_checkPathExists_given_a_path (JenkinsRule j ) throws Exception {
79
68
BitbucketApi client = BitbucketIntegrationClientFactory .getApiMockClient ("https://acme.bitbucket.org" );
80
69
assertThat (client .checkPathExists ("feature/pipeline" , "folder/Jenkinsfile" )).isTrue ();
81
70
82
- IRequestAudit clientAudit = ((IRequestAudit ) client ).getAudit ();
83
- HttpRequestBase request = extractRequest (clientAudit );
71
+ HttpRequestBase request = extractRequest (client );
84
72
assertThat (request ).isNotNull ()
85
73
.isInstanceOfSatisfying (HttpHead .class , head -> {
86
74
assertThat (head .getURI ())
@@ -90,20 +78,18 @@ public void verify_checkPathExists_given_a_path() throws Exception {
90
78
}
91
79
92
80
@ Test
93
- @ WithoutJenkins
94
- public void verify_checkPathExists_given_file () throws Exception {
81
+ void verify_checkPathExists_given_file (JenkinsRule j ) throws Exception {
95
82
BitbucketApi client = BitbucketIntegrationClientFactory .getApiMockClient ("https://acme.bitbucket.org" );
96
83
assertThat (client .checkPathExists ("feature/pipeline" , "Jenkinsfile" )).isTrue ();
97
84
98
- IRequestAudit clientAudit = ((IRequestAudit ) client ).getAudit ();
99
- HttpRequestBase request = extractRequest (clientAudit );
85
+ HttpRequestBase request = extractRequest (client );
100
86
assertThat (request ).isNotNull ()
101
87
.isInstanceOfSatisfying (HttpHead .class , head ->
102
88
assertThat (head .getURI ()).hasPath ("/rest/api/1.0/projects/amuniz/repos/test-repos/browse/Jenkinsfile" ));
103
89
}
104
90
105
91
@ Test
106
- public void filterArchivedRepositories () throws Exception {
92
+ void filterArchivedRepositories (JenkinsRule j ) throws Exception {
107
93
BitbucketApi client = BitbucketIntegrationClientFactory .getClient ("localhost" , "foo" , "test-repos" );
108
94
List <? extends BitbucketRepository > repos = client .getRepositories ();
109
95
List <String > names = repos .stream ().map (BitbucketRepository ::getRepositoryName ).toList ();
@@ -112,22 +98,19 @@ public void filterArchivedRepositories() throws Exception {
112
98
}
113
99
114
100
@ Test
115
- public void sortRepositoriesByName () throws Exception {
101
+ void sortRepositoriesByName (JenkinsRule j ) throws Exception {
116
102
BitbucketApi client = BitbucketIntegrationClientFactory .getClient ("localhost" , "amuniz" , "test-repos" );
117
103
List <? extends BitbucketRepository > repos = client .getRepositories ();
118
104
List <String > names = repos .stream ().map (BitbucketRepository ::getRepositoryName ).toList ();
119
105
assertThat (names , is (List .of ("another-repo" , "dogs-repo" , "test-repos" )));
120
106
}
121
107
122
108
@ Test
123
- public void disableCookieManager () throws Exception {
109
+ void disableCookieManager (JenkinsRule j ) throws Exception {
124
110
try (MockedStatic <HttpClientBuilder > staticHttpClientBuilder = mockStatic (HttpClientBuilder .class )) {
125
111
HttpClientBuilder httpClientBuilder = mock (HttpClientBuilder .class , RETURNS_SELF );
126
112
staticHttpClientBuilder .when (HttpClientBuilder ::create ).thenReturn (httpClientBuilder );
127
- CloseableHttpClient httpClient = mock (CloseableHttpClient .class );
128
- when (httpClientBuilder .build ()).thenReturn (httpClient );
129
- BitbucketApi client = BitbucketIntegrationClientFactory .getClient ("localhost" , "amuniz" , "test-repos" );
130
- client .getRepositories ();
113
+ BitbucketIntegrationClientFactory .getClient ("localhost" , "amuniz" , "test-repos" );
131
114
verify (httpClientBuilder ).disableCookieManagement ();
132
115
}
133
116
}
0 commit comments