Skip to content

Commit e59e244

Browse files
committed
Update some unit test to JUnit5, using BitbucketApiUtils in tests to understand if the serverURL is cloud or server.
Add the bitbucket cloud bad server url to the matches logic.
1 parent 0f77318 commit e59e244

File tree

3 files changed

+21
-48
lines changed

3 files changed

+21
-48
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/util/BitbucketApiUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static boolean isCloud(BitbucketApi client) {
3535
}
3636

3737
public static boolean isCloud(@NonNull String serverURL) {
38-
return StringUtils.startsWith(serverURL, BitbucketCloudEndpoint.SERVER_URL);
38+
return StringUtils.startsWithAny(serverURL, new String[] { BitbucketCloudEndpoint.SERVER_URL, BitbucketCloudEndpoint.BAD_SERVER_URL });
3939
}
4040

4141
public static ListBoxModel getFromBitbucket(SCMSourceOwner context,

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketIntegrationClientFactory.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
2727
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
28-
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
28+
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketApiUtils;
2929
import com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient;
3030
import java.io.IOException;
3131
import java.io.InputStream;
@@ -73,8 +73,7 @@ default CloseableHttpResponse loadResponseFromResources(Class<?> resourceBase, S
7373
}
7474

7575
public static BitbucketApi getClient(String payloadRootPath, String serverURL, String owner, String repositoryName) {
76-
if (BitbucketCloudEndpoint.SERVER_URL.equals(serverURL) ||
77-
BitbucketCloudEndpoint.BAD_SERVER_URL.equals(serverURL)) {
76+
if (BitbucketApiUtils.isCloud(serverURL)) {
7877
return new BitbucketClouldIntegrationClient(payloadRootPath, owner, repositoryName);
7978
} else {
8079
return new BitbucketServerIntegrationClient(payloadRootPath, serverURL, owner, repositoryName);
@@ -169,13 +168,4 @@ public static BitbucketApi getApiMockClient(String serverURL) {
169168
return BitbucketIntegrationClientFactory.getClient(null, serverURL, "amuniz", "test-repos");
170169
}
171170

172-
private static CloseableHttpResponse createRateLimitResponse() {
173-
StatusLine statusLine = mock(StatusLine.class);
174-
when(statusLine.getStatusCode()).thenReturn(429);
175-
176-
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
177-
when(response.getStatusLine()).thenReturn(statusLine);
178-
179-
return response;
180-
}
181171
}

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClientTest.java

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,19 @@
55
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketBuildStatus.Status;
66
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
77
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory;
8-
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory.BitbucketServerIntegrationClient;
98
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory.IRequestAudit;
109
import io.jenkins.cli.shaded.org.apache.commons.lang.RandomStringUtils;
1110
import java.io.InputStream;
1211
import java.nio.charset.StandardCharsets;
1312
import java.util.List;
14-
import java.util.logging.Level;
1513
import org.apache.commons.io.IOUtils;
1614
import org.apache.http.client.methods.HttpHead;
1715
import org.apache.http.client.methods.HttpPost;
1816
import org.apache.http.client.methods.HttpRequestBase;
19-
import org.apache.http.impl.client.CloseableHttpClient;
2017
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;
2419
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;
2721
import org.mockito.ArgumentCaptor;
2822
import org.mockito.MockedStatic;
2923

@@ -37,18 +31,12 @@
3731
import static org.mockito.Mockito.mock;
3832
import static org.mockito.Mockito.mockStatic;
3933
import static org.mockito.Mockito.verify;
40-
import static org.mockito.Mockito.when;
4134

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 {
4837

4938
@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 {
5240
BitbucketApi client = BitbucketIntegrationClientFactory.getApiMockClient("https://acme.bitbucket.org");
5341
BitbucketBuildStatus status = new BitbucketBuildStatus();
5442
status.setName(RandomStringUtils.randomAlphanumeric(300));
@@ -57,8 +45,7 @@ public void verify_status_notitication_name_max_length() throws Exception {
5745

5846
client.postBuildStatus(status);
5947

60-
IRequestAudit clientAudit = ((IRequestAudit) client).getAudit();
61-
HttpRequestBase request = extractRequest(clientAudit);
48+
HttpRequestBase request = extractRequest(client);
6249
assertThat(request).isNotNull()
6350
.isInstanceOf(HttpPost.class);
6451
try (InputStream content = ((HttpPost) request).getEntity().getContent()) {
@@ -67,20 +54,21 @@ public void verify_status_notitication_name_max_length() throws Exception {
6754
}
6855
}
6956

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+
7161
ArgumentCaptor<HttpRequestBase> captor = ArgumentCaptor.forClass(HttpRequestBase.class);
7262
verify(clientAudit).request(captor.capture());
7363
return captor.getValue();
7464
}
7565

7666
@Test
77-
@WithoutJenkins
78-
public void verify_checkPathExists_given_a_path() throws Exception {
67+
void verify_checkPathExists_given_a_path(JenkinsRule j) throws Exception {
7968
BitbucketApi client = BitbucketIntegrationClientFactory.getApiMockClient("https://acme.bitbucket.org");
8069
assertThat(client.checkPathExists("feature/pipeline", "folder/Jenkinsfile")).isTrue();
8170

82-
IRequestAudit clientAudit = ((IRequestAudit) client).getAudit();
83-
HttpRequestBase request = extractRequest(clientAudit);
71+
HttpRequestBase request = extractRequest(client);
8472
assertThat(request).isNotNull()
8573
.isInstanceOfSatisfying(HttpHead.class, head -> {
8674
assertThat(head.getURI())
@@ -90,20 +78,18 @@ public void verify_checkPathExists_given_a_path() throws Exception {
9078
}
9179

9280
@Test
93-
@WithoutJenkins
94-
public void verify_checkPathExists_given_file() throws Exception {
81+
void verify_checkPathExists_given_file(JenkinsRule j) throws Exception {
9582
BitbucketApi client = BitbucketIntegrationClientFactory.getApiMockClient("https://acme.bitbucket.org");
9683
assertThat(client.checkPathExists("feature/pipeline", "Jenkinsfile")).isTrue();
9784

98-
IRequestAudit clientAudit = ((IRequestAudit) client).getAudit();
99-
HttpRequestBase request = extractRequest(clientAudit);
85+
HttpRequestBase request = extractRequest(client);
10086
assertThat(request).isNotNull()
10187
.isInstanceOfSatisfying(HttpHead.class, head ->
10288
assertThat(head.getURI()).hasPath("/rest/api/1.0/projects/amuniz/repos/test-repos/browse/Jenkinsfile"));
10389
}
10490

10591
@Test
106-
public void filterArchivedRepositories() throws Exception {
92+
void filterArchivedRepositories(JenkinsRule j) throws Exception {
10793
BitbucketApi client = BitbucketIntegrationClientFactory.getClient("localhost", "foo", "test-repos");
10894
List<? extends BitbucketRepository> repos = client.getRepositories();
10995
List<String> names = repos.stream().map(BitbucketRepository::getRepositoryName).toList();
@@ -112,22 +98,19 @@ public void filterArchivedRepositories() throws Exception {
11298
}
11399

114100
@Test
115-
public void sortRepositoriesByName() throws Exception {
101+
void sortRepositoriesByName(JenkinsRule j) throws Exception {
116102
BitbucketApi client = BitbucketIntegrationClientFactory.getClient("localhost", "amuniz", "test-repos");
117103
List<? extends BitbucketRepository> repos = client.getRepositories();
118104
List<String> names = repos.stream().map(BitbucketRepository::getRepositoryName).toList();
119105
assertThat(names, is(List.of("another-repo", "dogs-repo", "test-repos")));
120106
}
121107

122108
@Test
123-
public void disableCookieManager() throws Exception {
109+
void disableCookieManager(JenkinsRule j) throws Exception {
124110
try (MockedStatic<HttpClientBuilder> staticHttpClientBuilder = mockStatic(HttpClientBuilder.class)) {
125111
HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class, RETURNS_SELF);
126112
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");
131114
verify(httpClientBuilder).disableCookieManagement();
132115
}
133116
}

0 commit comments

Comments
 (0)