package io.jenkins.plugins.analysis.warnings;

import java.io.IOException;
import java.util.function.Consumer;

import org.junit.Test;

import static org.assertj.core.api.Assertions.*;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.plugins.warnings.ParserConfiguration;
import hudson.plugins.warnings.WarningsPublisher;
import hudson.plugins.warnings.WarningsResult;
import hudson.plugins.warnings.WarningsResultAction;

/**
 * Integration tests for freestyle jobs with checkstyle plugin.
 *
 * @author Ullrich Hafner
 */
@SuppressWarnings({"OverlyBroadThrowsClause", "ProhibitedExceptionDeclared"})
public class FreestyleJobITest extends IntegrationTest {
    /**
     * Runs the Eclipse parser on an empty workspace: the build should report 0 issues and an error message.
     *
     * @throws Exception
     *         in case of an error
     */
    @Test
    public void shouldCreateEmptyResult() throws Exception {
        FreeStyleProject project = j.createFreeStyleProject();
        enableWarnings(project);

        WarningsResult result = scheduleBuild(project, Result.SUCCESS);

        assertThat(result.getNumberOfAnnotations()).isEqualTo(0);
        assertThat(result.getErrors()).isNotEmpty();
    }

    /**
     * Runs the Eclipse parser on an output file that contains several issues: the build should report 8 issues.
     *
     * @throws Exception
     *         in case of an error
     */
    @Test
    public void shouldCreateResultWithWarnings() throws Exception {
        FreeStyleProject project = createJobWithWorkspaceFile("eclipse.txt");
        enableWarnings(project);

        WarningsResult result = scheduleBuild(project, Result.SUCCESS);

        assertThat(result.getNumberOfAnnotations()).isEqualTo(8);
    }

    /**
     * Sets the UNSTABLE threshold to 8 and parse a file that contains exactly 8 warnings: the build should be unstable.
     *
     * @throws Exception
     *         in case of an error
     */
    @Test
    public void shouldCreateUnstableResult() throws Exception {
        FreeStyleProject project = createJobWithWorkspaceFile("eclipse.txt");
        enableWarnings(project, publisher -> publisher.setUnstableTotalAll("7"));

        WarningsResult result = scheduleBuild(project, Result.UNSTABLE);

        assertThat(result.getNumberOfAnnotations()).isEqualTo(8);
        // FIXME: SUCCESS?? assertThat(result.getPluginResult()).isEqualTo(Result.UNSTABLE);
    }

    private WarningsPublisher enableWarnings(final FreeStyleProject job) {
        WarningsPublisher publisher = new WarningsPublisher();
        publisher.setParserConfigurations(new ParserConfiguration[] {
                new ParserConfiguration("**/*issues.txt", "Java Compiler (Eclipse)")
        });
        job.getPublishersList().add(publisher);
        return publisher;
    }

    private WarningsPublisher enableWarnings(final FreeStyleProject job, final Consumer<WarningsPublisher> configuration) {
        WarningsPublisher publisher = enableWarnings(job);
        configuration.accept(publisher);
        return publisher;
    }

    private FreeStyleProject createJobWithWorkspaceFile(final String fileName) throws IOException, InterruptedException {
        FreeStyleProject job = j.createFreeStyleProject();
        copyFilesToWorkspace(job, fileName);
        return job;
    }

    /**
     * Schedules a new build for the specified job and returns the created {@link WarningsResult} after the build has
     * been finished.
     *
     * @param job
     *         the job to schedule
     *
     * @param status the expected result for the build
     * @return the created {@link WarningsResult}
     */
    private WarningsResult scheduleBuild(final FreeStyleProject job, final Result status) throws Exception {
        FreeStyleBuild build = j.assertBuildStatus(status, job.scheduleBuild2(0));

        WarningsResultAction action = build.getAction(WarningsResultAction.class);

        assertThat(action).isNotNull();

        return action.getResult();
    }

}

@@ -307,36 +307,33 @@
 ins.
-warnings.ParserConfiguration
+analysis.core.BuildResult
 ;%0Aim
@@ -361,33 +361,46 @@
 arnings.
-WarningsPublisher
+AggregatedWarningsResultAction
 ;%0Aimport
@@ -424,30 +424,35 @@
 arnings.
-WarningsResult
+ParserConfiguration
 ;%0Aimport
@@ -484,28 +484,25 @@
 Warnings
-ResultAction
+Publisher
 ;%0A%0A/**%0A 
@@ -1096,32 +1096,29 @@
 );%0A%0A        
-Warnings
+Build
 Result resul
@@ -1678,32 +1678,29 @@
 );%0A%0A        
-Warnings
+Build
 Result resul
@@ -2260,32 +2260,29 @@
 );%0A%0A        
-Warnings
+Build
 Result resul
@@ -2404,28 +2404,8 @@
     
- // FIXME: SUCCESS??
  ass
@@ -3455,32 +3455,29 @@
 ated %7B@link 
-Warnings
+Build
 Result%7D afte
@@ -3670,24 +3670,21 @@
  %7B@link 
-Warnings
+Build
 Result%7D%0A
@@ -3703,24 +3703,21 @@
 private 
-Warnings
+Build
 Result s
@@ -3881,32 +3881,42 @@
 2(0));%0A%0A        
+Aggregated
 WarningsResultAc
@@ -3945,16 +3945,26 @@
 tAction(
+Aggregated
 Warnings

Use AggregatedAction in tests.