From da55098737aa497403a889147e5fa158e779aa1d Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Sat, 30 Aug 2014 12:55:09 +0200 Subject: [PATCH] SONAR-5529 - Implemented findAfterDate in IssueDao V.2 --- .../org/sonar/server/issue/db/IssueDao.java | 23 +++ .../sonar/server/issue/db/IssueDaoTest.java | 44 +++++ .../server/issue/db/IssueDaoTest/shared.xml | 22 +++ .../db/IssueDaoTest/should_select_all.xml | 80 +++++++++ .../org/sonar/core/issue/db/IssueMapper.java | 10 +- .../java/org/sonar/core/rule/RuleMapper.java | 36 ---- .../org/sonar/core/issue/db/IssueMapper.xml | 158 +++++++++++------- .../org/sonar/core/rule/RuleMapper.xml | 83 ++++++--- 8 files changed, 326 insertions(+), 130 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueDaoTest.java create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueDaoTest/shared.xml create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueDaoTest/should_select_all.xml diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueDao.java b/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueDao.java index 2365a93b602..3cd0b1af7fc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueDao.java @@ -20,6 +20,7 @@ package org.sonar.server.issue.db; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; import org.sonar.api.utils.System2; import org.sonar.core.issue.db.IssueDto; import org.sonar.core.issue.db.IssueMapper; @@ -27,6 +28,9 @@ import org.sonar.core.persistence.DaoComponent; import org.sonar.core.persistence.DbSession; import org.sonar.server.db.BaseDao; +import java.sql.Timestamp; +import java.util.Date; + public class IssueDao extends BaseDao implements DaoComponent { public IssueDao() { @@ -42,4 +46,23 @@ public class IssueDao extends BaseDao implements protected IssueDto doGetNullableByKey(DbSession session, String key) { return mapper(session).selectByKey(key); } + + @Override + protected IssueDto doUpdate(DbSession session, IssueDto issue) { + mapper(session).update(issue); + return issue; + } + + @Override + protected IssueDto doInsert(DbSession session, IssueDto issue) { + Preconditions.checkNotNull(issue.getKey(), "Cannot insert Issue with empty key!"); + Preconditions.checkNotNull(issue.getComponentId(), "Cannot insert Issue with no Component!"); + mapper(session).insert(issue); + return issue; + } + + @Override + protected Iterable findAfterDate(DbSession session, Date date) { + return mapper(session).selectAfterDate(new Timestamp(date.getTime())); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueDaoTest.java new file mode 100644 index 00000000000..35f1fafd1aa --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueDaoTest.java @@ -0,0 +1,44 @@ +package org.sonar.server.issue.db; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.core.persistence.AbstractDaoTestCase; +import org.sonar.core.persistence.DbSession; + +import java.util.Date; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class IssueDaoTest extends AbstractDaoTestCase { + + private IssueDao dao; + private DbSession session; + private System2 system2; + + @Before + public void before() throws Exception { + this.session = getMyBatis().openSession(false); + this.system2 = mock(System2.class); + this.dao = new IssueDao(system2); + } + + @After + public void after() { + this.session.close(); + } + + @Test + public void find_after_dates() throws Exception { + setupData("shared", "should_select_all"); + + Date t0 = new Date(0); + assertThat(dao.findAfterDate(session, t0)).hasSize(3); + + Date t2014 = DateUtils.parseDate("2014-01-01"); + assertThat(dao.findAfterDate(session, t2014)).hasSize(1); + } +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueDaoTest/shared.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueDaoTest/shared.xml new file mode 100644 index 00000000000..41a2d34e288 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueDaoTest/shared.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueDaoTest/should_select_all.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueDaoTest/should_select_all.xml new file mode 100644 index 00000000000..96ada68ee82 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueDaoTest/should_select_all.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java index 64565efbffd..d89868867c7 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java @@ -25,6 +25,7 @@ import org.sonar.core.rule.RuleDto; import javax.annotation.Nullable; +import java.sql.Timestamp; import java.util.Collection; import java.util.Date; import java.util.List; @@ -38,16 +39,15 @@ public interface IssueMapper { * If the role is null, then the authorisation check is disabled. */ List selectIssueIds(@Param("query") IssueQuery query, @Param("componentRootKeys") Collection componentRootKeys, - @Nullable @Param("userId") Integer userId, @Nullable @Param("role") String role, - @Param("maxResults") Integer maxResult); + @Nullable @Param("userId") Integer userId, @Nullable @Param("role") String role, + @Param("maxResults") Integer maxResult); /** * Return a none paginated list of authorized issues for a user. * If the role is null, then the authorisation check is disabled. */ List selectIssues(@Param("query") IssueQuery query, @Param("componentRootKeys") Collection componentRootKeys, - @Nullable @Param("userId") Integer userId, @Nullable @Param("role") String role); - + @Nullable @Param("userId") Integer userId, @Nullable @Param("role") String role); List findRulesByComponent(@Param("componentKey") String componentKey, @Nullable @Param("createdAt") Date createdAtOrAfter); @@ -58,4 +58,6 @@ public interface IssueMapper { int update(IssueDto issue); int updateIfBeforeSelectedDate(IssueDto issue); + + List selectAfterDate(Timestamp timestamp); } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java index 3b9a1a9ab78..bba4f8e3136 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java @@ -19,10 +19,7 @@ */ package org.sonar.core.rule; -import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Result; -import org.apache.ibatis.annotations.Select; import org.sonar.api.rule.RuleKey; import java.sql.Timestamp; @@ -64,38 +61,5 @@ public interface RuleMapper { void deleteParameter(Integer paramId); - final String SELECT_FIELDS="r.id,\n" + - " r.plugin_rule_key as \"ruleKey\",\n" + - " r.plugin_name as \"repositoryKey\",\n" + - " r.description,\n" + - " r.description_format as \"descriptionFormat\",\n" + - " r.status,\n" + - " r.name,\n" + - " r.plugin_config_key as \"configKey\",\n" + - " r.priority as \"severity\",\n" + - " r.is_template as \"isTemplate\",\n" + - " r.language as \"language\",\n" + - " r.template_id as \"templateId\",\n" + - " r.note_data as \"noteData\",\n" + - " r.note_user_login as \"noteUserLogin\",\n" + - " r.note_created_at as \"noteCreatedAt\",\n" + - " r.note_updated_at as \"noteUpdatedAt\",\n" + - " r.characteristic_id as \"subCharacteristicId\",\n" + - " r.default_characteristic_id as \"defaultSubCharacteristicId\",\n" + - " r.remediation_function as \"remediationFunction\",\n" + - " r.default_remediation_function as \"defaultRemediationFunction\",\n" + - " r.remediation_coeff as \"remediationCoefficient\",\n" + - " r.default_remediation_coeff as \"defaultRemediationCoefficient\",\n" + - " r.remediation_offset as \"remediationOffset\",\n" + - " r.default_remediation_offset as \"defaultRemediationOffset\",\n" + - " r.effort_to_fix_description as \"effortToFixDescription\",\n" + - " r.tags as \"tagsField\",\n" + - " r.system_tags as \"systemTagsField\",\n" + - " r.created_at as \"createdAt\",\n" + - " r.updated_at as \"updatedAt\""; - - @Select("SELECT " + SELECT_FIELDS + " FROM rules r WHERE r.updated_at IS NULL or r.updated_at >= #{date} ") - @Options(fetchSize = 200, useCache = false, flushCache = true) - @Result(javaType = RuleDto.class) List selectAfterDate(@Param("date") Timestamp timestamp); } diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml index 2e26a8c6799..7ec31980008 100644 --- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml @@ -60,7 +60,7 @@ - + INSERT INTO issues (kee, component_id, root_component_id, rule_id, action_plan_key, severity, manual_severity, message, line, effort_to_fix, technical_debt, status, resolution, checksum, reporter, assignee, author_login, issue_attributes, issue_creation_date, issue_update_date, @@ -74,7 +74,7 @@ - + update issues set action_plan_key=#{actionPlanKey}, severity=#{severity}, @@ -101,7 +101,7 @@ - + update issues set action_plan_key=#{actionPlanKey}, severity=#{severity}, @@ -135,38 +135,49 @@ where i.kee=#{kee} - select - i.id, - i.kee as kee, - i.component_id as componentId, - i.root_component_id as rootComponentId, - i.rule_id as ruleId, - i.action_plan_key as actionPlanKey, - i.severity as severity, - i.manual_severity as manualSeverity, - i.message as message, - i.line as line, - i.effort_to_fix as effortToFix, - i.technical_debt as debt, - i.status as status, - i.resolution as resolution, - i.checksum as checksum, - i.reporter as reporter, - i.assignee as assignee, - i.author_login as authorLogin, - i.issue_attributes as issueAttributes, - i.issue_creation_date as issueCreationDate, - i.issue_update_date as issueUpdateDate, - i.issue_close_date as issueCloseDate, - i.created_at as createdAt, - i.updated_at as updatedAt, - r.plugin_rule_key as ruleKey, - r.plugin_name as ruleRepo, - p.kee as componentKey, - root.kee as rootComponentKey + from issues i - inner join (select p.id,p.kee from projects p where (p.root_id=#{id} and p.qualifier <> 'BRC') or (p.id=#{id})) p on p.id=i.component_id + inner join rules r on r.id=i.rule_id + inner join projects p on p.id=i.component_id + inner join projects root on root.id=i.root_component_id + where i.updated_at IS NULL or i.updated_at >= #{date} + + + - select top (#{maxResults}) i.id + select top (#{maxResults}) i.id + from issues i @@ -211,23 +225,28 @@ - inner join () authorizedProjects on authorizedProjects.root_project_id=i.root_component_id + inner join () + authorizedProjects on authorizedProjects.root_project_id=i.root_component_id - inner join () authorizedComponents on authorizedComponents.project_id=i.component_id + inner join () + authorizedComponents on authorizedComponents.project_id=i.component_id - inner join projects project_component on project_component.id=i.component_id and project_component.enabled=${_true} and - project_component.kee=#{component} + inner join projects project_component on project_component.id=i.component_id and + project_component.enabled=${_true} and + + project_component.kee=#{component} @@ -243,19 +262,27 @@ - and i.kee=#{key} + and + + i.kee=#{key} - and i.severity=#{severity} + and + + i.severity=#{severity} - and i.status=#{status} + and + + i.status=#{status} - and i.resolution=#{resolution} + and + + i.resolution=#{resolution} @@ -267,11 +294,15 @@ - and i.reporter=#{reporter} + and + + i.reporter=#{reporter} - and i.assignee=#{assignee} + and + + i.assignee=#{assignee} @@ -309,16 +340,17 @@ diff --git a/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml b/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml index bb2e84f6ca1..1455f9ca921 100644 --- a/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml @@ -36,33 +36,54 @@ + + @@ -99,7 +120,8 @@ - (plugin_rule_key, plugin_name, description, description_format, status, name, plugin_config_key, priority, is_template, language, template_id, + (plugin_rule_key, plugin_name, description, description_format, status, name, plugin_config_key, priority, + is_template, language, template_id, characteristic_id, default_characteristic_id, remediation_function, default_remediation_function, remediation_coeff, default_remediation_coeff, remediation_offset, default_remediation_offset, effort_to_fix_description, tags, system_tags, note_data, note_user_login, note_created_at, note_updated_at, @@ -107,17 +129,19 @@ - insert into rules + insert into rules + values (#{ruleKey}, #{repositoryKey}, #{description}, #{descriptionFormat}, #{status}, #{name}, #{configKey}, - #{severity}, #{isTemplate}, #{language}, #{templateId}, - #{subCharacteristicId}, #{defaultSubCharacteristicId}, #{remediationFunction}, #{defaultRemediationFunction}, - #{remediationCoefficient}, #{defaultRemediationCoefficient}, #{remediationOffset}, #{defaultRemediationOffset}, - #{effortToFixDescription}, #{tagsField}, #{systemTagsField}, #{noteData}, #{noteUserLogin}, #{noteCreatedAt}, - #{noteUpdatedAt}, #{createdAt}, #{updatedAt}) + #{severity}, #{isTemplate}, #{language}, #{templateId}, + #{subCharacteristicId}, #{defaultSubCharacteristicId}, #{remediationFunction}, #{defaultRemediationFunction}, + #{remediationCoefficient}, #{defaultRemediationCoefficient}, #{remediationOffset}, #{defaultRemediationOffset}, + #{effortToFixDescription}, #{tagsField}, #{systemTagsField}, #{noteData}, #{noteUserLogin}, #{noteCreatedAt}, + #{noteUpdatedAt}, #{createdAt}, #{updatedAt}) - insert into rules + insert into rules + values (#{ruleKey}, #{repositoryKey}, #{description}, #{descriptionFormat}, #{status}, #{name}, #{configKey}, #{severity}, #{isTemplate}, #{language}, #{templateId}, #{subCharacteristicId}, #{defaultSubCharacteristicId}, #{remediationFunction}, #{defaultRemediationFunction}, @@ -126,7 +150,7 @@ #{noteUpdatedAt}, #{createdAt}, #{updatedAt}) - + delete from active_rule_parameters where rules_parameter_id=#{id} @@ -136,45 +160,50 @@ - + delete from rules_parameters where id=#{id} - + INSERT INTO rules_parameters (rule_id, name, param_type, default_value, description) VALUES (#{ruleId}, #{name}, #{type}, #{defaultValue}, #{description}) - + UPDATE rules_parameters SET - param_type=#{type}, - default_value=#{defaultValue}, - description=#{description} + param_type=#{type}, + default_value=#{defaultValue}, + description=#{description} WHERE id=#{id} -- 2.39.5