]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 Remove SqlActiveRuleKey
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 10:11:47 +0000 (11:11 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 12:26:54 +0000 (13:26 +0100)
sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java
sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java
sonar-db/src/main/java/org/sonar/db/qualityprofile/SqlActiveRuleKey.java [deleted file]
sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml
sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java

index 60b90c7e2ccdce04b19839533a8d3eb25af7aab9..d81288a728c73ca37d22dadab0e4b1ed8dfec488 100644 (file)
@@ -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<ActiveRuleDto> selectByKeys(DbSession dbSession, List<ActiveRuleKey> keys) {
-    List<SqlActiveRuleKey> 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<ActiveRuleDto> 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<SqlActiveRuleKey>, List<ActiveRuleDto>> {
+  private static class KeyToDto implements Function<List<ActiveRuleKey>, List<ActiveRuleDto>> {
     private final ActiveRuleMapper mapper;
 
     private KeyToDto(ActiveRuleMapper mapper) {
@@ -194,7 +189,7 @@ public class ActiveRuleDao implements Dao {
     }
 
     @Override
-    public List<ActiveRuleDto> apply(@Nonnull List<SqlActiveRuleKey> partitionOfIds) {
+    public List<ActiveRuleDto> apply(@Nonnull List<ActiveRuleKey> partitionOfIds) {
       return mapper.selectByKeys(partitionOfIds);
     }
   }
index cfe9e0d3c8cc68991709859efdec28590068f048..d3c67a8abe1ae4bd62e82d93afabe617d024051b 100644 (file)
@@ -38,7 +38,7 @@ public interface ActiveRuleMapper {
 
   ActiveRuleDto selectByKey(@Param("profileKey") String profileKey, @Param("repository") String repository, @Param("rule") String rule);
 
-  List<ActiveRuleDto> selectByKeys(@Param("keys") List<SqlActiveRuleKey> keys);
+  List<ActiveRuleDto> selectByKeys(@Param("keys") List<ActiveRuleKey> keys);
 
   List<ActiveRuleDto> 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 (file)
index 908569d..0000000
+++ /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<SqlActiveRuleKey> {
-  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;
-  }
-}
index 17e6aa4a274213008835b2383d4ec6f98cf601aa..d8b0a1ab0aebf4e61efd23124dcc74212a323306 100644 (file)
     <include refid="activeRuleKeyJoin"/>
     WHERE
     <foreach collection="keys" item="key" open="(" separator=" or " close=")">
-      (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}
+      )
     </foreach>
   </select>
 
index 1db32fe9e92c53e556ed92e56bb78fe59d9e2700..e04b39f92967b5680f6b5f754f1fa31254850344 100644 (file)
  */
 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();
+  }
 }