package org.sonar.server.computation.task.projectanalysis.step;
import java.util.Arrays;
+import java.util.Date;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.issue.Issue;
import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.System2;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.server.computation.task.projectanalysis.issue.RuleRepositoryImpl;
import org.sonar.server.computation.task.projectanalysis.issue.UpdateConflictResolver;
import org.sonar.server.computation.task.step.ComputationStep;
+import org.sonar.server.util.cache.DiskCache;
+import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
+import static org.sonar.api.issue.Issue.STATUS_CLOSED;
+import static org.sonar.api.issue.Issue.STATUS_OPEN;
+import static org.sonar.api.rule.Severity.BLOCKER;
+import static org.sonar.db.component.ComponentTesting.newFileDto;
public class PersistIssuesStepTest extends BaseStepTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ public DbTester db = DbTester.create(System2.INSTANCE);
@Rule
public BatchReportReaderRule reportReader = new BatchReportReaderRule();
@Rule
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
.setOrganizationUuid("org-1");
- private DbSession session = dbTester.getSession();
- private DbClient dbClient = dbTester.getDbClient();
+ private DbSession session = db.getSession();
+ private DbClient dbClient = db.getDbClient();
private System2 system2;
private IssueCache issueCache;
private ComputationStep step;
@Test
public void insert_copied_issue() {
RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
- dbTester.rules().insert(rule);
- OrganizationDto organizationDto = dbTester.organizations().insert();
+ db.rules().insert(rule);
+ OrganizationDto organizationDto = db.organizations().insert();
ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
dbClient.componentDao().insert(session, project);
- ComponentDto file = ComponentTesting.newFileDto(project, null);
+ ComponentDto file = newFileDto(project, null);
dbClient.componentDao().insert(session, file);
session.commit();
.setRuleKey(rule.getKey())
.setComponentUuid(file.uuid())
.setProjectUuid(project.uuid())
- .setSeverity(Severity.BLOCKER)
- .setStatus(Issue.STATUS_OPEN)
+ .setSeverity(BLOCKER)
+ .setStatus(STATUS_OPEN)
.setNew(false)
.setCopied(true)
.setType(RuleType.BUG)
.setIssueKey("ISSUE")
.setUserLogin("john")
.setMarkdownText("Some text")
+ .setCreatedAt(new Date(NOW))
.setNew(true))
- .setCurrentChange(new FieldDiffs()
- .setIssueKey("ISSUE")
- .setUserLogin("john")
- .setDiff("technicalDebt", null, 1L)))
+ .setCurrentChange(
+ new FieldDiffs()
+ .setIssueKey("ISSUE")
+ .setUserLogin("john")
+ .setDiff("technicalDebt", null, 1L)
+ .setCreationDate(new Date(NOW))))
.close();
step.execute();
assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
- assertThat(result.getSeverity()).isEqualTo(Severity.BLOCKER);
- assertThat(result.getStatus()).isEqualTo(Issue.STATUS_OPEN);
+ assertThat(result.getSeverity()).isEqualTo(BLOCKER);
+ assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList("ISSUE"));
@Test
public void insert_merged_issue() {
RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
- dbTester.rules().insert(rule);
- OrganizationDto organizationDto = dbTester.organizations().insert();
+ db.rules().insert(rule);
+ OrganizationDto organizationDto = db.organizations().insert();
ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
dbClient.componentDao().insert(session, project);
- ComponentDto file = ComponentTesting.newFileDto(project, null);
+ ComponentDto file = newFileDto(project, null);
dbClient.componentDao().insert(session, file);
session.commit();
.setRuleKey(rule.getKey())
.setComponentUuid(file.uuid())
.setProjectUuid(project.uuid())
- .setSeverity(Severity.BLOCKER)
- .setStatus(Issue.STATUS_OPEN)
+ .setSeverity(BLOCKER)
+ .setStatus(STATUS_OPEN)
.setNew(true)
.setCopied(true)
.setType(RuleType.BUG)
.setIssueKey("ISSUE")
.setUserLogin("john")
.setMarkdownText("Some text")
+ .setCreatedAt(new Date(NOW))
.setNew(true))
.setCurrentChange(new FieldDiffs()
.setIssueKey("ISSUE")
.setUserLogin("john")
- .setDiff("technicalDebt", null, 1L)))
+ .setDiff("technicalDebt", null, 1L)
+ .setCreationDate(new Date(NOW))))
.close();
-
step.execute();
IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
- assertThat(result.getSeverity()).isEqualTo(Severity.BLOCKER);
- assertThat(result.getStatus()).isEqualTo(Issue.STATUS_OPEN);
+ assertThat(result.getSeverity()).isEqualTo(BLOCKER);
+ assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList("ISSUE"));
@Test
public void insert_new_issue() {
RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
- dbTester.rules().insert(rule);
- OrganizationDto organizationDto = dbTester.organizations().insert();
+ db.rules().insert(rule);
+ OrganizationDto organizationDto = db.organizations().insert();
ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
dbClient.componentDao().insert(session, project);
- ComponentDto file = ComponentTesting.newFileDto(project, null);
+ ComponentDto file = newFileDto(project, null);
dbClient.componentDao().insert(session, file);
session.commit();
.setRuleKey(rule.getKey())
.setComponentUuid(file.uuid())
.setProjectUuid(project.uuid())
- .setSeverity(Severity.BLOCKER)
- .setStatus(Issue.STATUS_OPEN)
+ .setSeverity(BLOCKER)
+ .setStatus(STATUS_OPEN)
.setNew(true)
.setType(RuleType.BUG)).close();
assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
- assertThat(result.getSeverity()).isEqualTo(Severity.BLOCKER);
- assertThat(result.getStatus()).isEqualTo(Issue.STATUS_OPEN);
+ assertThat(result.getSeverity()).isEqualTo(BLOCKER);
+ assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
}
@Test
public void close_issue() {
- dbTester.prepareDbUnit(getClass(), "shared.xml");
-
- issueCache.newAppender().append(new DefaultIssue()
- .setKey("ISSUE")
- .setType(RuleType.CODE_SMELL)
- .setRuleKey(RuleKey.of("xoo", "S01"))
- .setComponentUuid("COMPONENT")
- .setProjectUuid("PROJECT")
- .setSeverity(Severity.BLOCKER)
- .setStatus(Issue.STATUS_CLOSED)
- .setResolution(Issue.RESOLUTION_FIXED)
- .setSelectedAt(NOW)
- .setNew(false)
- .setChanged(true)).close();
-
+ ComponentDto project = db.components().insertPrivateProject();
+ ComponentDto file = db.components().insertComponent(newFileDto(project));
+ RuleDefinitionDto rule = db.rules().insert();
+ IssueDto issue = db.issues().insert(rule, project, file,
+ i -> i.setStatus(STATUS_OPEN)
+ .setResolution(null)
+ .setCreatedAt(NOW - 1_000_000_000L)
+ .setUpdatedAt(NOW - 1_000_000_000L));
+ DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
+
+ issueCacheAppender.append(
+ issue.toDefaultIssue()
+ .setStatus(STATUS_CLOSED)
+ .setResolution(RESOLUTION_FIXED)
+ .setSelectedAt(NOW)
+ .setNew(false)
+ .setChanged(true))
+ .close();
step.execute();
- dbTester.assertDbUnit(getClass(), "close_issue-result.xml", "issues");
+ IssueDto issueReloaded = db.getDbClient().issueDao().selectByKey(db.getSession(), issue.getKey()).get();
+ assertThat(issueReloaded.getStatus()).isEqualTo(STATUS_CLOSED);
+ assertThat(issueReloaded.getResolution()).isEqualTo(RESOLUTION_FIXED);
}
@Test
public void add_comment() {
- dbTester.prepareDbUnit(getClass(), "shared.xml");
-
- issueCache.newAppender().append(new DefaultIssue()
- .setKey("ISSUE")
- .setType(RuleType.CODE_SMELL)
- .setRuleKey(RuleKey.of("xoo", "S01"))
- .setComponentUuid("COMPONENT")
- .setProjectUuid("PROJECT")
- .setSeverity(Severity.BLOCKER)
- .setStatus(Issue.STATUS_CLOSED)
- .setResolution(Issue.RESOLUTION_FIXED)
- .setNew(false)
- .setChanged(true)
- .addComment(new DefaultIssueComment()
- .setKey("COMMENT")
- .setIssueKey("ISSUE")
- .setUserLogin("john")
- .setMarkdownText("Some text")
- .setNew(true)))
+ ComponentDto project = db.components().insertPrivateProject();
+ ComponentDto file = db.components().insertComponent(newFileDto(project));
+ RuleDefinitionDto rule = db.rules().insert();
+ IssueDto issue = db.issues().insert(rule, project, file,
+ i -> i.setStatus(STATUS_OPEN)
+ .setResolution(null)
+ .setCreatedAt(NOW - 1_000_000_000L)
+ .setUpdatedAt(NOW - 1_000_000_000L));
+ DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
+
+ issueCacheAppender.append(
+ issue.toDefaultIssue()
+ .setStatus(STATUS_CLOSED)
+ .setResolution(RESOLUTION_FIXED)
+ .setSelectedAt(NOW)
+ .setNew(false)
+ .setChanged(true)
+ .addComment(new DefaultIssueComment()
+ .setKey("COMMENT")
+ .setIssueKey(issue.getKey())
+ .setUserLogin("john")
+ .setMarkdownText("Some text")
+ .setCreatedAt(new Date(NOW))
+ .setNew(true)))
.close();
-
step.execute();
- dbTester.assertDbUnit(getClass(), "add_comment-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");
+ IssueChangeDto issueChangeDto = db.getDbClient().issueChangeDao().selectByIssueKeys(db.getSession(), singletonList(issue.getKey())).get(0);
+ assertThat(issueChangeDto)
+ .extracting(IssueChangeDto::getChangeType, IssueChangeDto::getUserLogin, IssueChangeDto::getChangeData, IssueChangeDto::getIssueKey,
+ IssueChangeDto::getIssueChangeCreationDate)
+ .containsOnly(IssueChangeDto.TYPE_COMMENT, "john", "Some text", issue.getKey(), NOW);
}
@Test
public void add_change() {
- dbTester.prepareDbUnit(getClass(), "shared.xml");
-
- issueCache.newAppender().append(new DefaultIssue()
- .setKey("ISSUE")
- .setType(RuleType.CODE_SMELL)
- .setRuleKey(RuleKey.of("xoo", "S01"))
- .setComponentUuid("COMPONENT")
- .setProjectUuid("PROJECT")
- .setSeverity(Severity.BLOCKER)
- .setStatus(Issue.STATUS_CLOSED)
- .setResolution(Issue.RESOLUTION_FIXED)
- .setNew(false)
- .setChanged(true)
- .setCurrentChange(new FieldDiffs()
- .setIssueKey("ISSUE")
- .setUserLogin("john")
- .setDiff("technicalDebt", null, 1L)))
+ ComponentDto project = db.components().insertPrivateProject();
+ ComponentDto file = db.components().insertComponent(newFileDto(project));
+ RuleDefinitionDto rule = db.rules().insert();
+ IssueDto issue = db.issues().insert(rule, project, file,
+ i -> i.setStatus(STATUS_OPEN)
+ .setResolution(null)
+ .setCreatedAt(NOW - 1_000_000_000L)
+ .setUpdatedAt(NOW - 1_000_000_000L));
+ DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
+
+ issueCacheAppender.append(
+ issue.toDefaultIssue()
+ .setStatus(STATUS_CLOSED)
+ .setResolution(RESOLUTION_FIXED)
+ .setSelectedAt(NOW)
+ .setNew(false)
+ .setChanged(true)
+ .setCurrentChange(new FieldDiffs()
+ .setIssueKey("ISSUE")
+ .setUserLogin("john")
+ .setDiff("technicalDebt", null, 1L)
+ .setCreationDate(new Date(NOW))))
.close();
-
step.execute();
- dbTester.assertDbUnit(getClass(), "add_change-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");
+ IssueChangeDto issueChangeDto = db.getDbClient().issueChangeDao().selectByIssueKeys(db.getSession(), singletonList(issue.getKey())).get(0);
+ assertThat(issueChangeDto)
+ .extracting(IssueChangeDto::getChangeType, IssueChangeDto::getUserLogin, IssueChangeDto::getChangeData, IssueChangeDto::getIssueKey,
+ IssueChangeDto::getIssueChangeCreationDate)
+ .containsOnly(IssueChangeDto.TYPE_FIELD_CHANGE, "john", "technicalDebt=1", issue.getKey(), NOW);
}
}
*/
package org.sonar.server.issue.ws;
+import java.util.Date;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Rule;
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER"));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
ComponentDto file2 = db.components().insertComponent(newFileDto(project));
IssueDto issueDto = db.issues().insertIssue(newDto(rule, file2, project));
userSession.logIn("john").addProjectPermission(USER, project, file1, file2);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", file1.uuid(), file2.uuid()));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", file1.uuid(), file2.uuid()).setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
public void changelog_of_file_move_is_empty_when_files_does_not_exists() throws Exception {
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", "UNKNOWN_1", "UNKNOWN_2"));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", "UNKNOWN_1", "UNKNOWN_2").setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
UserDto user = db.users().insertUser(UserTesting.newUserDto("john", "John", null));
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER"));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
public void return_changelog_not_having_user() throws Exception {
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(null).setDiff("severity", "MAJOR", "BLOCKER"));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(null).setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
public void return_changelog_on_none_existing_user() throws Exception {
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin("UNKNOWN").setDiff("severity", "MAJOR", "BLOCKER"));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin("UNKNOWN").setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER").setDiff("status", "RESOLVED", "CLOSED"));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin())
+ .setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date())
+ .setDiff("status", "RESOLVED", "CLOSED").setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", null, "BLOCKER"));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", null, "BLOCKER").setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", null));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", null).setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto,
- new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER"),
- new FieldDiffs().setDiff("status", "RESOLVED", "CLOSED"));
+ new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()),
+ new FieldDiffs().setDiff("status", "RESOLVED", "CLOSED").setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("technicalDebt", "10", "20"));
+ db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("technicalDebt", "10", "20").setCreationDate(new Date()));
ChangelogWsResponse result = call(issueDto.getKey());
userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs()
.setUserLogin(user.getLogin())
- .setDiff("severity", "MAJOR", "BLOCKER")
+ .setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date())
.setCreationDate(DateUtils.parseDateTime("2014-03-04T23:03:44+0100")));
String result = tester.newRequest().setParam("issue", issueDto.getKey()).execute().getInput();