|
| 1 | +package stashpullrequestbuilder.stashpullrequestbuilder; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.is; |
| 5 | +import static org.mockito.Mockito.lenient; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | + |
| 8 | +import hudson.model.FreeStyleProject; |
| 9 | +import java.util.Collections; |
| 10 | +import javax.servlet.ServletContext; |
| 11 | +import javax.servlet.http.HttpServletRequest; |
| 12 | +import net.sf.json.JSONObject; |
| 13 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
| 14 | +import org.junit.Rule; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.jvnet.hudson.test.JenkinsRule; |
| 18 | +import org.kohsuke.stapler.RequestImpl; |
| 19 | +import org.kohsuke.stapler.Stapler; |
| 20 | +import org.kohsuke.stapler.StaplerRequest; |
| 21 | +import org.kohsuke.stapler.WebApp; |
| 22 | +import org.mockito.junit.MockitoJUnit; |
| 23 | +import org.mockito.junit.MockitoJUnitRunner; |
| 24 | +import org.mockito.junit.MockitoRule; |
| 25 | +import org.mockito.quality.Strictness; |
| 26 | +import stashpullrequestbuilder.stashpullrequestbuilder.StashBuildTrigger.StashBuildTriggerDescriptor; |
| 27 | + |
| 28 | +@RunWith(MockitoJUnitRunner.class) |
| 29 | +public class StashBuildTriggerTest { |
| 30 | + |
| 31 | + @Rule public JenkinsRule jenkinsRule = new JenkinsRule(); |
| 32 | + @Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); |
| 33 | + |
| 34 | + private StaplerRequest makeStaplerRequest() { |
| 35 | + ServletContext servletContext = mock(ServletContext.class); |
| 36 | + WebApp webApp = new WebApp(servletContext); |
| 37 | + |
| 38 | + Stapler stapler = mock(Stapler.class); |
| 39 | + lenient().when(stapler.getWebApp()).thenReturn(webApp); |
| 40 | + |
| 41 | + HttpServletRequest servletRequest = mock(HttpServletRequest.class); |
| 42 | + |
| 43 | + return new RequestImpl(stapler, servletRequest, Collections.emptyList(), null); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void pipeline_jobs_not_supported_by_default() throws Exception { |
| 48 | + JSONObject json = new JSONObject(); |
| 49 | + |
| 50 | + StaplerRequest staplerRequest = makeStaplerRequest(); |
| 51 | + StashBuildTriggerDescriptor descriptor = new StashBuildTriggerDescriptor(); |
| 52 | + descriptor.configure(staplerRequest, json); |
| 53 | + |
| 54 | + FreeStyleProject project = jenkinsRule.createFreeStyleProject(); |
| 55 | + assertThat(descriptor.isApplicable(project), is(true)); |
| 56 | + |
| 57 | + WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class); |
| 58 | + assertThat(descriptor.isApplicable(workflowJob), is(false)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void pipeline_jobs_not_supported_when_pipeline_support_disabled() throws Exception { |
| 63 | + JSONObject json = new JSONObject(); |
| 64 | + json.put("enablePipelineSupport", "false"); |
| 65 | + |
| 66 | + StaplerRequest staplerRequest = makeStaplerRequest(); |
| 67 | + StashBuildTriggerDescriptor descriptor = new StashBuildTriggerDescriptor(); |
| 68 | + descriptor.configure(staplerRequest, json); |
| 69 | + |
| 70 | + FreeStyleProject project = jenkinsRule.createFreeStyleProject(); |
| 71 | + assertThat(descriptor.isApplicable(project), is(true)); |
| 72 | + |
| 73 | + WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class); |
| 74 | + assertThat(descriptor.isApplicable(workflowJob), is(false)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void pipeline_jobs_supported_when_pipeline_support_enabled() throws Exception { |
| 79 | + JSONObject json = new JSONObject(); |
| 80 | + json.put("enablePipelineSupport", "true"); |
| 81 | + |
| 82 | + StaplerRequest staplerRequest = makeStaplerRequest(); |
| 83 | + StashBuildTriggerDescriptor descriptor = new StashBuildTriggerDescriptor(); |
| 84 | + descriptor.configure(staplerRequest, json); |
| 85 | + |
| 86 | + FreeStyleProject project = jenkinsRule.createFreeStyleProject(); |
| 87 | + assertThat(descriptor.isApplicable(project), is(true)); |
| 88 | + |
| 89 | + WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class); |
| 90 | + assertThat(descriptor.isApplicable(workflowJob), is(true)); |
| 91 | + } |
| 92 | +} |
0 commit comments