Skip to content

Commit

Permalink
Add Job Badge Action Test (#354)
Browse files Browse the repository at this point in the history
* 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 b6321c4.

* 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 <[email protected]>
  • Loading branch information
yashpal2104 and MarkEWaite authored Nov 17, 2024
1 parent 0f32ca2 commit 22fb6fa
Showing 1 changed file with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {}
Expand All @@ -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 = "<stop offset=\".1\"";
unexpectedStyle = " fill=\"white\" fill-opacity=\"0.1\"/>";
break;
case "flat":
expectedStyle = "<linearGradient id=\"a\" x2=\"0\" y2=\"100%\">";
unexpectedStyle = "<stop offset=\".1\"";
break;
case "flat-square":
expectedStyle = " fill=\"white\" fill-opacity=\"0.1\"/>";
unexpectedStyle = "fill-opacity=\".3\">" + subject + "</text>";
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("<svg "));
assertThat(result, containsString(link));
assertThat(result, containsString(status));
assertThat(result, containsString(subject));
assertThat("For style " + style, result, containsString(expectedStyle));
assertThat("For style " + style, result, not(containsString(unexpectedStyle)));
}
}

@Test
public void testGetIconFileName() {
assertThat(notBuiltAction.getIconFileName(), is(nullValue()));
Expand Down Expand Up @@ -93,6 +181,7 @@ public void testGetUrl() {
public void testGetUrlEncodedFullName() {
assertThat(notBuiltAction.getUrlEncodedFullName(), is(NOT_BUILT_JOB_NAME));
assertThat(successfulAction.getUrlEncodedFullName(), is(SUCCESSFUL_JOB_NAME));
assertThat(new JobBadgeAction(null).getUrlEncodedFullName(), is("null-project-no-url-encoded-fullName"));
}

@Test
Expand Down

0 comments on commit 22fb6fa

Please sign in to comment.