}
@Test
- public void should_reject_invalid_prroperty_types() {
+ public void should_reject_invalid_property_types() {
exception.expect(SonarException.class);
exception.expectMessage("Invalid property type [INVALID]");
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.math.NumberUtils;
import org.apache.ibatis.session.SqlSession;
+import org.sonar.api.PropertyType;
import org.sonar.api.ServerComponent;
import org.sonar.api.profiles.ProfileImporter;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.server.rule.RuleRegistry;
import org.sonar.server.user.UserSession;
-import javax.annotation.CheckForNull;
-
import java.io.StringReader;
import java.util.Date;
import java.util.List;
public ActiveRuleDto createActiveRule(QualityProfileDto qualityProfile, RuleDto rule, String severity, UserSession userSession) {
checkPermission(userSession);
+ checkSeverity(severity);
SqlSession session = myBatis.openSession();
try {
ActiveRuleDto activeRule = new ActiveRuleDto()
public void updateSeverity(QualityProfileDto qualityProfile, ActiveRuleDto activeRule, String newSeverity, UserSession userSession) {
checkPermission(userSession);
+ checkSeverity(newSeverity);
SqlSession session = myBatis.openSession();
try {
Integer oldSeverity = activeRule.getSeverity();
SqlSession session = myBatis.openSession();
try {
- RuleParamDto ruleParam = ruleDao.selectParamByRuleAndKey(activeRule.getRulId(), key, session);
- if (ruleParam == null) {
- throw new IllegalArgumentException("No rule param found");
- }
+ RuleParamDto ruleParam = findRuleParamNotNull(activeRule.getRulId(), key, session);
+ validateParam(ruleParam.getType(), value);
ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setActiveRuleId(activeRule.getId()).setKey(key).setValue(value).setRulesParameterId(ruleParam.getId());
activeRuleDao.insert(activeRuleParam, session);
session.commit();
SqlSession session = myBatis.openSession();
try {
+ RuleParamDto ruleParam = findRuleParamNotNull(activeRule.getRulId(), activeRuleParam.getKey(), session);
+ validateParam(ruleParam.getType(), value);
+
String sanitizedValue = Strings.emptyToNull(value);
String oldValue = activeRuleParam.getValue();
activeRuleParam.setValue(sanitizedValue);
.setValue(activeRuleParam.getValue());
}
- @CheckForNull
- private ActiveRuleDto findActiveRule(QualityProfileDto qualityProfile, RuleDto rule) {
- return activeRuleDao.selectByProfileAndRule(qualityProfile.getId(), rule.getId());
- }
-
private void checkPermission(UserSession userSession) {
userSession.checkLoggedIn();
userSession.checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
return name;
}
+ private void checkSeverity(String severity) {
+ if (!Severity.ALL.contains(severity)) {
+ throw new BadRequestException("The severity is not valid");
+ }
+ }
+
+ private RuleParamDto findRuleParamNotNull(Integer ruleId, String key, SqlSession session) {
+ RuleParamDto ruleParam = ruleDao.selectParamByRuleAndKey(ruleId, key, session);
+ if (ruleParam == null) {
+ throw new IllegalArgumentException("No rule param found");
+ }
+ return ruleParam;
+ }
+
+ private void validateParam(String type, String value) {
+ if (type.equals(PropertyType.INTEGER.name()) && !NumberUtils.isDigits(value)) {
+ throw new BadRequestException(String.format("Value '%s' must be an integer.", value));
+ }
+ }
+
}
// delete note on an active rule (only E/S indexing)
// extends extension of a rule (only E/S indexing)
// revert modification on active rule with inheritance
+ // active rule parameter validation (only Integer types are checked)
//
// TEMPLATE RULES
// create template rule
call_backend do
result = Internal.quality_profiles.updateActiveRuleParam(params[:profile_id].to_i, params[:active_rule_id].to_i, params[:param_id], params[:value])
end
+
profile = result.profile
rule = result.rule
render :partial => 'rule', :locals => {:profile => profile, :rule => rule}
<% # locals = rule, profile, parameter, ancestor_active_rule
- # Display default value only for inactive rules
param_id = "#{rule.id}#{parameter.key}"
# Display default value only for inactive rules
param_value = parameter.default_value if !rule.activeRuleId
name="form-<%=u parameter.key-%>" method="post"
action="<%= ApplicationController.root_context -%>/new_rules_configuration/update_param/?active_rule_id=<%=active_rule_id-%>¶m_id=<%=parameter.key-%>&profile_id=<%=profile.id-%>">
- <div id="error_<%= active_rule_id -%><%= parameter.key -%>" class="error" style="display: none"></div>
+ <div id="error_<%= param_id -%>" class="error" style="display: none"></div>
- <span id="text_<%= active_rule_id -%><%= parameter.key -%>"><%= param_value_input(rule, parameter, param_value, :disabled => read_only) -%></span>
+ <span id="text_<%= param_id -%>"><%= param_value_input(rule, parameter, param_value, :disabled => read_only) -%></span>
<% unless read_only %>
<%= submit_tag(message('update_verb'), :id => 'submit_' + param_id.to_s) %>
<img src="<%= ApplicationController.root_context -%>/images/loading.gif" style="display:none;" id="param_loading_<%= param_id -%>" class="rule-param-loading">
-<%
-=begin %>
- <% if active_parameter and active_parameter.errors.size>0 %>
- <span class="error"><%= active_parameter.errors.on 'value' %></span>
- <% end %>
-<%
-=end %>
<% end %>
<% if !rule.nil? && rule.overrides? && ancestor_active_rule
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
+import org.sonar.api.PropertyType;
import org.sonar.api.profiles.ProfileImporter;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rule.Severity;
.addToIndex(idActiveRuleToUpdate)
.addToDelete(idActiveRuleToDelete);
when(profilesManager.activated(eq(1), anyInt(), eq("Nicolas"))).thenReturn(inheritanceActions);
- when(activeRuleDao.selectByIds(anyList(), isA(SqlSession.class))).thenReturn(ImmutableList.<ActiveRuleDto> of(mock(ActiveRuleDto.class)));
- when(activeRuleDao.selectParamsByActiveRuleIds(anyList(), isA(SqlSession.class))).thenReturn(ImmutableList.<ActiveRuleParamDto> of(mock(ActiveRuleParamDto.class)));
+ when(activeRuleDao.selectByIds(anyList(), isA(SqlSession.class))).thenReturn(ImmutableList.<ActiveRuleDto>of(mock(ActiveRuleDto.class)));
+ when(activeRuleDao.selectParamsByActiveRuleIds(anyList(), isA(SqlSession.class))).thenReturn(ImmutableList.<ActiveRuleParamDto>of(mock(ActiveRuleParamDto.class)));
ActiveRuleDto result = operations.createActiveRule(qualityProfile, rule, Severity.CRITICAL, authorizedUserSession);
assertThat(result).isNotNull();
verify(ruleRegistry).bulkIndexActiveRules(anyList(), isA(Multimap.class));
}
+ @Test
+ public void fail_to_update_severity_on_invalid_severity() throws Exception {
+ QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java");
+ Rule rule = Rule.create().setRepositoryKey("squid").setKey("AvoidCycle");
+ rule.setId(10);
+ ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
+
+ try {
+ operations.updateSeverity(qualityProfile, activeRule, "Unknown", authorizedUserSession);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(BadRequestException.class);
+ }
+ verify(activeRuleDao, never()).update(eq(activeRule), eq(session));
+ verifyZeroInteractions(profilesManager);
+ }
+
@Test
public void deactivate_rule() throws Exception {
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
public void update_active_rule_param() throws Exception {
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20");
+
+ RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name());
+ when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam);
+
when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("max"), eq("20"), eq("30"), eq("Nicolas"))).thenReturn(new RuleInheritanceActions());
operations.updateActiveRuleParam(activeRule, activeRuleParam, "30", authorizedUserSession);
@Test
public void create_active_rule_param() throws Exception {
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
- RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20");
+ RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name());
when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam);
when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("max"), eq((String) null), eq("30"), eq("Nicolas"))).thenReturn(new RuleInheritanceActions());
verifyZeroInteractions(profilesManager);
}
+ @Test
+ public void fail_to_create_active_rule_if_type_is_invalid() throws Exception {
+ RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name());
+ when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam);
+
+ ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
+ try {
+ operations.createActiveRuleParam(activeRule, "max", "invalid integer", authorizedUserSession);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(BadRequestException.class);
+ }
+ verify(activeRuleDao, never()).insert(any(ActiveRuleParamDto.class), eq(session));
+ verifyZeroInteractions(profilesManager);
+ }
+
@Test
public void update_active_rule_note_when_no_existing_note() throws Exception {
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setNoteCreatedAt(null).setNoteData(null);