Skip to content

Commit

Permalink
Improve NodeParameterValue and LabelParameterValue test coverage (#352)
Browse files Browse the repository at this point in the history
* added coverage for label parameters

* spaces

* Assert results of tested methods

* Call methods directly in assertion

* Assert isTriggerConcurrentBuilds and getNodeEligibility

* Move assertions nearer their usage

* Remove method call lacking assertion

---------

Co-authored-by: Dylan Wicker <[email protected]>
Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent 6b86ce6 commit de19930
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hudson.model.Cause;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.ParameterDefinition;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import hudson.model.Run;
Expand Down Expand Up @@ -264,6 +265,26 @@ public void testTriggerViaCurlWithLabel() throws Exception {
null,
new AllNodeEligibility());
String json = "{\"parameter\":[{\"name\":\"NODE\",\"label\":[\"" + controllerLabel + "\"]}]}";
LabelParameterValue lpv = new LabelParameterValue("test");
ParameterDefinition copy = parameterDefinition.copyWithDefaultValue(lpv);
assertThat(copy.getDescription(), is(parameterDefinition.getDescription()));
assertThat(copy.getType(), is(parameterDefinition.getType()));
assertThat(copy.getName(), is(parameterDefinition.getName()));
assertThat(
NodeParameterDefinition.getSlaveNamesForSelection(),
contains(
Constants.ALL_NODES,
controllerLabel,
onlineNode1.getNodeName(),
onlineNode2.getNodeName(),
offlineNode.getNodeName()));
assertThat(
NodeParameterDefinition.getSlaveNames(),
contains(
controllerLabel,
onlineNode1.getNodeName(),
onlineNode2.getNodeName(),
offlineNode.getNodeName()));
runTestViaCurl(projectA, parameterDefinition, json, 1, Result.SUCCESS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import hudson.model.AutoCompletionCandidates;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Project;
import hudson.model.labels.LabelAtom;
Expand All @@ -34,13 +35,16 @@
import hudson.plugins.parameterizedtrigger.ResultCondition;
import hudson.slaves.DumbSlave;
import hudson.util.FormValidation;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterDefinition;
import org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterValue;
import org.jvnet.jenkins.plugins.nodelabelparameter.NodeParameterValue;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;

public class NodeLabelBuildParameterTest {

Expand Down Expand Up @@ -75,13 +79,29 @@ public void test() throws Exception {

LabelParameterDefinition lb1 =
new LabelParameterDefinition(paramName, "some desc", "wrongNodeName", false, null, "");
Assert.assertTrue(lb1.isTriggerConcurrentBuilds());
Assert.assertTrue(lb1.getNodeEligibility() instanceof AllNodeEligibility);

LabelParameterDefinition lb2 =
new LabelParameterDefinition(paramName2, "some desc", "wrongNodeName", false, false, "");
Assert.assertTrue(lb2.isTriggerConcurrentBuilds());
Assert.assertTrue(lb2.getNodeEligibility() instanceof AllNodeEligibility);

LabelParameterDefinition lb3 = new LabelParameterDefinition(paramName3, "some desc", "wrongNodeName");
Assert.assertTrue(lb3.isTriggerConcurrentBuilds());
Assert.assertTrue(lb3.getNodeEligibility() instanceof AllNodeEligibility);

LabelParameterDefinition lb4 =
new LabelParameterDefinition(paramName, "some desc", "wrongNodeName", true, null, "");
Assert.assertTrue(lb4.isTriggerConcurrentBuilds());
Assert.assertTrue(lb4.getNodeEligibility() instanceof AllNodeEligibility);

ParametersDefinitionProperty pdp = new ParametersDefinitionProperty(lb1);
ParametersDefinitionProperty pdp2 = new ParametersDefinitionProperty(lb2);
ParametersDefinitionProperty pdp3 = new ParametersDefinitionProperty(lb3);
ParameterValue pv1 = lb4.createValue("test");
LabelParameterValue lpv1 = new LabelParameterValue("test");
LabelParameterValue lpv2 = new LabelParameterValue("test2");

projectB.addProperty(pdp);
projectB.addProperty(pdp2);
Expand All @@ -108,10 +128,18 @@ public void test() throws Exception {
final AutoCompletionCandidates candidates = descriptor.doAutoCompleteDefaultValue(paramName);
final FormValidation doListNodesForLabel = descriptor.doListNodesForLabel(nodeName);

Assert.assertEquals(
lb1.copyWithDefaultValue(new LabelParameterValue("")).getName(), paramName);
Assert.assertTrue(lpv1.equals(lpv1));
Assert.assertFalse(lpv1.equals(lpv2));
Assert.assertFalse(lpv1.equals(new NodeParameterValue("test", "description", "TestLabel")));

Assert.assertEquals(paramName, pv1.getName());
Assert.assertEquals("allCases", lb1.getTriggerIfResult());

Assert.assertEquals(lb1.copyWithDefaultValue(null).getName(), paramName);
List<String> labels = new ArrayList<>();
labels.add("wrongNodeName");
LabelParameterValue lpv3 = new LabelParameterValue("test", labels, lb1.getNodeEligibility());
Assert.assertEquals(paramName, lb1.copyWithDefaultValue(lpv3).getName());
Assert.assertEquals(paramName, lb1.copyWithDefaultValue(null).getName());

Assert.assertEquals(doListNodesForLabel.kind, FormValidation.Kind.OK);
Assert.assertEquals(candidates.getValues(), List.of(nodeName));
Expand Down

0 comments on commit de19930

Please sign in to comment.