@Before
 public void resetData() {
   xooSampleAnalysis =
       projectAnalysisRule
           .newProjectAnalysis(projectAnalysisRule.registerProject("shared/xoo-sample"))
           .withQualityProfile(
               projectAnalysisRule.registerProfile(
                   "/issue/IssueChangelogTest/one-issue-per-line-profile.xml"));
   xooSampleAnalysis.run();
   issue = searchRandomIssue();
 }
  @Before
  public void setup() {
    String qualityProfileKey =
        projectAnalysisRule.registerProfile(
            "/issue/IssueActionTest/xoo-one-issue-per-line-profile.xml");
    String projectKey = projectAnalysisRule.registerProject("shared/xoo-sample");

    this.projectAnalysis =
        projectAnalysisRule.newProjectAnalysis(projectKey).withQualityProfile(qualityProfileKey);
    this.projectAnalysis.run();
    this.issue = searchRandomIssue();
  }
public class IssueActionTest extends AbstractIssueTest {

  @Rule
  public final ProjectAnalysisRule projectAnalysisRule = ProjectAnalysisRule.from(ORCHESTRATOR);

  Issue issue;
  ProjectAnalysis projectAnalysis;

  @Before
  public void setup() {
    String qualityProfileKey =
        projectAnalysisRule.registerProfile(
            "/issue/IssueActionTest/xoo-one-issue-per-line-profile.xml");
    String projectKey = projectAnalysisRule.registerProject("shared/xoo-sample");

    this.projectAnalysis =
        projectAnalysisRule.newProjectAnalysis(projectKey).withQualityProfile(qualityProfileKey);
    this.projectAnalysis.run();
    this.issue = searchRandomIssue();
  }

  @Test
  public void no_comments_by_default() throws Exception {
    assertThat(issue.comments()).isEmpty();
  }

  @Test
  public void add_comment() throws Exception {
    IssueComment comment = adminIssueClient().addComment(issue.key(), "this is my *comment*");
    assertThat(comment.key()).isNotNull();
    assertThat(comment.htmlText()).isEqualTo("this is my <strong>comment</strong>");
    assertThat(comment.login()).isEqualTo("admin");
    assertThat(comment.createdAt()).isNotNull();

    // reload issue
    Issue reloaded = searchIssue(issue.key(), true);

    assertThat(reloaded.comments()).hasSize(1);
    assertThat(reloaded.comments().get(0).key()).isEqualTo(comment.key());
    assertThat(reloaded.comments().get(0).htmlText())
        .isEqualTo("this is my <strong>comment</strong>");
    assertThat(reloaded.updateDate().before(issue.creationDate())).isFalse();
  }

  /** SONAR-4450 */
  @Test
  public void should_reject_blank_comment() throws Exception {
    try {
      adminIssueClient().addComment(issue.key(), "  ");
      fail();
    } catch (HttpException ex) {
      assertThat(ex.status()).isEqualTo(400);
    }

    Issue reloaded = searchIssueByKey(issue.key());
    assertThat(reloaded.comments()).hasSize(0);
  }

  /** SONAR-4352 */
  @Test
  public void change_severity() {
    String componentKey = "sample";

    // there are no blocker issues
    assertThat(searchIssuesBySeverities(componentKey, "BLOCKER")).isEmpty();

    // increase the severity of an issue
    adminIssueClient().setSeverity(issue.key(), "BLOCKER");

    assertThat(searchIssuesBySeverities(componentKey, "BLOCKER")).hasSize(1);

    projectAnalysis.run();
    Issue reloaded = searchIssueByKey(issue.key());
    assertThat(reloaded.severity()).isEqualTo("BLOCKER");
    assertThat(reloaded.status()).isEqualTo("OPEN");
    assertThat(reloaded.resolution()).isNull();
    assertThat(reloaded.creationDate()).isEqualTo(issue.creationDate());
    assertThat(reloaded.creationDate().before(reloaded.updateDate())).isTrue();
  }

  /** SONAR-4287 */
  @Test
  public void assign() {
    assertThat(issue.assignee()).isNull();
    Issues issues = search(IssueQuery.create().issues(issue.key()));
    assertThat(issues.users()).isEmpty();

    adminIssueClient().assign(issue.key(), "admin");
    Assertions.assertThat(searchIssues(IssueQuery.create().assignees("admin"))).hasSize(1);

    projectAnalysis.run();
    Issue reloaded = searchIssueByKey(issue.key());
    assertThat(reloaded.assignee()).isEqualTo("admin");
    assertThat(reloaded.creationDate()).isEqualTo(issue.creationDate());

    issues = search(IssueQuery.create().issues(issue.key()));
    assertThat(issues.user("admin")).isNotNull();
    assertThat(issues.user("admin").name()).isEqualTo("Administrator");

    // unassign
    adminIssueClient().assign(issue.key(), null);
    reloaded = searchIssueByKey(issue.key());
    assertThat(reloaded.assignee()).isNull();
    Assertions.assertThat(searchIssues(IssueQuery.create().assignees("admin"))).isEmpty();
  }

  /** SONAR-4287 */
  @Test
  public void fail_assign_if_assignee_does_not_exist() {
    assertThat(issue.assignee()).isNull();
    try {
      adminIssueClient().assign(issue.key(), "unknown");
      fail();
    } catch (Exception e) {
      verifyHttpException(e, 400);
    }
  }

  private static List<Issue> searchIssuesBySeverities(String componentKey, String... severities) {
    return searchIssues(IssueQuery.create().componentRoots(componentKey).severities(severities));
  }
}
@Category(QaOnly.class)
public class IssueChangelogTest extends AbstractIssueTest {

  @Rule
  public final ProjectAnalysisRule projectAnalysisRule = ProjectAnalysisRule.from(ORCHESTRATOR);

  Issue issue;
  ProjectAnalysis xooSampleAnalysis;

  @Before
  public void resetData() {
    xooSampleAnalysis =
        projectAnalysisRule
            .newProjectAnalysis(projectAnalysisRule.registerProject("shared/xoo-sample"))
            .withQualityProfile(
                projectAnalysisRule.registerProfile(
                    "/issue/IssueChangelogTest/one-issue-per-line-profile.xml"));
    xooSampleAnalysis.run();
    issue = searchRandomIssue();
  }

  @Test
  public void update_changelog_when_assigning_issue_by_user() throws Exception {
    assertIssueHasNoChange(issue.key());

    adminIssueClient().assign(issue.key(), "admin");

    List<IssueChange> changes = retrieveChangeForIssue(issue.key());
    assertThat(changes).hasSize(1);
    IssueChange change = changes.get(0);
    assertThat(change.user()).isEqualTo("admin");
    assertThat(change.creationDate()).isNotNull();
    assertThat(change.diffs()).hasSize(1);
    IssueChangeDiff changeDiff = change.diffs().get(0);
    assertThat(changeDiff.key()).isEqualTo("assignee");
    assertThat(changeDiff.oldValue()).isNull();
    assertThat(changeDiff.newValue()).isEqualTo("Administrator");
  }

  @Test
  public void update_changelog_when_reopening_unresolved_issue_by_scan() throws Exception {
    assertIssueHasNoChange(issue.key());

    // re analyse the project after resolving an issue in order to reopen it
    adminIssueClient().doTransition(issue.key(), "resolve");
    xooSampleAnalysis.run();

    List<IssueChange> changes = retrieveChangeForIssue(issue.key());
    assertThat(changes).hasSize(2);

    // Change done by the user (first change is be the oldest one)
    IssueChange change1 = changes.get(0);
    assertThat(change1.user()).isEqualTo("admin");
    assertThat(change1.creationDate()).isNotNull();
    assertThat(change1.diffs()).hasSize(2);

    IssueChangeDiff change1Diff1 = change1.diffs().get(0);
    assertThat(change1Diff1.key()).isEqualTo("resolution");
    assertThat(change1Diff1.oldValue()).isNull();
    assertThat(change1Diff1.newValue()).isEqualTo("FIXED");

    IssueChangeDiff change1Diff2 = change1.diffs().get(1);
    assertThat(change1Diff2.key()).isEqualTo("status");
    assertThat(change1Diff2.oldValue()).isEqualTo("OPEN");
    assertThat(change1Diff2.newValue()).isEqualTo("RESOLVED");

    // Change done by scan
    IssueChange change2 = changes.get(1);
    assertThat(change2.user()).isNull();
    assertThat(change2.creationDate()).isNotNull();
    assertThat(change2.diffs()).hasSize(2);

    IssueChangeDiff changeDiff1 = change2.diffs().get(0);
    assertThat(changeDiff1.key()).isEqualTo("resolution");
    assertThat(changeDiff1.oldValue()).isNull();
    assertThat(changeDiff1.newValue()).isNull();

    IssueChangeDiff changeDiff2 = change2.diffs().get(1);
    assertThat(changeDiff2.key()).isEqualTo("status");
    assertThat(changeDiff2.oldValue()).isEqualTo("RESOLVED");
    assertThat(changeDiff2.newValue()).isEqualTo("REOPENED");
  }

  private void assertIssueHasNoChange(String issueKey) {
    assertThat(retrieveChangeForIssue(issueKey)).isEmpty();
  }

  private List<IssueChange> retrieveChangeForIssue(String issueKey) {
    return issueClient().changes(issueKey);
  }
}