Skip to content

Commit

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

* Change freestyleproject object name from project to j

* Make test results clearer by checking decision logger results

Since the API provides a decision logger and an adjustment was made to
the decision logger in the test setup, let's use that decision logger
to make the test expectations clearer and the assertions more precise.

Initialize the JenkinsRule only once and create the project only once,
since neither are modified substantially for the test.

Use a more precise name for the methods that are called before test
execution so that later readers understand the purpose of the methods
without reading their contents.

Use the common name 'j' for the JenkinsRule and a more descriptive name
for the FreeStyleProject variable name.

---------

Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
yashpal2104 and MarkEWaite authored Dec 28, 2024
1 parent 645c349 commit 0a56215
Showing 1 changed file with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package jenkins.advancedqueue.jobinclusion.strategy;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import com.cloudbees.hudson.plugins.folder.Folder;
import hudson.model.FreeStyleProject;
import java.util.ArrayList;
import java.util.List;
import jenkins.advancedqueue.DecisionLogger;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class FolderPropertyLoaderTest {

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

private static Folder folder;
private static FreeStyleProject project;
private DecisionLogger decisionLogger;
private List<String> loggedMessages;

@BeforeClass
public static void createJob() throws Exception {
folder = j.createProject(com.cloudbees.hudson.plugins.folder.Folder.class, "testFolder");
project = folder.createProject(FreeStyleProject.class, "testProject");
}

@Before
public void createDecisionLogger() throws Exception {
loggedMessages = new ArrayList<>();
decisionLogger = new DecisionLogger() {
@Override
public DecisionLogger addDecisionLog(int indent, String log) {
loggedMessages.add(log);
return this;
}
};
}

@Test
public void getJobGroupName_returnsGroupName_whenJobGroupIsEnabled() throws Exception {
JobInclusionFolderProperty property = new JobInclusionFolderProperty(true, "TestGroup");
folder.getProperties().add(property);

String result = FolderPropertyLoader.getJobGroupName(decisionLogger, project);

assertEquals("TestGroup", result);
assertThat(loggedMessages, hasItem("JobGroup is enabled, with JobGroup [TestGroup] ..."));
}

@Test
public void getJobGroupName_returnsNull_whenNoJobGroupProperty() throws Exception {
String result = FolderPropertyLoader.getJobGroupName(decisionLogger, project);

assertNull(result);
assertThat(loggedMessages, hasItem("No match ..."));
}

@Test
public void getJobGroupName_returnsNull_whenJobGroupIsDisabled() throws Exception {
JobInclusionFolderProperty property = new JobInclusionFolderProperty(false, "TestGroup");
folder.getProperties().add(property);

String result = FolderPropertyLoader.getJobGroupName(decisionLogger, project);

assertNull(result);
assertThat(loggedMessages, hasItem("No match ..."));
}

@Test
public void getJobGroupName_returnsNull_whenParentIsNotFolder() throws Exception {
FreeStyleProject standaloneProject = j.createFreeStyleProject("standaloneProject");

String result = FolderPropertyLoader.getJobGroupName(decisionLogger, standaloneProject);

assertNull(result);
assertThat(loggedMessages, hasItem("No match ..."));
}
}

0 comments on commit 0a56215

Please sign in to comment.