Skip to content

Commit

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

* Made getStrategy method package-protected and condition for non-integer input.

* Revert the name back to original

* Remove behavior change from production object

Value is set to 0 if a negative value is passed.

* Adjust comment for package protected method

* Use spotbugs NonNull instead of Atlassian NotNull

* Inherit javadoc from the parent object

* Clarify source of test values

Show when default is from test vs. parent object

---------

Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
yashpal2104 and MarkEWaite authored Jan 7, 2025
1 parent ce836d1 commit b91ea54
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ private ListBoxModel internalFillDefaultPriorityItems(int value) {
return items;
}

/* package-protected for testing */
@CheckForNull
private MultiBucketStrategy getStrategy() {
MultiBucketStrategy getStrategy() {
SorterStrategy strategy = PrioritySorterConfiguration.get().getStrategy();
if (strategy == null || !(strategy instanceof MultiBucketStrategy)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package jenkins.advancedqueue.sorter.strategy;

import static org.junit.Assert.assertEquals;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.Queue;
import hudson.util.ListBoxModel;
import jenkins.advancedqueue.sorter.SorterStrategyCallback;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class MultiBucketStrategyTest extends MultiBucketStrategy {

/** @{inheritDoc} */
@Override
public SorterStrategyCallback onNewItem(@NonNull Queue.Item item, SorterStrategyCallback weightCallback) {
return weightCallback;
}

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

private static MultiBucketStrategy strategy;
private static MultiBucketStrategy.MultiBucketStrategyDescriptor descriptor;
private static final int TEST_PRIORITY_COUNT = 10;
private static final int TEST_PRIORITY_DEFAULT = 5;

@Before
public void setUp() {
strategy = new MultiBucketStrategy(TEST_PRIORITY_COUNT, TEST_PRIORITY_DEFAULT) {
@Override
public SorterStrategyCallback onNewItem(@NonNull Queue.Item item, SorterStrategyCallback weightCallback) {
return weightCallback;
}
};

descriptor = new MultiBucketStrategy.MultiBucketStrategyDescriptor() {
@Override
public String getShortName() {
return "";
}

@Override
protected MultiBucketStrategy getStrategy() {
return strategy;
}
};
}

@Test
public void getNumberOfPrioritiesReturnsCorrectValue() {
assertEquals(TEST_PRIORITY_COUNT, strategy.getNumberOfPriorities());
}

@Test
public void getDefaultPriorityReturnsCorrectValue() {
assertEquals(TEST_PRIORITY_DEFAULT, strategy.getDefaultPriority());
}

@Test
public void doFillDefaultPriorityItemsReturnsCorrectItems() {
ListBoxModel items = descriptor.doFillDefaultPriorityItems();
assertEquals(TEST_PRIORITY_COUNT, items.size());
for (int i = 0; i < TEST_PRIORITY_COUNT; i++) {
assertEquals(String.valueOf(i + 1), items.get(i).value);
}
}

@Test
public void doUpdateDefaultPriorityItemsHandlesInvalidInput() {
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("invalid");
assertEquals(MultiBucketStrategy.DEFAULT_PRIORITY, items.size());
for (int i = 0; i < MultiBucketStrategy.DEFAULT_PRIORITY; i++) {
assertEquals(String.valueOf(i + 1), items.get(i).value);
}
}

@Test
public void doUpdateDefaultPriorityItemsHandlesValidInput() {
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("3");
assertEquals(3, items.size());
for (int i = 0; i < 3; i++) {
assertEquals(String.valueOf(i + 1), items.get(i).value);
}
}

@Test
public void doUpdateDefaultPriorityItemsHandlesNegativeInput() {
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("-1");
assertEquals(0, items.size());
}

@Test
public void doUpdateDefaultPriorityItemsHandlesZeroInput() {
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("0");
assertEquals(0, items.size());
}

@Test
public void doUpdateDefaultPriorityItemsHandlesLargeInput() {
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("100");
assertEquals(100, items.size());
for (int i = 0; i < 100; i++) {
assertEquals(String.valueOf(i + 1), items.get(i).value);
}
}
}

0 comments on commit b91ea54

Please sign in to comment.