From a1175857c5016dd57a36bf9c481adf834cb6e71e Mon Sep 17 00:00:00 2001 From: James Dumay Date: Sun, 13 Nov 2016 10:42:58 +1100 Subject: [PATCH] Test for autofavoriting --- pom.xml | 12 ++- .../autofavorite/FavoritingScmListener.java | 15 +++- .../FavoritingScmListenerTest.java | 74 +++++++++++++++++++ 3 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 src/test/java/io/jenkins/blueocean/autofavorite/FavoritingScmListenerTest.java diff --git a/pom.xml b/pom.xml index 2843760..3461311 100644 --- a/pom.xml +++ b/pom.xml @@ -65,9 +65,15 @@ org.jenkins-ci.plugins - github-branch-source - 1.9 - true + scm-api + 1.3 + test + + + org.jenkins-ci.plugins.workflow + workflow-multibranch + 2.9.2 + test diff --git a/src/main/java/io/jenkins/blueocean/autofavorite/FavoritingScmListener.java b/src/main/java/io/jenkins/blueocean/autofavorite/FavoritingScmListener.java index 56f5914..4039060 100644 --- a/src/main/java/io/jenkins/blueocean/autofavorite/FavoritingScmListener.java +++ b/src/main/java/io/jenkins/blueocean/autofavorite/FavoritingScmListener.java @@ -80,9 +80,18 @@ public void onCheckout(Run build, SCM scm, FilePath workspace, TaskListene Job job = build.getParent(); User author = first.getAuthor(); - if (!User.getUnknown().equals(author) && !Favorites.hasFavorite(author, job) && !Favorites.isFavorite(author, job)) { - Favorites.addFavorite(author, job); - logger.log(Level.INFO, "Automatically favorited " + job.getFullName() + " for " + author); + + // User does not exist or is unknown + if (User.getById(author.getId(), false) == null || User.getUnknown().equals(author)) { + return; + } + + // This user has previously favorited this job but has removed the favorite + if (Favorites.hasFavorite(author, job) && !Favorites.isFavorite(author, job)) { + return; } + + Favorites.addFavorite(author, job); + logger.log(Level.INFO, "Automatically favorited " + job.getFullName() + " for " + author); } } diff --git a/src/test/java/io/jenkins/blueocean/autofavorite/FavoritingScmListenerTest.java b/src/test/java/io/jenkins/blueocean/autofavorite/FavoritingScmListenerTest.java new file mode 100644 index 0000000..d93d5e6 --- /dev/null +++ b/src/test/java/io/jenkins/blueocean/autofavorite/FavoritingScmListenerTest.java @@ -0,0 +1,74 @@ +package io.jenkins.blueocean.autofavorite; + +import hudson.model.Result; +import hudson.model.User; +import hudson.plugins.favorite.Favorites; +import jenkins.branch.BranchSource; +import jenkins.branch.MultiBranchProject.BranchIndexing; +import jenkins.plugins.git.GitSCMSource; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class FavoritingScmListenerTest { + @Rule + public JenkinsRule j = new JenkinsRule(); + + @Test + public void testAutoFavoriteForRegisteredUser() throws Exception { + User.getById("jdumay", true); + WorkflowJob job = createAndRunPipeline(); + User user = User.getById("jdumay", false); + assertNotNull(user); + assertTrue(Favorites.isFavorite(user, job)); + } + +// @Test + /** Disabled because of https://issues.jenkins-ci.org/browse/JENKINS-39694 **/ + public void testAutoFavoriteForNonRegisteredUser() throws Exception { + assertNull(User.getById("jdumay", false)); + WorkflowJob job = createAndRunPipeline(); + User user = User.getById("jdumay", false); + assertNotNull(user); + assertFalse(Favorites.isFavorite(user, job)); + } + + private WorkflowJob createAndRunPipeline() throws java.io.IOException, InterruptedException { + WorkflowMultiBranchProject mbp = j.createProject(WorkflowMultiBranchProject.class, "WorkflowMultiBranchProject"); + mbp.getSourcesList().add(new BranchSource(new GitSCMSource(null, "https://github.com/i386/feedle.git", "", "*", "", true))); + + BranchIndexing indexing = mbp.getIndexing(); + indexing.run(); + + while (indexing.getResult() == Result.NOT_BUILT) { + Thread.sleep(TimeUnit.SECONDS.toMillis(1)); + } + + assertEquals(Result.SUCCESS, indexing.getResult()); + + WorkflowJob job = mbp.getItem("master"); + while (job.getBuilds().isEmpty()) { + Thread.sleep(TimeUnit.SECONDS.toMillis(1)); + } + + WorkflowRun run = job.getBuildByNumber(1); + assertNotNull(run); + + + while (run.getResult() == null) { + Thread.sleep(TimeUnit.SECONDS.toMillis(1)); + } + return job; + } +}