diff --git a/src/test/java/org/jenkinsci/plugins/badge/actions/JobBadgeActionTest.java b/src/test/java/org/jenkinsci/plugins/badge/actions/JobBadgeActionTest.java index 12d88133..4e8d2a77 100644 --- a/src/test/java/org/jenkinsci/plugins/badge/actions/JobBadgeActionTest.java +++ b/src/test/java/org/jenkinsci/plugins/badge/actions/JobBadgeActionTest.java @@ -23,12 +23,15 @@ */ package org.jenkinsci.plugins.badge.actions; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import hudson.model.FreeStyleProject; import hudson.model.Result; +import java.util.Random; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; @@ -39,10 +42,12 @@ public class JobBadgeActionTest { @ClassRule public static JenkinsRule j = new JenkinsRule(); - private static final String NOT_BUILT_JOB_NAME = "not-built-job"; + private static final Random random = new Random(); + + private static final String NOT_BUILT_JOB_NAME = "not-built-job-" + random.nextInt(10000); private static JobBadgeAction notBuiltAction; - private static final String SUCCESSFUL_JOB_NAME = "successful-job"; + private static final String SUCCESSFUL_JOB_NAME = "successful-job-" + random.nextInt(10000); private static JobBadgeAction successfulAction; public JobBadgeActionTest() {} @@ -59,6 +64,89 @@ public static void createAction() throws Exception { notBuiltAction = new JobBadgeAction(notBuiltJob); } + @Test + public void testBadgeStatusIcon() throws Exception { + checkBadgeStatus("icon"); + } + + @Test + public void testBadgeStatusIconSvg() throws Exception { + checkBadgeStatus("icon.svg"); + } + + @Test + public void testBadgeStatusIconWithBuild() throws Exception { + checkBadgeStatus("icon", 1); + } + + @Test + public void testBadgeStatusIconSvgWithBuild() throws Exception { + checkBadgeStatus("icon.svg", 1); + } + + private void checkBadgeStatus(String lastComponent) throws Exception { + checkBadgeStatus(lastComponent, null); + } + + private void checkBadgeStatus(String lastComponent, Integer build) throws Exception { + // Create an instance of JobBadgeAction + JobBadgeAction action = new JobBadgeAction(successfulAction.project); + + String link = "https://jenkins.io"; + String status = "my-status-" + random.nextInt(); + String subject = "my-subject-" + random.nextInt(); + String[] styles = {"plastic", "flat", "flat-square"}; + String style = styles[random.nextInt(styles.length)]; + String expectedStyle; + String unexpectedStyle; + switch (style) { + case "plastic": + expectedStyle = ""; + break; + case "flat": + expectedStyle = ""; + unexpectedStyle = ""; + unexpectedStyle = "fill-opacity=\".3\">" + subject + ""; + break; + default: + expectedStyle = "not-a-valid-style"; + unexpectedStyle = "never-should-be-used"; + break; + } + + // Get the Jenkins URL + String jenkinsUrl = j.getURL().toString() + "job/" + action.getUrlEncodedFullName() + "/badge/" + lastComponent; + String badgeUrl = jenkinsUrl + "?subject=" + subject; + if (build != null) { + badgeUrl = badgeUrl + "&build=" + build; + } + badgeUrl = badgeUrl + "&status=" + status; + badgeUrl = badgeUrl + "&link=" + link; + badgeUrl = badgeUrl + "&style=" + style; + + // Constant strings below do not affect result + badgeUrl = badgeUrl + "&color=orange"; + badgeUrl = badgeUrl + "&config=my-config"; + badgeUrl = badgeUrl + "&animatedOverlayColor=yellow"; + + // Check the badge for a job that has been built and run + try (JenkinsRule.WebClient webClient = j.createWebClient()) { + JenkinsRule.JSONWebResponse json = webClient.getJSON(badgeUrl); + String result = json.getContentAsString(); + + assertThat(result, containsString("