From 1439faf3a8959c1c2255441bd986b2eaed20f7bd Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Wed, 25 Dec 2024 06:15:20 -0700 Subject: [PATCH] Test getNextLabels and deprecated constructors more deeply The getNextLabels test needs JenkinsRule and an agent, so those are added to the test. The deprecated constructor tests now check more values that are assigned during object construction. --- .../LabelParameterValueTest.java | 83 +++++++++++++++++-- 1 file changed, 77 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterValueTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterValueTest.java index c8ce49c..44db160 100644 --- a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterValueTest.java +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterValueTest.java @@ -23,14 +23,39 @@ */ package org.jvnet.jenkins.plugins.nodelabelparameter; -import static org.junit.Assert.assertNotNull; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import hudson.slaves.DumbSlave; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; import nl.jqno.equalsverifier.EqualsVerifier; import nl.jqno.equalsverifier.Warning; +import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility; +import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility; public class LabelParameterValueTest { + @ClassRule + public static JenkinsRule j = new JenkinsRule(); + + private static DumbSlave agent; + + @BeforeClass + public static void createAgent() throws Exception { + agent = j.createOnlineSlave(); + } + + private final Random random = new Random(); + public LabelParameterValueTest() {} @Test @@ -43,14 +68,60 @@ public void testEqualsContract() { } @Test - public void testLabelParameterValue() { - LabelParameterValue labelParameterValue = new LabelParameterValue("name", "label"); - assertNotNull(labelParameterValue); + @Deprecated + public void testLabelParameterValueDeprecated2ArgConstructor() { + String value = " my-label "; // Intentionally has leading and trailing spaces + String trimmedValue = value.trim(); + // Use either value or trimmedValue randomly, does not change any assertion + LabelParameterValue labelParameterValue = + new LabelParameterValue("my-name", random.nextBoolean() ? value : trimmedValue); + assertThat(labelParameterValue.getName(), is("my-name")); + assertThat(labelParameterValue.getLabel(), is(trimmedValue)); + assertThat(labelParameterValue.getDescription(), is(nullValue())); + assertThat(labelParameterValue.getNextLabels(), is(empty())); + } + + @Test + @Deprecated + public void testLabelParameterValueDeprecated2ArgConstructorNullName() { + String value = " my-label "; // Intentionally has leading and trailing spaces + String trimmedValue = value.trim(); + // Use either value or trimmedValue randomly, does not change any assertion + LabelParameterValue labelParameterValue = + new LabelParameterValue(null, random.nextBoolean() ? value : trimmedValue); + assertThat(labelParameterValue.getName(), is("NODELABEL")); + assertThat(labelParameterValue.getLabel(), is(trimmedValue)); + assertThat(labelParameterValue.getDescription(), is(nullValue())); + assertThat(labelParameterValue.getNextLabels(), is(empty())); + } + + @Test + @Deprecated + public void testLabelParameterValueDeprecated3ArgConstructor() { + String value = " my-label "; // Intentionally has leading and trailing spaces + String trimmedValue = value.trim(); + String name = "my-name"; + String description = "My description"; + // Use either value or trimmedValue randomly, does not change any assertion + LabelParameterValue labelParameterValue = + new LabelParameterValue(name, description, random.nextBoolean() ? value : trimmedValue); + assertThat(labelParameterValue.getName(), is(name)); + assertThat(labelParameterValue.getLabel(), is(trimmedValue)); + assertThat(labelParameterValue.getDescription(), is(description)); + assertThat(labelParameterValue.getNextLabels(), is(empty())); } @Test public void testGetNextLabels() { - LabelParameterValue labelParameterValue = new LabelParameterValue("name", "label"); - assertNotNull(labelParameterValue.getNextLabels()); + String name = "my-name"; + List extraNodeNames = Arrays.asList("built-in", "not-a-valid-node-name"); + List nodeNames = new ArrayList<>(); + nodeNames.add(agent.getNodeName()); + nodeNames.addAll(extraNodeNames); + NodeEligibility eligibility = new AllNodeEligibility(); + LabelParameterValue labelParameterValue = new LabelParameterValue(name, nodeNames, eligibility); + assertThat(labelParameterValue.getName(), is(name)); + assertThat(labelParameterValue.getLabel(), is(agent.getNodeName())); + assertThat(labelParameterValue.getNextLabels(), is(extraNodeNames)); } }