aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2014-05-06 13:53:21 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2014-05-06 13:53:21 +0200
commit6a600793c5145d4e1753d6ce65415d4fcd5f8a47 (patch)
tree792a3d44ce3f3d99daae447620e7e6497e7cd785
parentf25d19f229d4e166452537232c4bc5e825e4a66b (diff)
downloadsonarqube-6a600793c5145d4e1753d6ce65415d4fcd5f8a47.tar.gz
sonarqube-6a600793c5145d4e1753d6ce65415d4fcd5f8a47.zip
Fix quality flaws
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java22
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java20
2 files changed, 35 insertions, 7 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java
index ed25e2d1984..4eb5b5f8d79 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java
@@ -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;
+ }
+
}
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java
index e43169f12c6..e1daed22d34 100644
--- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java
@@ -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;
@@ -412,6 +413,25 @@ public class QProfileActiveRuleOperationsTest {
}
@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);
when(activeRuleDao.selectById(5, session)).thenReturn(activeRule);