From 22fb6fabd4dd35df5c8dd2db51b62c9bb93f21b5 Mon Sep 17 00:00:00 2001 From: Yash Pal <112522911+yashpal2104@users.noreply.github.com> Date: Sun, 17 Nov 2024 20:43:40 +0530 Subject: [PATCH] Add Job Badge Action Test (#354) * Add tests for JobBadgeAction * Improved readability * Typo fix in the method resolveWithNullParameter * Format with mvn spotless:apply * Fixed the path URL and replaced reading response with scanner to Jenkins.Webclient * Fixed the path URL and instead of reading response with scanner tried Jenkins.Webclient * Fixed the path URL and replaced reading response with scanner to Jenkins.Webclient * tried to apply the PublicBuildStatusAction markers green and grey hex code to match in my code too * Combined both the test cases passing and failing into one Test and changed the failing marker * Remove println from test * Reduce diffs to master branch, refine based on PR comments See the pull request comments for details of each change. * Temporarily comment failure to see coverage report * Fix formatting * Revert "Temporarily comment failure to see coverage report" This reverts commit b6321c4ed0d2f3f2e3f38e85c1b6cbf36df6be38. * Add test for increasing coverage * Cover the null project case with a test It is not expected to encounter a null project in Jenkins, but the null case is handled in the production code, so let's cover it with a test. * Randomize style, subject, and status checks to assure accuracy Test value randomization with assertions of expected results are more difficult, but they make it less likely that an error will be undetected. Removes the BUILD_AND_RUN_MARKER and BUILD_NOT_RUN_MARKER checks because they never seem to be in the test output. Adds comments that will guide future changes to increase coverage. * Increase coverage by reusing test for more cases --------- Co-authored-by: Mark Waite --- .../badge/actions/JobBadgeActionTest.java | 93 ++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) 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("