]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 6 May 2014 11:53:21 +0000 (13:53 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 6 May 2014 11:53:21 +0000 (13:53 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java

index ed25e2d19842fb57fbc1768c1a7d996e46c936ef..4eb5b5f8d79248bfa11b17ac3bed75a205f3aa79 100644 (file)
@@ -38,6 +38,7 @@ import org.sonar.core.rule.RuleDao;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.core.rule.RuleParamDto;
 import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.user.UserSession;
 import org.sonar.server.util.TypeValidations;
 
@@ -239,20 +240,19 @@ public class QProfileActiveRuleOperations implements ServerComponent {
     notifyParamsDeleted(activeRule, newArrayList(activeRuleParam), session, userSession);
   }
 
-  void updateActiveRuleParam(ActiveRuleDto activeRule, String key, String value, SqlSession session) {
+  void updateActiveRuleParam(ActiveRuleDto activeRule, String key, String sanitizedValue, SqlSession session) {
     RuleParamDto ruleParam = findRuleParamNotNull(activeRule.getRulId(), key, session);
-    ActiveRuleParamDto activeRuleParam = findActiveRuleParam(activeRule.getId(), key, session);
-    validateParam(ruleParam, value);
+    ActiveRuleParamDto activeRuleParam = findActiveRuleParamNotNull(activeRule.getId(), key, session);
+    validateParam(ruleParam, sanitizedValue);
 
-    activeRuleParam.setValue(value);
+    activeRuleParam.setValue(sanitizedValue);
     activeRuleDao.update(activeRuleParam, session);
   }
 
-  private void updateActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, String value, UserSession userSession, SqlSession session) {
+  private void updateActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, String sanitizedValue, UserSession userSession, SqlSession session) {
     RuleParamDto ruleParam = findRuleParamNotNull(activeRule.getRulId(), activeRuleParam.getKey(), session);
-    validateParam(ruleParam, value);
+    validateParam(ruleParam, sanitizedValue);
 
-    String sanitizedValue = Strings.emptyToNull(value);
     String oldValue = activeRuleParam.getValue();
     activeRuleParam.setValue(sanitizedValue);
     activeRuleDao.update(activeRuleParam, session);
@@ -505,4 +505,12 @@ public class QProfileActiveRuleOperations implements ServerComponent {
     return activeRuleDao.selectParamByActiveRuleAndKey(activeRuleId, key, session);
   }
 
+  private ActiveRuleParamDto findActiveRuleParamNotNull(int activeRuleId, String key, SqlSession session) {
+    ActiveRuleParamDto activeRuleParam = findActiveRuleParam(activeRuleId, key, session);
+    if (activeRuleParam == null) {
+      throw new NotFoundException(String.format("No active rule parameter '%s' has been found on active rule id '%s'", key, activeRuleId));
+    }
+    return activeRuleParam;
+  }
+
 }
index e43169f12c62b0f021702e5932153813a7816a04..e1daed22d34a07b9c1521127ed84d4c48f5d4870 100644 (file)
@@ -45,6 +45,7 @@ import org.sonar.core.rule.RuleDto;
 import org.sonar.core.rule.RuleParamDto;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.user.MockUserSession;
 import org.sonar.server.user.UserSession;
 import org.sonar.server.util.TypeValidations;
@@ -411,6 +412,25 @@ public class QProfileActiveRuleOperationsTest {
     verifyZeroInteractions(esActiveRule);
   }
 
+  @Test
+  public void fail_to_update_active_rule_param_from_active_rule_when_active_rule_param_does_not_exists() throws Exception {
+    ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(Severity.MINOR);
+    RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name());
+    when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam);
+    when(activeRuleDao.selectParamByActiveRuleAndKey(5, "max", session)).thenReturn(null);
+
+    try {
+      operations.updateActiveRuleParam(activeRule, "max", "30", session);
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("No active rule parameter 'max' has been found on active rule id '5'");
+    }
+    verify(activeRuleDao, never()).update(any(ActiveRuleParamDto.class), eq(session));
+    verifyZeroInteractions(session);
+    verifyZeroInteractions(profilesManager);
+    verifyZeroInteractions(esActiveRule);
+  }
+
   @Test
   public void remove_active_rule_param() throws Exception {
     ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(Severity.MINOR);