Skip to content

Commit

Permalink
Test getNextLabels and deprecated constructors more deeply
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MarkEWaite committed Dec 25, 2024
1 parent 14fd1ad commit 1439faf
Showing 1 changed file with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> extraNodeNames = Arrays.asList("built-in", "not-a-valid-node-name");
List<String> 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));
}
}

0 comments on commit 1439faf

Please sign in to comment.