Skip to content

Commit

Permalink
Add test for BuildParameterStrategy class (#435)
Browse files Browse the repository at this point in the history
* Add test for BuildParameterStrategy class

* remove extra BuildParameterStrategy object

* remove unused Exception from the Test methods
  • Loading branch information
yashpal2104 authored Jan 7, 2025
1 parent f1aa5dd commit 52d2639
Showing 1 changed file with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package jenkins.advancedqueue.priority.strategy;

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

import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.Queue;
import hudson.model.StringParameterValue;
import java.io.IOException;
import java.util.Calendar;
import java.util.Collections;
import jenkins.advancedqueue.PrioritySorterConfiguration;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class BuildParameterStrategyTest {

@ClassRule
public static JenkinsRule j = new JenkinsRule();

private static FreeStyleProject project;
private static BuildParameterStrategy strategy;

@BeforeClass
public static void setUp() throws IOException {
project = j.createFreeStyleProject();
strategy = new BuildParameterStrategy("priority");
}

@Test
public void getPriority_returnsPriorityFromParameter() {
StringParameterValue param = new StringParameterValue("priority", "5");
ParametersAction action = new ParametersAction(param);
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action));

int priority = strategy.getPriority(item);

assertEquals(5, priority);
}

@Test
public void getPriority_returnsDefaultPriorityWhenParameterIsMissing() {
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.emptyList());

int priority = strategy.getPriority(item);

assertEquals(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority(), priority);
}

@Test
public void getPriority_returnsDefaultPriorityWhenParameterIsNotANumber() {
StringParameterValue param = new StringParameterValue("priority", "not-a-number");
ParametersAction action = new ParametersAction(param);
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action));

int priority = strategy.getPriority(item);

assertEquals(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority(), priority);
}

@Test
public void isApplicable_returnsTrueWhenParameterIsPresentAndValid() {
StringParameterValue param = new StringParameterValue("priority", "5");
ParametersAction action = new ParametersAction(param);
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action));

assertTrue(strategy.isApplicable(item));
}

@Test
public void isApplicable_returnsFalseWhenParameterIsMissing() {
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.emptyList());

assertFalse(strategy.isApplicable(item));
}

@Test
public void isApplicable_returnsFalseWhenParameterIsNotANumber() {
StringParameterValue param = new StringParameterValue("priority", "not-a-number");
ParametersAction action = new ParametersAction(param);
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action));

assertFalse(strategy.isApplicable(item));
}
}

0 comments on commit 52d2639

Please sign in to comment.