Skip to content

Commit

Permalink
Improve Test Coverage of NodeLabelParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
code-arnab committed Dec 23, 2024
1 parent e98e630 commit f3064b9
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jvnet.jenkins.plugins.nodelabelparameter;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import hudson.DescriptorExtensionList;
import hudson.model.Node;
import hudson.model.labels.LabelAtom;
import hudson.slaves.DumbSlave;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.IgnoreTempOfflineNodeEligibility;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility.NodeEligibilityDescriptor;

public class IgnoreTempOfflineNodeEligibilityTest {
@Rule
public JenkinsRule j = new JenkinsRule();

private DumbSlave onlineNode1;

@Before
public void setUp() throws Exception {
onlineNode1 = j.createOnlineSlave(new LabelAtom("label"));
}

IgnoreTempOfflineNodeEligibility ignoreTempOfflineNodeEligibility = new IgnoreTempOfflineNodeEligibility();

@Test
public void testGetComputer() throws Exception {
Node node = null;
// Node is null
assertFalse(ignoreTempOfflineNodeEligibility.isEligible(node));
// Node is not null
assertFalse(ignoreTempOfflineNodeEligibility.isEligible(onlineNode1.getLabelString()));
// Node is null and nodeName is empty
assertTrue(ignoreTempOfflineNodeEligibility.isEligible(""));
}

@Test
public void testGetDescriptor() {
NodeEligibilityDescriptor descriptor = ignoreTempOfflineNodeEligibility.getDescriptor();
// Check if descriptor is not null
assertNotNull(descriptor);
// Check if descriptor is an instance of NodeEligibilityDescriptor
assertTrue(descriptor instanceof NodeEligibilityDescriptor);
}

@Test
public void testAll() {
DescriptorExtensionList<NodeEligibility, NodeEligibilityDescriptor> descriptors = NodeEligibility.all();
// Check if descriptors is not empty
assertTrue(!descriptors.isEmpty());
// Check if descriptors is not null
assertNotNull(descriptors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.jvnet.jenkins.plugins.nodelabelparameter;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import hudson.model.labels.LabelAtom;
import hudson.slaves.DumbSlave;
import hudson.util.FormValidation;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class LabelParameterDefinitionTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@Before
public void setUp() throws Exception {
final DumbSlave node = j.createOnlineSlave(new LabelAtom("node"));
}

@Test
public void testNodeParameterDefinition() {
String name = "name";
String description = "description";
String defaultValue = "defaultValue";
List<String> defaultSlaves = new ArrayList<>();
List<String> allowedSlaves = new ArrayList<>();
allowedSlaves.add("defaultValue");
String triggerIfResult = "triggerIfResult";

LabelParameterDefinition nodeParameterDefinition1 =
new LabelParameterDefinition(name, description, defaultValue, true, true, triggerIfResult);

assertEquals(nodeParameterDefinition1.defaultValue, defaultValue);

LabelParameterDefinition nodeParameterDefinition2 =
new LabelParameterDefinition(name, description, defaultValue, true, null, triggerIfResult);

assertEquals(nodeParameterDefinition1.defaultValue, defaultValue);
}

@Test
public void testDoListNodesForLabel() throws Exception {
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();

String label = "node";

FormValidation validation = nodeParameterDefinition.doListNodesForLabel(label);

assertNotNull(validation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.jvnet.jenkins.plugins.nodelabelparameter;

import static org.junit.Assert.assertNotNull;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.junit.Test;
Expand All @@ -39,4 +41,16 @@ public void testEqualsContract() {
.withIgnoredFields("description", "nextLabels")
.verify();
}

@Test
public void testLabelParameterValue() {
LabelParameterValue labelParameterValue = new LabelParameterValue("name", "label");
assertNotNull(labelParameterValue);
}

@Test
public void testGetNextLabels() {
LabelParameterValue labelParameterValue = new LabelParameterValue("name", "label");
assertNotNull(labelParameterValue.getNextLabels());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// package org.jvnet.jenkins.plugins.nodelabelparameter;
//
// import nl.jqno.equalsverifier.EqualsVerifier;
// import nl.jqno.equalsverifier.Warning;
// import org.junit.Test;
//
// public class NextLabelCauseTest {
// @Test
// public void equalsContract() {
// EqualsVerifier.forClass(NextLabelCause.class)
// .usingGetClass()
// .suppress(Warning.NONFINAL_FIELDS)
// .withIgnoredFields()
// .verify();
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package org.jvnet.jenkins.plugins.nodelabelparameter;

import static org.junit.Assert.*;

import hudson.model.labels.LabelAtom;
import hudson.slaves.DumbSlave;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;

public class NodeParameterDefinitionTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@Before
public void setUp() throws Exception {
final DumbSlave node = j.createOnlineSlave(new LabelAtom("node"));
}

@Test
public void testReadResolve_DefaultValueIsNotNull() {
String name = "name";
String description = "description";
List<String> defaultSlaves1 = new ArrayList<>();
List<String> allowedSlaves = new ArrayList<>();
String triggerIfResult = "triggerIfResult";

// deaultValue is not null and defaultSlaves is not null

NodeParameterDefinition nodeParameterDefinition1 = new NodeParameterDefinition(
name, description, defaultSlaves1, allowedSlaves, triggerIfResult, new AllNodeEligibility());

nodeParameterDefinition1.defaultValue = "defaultValue";

assertNotNull(nodeParameterDefinition1.readResolve());
assertEquals(
nodeParameterDefinition1.getClass(),
nodeParameterDefinition1.readResolve().getClass());

// deaultValue is not null and defaultSlaves is null

List<String> defaultSlaves2 = null;
NodeParameterDefinition nodeParameterDefinition2 = new NodeParameterDefinition(
name, description, defaultSlaves2, allowedSlaves, triggerIfResult, new AllNodeEligibility());

nodeParameterDefinition2.defaultValue = "defaultValue";

assertNotNull(nodeParameterDefinition2.readResolve());
assertEquals(
nodeParameterDefinition2.getClass(),
nodeParameterDefinition2.readResolve().getClass());
}

@Test
public void testReadResolve_NodeEligibilityIsNull() {
String name = "name";
String description = "description";
List<String> defaultSlaves = null;
List<String> allowedSlaves = new ArrayList<>();
String triggerIfResult = "triggerIfResult";

NodeParameterDefinition nodeParameterDefinition1 =
new NodeParameterDefinition(name, description, defaultSlaves, allowedSlaves, triggerIfResult, true);

nodeParameterDefinition1.defaultValue = null;

assertNotNull(nodeParameterDefinition1.readResolve());

NodeParameterDefinition nodeParameterDefinition2 =
new NodeParameterDefinition(name, description, defaultSlaves, allowedSlaves, triggerIfResult, false);

nodeParameterDefinition2.defaultValue = null;

assertNotNull(nodeParameterDefinition2.readResolve());
}

@Test
public void testNodeParameterDefinition() {
String name = "name";
String description = "description";
List<String> defaultSlaves = new ArrayList<>();
List<String> allowedSlaves = new ArrayList<>();
allowedSlaves.add("defaultValue");
String triggerIfResult = "triggerIfResult";

NodeParameterDefinition nodeParameterDefinition1 =
new NodeParameterDefinition(name, description, "defaultValue", allowedSlaves, triggerIfResult);

assertTrue(allowedSlaves.contains("defaultValue"));

NodeParameterDefinition nodeParameterDefinition2 = new NodeParameterDefinition(
name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility());

assertNotNull(nodeParameterDefinition2);
}

@Test
public void testCreateValue_String() {
String name = "name";
String description = "description";
List<String> defaultSlaves = new ArrayList<>();
List<String> allowedSlaves = new ArrayList<>();
allowedSlaves.add("defaultValue");
String triggerIfResult = "triggerIfResult";

NodeParameterDefinition nodeParameterDefinition = new NodeParameterDefinition(
name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility());

assertNotNull(nodeParameterDefinition.createValue("value"));
}

@Test
public void testGetAllowedNodesOrAll() {
String name = "name";
String description = "description";
List<String> defaultSlaves = new ArrayList<>();
List<String> allowedSlaves = new ArrayList<>();
allowedSlaves.add("node");
String triggerIfResult = "triggerIfResult";

NodeParameterDefinition nodeParameterDefinition1 = new NodeParameterDefinition(
name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility());

assertEquals(allowedSlaves, nodeParameterDefinition1.getAllowedNodesOrAll());

allowedSlaves = new ArrayList<>();
allowedSlaves.add("master");

NodeParameterDefinition nodeParameterDefinition2 = new NodeParameterDefinition(
name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility());

assertEquals(allowedSlaves, nodeParameterDefinition2.getAllowedNodesOrAll());
}

@Test
public void testGetHelpFile() {
NodeParameterDefinition.DescriptorImpl descriptorImpl = new NodeParameterDefinition.DescriptorImpl();

assertEquals(descriptorImpl.getHelpFile(), "/plugin/nodelabelparameter/nodeparam.html");
}

@Test
public void testGetDefaultNodeEligibility() {
NodeParameterDefinition.DescriptorImpl descriptorImpl = new NodeParameterDefinition.DescriptorImpl();

assertNotNull(descriptorImpl.getDefaultNodeEligibility());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.junit.Test;
Expand All @@ -11,6 +12,9 @@ public class NodeParameterValueTest {
public void testToString() {
NodeParameterValue nvp = new NodeParameterValue("node", "description", "label");
assertEquals("[NodeParameterValue: node=label]", nvp.toString());

nvp.nextLabels = Arrays.asList("label1");
assertEquals("[NodeParameterValue: node=label, nextNodes=label1]", nvp.toString());
}

@Test
Expand Down

0 comments on commit f3064b9

Please sign in to comment.