package hudson.plugins.warnings;

import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import com.cloudbees.hudson.plugins.folder.computed.FolderComputation;
import jenkins.scm.api.SCMHead;
import javax.annotation.Nonnull;
import jenkins.branch.BranchProperty;
import jenkins.branch.BranchSource;
import jenkins.branch.DefaultBranchPropertyStrategy;
import jenkins.plugins.git.GitSCMSource;
import jenkins.plugins.git.GitSampleRepoRule;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.WebAssert;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.containsString;

/**
 * Tests the class {@link WarningsPublisher}.
 *
 * @author Ulli Hafner
 */
public class WarningsMultiBranchTest {
    private static final String SUFFIX_NAME = " Warnings";
    private static final String PARSER = "Maven";
    private static final String PATTERN = "Pattern";
    
    @Rule public JenkinsRule jenkinsRule = new JenkinsRule();
    @Rule public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();

   /**
     * Verifies that the graph is shown in multibranch job.
     *
     * @see <a href="https://issues.jenkins-ci.org/browse/JENKINS-31202">Issue 31202</a>
     */
    @Test 
    public void testIssue31202() throws Exception {
        sampleRepo.init();
        sampleRepo.write("Jenkinsfile", "" 
                       + "node {\n"
                       + "  checkout scm\n"
                       + "  step([$class: 'WarningsPublisher', parserConfigurations: [[parserName: '" + PARSER + "', pattern: '" + PATTERN + "']]])\n"
                       + "}\n"
                        );
        sampleRepo.git("add", "Jenkinsfile"); 
        sampleRepo.git("commit", "--all", "--message=flow1");
	
        WorkflowMultiBranchProject mp = jenkinsRule.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
        mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
        WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
        assertEquals(new SCMHead("master"), SCMHead.HeadByItem.findHead(p));
        assertEquals(1, mp.getItems().size());
        jenkinsRule.waitUntilNoActivity();
        WorkflowRun b1 = p.getLastBuild();
        assertEquals(1, b1.getNumber());
        sampleRepo.write("new", "empty");
        sampleRepo.git("add", "new"); 
        sampleRepo.git("commit", "--all", "--message=flow2");
        p = scheduleAndFindBranchProject(mp, "master");
        jenkinsRule.waitUntilNoActivity();
        b1 = p.getLastBuild();
        assertEquals(2, b1.getNumber());

        HtmlPage page = jenkinsRule.createWebClient().getPage(p,"");
        WebAssert.assertTextPresent(page, "Pipeline master");
        WebAssert.assertTextPresent(page, PARSER + SUFFIX_NAME + " Trend");
        WebAssert.assertTextPresent(page, "Enlarge");
        WebAssert.assertTextPresent(page, "Configure");
    }

    public static @Nonnull WorkflowJob scheduleAndFindBranchProject(@Nonnull WorkflowMultiBranchProject mp, @Nonnull String name) throws Exception {
        mp.scheduleBuild2(0).getFuture().get();
        return findBranchProject(mp, name);
    }

    public static @Nonnull WorkflowJob findBranchProject(@Nonnull WorkflowMultiBranchProject mp, @Nonnull String name) throws Exception {
        WorkflowJob p = mp.getItem(name);
        showIndexing(mp);
        if (p == null) {
            fail(name + " project not found");
        }
        return p;
    }

    static void showIndexing(@Nonnull WorkflowMultiBranchProject mp) throws Exception {
        FolderComputation<?> indexing = mp.getIndexing();
        System.out.println("---%<--- " + indexing.getUrl());
        indexing.writeWholeLogTo(System.out);
        System.out.println("---%<--- ");
    }
}



@@ -38,428 +38,34 @@
 ort 
-org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;%0Aimport com.cloudbees.hudson.plugins.folder.computed.FolderComputation;%0Aimport jenkins.scm.api.SCMHead;%0Aimport javax.annotation.Nonnull;%0Aimport jenkins.branch.BranchProperty;%0Aimport jenkins.branch.BranchSource;%0Aimport jenkins.branch.DefaultBranchPropertyStrategy;%0Aimport jenkins.plugins.git.GitSCMSource;%0Aimport jenkins.plugins.git.GitSampleRepoRule;
+javax.annotation.Nonnull;%0A
 %0Aimp
@@ -171,16 +171,94 @@
 lowRun;%0A
+import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;%0A
 import o
@@ -353,47 +353,66 @@
 com.
-gargoylesoftware.htmlunit.html.HtmlPage
+cloudbees.hudson.plugins.folder.computed.FolderComputation
 ;%0Aim
@@ -468,89 +468,332 @@
 ort 
-static org.junit.Assert.*
+com.gargoylesoftware.htmlunit.html.HtmlPage
 ;%0A
+%0A
 import 
-static org.hamcrest.CoreMatchers.containsString
+jenkins.branch.BranchProperty;%0Aimport jenkins.branch.BranchSource;%0Aimport jenkins.branch.DefaultBranchPropertyStrategy;%0Aimport jenkins.plugins.git.GitSCMSource;%0Aimport jenkins.plugins.git.GitSampleRepoRule;%0Aimport jenkins.scm.api.SCMHead;%0Aimport static org.junit.Assert.*
 ;%0A%0A/

Make remaining plugins compatible to SimpleBuildStep.