package org.jenkinsci.plugins.DependencyCheck;

import hudson.FilePath;
import hudson.model.Result;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.*;

public class DependencyCheckWorkflowTest {

    @Rule
    public JenkinsRule jenkinsRule = new JenkinsRule();

    /**
     * Run a workflow job using org.jenkinsci.plugins.DependencyCheck.DependencyCheckPublisher and check for success.
     */
    //@Test
    public void dependencyCheckPublisherWorkflowStep() throws Exception {
        WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "dependencyCheckWorkPublisherWorkflowStep");
        FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
        FilePath report = workspace.child("target").child("dependency-check-report.xml");
        report.copyFrom(DependencyCheckWorkflowTest.class.getResourceAsStream("/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report.xml"));
        job.setDefinition(new CpsFlowDefinition(""
                        + "node {\n"
                        + "  step([$class: 'DependencyCheckPublisher'])\n"
                        + "}\n", true)
        );
        jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
        //TODO
        //DependencyCheckResultAction result = job.getLastBuild().getAction(DependencyCheckResultAction.class);
        //assertTrue(result.getResult().getAnnotations().size() == 2);
    }

    /**
     * Run a workflow job using DependencyCheckPublisher with a failing threshold of 0, so the given example file
     * "/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report2.xml" will make the build to fail.
     */
    //@Test
    public void dependencyCheckPublisherWorkflowStepSetLimits() throws Exception {
        WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "dependencyCheckPublisherWorkflowStepSetLimits");
        FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
        FilePath report = workspace.child("target").child("dependency-check-report.xml");
        report.copyFrom(DependencyCheckWorkflowTest.class.getResourceAsStream("/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report.xml"));
        job.setDefinition(new CpsFlowDefinition(""
                        + "node {\n"
                        + "  step([$class: 'DependencyCheckPublisher', pattern: '**/dependency-check-report.xml', failedTotalAll: '0', usePreviousBuildAsReference: false])\n"
                        + "}\n", true)
        );
        jenkinsRule.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0).get());
        //DependencyCheckResultAction result = job.getLastBuild().getAction(DependencyCheckResultAction.class);
        //assertTrue(result.getResult().getAnnotations().size() == 2);
    }

    /**
     * Run a workflow job using DependencyCheckPublisher with a unstable threshold of 0, so the given example file
     * "/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report2.xml" will make the build to fail.
     */
    //@Test
    public void dependencyCheckPublisherWorkflowStepFailure() throws Exception {
        WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "dependencyCheckPublisherWorkflowStepFailure");
        FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
        FilePath report = workspace.child("target").child("dependency-check-report.xml");
        report.copyFrom(DependencyCheckWorkflowTest.class.getResourceAsStream("/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report.xml"));
        job.setDefinition(new CpsFlowDefinition(""
                        + "node {\n"
                        + "  step([$class: 'DependencyCheckPublisher', pattern: '**/dependency-check-report.xml', unstableTotalAll: '0', usePreviousBuildAsReference: false])\n"
                        + "}\n")
        );
        jenkinsRule.assertBuildStatus(Result.UNSTABLE, job.scheduleBuild2(0).get());
        //DependencyCheckResultAction result = job.getLastBuild().getAction(DependencyCheckResultAction.class);
        //assertTrue(result.getResult().getAnnotations().size() == 2);
    }
}


@@ -1068,33 +1068,33 @@
 nkinsci/plugins/
-d
+D
 ependencycheck/p
@@ -1078,33 +1078,33 @@
 ugins/Dependency
-c
+C
 heck/parser/depe
@@ -1777,33 +1777,33 @@
 nkinsci/plugins/
-d
+D
 ependencycheck/p
@@ -1787,33 +1787,33 @@
 ugins/Dependency
-c
+C
 heck/parser/depe
@@ -2361,33 +2361,33 @@
 nkinsci/plugins/
-d
+D
 ependencycheck/p
@@ -2371,33 +2371,33 @@
 ugins/Dependency
-c
+C
 heck/parser/depe
@@ -3170,33 +3170,33 @@
 nkinsci/plugins/
-d
+D
 ependencycheck/p
@@ -3180,33 +3180,33 @@
 ugins/Dependency
-c
+C
 heck/parser/depe
@@ -3758,17 +3758,17 @@
 plugins/
-d
+D
 ependenc
@@ -3768,17 +3768,17 @@
 pendency
-c
+C
 heck/par

Correct case on packages