Skip to content

Commit

Permalink
Add test for BuildParameterStrategy class
Browse files Browse the repository at this point in the history
  • Loading branch information
yashpal2104 committed Jan 6, 2025
1 parent f1aa5dd commit 6f93df9
Showing 1 changed file with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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() throws Exception {
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() throws Exception {
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() throws Exception {
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() throws Exception {
StringParameterValue param = new StringParameterValue("priority", "5");
ParametersAction action = new ParametersAction(param);
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action));

BuildParameterStrategy strategy = new BuildParameterStrategy("priority");
assertTrue(strategy.isApplicable(item));
}

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

assertFalse(strategy.isApplicable(item));
}

@Test
public void isApplicable_returnsFalseWhenParameterIsNotANumber() throws Exception {
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 6f93df9

Please sign in to comment.