Skip to content

Commit

Permalink
[Jenkins-69756] Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
code-arnab committed Dec 16, 2024
1 parent a0de40c commit 99d2d80
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jvnet.jenkins.plugins.nodelabelparameter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class NodeParameterValueTest {
@Test
public void testToString() {
NodeParameterValue nvp = new NodeParameterValue("node", "description", "label");
assertEquals("[NodeParameterValue: node=label]", nvp.toString());
}

@Test
public void testHashCode() {
NodeParameterValue nvp = new NodeParameterValue("node", "description", "label");
NodeParameterValue nvp2 = new NodeParameterValue("node", "description", "label");
assertEquals(nvp.hashCode(), nvp2.hashCode());

NodeParameterValue nvp3 = new NodeParameterValue("node", "description", "label2");
assertNotEquals(nvp, nvp3);
}

@Test
public void testEquals() {
NodeParameterValue nvp = new NodeParameterValue("node", "description", "label");
NodeParameterValue nvp2 = new NodeParameterValue("node", "description", "label");
assertTrue(nvp.equals(nvp2));

NodeParameterValue nvp3 = new NodeParameterValue("node", "description", "label2");
assertFalse(nvp.equals(nvp3));

assertFalse(nvp.equals(null));
assertFalse(nvp.equals(new Object()));
assertTrue(nvp.equals(nvp));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;

import hudson.EnvVars;
import hudson.Launcher;
Expand Down Expand Up @@ -92,6 +93,28 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
assertThat(executed, contains(Boolean.TRUE));
}

@Test
public void testGetDisplayName() {
AllNodesForLabelBuildParameterFactory.DescriptorImpl descriptor =
new AllNodesForLabelBuildParameterFactory.DescriptorImpl();

assertEquals("All Nodes for Label Factory", descriptor.getDisplayName());
}

@Test
public void testIsIgnoreOfflineNodes() {
AllNodesForLabelBuildParameterFactory factory =
new AllNodesForLabelBuildParameterFactory("name", "label", true);

// Assert that the ignoreOfflineNodes flag is set to true
assertEquals(true, factory.isIgnoreOfflineNodes());

factory = new AllNodesForLabelBuildParameterFactory("name", "label", false);

// Assert that the ignoreOfflineNodes flag is set to false
assertEquals(false, factory.isIgnoreOfflineNodes());
}

public static void noMatchingNodeShouldYieldSameLabel(
final AllNodesForLabelBuildParameterFactory dummyNodesFactory,
AbstractBuild<?, ?> build,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,11 @@ public void testValidateBuildNoExceptionIfConcurrentBuildsAllowed() throws Excep

j.jenkins.removeNode(slave);
}

@Test
public void testGetDisplayName() {
NodeLabelBuildParameter.DescriptorImpl descriptor = new NodeLabelBuildParameter.DescriptorImpl();

Assert.assertEquals("NodeLabel parameter", descriptor.getDisplayName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
assertThat(executed, contains(Boolean.TRUE));
}

@Test
public void testDoCheckNodeListString() throws Exception {
NodeListBuildParameterFactory.DescriptorImpl descriptor = new NodeListBuildParameterFactory.DescriptorImpl();

FreeStyleProject projectA = j.createFreeStyleProject("projectA");

Item parent1 = j.jenkins.getItem("projectA");
Item parent2 = projectA.asItem();

// Validate the FormValidation.ok() case
FormValidation okValidation = descriptor.doCheckNodeListString(parent2, "node1");

Assert.assertEquals(okValidation.kind, FormValidation.Kind.OK);

// Validate the FormValidation.error() case if node is null
FormValidation errorValidation = descriptor.doCheckNodeListString(parent1, null);

Assert.assertEquals(errorValidation.kind, FormValidation.Kind.ERROR);
}

private TriggerBuilder createTriggerBuilder(AbstractProject<?, ?> project, AbstractBuildParameterFactory factory) {
TriggerBuilder tBuilder = new TriggerBuilder(new BlockableBuildTriggerConfig(
project.getName(),
Expand Down

0 comments on commit 99d2d80

Please sign in to comment.