/**
   * Sets up a dashboard view with a warnings-per-project portlet. Builds a job and checks if the
   * portlet shows the correct number of warnings. Then one of the tools is deselected. The portlet
   * should then show only the remaining number of warnings.
   */
  @Test
  @WithPlugins("dashboard-view")
  public void should_aggregate_warnings_in_dashboard_portlet() {
    FreeStyleJob job = createFreeStyleJob();
    buildSuccessfulJob(job);

    DashboardView dashboard = jenkins.views.create(DashboardView.class, createRandomName());
    dashboard.configure();
    dashboard.matchAllJobs();
    WarningsPerProjectPortlet portlet = dashboard.addBottomPortlet(WarningsPerProjectPortlet.class);
    portlet.setName("My Warnings");
    portlet.hideZeroWarningsProjects(false).showImagesInTableHeader(true);
    dashboard.save();

    dashboard.open();
    assertThat(dashboard, hasWarningsFor(job, CHECKSTYLE, CHECKSTYLE_ALL));
    assertThat(dashboard, hasWarningsFor(job, PMD, PMD_ALL));
    assertThat(dashboard, hasWarningsFor(job, FINDBUGS, FINDBUGS_ALL));
    assertThat(dashboard, hasWarningsFor(job, TASKS, TASKS_ALL));
    assertThat(dashboard, hasWarningsFor(job, WARNINGS, WARNINGS_ALL));

    // uncheck Open Tasks
    dashboard.configure();
    portlet = dashboard.getBottomPortlet(WarningsPerProjectPortlet.class);
    portlet.checkCollectedPlugin(TASKS, false);
    dashboard.save();
    dashboard.open();
    assertThat(dashboard, not(hasWarningsFor(job, TASKS, TASKS_ALL)));
  }
  /**
   * Verifies that no other trend graphs are shown if configured in the graph configuration screen
   * per user.
   */
  @Test
  @Issue("JENKINS-30270")
  public void should_deactivate_all_other_trend_graphs() {
    FreeStyleJob job = createFreeStyleJob();

    buildSuccessfulJob(job);
    buildSuccessfulJob(job);

    job.open();
    elasticSleep(500);

    assertThatNumberOfGraphsIs(job, 48);

    AnalysisCollectorAction action = new AnalysisCollectorAction(job);
    AnalysisGraphConfigurationView view = action.configureTrendGraphForUser();

    deactivateOtherTrendGraphs(view, true);

    assertThatNumberOfGraphsIs(job, 6);

    deactivateOtherTrendGraphs(view, false);

    assertThatNumberOfGraphsIs(job, 48);
  }
  /**
   * Verifies that the plugin shows on the job summary page a section with the individual results
   * for each aggregated warning type. Each result is shown on a separate line (HTML item) with the
   * plugin icon and the number of warnings per tool.
   */
  @Test
  public void should_show_job_summary_with_warnings_per_tool() {
    FreeStyleJob job = createFreeStyleJob();
    buildSuccessfulJob(job);

    assertThat(
        job,
        allOf(
            hasAnalysisWarningsFor(CHECKSTYLE),
            hasAnalysisWarningsFor(PMD),
            hasAnalysisWarningsFor(FINDBUGS),
            hasAnalysisWarningsFor(TASKS),
            hasAnalysisWarningsFor(WARNINGS)));
    assertThat(job, not(hasAnalysisWarningsFor(DRY)));
  }
  /**
   * Verifies that the plugin correctly identifies new open tasks. The first build contains 4 open
   * tasks. The second builds adds another 4 open tasks, summing up to a total of 8 open tasks. The
   * second build should then contain 4 new warnings.
   */
  @Test
  public void should_compute_all_new_open_tasks() {
    FreeStyleJob job = createJob(ANALYSIS_COLLECTOR_PLUGIN_RESOURCES + "/Tasks.java", true);
    buildSuccessfulJob(job);

    job.configure();
    job.copyResource(ANALYSIS_COLLECTOR_PLUGIN_RESOURCES + "/Tasks2.java");
    job.save();

    Build build = buildSuccessfulJob(job);

    AnalysisCollectorAction action = new AnalysisCollectorAction(build);
    action.open();

    assertThat(action.getNumberOfWarnings(), is(8));
    assertThat(action.getNumberOfWarningsWithHighPriority(), is(2));
    assertThat(action.getNumberOfWarningsWithNormalPriority(), is(4));
    assertThat(action.getNumberOfWarningsWithLowPriority(), is(2));
    assertThat(action.getNumberOfNewWarnings(), is(4));
  }
  /**
   * Sets up a list view with a warnings column. Builds a job and checks if the column shows the
   * correct number of warnings and provides a direct link to the actual warning results. Also
   * verifies that the mouse-over tooltip will show the correct number of warnings per checked
   * plugin.
   */
  @Test
  public void should_set_warnings_count_in_list_view_column() {
    FreeStyleJob job = createFreeStyleJob();
    buildSuccessfulJob(job);

    ListView view = jenkins.views.create(ListView.class, createRandomName());
    view.configure();
    view.matchAllJobs();
    view.addColumn(AnalysisCollectorColumn.class);
    view.save();

    view.open();
    WebElement warningsCell = view.find(by.xpath(XPATH_LISTVIEW_WARNING_TD));
    assertThat(warningsCell.getText(), is(String.valueOf(TOTAL)));

    String tooltip = warningsCell.getAttribute("tooltip");
    assertThat(
        tooltip,
        allOf(
            containsString(
                "<a href=\"job/" + job.name + "/checkstyle\">" + CHECKSTYLE_ALL + "</a>"),
            containsString("<a href=\"job/" + job.name + "/findbugs\">" + FINDBUGS_ALL + "</a>"),
            containsString("<a href=\"job/" + job.name + "/pmd\">" + PMD_ALL + "</a>"),
            containsString("<a href=\"job/" + job.name + "/warnings\">" + WARNINGS_ALL + "</a>")));

    view.configure();
    AnalysisCollectorColumn column = view.getColumn(AnalysisCollectorColumn.class);
    column.checkPlugin(PMD, false);
    view.save();

    view.open();
    // check that PMD warnings are not collected to total warning number and tooltip
    warningsCell = view.find(by.xpath(XPATH_LISTVIEW_WARNING_TD));
    assertThat(warningsCell.getText(), is(String.valueOf(TOTAL - PMD_ALL)));
    tooltip = warningsCell.getAttribute("tooltip");
    assertThat(
        tooltip, not(containsString("<a href=\"job/" + job.name + "/pmd\">" + PMD_ALL + "</a>")));
  }