]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 RuleOperations is now using RuleIndexer
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 22 Feb 2016 11:41:53 +0000 (12:41 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 12:26:54 +0000 (13:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java
server/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java

index 729f44d3529dcf9bf2c8188f589c46ab26286b67..64cc7365f96dc57a5fd9f30869d787af0808773d 100644 (file)
@@ -27,12 +27,13 @@ import org.sonar.api.server.ServerSide;
 import org.sonar.api.server.debt.DebtRemediationFunction;
 import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.rule.RuleDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.rule.index.RuleIndexer;
 import org.sonar.server.user.UserSession;
 
 /**
@@ -42,9 +43,11 @@ import org.sonar.server.user.UserSession;
 @ServerSide
 public class RuleOperations {
 
+  private final RuleIndexer ruleIndexer;
   private final DbClient dbClient;
 
-  public RuleOperations(DbClient dbClient) {
+  public RuleOperations(RuleIndexer ruleIndexer, DbClient dbClient) {
+    this.ruleIndexer = ruleIndexer;
     this.dbClient = dbClient;
   }
 
@@ -52,15 +55,12 @@ public class RuleOperations {
     checkPermission(userSession);
     DbSession session = dbClient.openSession(false);
     try {
-      RuleDto ruleDto = dbClient.deprecatedRuleDao().getNullableByKey(session, ruleChange.ruleKey());
-      if (ruleDto == null) {
-        throw new NotFoundException(String.format("Unknown rule '%s'", ruleChange.ruleKey()));
-      }
-
+      RuleDto ruleDto = dbClient.ruleDao().selectOrFailByKey(session, ruleChange.ruleKey());
       boolean needUpdate = updateRule(ruleDto, ruleChange.debtRemediationFunction(), ruleChange.debtRemediationCoefficient(),
         ruleChange.debtRemediationOffset(),
         session);
       if (needUpdate) {
+        ruleIndexer.index();
         session.commit();
       }
     } catch (IllegalArgumentException e) {
@@ -102,7 +102,7 @@ public class RuleOperations {
     }
 
     if (needUpdate) {
-      dbClient.deprecatedRuleDao().update(session, ruleDto);
+      dbClient.ruleDao().update(session, ruleDto);
     }
     return needUpdate;
   }
index 1725000bed31e3bfb6b2ff5dd50dc3fb18626663..ece0b19f3f5d839cc72e84a469a193a72c7ae29e 100644 (file)
@@ -28,13 +28,14 @@ import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.rule.RuleDao;
 import org.sonar.db.rule.RuleDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.rule.RuleOperations.RuleChange;
-import org.sonar.server.rule.db.RuleDao;
+import org.sonar.server.rule.index.RuleIndexer;
 import org.sonar.server.tester.MockUserSession;
 import org.sonar.server.user.UserSession;
 
@@ -56,6 +57,9 @@ public class RuleOperationsTest {
   @Mock
   RuleDao ruleDao;
 
+  @Mock
+  RuleIndexer ruleIndexer;
+
   @Captor
   ArgumentCaptor<RuleDto> ruleCaptor;
 
@@ -66,8 +70,8 @@ public class RuleOperationsTest {
   @Before
   public void setUp() {
     when(dbClient.openSession(false)).thenReturn(session);
-    when(dbClient.deprecatedRuleDao()).thenReturn(ruleDao);
-    operations = new RuleOperations(dbClient);
+    when(dbClient.ruleDao()).thenReturn(ruleDao);
+    operations = new RuleOperations(ruleIndexer, dbClient);
   }
 
   @Test
@@ -76,7 +80,7 @@ public class RuleOperationsTest {
       .setRemediationFunction("CONSTANT_ISSUE").setRemediationOffset("10min");
     RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
 
-    when(ruleDao.getNullableByKey(session, ruleKey)).thenReturn(dto);
+    when(ruleDao.selectOrFailByKey(session, ruleKey)).thenReturn(dto);
 
     operations.updateRule(
       new RuleChange().setRuleKey(ruleKey)
@@ -93,6 +97,8 @@ public class RuleOperationsTest {
     assertThat(result.getRemediationFunction()).isEqualTo("LINEAR_OFFSET");
     assertThat(result.getRemediationCoefficient()).isEqualTo("2h");
     assertThat(result.getRemediationOffset()).isEqualTo("20min");
+
+    verify(ruleIndexer).index();
   }
 
   @Test
@@ -102,7 +108,7 @@ public class RuleOperationsTest {
       .setDefaultRemediationFunction("CONSTANT_ISSUE").setDefaultRemediationOffset("10min");
     RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
 
-    when(ruleDao.getNullableByKey(session, ruleKey)).thenReturn(dto);
+    when(ruleDao.selectOrFailByKey(session, ruleKey)).thenReturn(dto);
 
     operations.updateRule(
       // Same value as default values -> overridden values will be set to null
@@ -128,7 +134,7 @@ public class RuleOperationsTest {
       .setRemediationFunction("CONSTANT_ISSUE").setRemediationOffset("10min");
     RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
 
-    when(ruleDao.getNullableByKey(session, ruleKey)).thenReturn(dto);
+    when(ruleDao.selectOrFailByKey(session, ruleKey)).thenReturn(dto);
 
     operations.updateRule(
       new RuleChange().setRuleKey(ruleKey)
@@ -138,6 +144,7 @@ public class RuleOperationsTest {
 
     verify(ruleDao, never()).update(eq(session), any(RuleDto.class));
     verify(session, never()).commit();
+    verify(ruleIndexer, never()).index();
   }
 
   @Test
@@ -146,7 +153,7 @@ public class RuleOperationsTest {
       .setDefaultRemediationFunction("CONSTANT_ISSUE").setDefaultRemediationOffset("10min");
     RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
 
-    when(ruleDao.getNullableByKey(session, ruleKey)).thenReturn(dto);
+    when(ruleDao.selectOrFailByKey(session, ruleKey)).thenReturn(dto);
 
     operations.updateRule(
       // Characteristic is the not same as the default one -> Overridden values should be set
@@ -166,14 +173,13 @@ public class RuleOperationsTest {
     assertThat(result.getRemediationCoefficient()).isEqualTo("10min");
   }
 
-
   @Test
   public void disable_rule_debt_when_update_rule_with_no_function() {
     RuleDto dto = new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck")
       .setDefaultRemediationFunction("CONSTANT_ISSUE").setDefaultRemediationOffset("10min");
     RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
 
-    when(ruleDao.getNullableByKey(session, ruleKey)).thenReturn(dto);
+    when(ruleDao.selectOrFailByKey(session, ruleKey)).thenReturn(dto);
 
     operations.updateRule(new RuleChange().setRuleKey(ruleKey), authorizedUserSession);
 
@@ -188,33 +194,13 @@ public class RuleOperationsTest {
     assertThat(result.getRemediationOffset()).isNull();
   }
 
-  @Test
-  public void fail_to_update_rule_on_unknown_rule() {
-    RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
-
-    when(ruleDao.getNullableByKey(session, ruleKey)).thenReturn(null);
-
-    try {
-      operations.updateRule(
-        new RuleChange().setRuleKey(ruleKey)
-          .setDebtRemediationFunction("LINEAR_OFFSET").setDebtRemediationCoefficient("2h").setDebtRemediationOffset("20min"),
-        authorizedUserSession
-      );
-    } catch (Exception e) {
-      assertThat(e).isInstanceOf(NotFoundException.class);
-    }
-
-    verify(ruleDao, never()).update(eq(session), any(RuleDto.class));
-    verify(session, never()).commit();
-  }
-
   @Test
   public void fail_to_update_rule_on_invalid_coefficient() {
     RuleDto dto = new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck")
       .setRemediationFunction("LINEAR").setRemediationCoefficient("1h");
     RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
 
-    when(ruleDao.getNullableByKey(session, ruleKey)).thenReturn(dto);
+    when(ruleDao.selectOrFailByKey(session, ruleKey)).thenReturn(dto);
 
     try {
       operations.updateRule(