diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterDefinitionTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterDefinitionTest.java index a4ab9af..eb7a725 100644 --- a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterDefinitionTest.java +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/NodeParameterDefinitionTest.java @@ -1,152 +1,151 @@ package org.jvnet.jenkins.plugins.nodelabelparameter; -import static org.junit.Assert.*; - +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import hudson.model.ParameterValue; import hudson.model.labels.LabelAtom; import hudson.slaves.DumbSlave; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -import org.junit.Before; -import org.junit.Rule; +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; 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 + @ClassRule + public static JenkinsRule j = new JenkinsRule(); - NodeParameterDefinition nodeParameterDefinition1 = new NodeParameterDefinition( - name, description, defaultSlaves1, allowedSlaves, triggerIfResult, new AllNodeEligibility()); + private static DumbSlave agent; - 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()); + @BeforeClass + public static void createAgent() throws Exception { + agent = j.createOnlineSlave(new LabelAtom("my-agent-label")); } @Test - public void testReadResolve_NodeEligibilityIsNull() { + @Deprecated + public void testNodeParameterDefinitionDeprecatedReordersAllowedAgents() { 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()); + List allowedAgents = new ArrayList<>(); + allowedAgents.add(agent.getNodeName()); + allowedAgents.add("non-existent-agent"); + String triggerIfResult = Constants.CASE_MULTISELECT_DISALLOWED; + + assertThat(allowedAgents.get(0), is(agent.getNodeName())); + NodeParameterDefinition parameterDefinition = + new NodeParameterDefinition(name, description, "non-existent-agent", allowedAgents, triggerIfResult); + assertThat(allowedAgents.get(0), is("non-existent-agent")); // List reordered by constructor + assertThat(parameterDefinition.getName(), is(name)); + assertThat(parameterDefinition.getDescription(), is(description)); + assertThat(parameterDefinition.defaultValue, is(nullValue())); + assertThat(parameterDefinition.getTriggerIfResult(), is(triggerIfResult)); + assertFalse(parameterDefinition.getAllowMultiNodeSelection()); + assertFalse(parameterDefinition.isTriggerConcurrentBuilds()); } @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); + List defaultAgents = Arrays.asList("built-in"); + List allowedAgents = new ArrayList<>(); + allowedAgents.add(agent.getNodeName()); + allowedAgents.add("non-existent-agent"); + String triggerIfResult = Constants.CASE_MULTISELECT_CONCURRENT_BUILDS; + + NodeParameterDefinition parameterDefinition = new NodeParameterDefinition( + name, description, defaultAgents, allowedAgents, triggerIfResult, new AllNodeEligibility()); + assertThat(allowedAgents.get(0), is(agent.getNodeName())); // List not reordered by constructor + assertThat(parameterDefinition.getName(), is(name)); + assertThat(parameterDefinition.getDescription(), is(description)); + assertThat(parameterDefinition.defaultValue, is(nullValue())); + assertThat(parameterDefinition.getTriggerIfResult(), is(triggerIfResult)); + assertTrue(parameterDefinition.getAllowMultiNodeSelection()); + assertTrue(parameterDefinition.isTriggerConcurrentBuilds()); } @Test public void testCreateValue_String() { String name = "name"; String description = "description"; - List defaultSlaves = new ArrayList<>(); - List allowedSlaves = new ArrayList<>(); - allowedSlaves.add("defaultValue"); + List defaultAgents = new ArrayList<>(); + List allowedAgents = new ArrayList<>(); + allowedAgents.add("defaultValue"); String triggerIfResult = "triggerIfResult"; - NodeParameterDefinition nodeParameterDefinition = new NodeParameterDefinition( - name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility()); - - assertNotNull(nodeParameterDefinition.createValue("value")); + NodeParameterDefinition parameterDefinition = new NodeParameterDefinition( + name, description, defaultAgents, allowedAgents, triggerIfResult, new AllNodeEligibility()); + assertThat(parameterDefinition.getName(), is(name)); + assertThat(parameterDefinition.getDescription(), is(description)); + assertThat(parameterDefinition.defaultValue, is(nullValue())); + assertThat(parameterDefinition.getTriggerIfResult(), is(triggerIfResult)); + assertTrue(parameterDefinition.getAllowMultiNodeSelection()); + assertFalse(parameterDefinition.isTriggerConcurrentBuilds()); + + String myValue = "my-value"; + ParameterValue parameterValue = parameterDefinition.createValue(myValue); + assertThat(parameterValue.getName(), is(name)); + assertThat(parameterValue.getDescription(), is(description)); + + // Unexpected that myValue is not returned by parameterValue.getValue() + // Seems to be a bug in the NodeParameterDefinition implementation + // NodeParameterDefinition declares a private field 'label' that receives + // the value instead of it being stored in LabelParameterValue.label but + // does not override the LabelParameterValue implementation of getValue(). + assertThat(parameterValue.getValue(), is(nullValue())); } @Test public void testGetAllowedNodesOrAll() { String name = "name"; String description = "description"; - List defaultSlaves = new ArrayList<>(); - List allowedSlaves = new ArrayList<>(); - allowedSlaves.add("node"); + List defaultAgents = new ArrayList<>(); + List allowedAgents = new ArrayList<>(); + allowedAgents.add(agent.getNodeName()); String triggerIfResult = "triggerIfResult"; - NodeParameterDefinition nodeParameterDefinition1 = new NodeParameterDefinition( - name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility()); + NodeParameterDefinition parameterDefinition = new NodeParameterDefinition( + name, description, defaultAgents, allowedAgents, triggerIfResult, new AllNodeEligibility()); - assertEquals(allowedSlaves, nodeParameterDefinition1.getAllowedNodesOrAll()); + assertThat(parameterDefinition.getAllowedNodesOrAll(), is(allowedAgents)); + } - allowedSlaves = new ArrayList<>(); - allowedSlaves.add("master"); + @Test + public void testGetAllowedNodesOrAllWithBuiltIn() { + String name = "name"; + String description = "description"; + List defaultAgents = new ArrayList<>(); + List allowedAgents = new ArrayList<>(); + allowedAgents.add("built-in"); + String triggerIfResult = "triggerIfResult"; - NodeParameterDefinition nodeParameterDefinition2 = new NodeParameterDefinition( - name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility()); + NodeParameterDefinition parameterDefinition = new NodeParameterDefinition( + name, description, defaultAgents, allowedAgents, triggerIfResult, new AllNodeEligibility()); - assertEquals(allowedSlaves, nodeParameterDefinition2.getAllowedNodesOrAll()); + assertThat(parameterDefinition.getAllowedNodesOrAll(), is(allowedAgents)); } @Test public void testGetHelpFile() { NodeParameterDefinition.DescriptorImpl descriptorImpl = new NodeParameterDefinition.DescriptorImpl(); - assertEquals(descriptorImpl.getHelpFile(), "/plugin/nodelabelparameter/nodeparam.html"); + assertThat(descriptorImpl.getHelpFile(), is("/plugin/nodelabelparameter/nodeparam.html")); } @Test public void testGetDefaultNodeEligibility() { NodeParameterDefinition.DescriptorImpl descriptorImpl = new NodeParameterDefinition.DescriptorImpl(); - assertNotNull(descriptorImpl.getDefaultNodeEligibility()); + assertThat(descriptorImpl.getDefaultNodeEligibility(), instanceOf(AllNodeEligibility.class)); } }