|
| 1 | +package io.jenkins.plugins.gitlabbranchsource; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import jenkins.model.JenkinsLocationConfiguration; |
| 5 | +import org.junit.ClassRule; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.junit.runners.Parameterized; |
| 9 | +import org.junit.runners.Parameterized.Parameters; |
| 10 | +import org.jvnet.hudson.test.JenkinsRule; |
| 11 | + |
| 12 | +import static org.hamcrest.Matchers.containsString; |
| 13 | +import static org.hamcrest.Matchers.not; |
| 14 | +import static org.hamcrest.core.Is.is; |
| 15 | +import static org.junit.Assert.assertThat; |
| 16 | + |
| 17 | +@RunWith(Parameterized.class) |
| 18 | +public class GitLabHookCreatorParameterizedTest { |
| 19 | + |
| 20 | + @ClassRule |
| 21 | + public static JenkinsRule r = new JenkinsRule(); |
| 22 | + |
| 23 | + private final String jenkinsUrl; |
| 24 | + private final boolean hookType; |
| 25 | + private final String expectedPath; |
| 26 | + |
| 27 | + @Parameters(name = "check {0}") |
| 28 | + public static Iterable<Object[]> data() { |
| 29 | + return Arrays.asList(new Object[][]{ |
| 30 | + {"intranet.local:8080", false, "/gitlab-systemhook/post"}, |
| 31 | + {"intranet.local", true, "/gitlab-webhook/post"}, |
| 32 | + {"www.mydomain.com:8000", true, "/gitlab-webhook/post"}, |
| 33 | + {"www.mydomain.com", false, "/gitlab-systemhook/post"} |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + public GitLabHookCreatorParameterizedTest(String jenkinsUrl, boolean hookType, String expectedPath) { |
| 38 | + this.jenkinsUrl = jenkinsUrl; |
| 39 | + this.hookType = hookType; |
| 40 | + this.expectedPath = expectedPath; |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void hookUrl() { |
| 45 | + Arrays.asList("http://", "https://").forEach( |
| 46 | + proto -> { |
| 47 | + String expected = proto + jenkinsUrl + expectedPath; |
| 48 | + JenkinsLocationConfiguration.get().setUrl(proto + jenkinsUrl); |
| 49 | + String hookUrl = GitLabHookCreator.getHookUrl(hookType); |
| 50 | + GitLabHookCreator.checkURL(hookUrl); |
| 51 | + assertThat(hookUrl.replaceAll(proto, ""), not(containsString("//"))); |
| 52 | + assertThat(hookUrl, is(expected)); |
| 53 | + }); |
| 54 | + } |
| 55 | +} |
0 commit comments