Skip to content

Commit

Permalink
Increase coverage by testing special parameters with a build
Browse files Browse the repository at this point in the history
Uses a JUnit 5 WithJenkins annotation to provide a JenkinsRule argument to
the test method.  The JenkinsRule argument provides access to a running
Jenkins from within the test.  The test creates a freestyle job and
runs it twice.  It changes the description of each of those builds and
confirms that the change is resolved by the parameter resolver.

Plugin documentation describes the parameter resolver
https://plugins.jenkins.io/embeddable-build-status/#plugin-content-parameter-resolver
that can be applied to builds and provides replacement text in the
embeddable build status result based on the data from the build.
  • Loading branch information
MarkEWaite committed Oct 27, 2024
1 parent 604ae2e commit 294fc40
Showing 1 changed file with 31 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,65 @@
import static org.hamcrest.MatcherAssert.assertThat;

import hudson.model.Actionable;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.mockito.Mockito;

class ParameterResolverTest {
private ParameterResolver resolver;

@BeforeEach
void defineResolver() {
resolver = new ParameterResolver();
}

@ParameterizedTest
@CsvSource({
"Build ${params.BUILD_BRANCH},Build params.BUILD_BRANCH",
"Build ${params.BUILD_BRANCH|master},Build params.BUILD_BRANCH|master",
"Build ${params.BUILD_BRANCH|master} (${displayName}),Build params.BUILD_BRANCH|master (displayName)"
})
void shouldResolveSubjectWithVariables(String queryParameter, String expectedParameter) {
String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), queryParameter);
String resolvedParameter = resolver.resolve(Mockito.mock(Actionable.class), queryParameter);
assertThat(resolvedParameter, is(expectedParameter));
}

@Test
public void shouldResolveNullParameter() {
String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), null);
String resolvedParameter = resolver.resolve(Mockito.mock(Actionable.class), null);
assertThat(resolvedParameter, is(nullValue()));
}

@Test
public void shouldResolveEmptyParameter() {
String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), "");
String resolvedParameter = resolver.resolve(Mockito.mock(Actionable.class), "");
assertThat(resolvedParameter, is(""));
}

@Test
public void shouldResolveParameterWithoutVariables() {
String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), "Build");
assertThat(resolvedParameter, is("Build"));
}

@Test
public void shouldResolveParameterWithVariables() {
String resolvedParameter =
new ParameterResolver().resolve(Mockito.mock(Actionable.class), "Build ${params.BUILD_BRANCH}");
assertThat(resolvedParameter, is("Build params.BUILD_BRANCH"));
}
@WithJenkins
public void shouldResolveSpecialParameters(JenkinsRule r) throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.setDescription("Project description is ignored");

@Test
public void shouldResolveParameterWithVariablesAndText() {
String resolvedParameter = new ParameterResolver()
.resolve(Mockito.mock(Actionable.class), "Build ${params.BUILD_BRANCH} (${displayName})");
assertThat(resolvedParameter, is("Build params.BUILD_BRANCH (displayName)"));
}
FreeStyleBuild build1 = r.buildAndAssertSuccess(p);
String myDescription1 = "Build 1 description is used";
build1.setDescription(myDescription1);
String resolved1 = resolver.resolve(
build1, "build-${buildNumber} ${displayName} id-${buildId} ${description} ${duration}");
assertThat(resolved1, is("build-1 #1 id-1 " + myDescription1 + " " + build1.getDurationString()));

@Test
public void shouldResolveParameterWithVariablesAndTextAndDefault() {
String resolvedParameter = new ParameterResolver()
.resolve(Mockito.mock(Actionable.class), "Build ${params.BUILD_BRANCH|master} (${displayName})");
assertThat(resolvedParameter, is("Build params.BUILD_BRANCH|master (displayName)"));
}

@Test
public void shouldNotResolveParameterOfVaryingLengthInRegex() {
ParameterResolver resolver = new ParameterResolver();
String resolvedParameter = resolver.resolve(Mockito.mock(Actionable.class), "(?<=\\w*)");
assertThat(resolvedParameter, is("(?<=\\w*)"));
}

@Test
public void shouldNotResolveParameterWithFullRangeUnicode() {
String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), "\\p{InGreek}");
assertThat(resolvedParameter, is("\\p{InGreek}"));
}

@Test
public void shouldNotResolveParameterWithRegexInlineFlags() {
String resolvedParameter =
new ParameterResolver().resolve(Mockito.mock(Actionable.class), "(${\\Build(?i) params\\b})");
assertThat(resolvedParameter, is("(${\\Build(?i) params\\b})"));
FreeStyleBuild build2 = r.buildAndAssertSuccess(p);
String myDescription2 = "Build 2 description is used";
build2.setDescription(myDescription2);
String resolved2 = resolver.resolve(
build2, "${displayName} build-${buildNumber} id-${buildId} ${duration} ${description}");
assertThat(resolved2, is("#2 build-2 id-2 " + build2.getDurationString() + " " + myDescription2));
}
}

0 comments on commit 294fc40

Please sign in to comment.