From 7ad9ede752fad42ff8fb236af852463219032585 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 3 Apr 2014 12:23:50 +0200 Subject: [PATCH] SONAR-5056 Fix NPE when disabling debt on rule --- .../org/sonar/server/rule/RuleOperations.java | 2 +- .../sonar/server/rule/RuleOperationsTest.java | 28 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java index 5fd1474efe2..bd5fd9df3c9 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java @@ -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); diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java index 6387d64c40e..99663635a05 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java @@ -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)); -- 2.39.5