diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/IgnoreTempOfflineNodeEligibilityTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/IgnoreTempOfflineNodeEligibilityTest.java new file mode 100644 index 0000000..a99469e --- /dev/null +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/IgnoreTempOfflineNodeEligibilityTest.java @@ -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 descriptors = NodeEligibility.all(); + // Check if descriptors is not empty + assertTrue(!descriptors.isEmpty()); + // Check if descriptors is not null + assertNotNull(descriptors); + } +} diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterDefinitionTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterDefinitionTest.java new file mode 100644 index 0000000..b7582ac --- /dev/null +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterDefinitionTest.java @@ -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 defaultSlaves = new ArrayList<>(); + List 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); + } +} 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 90ea84b..c8ce49c 100644 --- a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterValueTest.java +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterValueTest.java @@ -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; @@ -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()); + } } diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NextLabelCauseTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NextLabelCauseTest.java new file mode 100644 index 0000000..353af54 --- /dev/null +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NextLabelCauseTest.java @@ -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(); +// } +// } diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterDefinitionTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterDefinitionTest.java new file mode 100644 index 0000000..a4ab9af --- /dev/null +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterDefinitionTest.java @@ -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 defaultSlaves1 = new ArrayList<>(); + List 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 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 defaultSlaves = null; + List 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 defaultSlaves = new ArrayList<>(); + List 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 defaultSlaves = new ArrayList<>(); + List 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 defaultSlaves = new ArrayList<>(); + List 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()); + } +} diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterValueTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterValueTest.java index 56aff7a..a6ad226 100644 --- a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterValueTest.java +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterValueTest.java @@ -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; @@ -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