From 28b3b2300ff8e68cda99f114c0011f7896dccfba Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 29 Feb 2016 11:11:47 +0100 Subject: [PATCH] SONAR-7330 Remove SqlActiveRuleKey --- .../db/qualityprofile/ActiveRuleDao.java | 11 +-- .../db/qualityprofile/ActiveRuleMapper.java | 2 +- .../db/qualityprofile/SqlActiveRuleKey.java | 82 ------------------- .../db/qualityprofile/ActiveRuleMapper.xml | 7 +- .../db/qualityprofile/ActiveRuleDaoTest.java | 62 +++++++++++++- 5 files changed, 69 insertions(+), 95 deletions(-) delete mode 100644 sonar-db/src/main/java/org/sonar/db/qualityprofile/SqlActiveRuleKey.java diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java index 60b90c7e2cc..d81288a728c 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java +++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java @@ -22,7 +22,6 @@ package org.sonar.db.qualityprofile; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Preconditions; -import java.util.ArrayList; import java.util.List; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; @@ -57,11 +56,7 @@ public class ActiveRuleDao implements Dao { } public List selectByKeys(DbSession dbSession, List keys) { - List sqlKeys = new ArrayList<>(); - for (ActiveRuleKey key : keys) { - sqlKeys.add(new SqlActiveRuleKey(key)); - } - return DatabaseUtils.executeLargeInputs(sqlKeys, new KeyToDto(mapper(dbSession))); + return DatabaseUtils.executeLargeInputs(keys, new KeyToDto(mapper(dbSession))); } public List selectByRule(DbSession dbSession, RuleDto rule) { @@ -186,7 +181,7 @@ public class ActiveRuleDao implements Dao { return session.getMapper(ActiveRuleMapper.class); } - private static class KeyToDto implements Function, List> { + private static class KeyToDto implements Function, List> { private final ActiveRuleMapper mapper; private KeyToDto(ActiveRuleMapper mapper) { @@ -194,7 +189,7 @@ public class ActiveRuleDao implements Dao { } @Override - public List apply(@Nonnull List partitionOfIds) { + public List apply(@Nonnull List partitionOfIds) { return mapper.selectByKeys(partitionOfIds); } } diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java index cfe9e0d3c8c..d3c67a8abe1 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java @@ -38,7 +38,7 @@ public interface ActiveRuleMapper { ActiveRuleDto selectByKey(@Param("profileKey") String profileKey, @Param("repository") String repository, @Param("rule") String rule); - List selectByKeys(@Param("keys") List keys); + List selectByKeys(@Param("keys") List keys); List selectByRuleId(int ruleId); diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/SqlActiveRuleKey.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/SqlActiveRuleKey.java deleted file mode 100644 index 908569d7124..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/SqlActiveRuleKey.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.db.qualityprofile; - -public class SqlActiveRuleKey implements Comparable { - private final String qProfile; - private final String rule; - private final String repository; - - public SqlActiveRuleKey(ActiveRuleKey key) { - this.qProfile = key.qProfile(); - this.rule = key.ruleKey().rule(); - this.repository = key.ruleKey().repository(); - } - - @Override - public int compareTo(SqlActiveRuleKey o) { - int result = qProfile.compareTo(o.qProfile); - if (result != 0) { - return result; - } - result = rule.compareTo(o.rule); - if (result != 0) { - return result; - } - - return repository.compareTo(o.repository); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - SqlActiveRuleKey activeRuleKey = (SqlActiveRuleKey) o; - return qProfile.equals(activeRuleKey.qProfile) - && rule.equals(activeRuleKey.rule) - && repository.equals(activeRuleKey.repository); - } - - @Override - public int hashCode() { - int result = qProfile.hashCode(); - result = 31 * result + rule.hashCode(); - result = 31 * result + repository.hashCode(); - return result; - } - - public String getqProfile() { - return qProfile; - } - - public String getRule() { - return rule; - } - - public String getRepository() { - return repository; - } -} diff --git a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml index 17e6aa4a274..d8b0a1ab0ae 100644 --- a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml @@ -98,9 +98,10 @@ WHERE - (qp.kee = #{key.qProfile} - AND r.plugin_rule_key = #{key.rule} - AND r.plugin_name = #{key.repository}) + (qp.kee = #{key.qualityProfileKey} + AND r.plugin_rule_key = #{key.ruleKey.rule} + AND r.plugin_name = #{key.ruleKey.repository} + ) diff --git a/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java b/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java index 1db32fe9e92..e04b39f9296 100644 --- a/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java @@ -19,7 +19,67 @@ */ package org.sonar.db.qualityprofile; -// TODO +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.sonar.api.rule.Severity; +import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.rule.RuleDto; +import org.sonar.db.rule.RuleTesting; +import org.sonar.test.DbTests; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +// TODO add missing tests +@Category(DbTests.class) public class ActiveRuleDaoTest { + static final long NOW = 10000000L; + + static final QualityProfileDto QPROFILE_1 = QualityProfileDto.createFor("qp1").setName("QProile1"); + static final QualityProfileDto QPROFILE_2 = QualityProfileDto.createFor("qp2").setName("QProile2"); + + static final RuleDto RULE_1 = RuleTesting.newDto(RuleTesting.XOO_X1); + static final RuleDto RULE_2 = RuleTesting.newDto(RuleTesting.XOO_X2); + + System2 system = mock(System2.class); + + @Rule + public DbTester dbTester = DbTester.create(system); + + DbClient dbClient = dbTester.getDbClient(); + DbSession dbSession = dbTester.getSession(); + + ActiveRuleDao underTest = dbTester.getDbClient().activeRuleDao(); + + @Before + public void createDao() { + when(system.now()).thenReturn(NOW); + + dbClient.qualityProfileDao().insert(dbTester.getSession(), QPROFILE_1); + dbClient.qualityProfileDao().insert(dbTester.getSession(), QPROFILE_2); + dbClient.ruleDao().insert(dbTester.getSession(), RULE_1); + dbClient.ruleDao().insert(dbTester.getSession(), RULE_2); + dbSession.commit(); + } + + @Test + public void select_by_keys() throws Exception { + ActiveRuleDto activeRule1 = ActiveRuleDto.createFor(QPROFILE_1, RULE_1).setSeverity(Severity.BLOCKER); + ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(QPROFILE_1, RULE_2).setSeverity(Severity.BLOCKER); + underTest.insert(dbTester.getSession(), activeRule1); + underTest.insert(dbTester.getSession(), activeRule2); + dbSession.commit(); + + assertThat(underTest.selectByKeys(dbSession, asList(activeRule1.getKey(), activeRule2.getKey()))).hasSize(2); + assertThat(underTest.selectByKeys(dbSession, asList(activeRule1.getKey()))).hasSize(1); + assertThat(underTest.selectByKeys(dbSession, asList(ActiveRuleKey.of(QPROFILE_2.getKey(), RULE_1.getKey())))).isEmpty(); + } } -- 2.39.5