|
1 | 1 | package org.jvnet.jenkins.plugins.nodelabelparameter;
|
2 | 2 |
|
3 |
| -import static org.junit.Assert.assertEquals; |
4 |
| -import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.allOf; |
| 5 | +import static org.hamcrest.Matchers.containsString; |
| 6 | +import static org.hamcrest.Matchers.is; |
| 7 | +import static org.junit.Assert.assertFalse; |
| 8 | +import static org.junit.Assert.assertTrue; |
5 | 9 |
|
6 | 10 | import hudson.model.labels.LabelAtom;
|
7 | 11 | import hudson.slaves.DumbSlave;
|
8 | 12 | import hudson.util.FormValidation;
|
9 |
| -import java.util.ArrayList; |
10 |
| -import java.util.List; |
11 |
| -import org.junit.Before; |
12 |
| -import org.junit.Rule; |
| 13 | +import org.junit.BeforeClass; |
| 14 | +import org.junit.ClassRule; |
13 | 15 | import org.junit.Test;
|
14 | 16 | import org.jvnet.hudson.test.JenkinsRule;
|
15 | 17 |
|
16 | 18 | public class LabelParameterDefinitionTest {
|
17 |
| - @Rule |
18 |
| - public JenkinsRule j = new JenkinsRule(); |
19 | 19 |
|
20 |
| - @Before |
21 |
| - public void setUp() throws Exception { |
22 |
| - final DumbSlave node = j.createOnlineSlave(new LabelAtom("node")); |
| 20 | + @ClassRule |
| 21 | + public static JenkinsRule j = new JenkinsRule(); |
| 22 | + |
| 23 | + private static DumbSlave agent; |
| 24 | + |
| 25 | + private static final String LABEL_NAME = "my-agent-label"; |
| 26 | + private static final LabelAtom label = new LabelAtom(LABEL_NAME); |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public static void createAgent() throws Exception { |
| 30 | + agent = j.createOnlineSlave(label); |
23 | 31 | }
|
24 | 32 |
|
25 | 33 | @Test
|
26 |
| - public void testNodeParameterDefinition() { |
| 34 | + @Deprecated |
| 35 | + public void testNodeParameterDefinitionDeprecated() { |
27 | 36 | String name = "name";
|
28 |
| - String description = "description"; |
29 |
| - String defaultValue = "defaultValue"; |
30 |
| - List<String> defaultSlaves = new ArrayList<>(); |
31 |
| - List<String> allowedSlaves = new ArrayList<>(); |
32 |
| - allowedSlaves.add("defaultValue"); |
33 |
| - String triggerIfResult = "triggerIfResult"; |
34 |
| - |
35 |
| - LabelParameterDefinition nodeParameterDefinition1 = |
| 37 | + String description = "The description"; |
| 38 | + String defaultValue = "built-in || master"; |
| 39 | + String triggerIfResult = "The triggerIfResult value"; |
| 40 | + |
| 41 | + LabelParameterDefinition nodeParameterDefinition = |
36 | 42 | new LabelParameterDefinition(name, description, defaultValue, true, true, triggerIfResult);
|
37 | 43 |
|
38 |
| - assertEquals(nodeParameterDefinition1.defaultValue, defaultValue); |
| 44 | + assertThat(nodeParameterDefinition.defaultValue, is(defaultValue)); |
| 45 | + assertThat(nodeParameterDefinition.getName(), is(name)); |
| 46 | + assertThat(nodeParameterDefinition.getDescription(), is(description)); |
| 47 | + assertThat(nodeParameterDefinition.getDefaultParameterValue().getLabel(), is(defaultValue)); |
| 48 | + assertThat(nodeParameterDefinition.getTriggerIfResult(), is(triggerIfResult)); |
| 49 | + assertTrue(nodeParameterDefinition.isAllNodesMatchingLabel()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @Deprecated |
| 54 | + public void testNodeParameterDefinitionDeprecated3Arg() { |
| 55 | + String name = "name"; |
| 56 | + String description = "The description"; |
| 57 | + String defaultValue = "built-in || master"; |
39 | 58 |
|
40 |
| - LabelParameterDefinition nodeParameterDefinition2 = |
41 |
| - new LabelParameterDefinition(name, description, defaultValue, true, null, triggerIfResult); |
| 59 | + LabelParameterDefinition nodeParameterDefinition = |
| 60 | + new LabelParameterDefinition(name, description, defaultValue); |
42 | 61 |
|
43 |
| - assertEquals(nodeParameterDefinition1.defaultValue, defaultValue); |
| 62 | + assertThat(nodeParameterDefinition.defaultValue, is(defaultValue)); |
| 63 | + assertThat(nodeParameterDefinition.getName(), is(name)); |
| 64 | + assertThat(nodeParameterDefinition.getDescription(), is(description)); |
| 65 | + assertThat(nodeParameterDefinition.getDefaultParameterValue().getLabel(), is(defaultValue)); |
| 66 | + assertThat(nodeParameterDefinition.getTriggerIfResult(), is("allCases")); |
| 67 | + assertFalse(nodeParameterDefinition.isAllNodesMatchingLabel()); |
44 | 68 | }
|
45 | 69 |
|
46 | 70 | @Test
|
47 |
| - public void testDoListNodesForLabel() throws Exception { |
48 |
| - LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl(); |
| 71 | + public void testNodeParameterDefinition() { |
| 72 | + String name = "name"; |
| 73 | + String description = "The description"; |
| 74 | + String defaultValue = "built-in || master"; |
| 75 | + String triggerIfResult = "The triggerIfResult value"; |
49 | 76 |
|
50 |
| - String label = "node"; |
| 77 | + LabelParameterDefinition nodeParameterDefinition = |
| 78 | + new LabelParameterDefinition(name, description, defaultValue, false, null, triggerIfResult); |
51 | 79 |
|
52 |
| - FormValidation validation = nodeParameterDefinition.doListNodesForLabel(label); |
| 80 | + assertThat(nodeParameterDefinition.defaultValue, is(defaultValue)); |
| 81 | + assertThat(nodeParameterDefinition.getName(), is(name)); |
| 82 | + assertThat(nodeParameterDefinition.getDescription(), is(description)); |
| 83 | + assertThat(nodeParameterDefinition.getDefaultParameterValue().getLabel(), is(defaultValue)); |
| 84 | + assertThat(nodeParameterDefinition.getTriggerIfResult(), is(triggerIfResult)); |
| 85 | + assertFalse(nodeParameterDefinition.isAllNodesMatchingLabel()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testDoListNodesForAgentLabel() throws Exception { |
| 90 | + LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl(); |
| 91 | + FormValidation validation = nodeParameterDefinition.doListNodesForLabel(LABEL_NAME); |
| 92 | + String msg = validation.getMessage(); |
| 93 | + assertThat(msg, allOf(containsString("Matching nodes"), containsString(agent.getNodeName()))); |
| 94 | + } |
53 | 95 |
|
54 |
| - assertNotNull(validation); |
| 96 | + @Test |
| 97 | + public void testDoListNodesForControllerLabel() throws Exception { |
| 98 | + String controllerLabel = "built-in"; |
| 99 | + LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl(); |
| 100 | + FormValidation validation = nodeParameterDefinition.doListNodesForLabel(controllerLabel); |
| 101 | + String msg = validation.getMessage(); |
| 102 | + assertThat(msg, allOf(containsString("Matching nodes"), containsString(controllerLabel))); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testDoListNodesForNonExistentLabel() throws Exception { |
| 107 | + String badLabel = "this-label-does-not-exist"; |
| 108 | + LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl(); |
| 109 | + FormValidation validation = nodeParameterDefinition.doListNodesForLabel(badLabel); |
| 110 | + String msg = validation.getMessage(); |
| 111 | + assertThat(msg, allOf(containsString("The label expression"), containsString(badLabel))); |
55 | 112 | }
|
56 | 113 | }
|
0 commit comments