]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5056 Fix NPE when disabling debt on rule
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Apr 2014 10:23:50 +0000 (12:23 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Apr 2014 10:23:50 +0000 (12:23 +0200)
sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java
sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java

index 5fd1474efe27931fbac2f64819b166494f2108cf..bd5fd9df3c9b0c2746cf64b2ab66d78c389f9237 100644 (file)
@@ -294,7 +294,7 @@ public class RuleOperations implements ServerComponent {
         // No sub-characteristic is given -> disable rule debt if not already disabled
       } else {
         // Rule characteristic is not already disabled -> update it
-        if (!ruleDto.getSubCharacteristicId().equals(RuleDto.DISABLED_CHARACTERISTIC_ID)) {
+        if (!RuleDto.DISABLED_CHARACTERISTIC_ID.equals(ruleDto.getSubCharacteristicId())) {
           ruleDto.setSubCharacteristicId(RuleDto.DISABLED_CHARACTERISTIC_ID);
           ruleDto.setRemediationFunction(null);
           ruleDto.setRemediationCoefficient(null);
index 6387d64c40eba2a7bef9d820c5ddcbc136721f6b..99663635a0523bc846ff38f2a9912daf4a2519e9 100644 (file)
@@ -504,10 +504,30 @@ public class RuleOperationsTest {
 
     when(ruleDao.selectByKey(ruleKey, session)).thenReturn(dto);
 
-    operations.updateRule(
-      new RuleChange().setRuleKey(ruleKey).setDebtCharacteristicKey(null),
-      authorizedUserSession
-    );
+    operations.updateRule(new RuleChange().setRuleKey(ruleKey).setDebtCharacteristicKey(null), authorizedUserSession);
+
+    verify(ruleDao).update(ruleCaptor.capture(), eq(session));
+    verify(ruleRegistry).reindex(eq(ruleCaptor.getValue()), eq(session));
+    verify(session).commit();
+
+    RuleDto result = ruleCaptor.getValue();
+
+    assertThat(result.getId()).isEqualTo(1);
+    assertThat(result.getSubCharacteristicId()).isEqualTo(-1);
+    assertThat(result.getRemediationFunction()).isNull();
+    assertThat(result.getRemediationCoefficient()).isNull();
+    assertThat(result.getRemediationOffset()).isNull();
+    assertThat(result.getUpdatedAt()).isEqualTo(now);
+  }
+
+  @Test
+  public void disable_characteristic_on_rule_having_no_debt_info() throws Exception {
+    RuleDto dto = new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck");
+    RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
+
+    when(ruleDao.selectByKey(ruleKey, session)).thenReturn(dto);
+
+    operations.updateRule(new RuleChange().setRuleKey(ruleKey).setDebtCharacteristicKey(null), authorizedUserSession);
 
     verify(ruleDao).update(ruleCaptor.capture(), eq(session));
     verify(ruleRegistry).reindex(eq(ruleCaptor.getValue()), eq(session));