Add tests for BuildPageRedirectActions

This commit is contained in:
Robin Müller 2016-03-12 16:47:51 +01:00
parent e9313894ab
commit 893d92c753
3 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package com.dabsquared.gitlabjenkins.webhook.status;
import hudson.model.FreeStyleProject;
/**
* @author Robin Müller
*/
public class BranchBuildPageRedirectActionTest extends BuildPageRedirectActionTest {
@Override
protected BuildPageRedirectAction getBuildPageRedirectAction(FreeStyleProject project) {
return new BranchBuildPageRedirectAction(project, branch);
}
}

View File

@ -0,0 +1,81 @@
package com.dabsquared.gitlabjenkins.webhook.status;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.queue.QueueTaskFuture;
import hudson.plugins.git.GitSCM;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.JenkinsRule;
import org.kohsuke.stapler.StaplerResponse;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
/**
* @author Robin Müller
*/
@RunWith(MockitoJUnitRunner.class)
public abstract class BuildPageRedirectActionTest {
@Rule
public JenkinsRule jenkins = new JenkinsRule();
@Rule
public TemporaryFolder tmp = new TemporaryFolder();
@Mock
private StaplerResponse response;
private String gitRepoUrl;
protected String commitSha1;
protected String branch = "master";
@Before
public void setup() throws Exception {
Git.init().setDirectory(tmp.getRoot()).call();
tmp.newFile("test");
Git git = Git.open(tmp.getRoot());
git.add().addFilepattern("test");
RevCommit commit = git.commit().setMessage("test").call();
commitSha1 = commit.getId().getName();
gitRepoUrl = tmp.getRoot().toURI().toString();
}
@Test
public void redirectToBuildUrl() throws IOException, ExecutionException, InterruptedException {
FreeStyleProject testProject = jenkins.createFreeStyleProject("test");
testProject.setScm(new GitSCM(gitRepoUrl));
QueueTaskFuture<FreeStyleBuild> future = testProject.scheduleBuild2(0);
FreeStyleBuild build = future.get();
getBuildPageRedirectAction(testProject).execute(response);
verify(response).sendRedirect2(jenkins.getInstance().getRootUrl() + build.getUrl());
}
@Test
public void redirectToBuildStatusUrl() throws IOException, ExecutionException, InterruptedException {
FreeStyleProject testProject = jenkins.createFreeStyleProject("test");
testProject.setScm(new GitSCM(gitRepoUrl));
QueueTaskFuture<FreeStyleBuild> future = testProject.scheduleBuild2(0);
FreeStyleBuild build = future.get();
doThrow(IOException.class).when(response).sendRedirect2(jenkins.getInstance().getRootUrl() + build.getUrl());
getBuildPageRedirectAction(testProject).execute(response);
verify(response).sendRedirect2(jenkins.getInstance().getRootUrl() + build.getBuildStatusUrl());
}
protected abstract BuildPageRedirectAction getBuildPageRedirectAction(FreeStyleProject project);
}

View File

@ -0,0 +1,13 @@
package com.dabsquared.gitlabjenkins.webhook.status;
import hudson.model.FreeStyleProject;
/**
* @author Robin Müller
*/
public class CommitBuildPageRedirectActionTest extends BuildPageRedirectActionTest {
@Override
protected BuildPageRedirectAction getBuildPageRedirectAction(FreeStyleProject project) {
return new CommitBuildPageRedirectAction(project, commitSha1);
}
}