package hudson.plugins.tasks.workflow;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import hudson.FilePath;

import hudson.model.Result;

import hudson.plugins.tasks.TasksResultAction;

import static org.junit.Assert.*;

/**
 * Test workflow compatibility.
 */
public class WorkflowCompatibilityTest {

    @ClassRule
    public static JenkinsRule j = new JenkinsRule();

    private static final String TASKS_FILE = "/hudson/plugins/tasks/parser/file-with-tasks.txt";

    /**
     * Run a workflow job using {@link TasksPublisher} and check for success.
     */
    @Test
    public void tasksPublisherWorkflowStep() throws Exception {
        WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "wf");
        FilePath workspace = j.jenkins.getWorkspaceFor(job);
        FilePath report = workspace.child("target").child("tasks.txt");
        report.copyFrom(WorkflowCompatibilityTest.class.getResourceAsStream(TASKS_FILE));
        job.setDefinition(new CpsFlowDefinition(
        "node {" +
        "  step([$class: 'TasksPublisher', pattern: '**/tasks.txt', high: 'FIXME', normal: 'TODO'])" +
        "}"));

        // Build must be success, since there are no thresholds set
        j.assertBuildStatusSuccess(job.scheduleBuild2(0));
        TasksResultAction result = job.getLastBuild().getAction(TasksResultAction.class);

        // The result has to report exactly 2 annotations coming from two tasks markers in the original file
        assertTrue(result.getResult().getAnnotations().size() == 2);
    }

    /**
     * Run a workflow job using {@link TasksPublisher} with a failing threshols of 0, so the given example file
     * "/hudson/plugins/tasks/parser/tasks-case-test.txt" will make the build to fail.
     */
    @Test
    public void tasksPublisherWorkflowStepSetLimits() throws Exception {
        WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "wf2");
        FilePath workspace = j.jenkins.getWorkspaceFor(job);
        FilePath report = workspace.child("target").child("tasks.txt");
        report.copyFrom(WorkflowCompatibilityTest.class.getResourceAsStream(TASKS_FILE));
        job.setDefinition(new CpsFlowDefinition(
        "node {" +
        "  step([$class: 'TasksPublisher', pattern: '**/tasks.txt', high: 'FIXME', normal: 'TODO', failedTotalAll: '0', usePreviousBuildAsReference: false])" +
        "}"));

        // The build must fail, since there is a failing threshold set (failedTotalAll: '0') 
        j.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0).get());
        TasksResultAction result = job.getLastBuild().getAction(TasksResultAction.class);

        // The result has to report exactly 2 annotations coming from two tasks markers in the original file
        assertTrue(result.getResult().getAnnotations().size() == 2);
    }

    /**
     * Run a workflow job using {@link TasksPublisher} with a unstable threshols of 0, so the given example file
     * "/hudson/plugins/tasks/parser/tasks-case-test.txt" will make the build to fail.
     */
    @Test
    public void tasksPublisherWorkflowStepFailure() throws Exception {
        WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "wf3");
        FilePath workspace = j.jenkins.getWorkspaceFor(job);
        FilePath report = workspace.child("target").child("tasks.txt");
        report.copyFrom(WorkflowCompatibilityTest.class.getResourceAsStream(TASKS_FILE));
        job.setDefinition(new CpsFlowDefinition(
        "node {" +
        "  step([$class: 'TasksPublisher', pattern: '**/tasks.txt', high: 'FIXME', normal: 'TODO', unstableTotalAll: '0', usePreviousBuildAsReference: false])" +
        "}"));

        // The build must report unstability, since there is an unstable threshold set (unstableTotalAll: '0') 
        j.assertBuildStatus(Result.UNSTABLE, job.scheduleBuild2(0).get());
        TasksResultAction result = job.getLastBuild().getAction(TasksResultAction.class);

        // The result has to report exactly 2 annotations coming from two tasks markers in the original file
        assertTrue(result.getResult().getAnnotations().size() == 2);
    }
}

@@ -33,16 +33,51 @@
 kflow;%0A%0A
+import static org.junit.Assert.*;%0A%0A
 import o
@@ -129,16 +129,16 @@
 nition;%0A
-
 import o
@@ -282,33 +282,8 @@
 e;%0A%0A
-import hudson.FilePath;%0A%0A
 impo
@@ -307,16 +307,60 @@
 esult;%0A%0A
+import hudson.plugins.tasks.TasksPublisher;%0A
 import h
@@ -410,33 +410,23 @@
 ort 
-static org.junit.Assert.*
+hudson.FilePath
 ;%0A%0A/
@@ -1660,36 +1660,38 @@
 e%0A        assert
-True
+Equals
 (result.getResul
@@ -1709,35 +1709,33 @@
 tations().size()
- ==
+,
  2);%0A    %7D%0A%0A    
@@ -2656,33 +2656,32 @@
 edTotalAll: '0')
- 
 %0A        j.asser
@@ -2945,36 +2945,38 @@
 e%0A        assert
-True
+Equals
 (result.getResul
@@ -2994,35 +2994,33 @@
 tations().size()
- ==
+,
  2);%0A    %7D%0A%0A    
@@ -3968,17 +3968,16 @@
 ll: '0')
- 
 %0A       
@@ -4262,12 +4262,14 @@
 sert
-True
+Equals
 (res
@@ -4311,11 +4311,9 @@
 ze()
- ==
+,
  2);

Fix @uhafner review comments