|
| 1 | +package com.cloudbees.jenkins.plugins.bitbucket; |
| 2 | + |
| 3 | +import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi; |
| 4 | +import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint; |
| 5 | +import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration; |
| 6 | +import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint; |
| 7 | +import java.util.Arrays; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.ClassRule; |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.rules.ExpectedException; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | +import org.junit.runners.Parameterized; |
| 15 | +import org.junit.runners.Parameterized.Parameters; |
| 16 | +import org.jvnet.hudson.test.JenkinsRule; |
| 17 | + |
| 18 | +import static com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory.getApiMockClient; |
| 19 | +import static org.junit.Assert.assertNotNull; |
| 20 | + |
| 21 | +@RunWith(Parameterized.class) |
| 22 | +public class BitbucketCheckURLTest { |
| 23 | + |
| 24 | + private static final String BITBUCKET_SERVER_URL = "https://bitbucket.server"; |
| 25 | + private static final Class<IllegalStateException> ISE = IllegalStateException.class; |
| 26 | + private static final String localHost = "Jenkins URL cannot start with http://localhost"; |
| 27 | + private static final String CLOUD = "cloud"; |
| 28 | + private static final String SERVER = "server"; |
| 29 | + private static final String BOTH = String.format("%s and %s", CLOUD, SERVER); |
| 30 | + private static final String FQDN = "Please use a fully qualified name or an IP address for Jenkins URL, this is required by Bitbucket cloud"; |
| 31 | + private static final String DETERMINE = "Could not determine Jenkins URL."; |
| 32 | + private final String jenkinsUrl; |
| 33 | + private final Class<? extends Exception> expectedException; |
| 34 | + private final String expectedExceptionMsg; |
| 35 | + private final String serverOrCloud; |
| 36 | + private static BitbucketApi bitbucketServer; |
| 37 | + private static BitbucketApi bitbucketCloud; |
| 38 | + |
| 39 | + @ClassRule |
| 40 | + public static JenkinsRule r = new JenkinsRule(); |
| 41 | + |
| 42 | + @Rule |
| 43 | + public ExpectedException thrown = ExpectedException.none(); |
| 44 | + |
| 45 | + public BitbucketCheckURLTest(String jenkinsUrl, |
| 46 | + String expectedExceptionMsg, |
| 47 | + Class<? extends Exception> expectedException, String serverOrCloud) { |
| 48 | + this.jenkinsUrl = jenkinsUrl; |
| 49 | + this.expectedException = expectedException; |
| 50 | + this.expectedExceptionMsg = expectedExceptionMsg; |
| 51 | + this.serverOrCloud = serverOrCloud; |
| 52 | + } |
| 53 | + |
| 54 | + @Parameters(name = "check {0} URL against {3}") |
| 55 | + public static Iterable<Object[]> data() { |
| 56 | + return Arrays.asList(new Object[][]{ |
| 57 | + {"localhost", localHost, ISE, BOTH}, |
| 58 | + {"unconfigured-jenkins-location", DETERMINE, ISE, BOTH}, |
| 59 | + {"intranet", FQDN, ISE, CLOUD}, |
| 60 | + {"intranet:8080", FQDN, ISE, CLOUD}, |
| 61 | + {"localhost.local", null, null, BOTH}, |
| 62 | + {"intranet.local:8080", null, null, BOTH}, |
| 63 | + {"www.mydomain.com:8000", null, null, BOTH}, |
| 64 | + {"www.mydomain.com", null, null, BOTH} |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + @BeforeClass |
| 69 | + public static void setUp() { |
| 70 | + BitbucketEndpointConfiguration instance = BitbucketEndpointConfiguration.get(); |
| 71 | + instance.setEndpoints(Arrays.asList( |
| 72 | + new BitbucketServerEndpoint( |
| 73 | + "Bitbucket Server", |
| 74 | + BITBUCKET_SERVER_URL, |
| 75 | + true, |
| 76 | + "dummy"), |
| 77 | + new BitbucketCloudEndpoint( |
| 78 | + true, |
| 79 | + "second") |
| 80 | + )); |
| 81 | + bitbucketServer = getApiMockClient(BITBUCKET_SERVER_URL); |
| 82 | + bitbucketCloud = getApiMockClient(BitbucketCloudEndpoint.SERVER_URL); |
| 83 | + } |
| 84 | + |
| 85 | + private void setupException() { |
| 86 | + if (expectedException != null) { |
| 87 | + thrown.expect(expectedException); |
| 88 | + thrown.expectMessage(expectedExceptionMsg); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private void doubleTrouble(BitbucketApi bitbucketApi) { |
| 93 | + Arrays.asList("http://", "https://").forEach( |
| 94 | + url -> assertNotNull(BitbucketBuildStatusNotifications |
| 95 | + .checkURL(url + jenkinsUrl + "/build/sample", bitbucketApi))); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void checkURLForBitbucketServer() { |
| 100 | + if (serverOrCloud.contains(SERVER)) { |
| 101 | + setupException(); |
| 102 | + } |
| 103 | + doubleTrouble(bitbucketServer); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void checkURLForBitbucketCloud() { |
| 108 | + if (serverOrCloud.contains(CLOUD)) { |
| 109 | + setupException(); |
| 110 | + } |
| 111 | + doubleTrouble(bitbucketCloud); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments