From de1993042c6a35e103740ae719734a83a2e16363 Mon Sep 17 00:00:00 2001 From: pardoner <143449651+pardoner@users.noreply.github.com> Date: Tue, 22 Oct 2024 19:50:56 -0500 Subject: [PATCH] Improve NodeParameterValue and LabelParameterValue test coverage (#352) * 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 Co-authored-by: Mark Waite --- .../nodelabelparameter/TriggerJobsTest.java | 21 ++++++++++++ .../NodeLabelBuildParameterTest.java | 34 +++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/TriggerJobsTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/TriggerJobsTest.java index ea7eac3..cec9124 100644 --- a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/TriggerJobsTest.java +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/TriggerJobsTest.java @@ -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; @@ -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); } diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/parameterizedtrigger/NodeLabelBuildParameterTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/parameterizedtrigger/NodeLabelBuildParameterTest.java index 6905cb9..fb92eb0 100644 --- a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/parameterizedtrigger/NodeLabelBuildParameterTest.java +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/parameterizedtrigger/NodeLabelBuildParameterTest.java @@ -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; @@ -34,6 +35,7 @@ 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; @@ -41,6 +43,8 @@ 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 { @@ -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); @@ -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 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));