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;
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);
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;
+ }
+
}
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;
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);