*/
package org.sonar.db.issue;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
+import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.rule.RuleDto;
+import org.sonar.db.rule.RuleTesting;
import static org.assertj.core.api.Assertions.assertThat;
-
public class IssueMapperTest {
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
- IssueMapper mapper = dbTester.getSession().getMapper(IssueMapper.class);
+ DbSession dbSession = dbTester.getSession();
+
+ IssueMapper underTest = dbSession.getMapper(IssueMapper.class);
+
+ ComponentDto project, file;
+ RuleDto rule;
+
+ @Before
+ public void setUp() throws Exception {
+ project = ComponentTesting.newProjectDto();
+ dbTester.getDbClient().componentDao().insert(dbSession, project);
+ file = ComponentTesting.newFileDto(project);
+ dbTester.getDbClient().componentDao().insert(dbSession, file);
+ rule = RuleTesting.newXooX1();
+ dbTester.getDbClient().ruleDao().insert(dbSession, rule);
+ dbSession.commit();
+ }
@Test
public void insert() {
- IssueDto dto = new IssueDto();
- dto.setComponentUuid("uuid-123");
- dto.setProjectUuid("uuid-100");
- dto.setRuleId(200);
- dto.setType(2);
- dto.setKee("ABCDE");
- dto.setLine(500);
- dto.setGap(3.14);
- dto.setEffort(10L);
- dto.setResolution("FIXED");
- dto.setStatus("RESOLVED");
- dto.setSeverity("BLOCKER");
- dto.setReporter("emmerik");
- dto.setAuthorLogin("morgan");
- dto.setAssignee("karadoc");
- dto.setActionPlanKey("current_sprint");
- dto.setIssueAttributes("JIRA=FOO-1234");
- dto.setChecksum("123456789");
- dto.setMessage("the message");
-
- dto.setIssueCreationTime(1_401_000_000_000L);
- dto.setIssueUpdateTime(1_402_000_000_000L);
- dto.setIssueCloseTime(1_403_000_000_000L);
- dto.setCreatedAt(1_400_000_000_000L);
- dto.setUpdatedAt(1_500_000_000_000L);
-
- mapper.insert(dto);
+ underTest.insert(newIssue());
dbTester.getSession().commit();
- dbTester.assertDbUnit(getClass(), "testInsert-result.xml", new String[]{"id"}, "issues");
+ IssueDto result = underTest.selectByKey("ABCDE");
+ assertThat(result).isNotNull();
+ assertThat(result.getId()).isNotNull();
+ assertThat(result.getKey()).isEqualTo("ABCDE");
+ assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
+ assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
+ assertThat(result.getRuleId()).isEqualTo(rule.getId());
+ assertThat(result.getType()).isEqualTo(2);
+ assertThat(result.getLine()).isEqualTo(500);
+ assertThat(result.getGap()).isEqualTo(3.14d);
+ assertThat(result.getEffort()).isEqualTo(10L);
+ assertThat(result.getResolution()).isEqualTo("FIXED");
+ assertThat(result.getStatus()).isEqualTo("RESOLVED");
+ assertThat(result.getSeverity()).isEqualTo("BLOCKER");
+ assertThat(result.getReporter()).isEqualTo("emmerik");
+ assertThat(result.getAuthorLogin()).isEqualTo("morgan");
+ assertThat(result.getAssignee()).isEqualTo("karadoc");
+ assertThat(result.getActionPlanKey()).isEqualTo("current_sprint");
+ assertThat(result.getIssueAttributes()).isEqualTo("JIRA=FOO-1234");
+ assertThat(result.getChecksum()).isEqualTo("123456789");
+ assertThat(result.getMessage()).isEqualTo("the message");
+ assertThat(result.getIssueCreationTime()).isEqualTo(1_401_000_000_000L);
+ assertThat(result.getIssueUpdateTime()).isEqualTo(1_402_000_000_000L);
+ assertThat(result.getIssueCloseTime()).isEqualTo(1_403_000_000_000L);
+ assertThat(result.getCreatedAt()).isEqualTo(1_400_000_000_000L);
+ assertThat(result.getUpdatedAt()).isEqualTo(1_500_000_000_000L);
}
@Test
public void update() {
- dbTester.prepareDbUnit(getClass(), "testUpdate.xml");
-
- IssueDto dto = new IssueDto();
- dto.setComponentUuid("uuid-123");
- dto.setProjectUuid("uuid-101");
- dto.setRuleId(200);
- dto.setType(3);
- dto.setKee("ABCDE");
- dto.setLine(500);
- dto.setGap(3.14);
- dto.setEffort(10L);
- dto.setResolution("FIXED");
- dto.setStatus("RESOLVED");
- dto.setSeverity("BLOCKER");
- dto.setReporter("emmerik");
- dto.setAuthorLogin("morgan");
- dto.setAssignee("karadoc");
- dto.setActionPlanKey("current_sprint");
- dto.setIssueAttributes("JIRA=FOO-1234");
- dto.setChecksum("123456789");
- dto.setMessage("the message");
-
- dto.setIssueCreationTime(1_401_000_000_000L);
- dto.setIssueUpdateTime(1_402_000_000_000L);
- dto.setIssueCloseTime(1_403_000_000_000L);
- dto.setCreatedAt(1_400_000_000_000L);
- dto.setUpdatedAt(1_500_000_000_000L);
-
- mapper.update(dto);
+ underTest.insert(newIssue());
+
dbTester.getSession().commit();
- dbTester.assertDbUnit(getClass(), "testUpdate-result.xml", new String[]{"id"}, "issues");
+ IssueDto update = new IssueDto();
+ update.setKee("ABCDE");
+ update.setComponentUuid(file.uuid());
+ update.setProjectUuid(project.uuid());
+ update.setRuleId(rule.getId());
+ update.setType(3);
+ update.setLine(500);
+ update.setGap(3.14);
+ update.setEffort(10L);
+ update.setResolution("FIXED");
+ update.setStatus("RESOLVED");
+ update.setSeverity("BLOCKER");
+ update.setReporter("emmerik");
+ update.setAuthorLogin("morgan");
+ update.setAssignee("karadoc");
+ update.setActionPlanKey("current_sprint");
+ update.setIssueAttributes("JIRA=FOO-1234");
+ update.setChecksum("123456789");
+ update.setMessage("the message");
+
+ update.setIssueCreationTime(1_550_000_000_000L);
+ update.setIssueUpdateTime(1_550_000_000_000L);
+ update.setIssueCloseTime(1_550_000_000_000L);
+ // Should not change
+ update.setCreatedAt(1_400_123_456_789L);
+ update.setUpdatedAt(1_550_000_000_000L);
+
+ underTest.update(update);
+ dbTester.getSession().commit();
+
+ IssueDto result = underTest.selectByKey("ABCDE");
+ assertThat(result).isNotNull();
+ assertThat(result.getId()).isNotNull();
+ assertThat(result.getKey()).isEqualTo("ABCDE");
+ assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
+ assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
+ assertThat(result.getRuleId()).isEqualTo(rule.getId());
+ assertThat(result.getType()).isEqualTo(3);
+ assertThat(result.getLine()).isEqualTo(500);
+ assertThat(result.getGap()).isEqualTo(3.14d);
+ assertThat(result.getEffort()).isEqualTo(10L);
+ assertThat(result.getResolution()).isEqualTo("FIXED");
+ assertThat(result.getStatus()).isEqualTo("RESOLVED");
+ assertThat(result.getSeverity()).isEqualTo("BLOCKER");
+ assertThat(result.getReporter()).isEqualTo("emmerik");
+ assertThat(result.getAuthorLogin()).isEqualTo("morgan");
+ assertThat(result.getAssignee()).isEqualTo("karadoc");
+ assertThat(result.getActionPlanKey()).isEqualTo("current_sprint");
+ assertThat(result.getIssueAttributes()).isEqualTo("JIRA=FOO-1234");
+ assertThat(result.getChecksum()).isEqualTo("123456789");
+ assertThat(result.getMessage()).isEqualTo("the message");
+ assertThat(result.getIssueCreationTime()).isEqualTo(1_550_000_000_000L);
+ assertThat(result.getIssueUpdateTime()).isEqualTo(1_550_000_000_000L);
+ assertThat(result.getIssueCloseTime()).isEqualTo(1_550_000_000_000L);
+ assertThat(result.getCreatedAt()).isEqualTo(1_400_000_000_000L);
+ assertThat(result.getUpdatedAt()).isEqualTo(1_550_000_000_000L);
}
@Test
public void updateBeforeSelectedDate_without_conflict() {
- dbTester.prepareDbUnit(getClass(), "testUpdate.xml");
-
- IssueDto dto = new IssueDto();
- dto.setComponentUuid("uuid-123");
- dto.setProjectUuid("uuid-101");
- dto.setRuleId(200);
- dto.setType(3);
- dto.setKee("ABCDE");
- dto.setLine(500);
- dto.setGap(3.14);
- dto.setEffort(10L);
- dto.setResolution("FIXED");
- dto.setStatus("RESOLVED");
- dto.setSeverity("BLOCKER");
- dto.setReporter("emmerik");
- dto.setAuthorLogin("morgan");
- dto.setAssignee("karadoc");
- dto.setActionPlanKey("current_sprint");
- dto.setIssueAttributes("JIRA=FOO-1234");
- dto.setChecksum("123456789");
- dto.setMessage("the message");
- dto.setIssueCreationTime(1_401_000_000_000L);
- dto.setIssueUpdateTime(1_402_000_000_000L);
- dto.setIssueCloseTime(1_403_000_000_000L);
- dto.setCreatedAt(1_400_000_000_000L);
- dto.setUpdatedAt(1_500_000_000_000L);
+ underTest.insert(newIssue());
+
+ IssueDto dto = newIssue()
+ .setType(3)
+ .setLine(600)
+ .setGap(1.12d)
+ .setEffort(50L)
+ .setIssueUpdateTime(1_600_000_000_000L)
+ .setUpdatedAt(1_600_000_000_000L);
// selected after last update -> ok
dto.setSelectedAt(1500000000000L);
- int count = mapper.updateIfBeforeSelectedDate(dto);
+ int count = underTest.updateIfBeforeSelectedDate(dto);
assertThat(count).isEqualTo(1);
dbTester.getSession().commit();
- dbTester.assertDbUnit(getClass(), "testUpdate-result.xml", new String[]{"id"}, "issues");
+ IssueDto result = underTest.selectByKey("ABCDE");
+ assertThat(result).isNotNull();
+ assertThat(result.getType()).isEqualTo(3);
+ assertThat(result.getLine()).isEqualTo(600);
+ assertThat(result.getGap()).isEqualTo(1.12d);
+ assertThat(result.getEffort()).isEqualTo(50L);
+ assertThat(result.getIssueUpdateTime()).isEqualTo(1_600_000_000_000L);
+ assertThat(result.getUpdatedAt()).isEqualTo(1_600_000_000_000L);
}
@Test
public void updateBeforeSelectedDate_with_conflict() {
- dbTester.prepareDbUnit(getClass(), "updateBeforeSelectedDate_with_conflict.xml");
-
- IssueDto dto = new IssueDto();
- dto.setComponentUuid("uuid-123");
- dto.setProjectUuid("uuid-101");
- dto.setRuleId(200);
- dto.setKee("ABCDE");
- dto.setLine(500);
- dto.setGap(3.14);
- dto.setEffort(10L);
- dto.setResolution("FIXED");
- dto.setStatus("RESOLVED");
- dto.setSeverity("BLOCKER");
- dto.setReporter("emmerik");
- dto.setAuthorLogin("morgan");
- dto.setAssignee("karadoc");
- dto.setActionPlanKey("current_sprint");
- dto.setIssueAttributes("JIRA=FOO-1234");
- dto.setChecksum("123456789");
- dto.setMessage("the message");
- dto.setIssueCreationDate(DateUtils.parseDate("2013-05-18"));
- dto.setIssueUpdateDate(DateUtils.parseDate("2013-05-19"));
- dto.setIssueCloseDate(DateUtils.parseDate("2013-05-20"));
- dto.setCreatedAt(1400000000000L);
- dto.setUpdatedAt(1460000000000L);
+ underTest.insert(newIssue());
+
+ IssueDto dto = newIssue()
+ .setType(3)
+ .setLine(600)
+ .setGap(1.12d)
+ .setEffort(50L)
+ .setIssueUpdateTime(1_600_000_000_000L)
+ .setUpdatedAt(1_600_000_000_000L);
// selected before last update -> ko
dto.setSelectedAt(1400000000000L);
- int count = mapper.updateIfBeforeSelectedDate(dto);
+ int count = underTest.updateIfBeforeSelectedDate(dto);
assertThat(count).isEqualTo(0);
dbTester.getSession().commit();
- dbTester.assertDbUnit(getClass(), "updateBeforeSelectedDate_with_conflict-result.xml", new String[]{"id"}, "issues");
+ // No change
+ IssueDto result = underTest.selectByKey("ABCDE");
+ assertThat(result).isNotNull();
+ assertThat(result.getType()).isEqualTo(2);
+ assertThat(result.getLine()).isEqualTo(500);
+ assertThat(result.getGap()).isEqualTo(3.14d);
+ assertThat(result.getEffort()).isEqualTo(10L);
+ assertThat(result.getIssueUpdateTime()).isEqualTo(1_402_000_000_000L);
+ assertThat(result.getUpdatedAt()).isEqualTo(1_500_000_000_000L);
+ }
+
+ private IssueDto newIssue() {
+ return new IssueDto()
+ .setKee("ABCDE")
+ .setComponentUuid(file.uuid())
+ .setProjectUuid(project.uuid())
+ .setRuleId(rule.getId())
+ .setType(2)
+ .setLine(500)
+ .setGap(3.14)
+ .setEffort(10L)
+ .setResolution("FIXED")
+ .setStatus("RESOLVED")
+ .setSeverity("BLOCKER")
+ .setReporter("emmerik")
+ .setAuthorLogin("morgan")
+ .setAssignee("karadoc")
+ .setActionPlanKey("current_sprint")
+ .setIssueAttributes("JIRA=FOO-1234")
+ .setChecksum("123456789")
+ .setMessage("the message")
+ .setIssueCreationTime(1_401_000_000_000L)
+ .setIssueUpdateTime(1_402_000_000_000L)
+ .setIssueCloseTime(1_403_000_000_000L)
+ .setCreatedAt(1_400_000_000_000L)
+ .setUpdatedAt(1_500_000_000_000L);
}
}