package io.jenkins.blueocean.service.embedded;

import hudson.model.Label;
import io.jenkins.blueocean.service.embedded.rest.PipelineImpl;
import io.jenkins.blueocean.service.embedded.rest.PipelineRunImpl;
import io.jenkins.blueocean.service.embedded.scm.GitSampleRepoRule;
import jenkins.branch.BranchProperty;
import jenkins.branch.BranchSource;
import jenkins.branch.DefaultBranchPropertyStrategy;
import jenkins.plugins.git.GitSCMSource;
import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.replay.ReplayAction;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
 * @author Ivan Meredith
 */
public class AbstractRunImplTest extends BaseTest {
    @Rule
    public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();

    @Before
    public void setup() throws Exception{
        super.setup();
        sampleRepo.init();
    }
    //Disabled, see JENKINS-36453
    //@Test
    public void replayRunTest() throws Exception {
        WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
        j.createOnlineSlave(Label.get("remote"));
        job1.setDefinition(new CpsFlowDefinition(
            "node('remote') {\n" +
                "    ws {\n" +
                "        git($/" + sampleRepo + "/$)\n" +
                "    }\n" +
                "}"));


        WorkflowRun b1 = job1.scheduleBuild2(0).get();
        j.assertBuildStatusSuccess(b1);

        sampleRepo.write("file1", "");
        sampleRepo.git("add", "file1");
        sampleRepo.git("commit", "--message=init");

        WorkflowRun b2 = job1.scheduleBuild2(0).get();
        j.assertBuildStatusSuccess(b2);

        Assert.assertNotEquals(new PipelineRunImpl(b1, null).getCommitId(), new PipelineRunImpl(b2, null).getCommitId());

        request().post("/organizations/jenkins/pipelines/pipeline1/runs/1/replay").build(String.class);

        j.waitForCompletion(job1.getLastBuild());

        request().get("/organizations/jenkins/pipelines/pipeline1/runs/1/").build(Map.class);
        request().get("/organizations/jenkins/pipelines/pipeline1/runs/2/").build(Map.class);
        Map r = request().get("/organizations/jenkins/pipelines/pipeline1/runs/3/").build(Map.class);
        Assert.assertEquals(r.get("commitId"), new PipelineRunImpl(b2,null).getCommitId());
    }

    @Test
    public void replayRunTestMB() throws Exception {
        j.createOnlineSlave(Label.get("remote"));

        sampleRepo.write("Jenkinsfile", "node('remote') {\n" +
            "    ws {\n" +
            "       checkout scm\n" +
            "       stage 'build'\n "+"node {echo 'Building'}\n"+
            "       stage 'test'\nnode { echo 'Testing'}\n"+
            "       stage 'deploy'\nnode { echo 'Deploying'}\n" +
            "       }\n" +
            "   }");
        sampleRepo.git("add", "Jenkinsfile");
        sampleRepo.git("commit", "--message=init");

        WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
        mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false),
            new DefaultBranchPropertyStrategy(new BranchProperty[0])));
        for (SCMSource source : mp.getSCMSources()) {
            assertEquals(mp, source.getOwner());
        }


        mp.scheduleBuild2(0).getFuture().get();
        WorkflowJob job1 = mp.getItem("master");
        WorkflowRun b1 = job1.scheduleBuild2(0).waitForStart();
        j.waitForCompletion(b1);
        j.assertBuildStatusSuccess(b1);

        sampleRepo.write("file1", "");
        sampleRepo.git("add", "file1");
        sampleRepo.git("commit", "--message=init");

        WorkflowRun b2 = job1.scheduleBuild2(0).get();
        j.assertBuildStatusSuccess(b2);

        Assert.assertNotEquals(new PipelineRunImpl(b1, null).getCommitId(), new PipelineRunImpl(b2, null).getCommitId());

        request().post("/organizations/jenkins/pipelines/p/branches/master/runs/1/replay").build(String.class);

        j.waitForCompletion(job1.getLastBuild());

        request().get("/organizations/jenkins/pipelines/p/branches/master/runs/3/").build(Map.class);
        request().get("/organizations/jenkins/pipelines/p/branches/master/runs/3/").build(Map.class);
        Map r = request().get("/organizations/jenkins/pipelines/p/branches/master/runs/3/").build(Map.class);
        Assert.assertEquals(new PipelineRunImpl(b1,null).getCommitId(), r.get("commitId"));
    }
}

@@ -68,16 +68,43 @@
 .Label;%0A
+import hudson.model.Queue;%0A
 import i
@@ -4271,32 +4271,50 @@
 tId());%0A%0A       
+ Map replayBuild =
  request().post(
@@ -4366,25 +4366,43 @@
 master/runs/
-1
+%22+ b1.getNumber()+%22
 /replay%22).bu
@@ -4397,38 +4397,35 @@
 /replay%22).build(
-String
+Map
 .class);%0A%0A      
@@ -4413,33 +4413,32 @@
 Map.class);%0A
-%0A
         
 j.waitForCom
@@ -4429,161 +4429,118 @@
     
-j.waitForCompletion(job1.getLastBuild());%0A%0A        request().get(%22/organizations/jenkins/pipelines/p/branches/master/runs/3/%22).build(Map.class
+Queue.Item item = j.getInstance().getQueue().getItem(Long.parseLong((String)replayBuild.get(%22id%22))
 );%0A
+%0A
         
 requ
@@ -4539,102 +4539,72 @@
     
-request().get(%22/organizations/jenkins/pipelines/p/branches/master/runs/3/%22).build(Map.class
+WorkflowRun replayedRun = (WorkflowRun)item.getFuture().get(
 );%0A
+%0A
     
@@ -4682,25 +4682,51 @@
 master/runs/
-3
+%22+replayedRun.getNumber()+%22
 /%22).build(Ma

Fix test