import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
import org.sonar.core.rule.RuleDao;
+import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.server.configuration.ProfilesManager;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.rule.ProfileRules;
import org.sonar.server.rule.RuleRegistry;
import org.sonar.server.user.UserSession;
private final QProfileLookup profileLookup;
private final RuleRegistry ruleRegistry;
private final ProfilesManager profilesManager;
- private final ProfileRules rulesLookup;
private final System2 system;
public QProfileActiveRuleOperations(MyBatis myBatis, ActiveRuleDao activeRuleDao, RuleDao ruleDao, QProfileLookup profileLookup, RuleRegistry ruleRegistry,
- ProfilesManager profilesManager, ProfileRules rulesLookup) {
- this(myBatis, activeRuleDao, ruleDao, profileLookup, ruleRegistry, profilesManager, rulesLookup, System2.INSTANCE);
+ ProfilesManager profilesManager) {
+ this(myBatis, activeRuleDao, ruleDao, profileLookup, ruleRegistry, profilesManager, System2.INSTANCE);
}
@VisibleForTesting
QProfileActiveRuleOperations(MyBatis myBatis, ActiveRuleDao activeRuleDao, RuleDao ruleDao, QProfileLookup profileLookup, RuleRegistry ruleRegistry,
- ProfilesManager profilesManager, ProfileRules rulesLookup, System2 system) {
+ ProfilesManager profilesManager, System2 system) {
this.myBatis = myBatis;
this.activeRuleDao = activeRuleDao;
this.ruleDao = ruleDao;
this.profileLookup = profileLookup;
this.ruleRegistry = ruleRegistry;
this.profilesManager = profilesManager;
- this.rulesLookup = rulesLookup;
this.system = system;
}
- public QProfileRule activateRule(int profileId, int ruleId, String severity, UserSession userSession) {
+ public void activateRule(int profileId, int ruleId, String severity, UserSession userSession) {
validatePermission(userSession);
validateSeverity(severity);
QProfile profile = findProfileNotNull(profileId);
- QProfileRule rule = findRuleNotNull(ruleId);
+ RuleDto rule = findRuleNotNull(ruleId);
ActiveRuleDto activeRule = findActiveRule(profileId, ruleId);
if (activeRule == null) {
- activeRule = createActiveRule(profile, rule, severity, userSession);
+ createActiveRule(profile.id(), rule.getId(), severity, userSession);
} else {
updateSeverity(activeRule, severity, userSession);
}
- return rulesLookup.findByActiveRuleId(activeRule.getId());
}
- private ActiveRuleDto createActiveRule(QProfile profile, QProfileRule rule, String severity, UserSession userSession) {
+ private ActiveRuleDto createActiveRule(int profileId, int ruleId, String severity, UserSession userSession) {
SqlSession session = myBatis.openSession();
try {
ActiveRuleDto activeRule = new ActiveRuleDto()
- .setProfileId(profile.id())
- .setRuleId(rule.id())
+ .setProfileId(profileId)
+ .setRuleId(ruleId)
.setSeverity(getSeverityOrdinal(severity));
activeRuleDao.insert(activeRule, session);
- List<RuleParamDto> ruleParams = ruleDao.selectParameters(rule.id(), session);
+ List<RuleParamDto> ruleParams = ruleDao.selectParameters(ruleId, session);
List<ActiveRuleParamDto> activeRuleParams = newArrayList();
for (RuleParamDto ruleParam : ruleParams) {
ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto()
}
session.commit();
- ProfilesManager.RuleInheritanceActions actions = profilesManager.activated(profile.id(), activeRule.getId(), userSession.name());
+ ProfilesManager.RuleInheritanceActions actions = profilesManager.activated(profileId, activeRule.getId(), userSession.name());
reindexInheritanceResult(actions, session);
return activeRule;
}
}
- public QProfileRule deactivateRule(int profileId, int ruleId, UserSession userSession) {
+ public void deactivateRule(int profileId, int ruleId, UserSession userSession) {
validatePermission(userSession);
ActiveRuleDto activeRule = findActiveRule(profileId, ruleId);
deactivateRule(activeRule, userSession);
- return rulesLookup.findByRuleId(ruleId);
}
private void deactivateRule(ActiveRuleDto activeRule, UserSession userSession) {
}
}
- public QProfileRule updateActiveRuleParam(int activeRuleId, String key, @Nullable String value, UserSession userSession) {
+ public void updateActiveRuleParam(int activeRuleId, String key, @Nullable String value, UserSession userSession) {
validatePermission(userSession);
String sanitizedValue = Strings.emptyToNull(value);
ActiveRuleParamDto activeRuleParam = findActiveRuleParam(activeRuleId, key);
updateActiveRuleParam(activeRule, activeRuleParam, value, userSession);
}
// If no active rule param and no value -> do nothing
-
- return rulesLookup.findByActiveRuleId(activeRuleId);
}
private void createActiveRuleParam(ActiveRuleDto activeRule, String key, String value, UserSession userSession) {
}
}
- public QProfileRule revertActiveRule(int activeRuleId, UserSession userSession) {
+ public void revertActiveRule(int activeRuleId, UserSession userSession) {
validatePermission(userSession);
ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId);
revertActiveRule(activeRule, userSession);
- return rulesLookup.findByActiveRuleId(activeRule.getId());
}
private void revertActiveRule(ActiveRuleDto activeRule, UserSession userSession) {
return profile;
}
- private QProfileRule findRuleNotNull(int ruleId) {
- QProfileRule rule = rulesLookup.findByRuleId(ruleId);
+ private RuleDto findRuleNotNull(int ruleId) {
+ RuleDto rule = ruleDao.selectById(ruleId);
QProfileValidations.checkRuleIsNotNull(rule);
return rule;
}
// PROFILE RULES
+ public QProfileRule findByRule(int ruleId) {
+ return rules.findByRuleId(ruleId);
+ }
+
+ public QProfileRule findByActiveRuleId(int activeRuleId) {
+ return rules.findByActiveRuleId(activeRuleId);
+ }
+
+ public QProfileRule findByProfileAndRule(int profileId, int ruleId) {
+ return rules.findByProfileIdAndRuleId(profileId, ruleId);
+ }
+
public ProfileRules.QProfileRuleResult searchProfileRules(ProfileRuleQuery query, Paging paging) {
return rules.search(query, paging);
}
return rules.countProfileRules(ProfileRuleQuery.create(profile.id()).setInheritance(QProfileRule.OVERRIDES));
}
- public QProfileRule activateRule(int profileId, int ruleId, String severity) {
- return activeRuleOperations.activateRule(profileId, ruleId, severity, UserSession.get());
+ public void activateRule(int profileId, int ruleId, String severity) {
+ activeRuleOperations.activateRule(profileId, ruleId, severity, UserSession.get());
}
- public QProfileRule deactivateRule(int profileId, int ruleId) {
- return activeRuleOperations.deactivateRule(profileId, ruleId, UserSession.get());
+ public void deactivateRule(int profileId, int ruleId) {
+ activeRuleOperations.deactivateRule(profileId, ruleId, UserSession.get());
}
- public QProfileRule updateActiveRuleParam(int activeRuleId, String key, @Nullable String value) {
- return activeRuleOperations.updateActiveRuleParam(activeRuleId, key, value, UserSession.get());
+ public void updateActiveRuleParam(int activeRuleId, String key, @Nullable String value) {
+ activeRuleOperations.updateActiveRuleParam(activeRuleId, key, value, UserSession.get());
}
- public QProfileRule revertActiveRule(int activeRuleId) {
- return activeRuleOperations.revertActiveRule(activeRuleId, UserSession.get());
+ public void revertActiveRule(int activeRuleId) {
+ activeRuleOperations.revertActiveRule(activeRuleId, UserSession.get());
}
public QProfileRule updateActiveRuleNote(int activeRuleId, String note) {
return null;
}
+ @CheckForNull
+ public QProfileRule findByProfileIdAndRuleId(int profileId, int ruleId) {
+ Map<String, Object> ruleSource = index.client().prepareGet(INDEX_RULES, TYPE_RULE, Integer.toString(ruleId))
+ .execute().actionGet().getSourceAsMap();
+ if (ruleSource != null) {
+ SearchHits activeRuleHits = searchActiveRules(ProfileRuleQuery.create(profileId), newArrayList(ruleId), FIELD_SOURCE, FIELD_PARENT);
+ long resultSize = activeRuleHits.totalHits();
+ if (resultSize > 0) {
+ if (resultSize == 1) {
+ return new QProfileRule(ruleSource, activeRuleHits.getAt(0).sourceAsMap());
+ } else {
+ throw new IllegalStateException("There is more than one result.");
+ }
+ }
+ }
+ return null;
+ }
+
@CheckForNull
public QProfileRule findByRuleId(int ruleId) {
Map<String, Object> ruleSource = index.client().prepareGet(INDEX_RULES, TYPE_RULE, Integer.toString(ruleId))
rule = nil
profile_id = params[:id].to_i
+ rule_id = params[:rule_id].to_i
call_backend do
severity = params[:level]
if severity.blank?
# deactivate the rule
- rule = Internal.quality_profiles.deactivateRule(profile_id, params[:rule_id].to_i)
+ Internal.quality_profiles.deactivateRule(profile_id, rule_id)
+ rule = Internal.quality_profiles.findByRule(rule_id)
else
# activate the rule
- rule = Internal.quality_profiles.activateRule(profile_id, params[:rule_id].to_i, severity)
+ Internal.quality_profiles.activateRule(profile_id, rule_id, severity)
+ rule = Internal.quality_profiles.findByProfileAndRule(profile_id, rule_id)
end
end
require_parameters :param_id, :active_rule_id, :profile_id
rule = nil
+ profile_id = params[:profile_id].to_i
+ active_rule_id = params[:active_rule_id].to_i
call_backend do
- rule = Internal.quality_profiles.updateActiveRuleParam(params[:active_rule_id].to_i, params[:param_id], params[:value])
+ Internal.quality_profiles.updateActiveRuleParam(active_rule_id, params[:param_id], params[:value])
+ rule = Internal.quality_profiles.findByActiveRuleId(active_rule_id)
end
- profile = Internal.quality_profiles.profile(params[:profile_id].to_i)
+ profile = Internal.quality_profiles.profile(profile_id)
parent_profile = Internal.quality_profiles.parent(profile)
render :partial => 'rule', :locals => {:rule => rule, :profile => profile, :parent_profile => parent_profile}
end
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
import org.sonar.core.rule.RuleDao;
+import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.server.configuration.ProfilesManager;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.rule.ProfileRules;
import org.sonar.server.rule.RuleRegistry;
import org.sonar.server.user.MockUserSession;
import org.sonar.server.user.UserSession;
@Mock
ProfilesManager profilesManager;
- @Mock
- ProfileRules rulesLookup;
-
@Mock
System2 system;
}
}).when(activeRuleDao).insert(any(ActiveRuleDto.class), any(SqlSession.class));
- operations = new QProfileActiveRuleOperations(myBatis, activeRuleDao, ruleDao, profileLookup, ruleRegistry, profilesManager, rulesLookup, system);
+ operations = new QProfileActiveRuleOperations(myBatis, activeRuleDao, ruleDao, profileLookup, ruleRegistry, profilesManager, system);
}
@Test
public void fail_to_activate_rule_without_profile_admin_permission() throws Exception {
QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
when(profileLookup.profile(1)).thenReturn(profile);
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
try {
operations.activateRule(1, 10, Severity.CRITICAL, unauthorizedUserSession);
@Test
public void activate_rule() throws Exception {
- QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
- when(profileLookup.profile(1)).thenReturn(profile);
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
+ when(profileLookup.profile(1)).thenReturn(new QProfile().setId(1).setName("Default").setLanguage("java"));
+ when(ruleDao.selectById(10)).thenReturn(new RuleDto().setId(10));
when(ruleDao.selectParameters(eq(10), eq(session))).thenReturn(newArrayList(new RuleParamDto().setId(20).setName("max").setDefaultValue("10")));
final int idActiveRuleToUpdate = 42;
@Test
public void update_severity() throws Exception {
- QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
- when(profileLookup.profile(1)).thenReturn(profile);
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
+ when(profileLookup.profile(1)).thenReturn(new QProfile().setId(1).setName("Default").setLanguage("java"));
+ when(ruleDao.selectById(10)).thenReturn(new RuleDto().setId(10));
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule);
@Test
public void fail_to_update_severity_on_invalid_severity() throws Exception {
- QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
- when(profileLookup.profile(1)).thenReturn(profile);
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
+ when(profileLookup.profile(1)).thenReturn(new QProfile().setId(1).setName("Default").setLanguage("java"));
+ when(ruleDao.selectById(10)).thenReturn(new RuleDto().setId(10));
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule);
@Test
public void deactivate_rule() throws Exception {
- QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
- when(profileLookup.profile(1)).thenReturn(profile);
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
+ when(profileLookup.profile(1)).thenReturn(new QProfile().setId(1).setName("Default").setLanguage("java"));
+ when(ruleDao.selectById(10)).thenReturn(new RuleDto().setId(10));
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule);
when(profilesManager.deactivated(eq(1), anyInt(), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions());
@Test
public void create_active_rule_param() throws Exception {
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
when(activeRuleDao.selectById(5)).thenReturn(activeRule);
RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name());
@Test
public void fail_to_create_active_rule_if_no_rule_param() throws Exception {
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
when(activeRuleDao.selectById(5)).thenReturn(activeRule);
when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(null);
@Test
public void fail_to_create_active_rule_if_type_is_invalid() throws Exception {
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
when(activeRuleDao.selectById(5)).thenReturn(activeRule);
RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name());
@Test
public void update_active_rule_param() throws Exception {
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
when(activeRuleDao.selectById(5)).thenReturn(activeRule);
RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name());
@Test
public void remove_active_rule_param() throws Exception {
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
- when(rulesLookup.findByRuleId(10)).thenReturn(rule);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
when(activeRuleDao.selectById(5)).thenReturn(activeRule);
RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name());
verify(rules, never()).findByActiveRuleId(anyInt());
}
+ @Test
+ public void find_by_rule() throws Exception {
+ qProfiles.findByRule(1);
+ verify(rules).findByRuleId(1);
+ }
+
+ @Test
+ public void find_by_active_rule() throws Exception {
+ qProfiles.findByActiveRuleId(1);
+ verify(rules).findByActiveRuleId(1);
+ }
+
+ @Test
+ public void find_by_profile_an_rule() throws Exception {
+ qProfiles.findByProfileAndRule(1, 2);
+ verify(rules).findByProfileIdAndRuleId(1, 2);
+ }
+
@Test
public void search_active_rules() throws Exception {
final int profileId = 42;
assertThat(profileRules.findByActiveRuleId(9999)).isNull();
}
+ @Test
+ public void find_by_profile_id_and_rule_id() {
+ // Rules on profiles
+ QProfileRule rule = profileRules.findByProfileIdAndRuleId(1, 759);
+ assertThat(rule).isNotNull();
+ assertThat(rule.key()).isEqualTo("UnusedNullCheckInEquals");
+ assertThat(rule.severity()).isEqualTo(Severity.MAJOR);
+
+ // Rules on no profiles
+ assertThat(profileRules.findByProfileIdAndRuleId(1, 944)).isNull();
+
+ // Not existing rule
+ assertThat(profileRules.findByProfileIdAndRuleId(1, 99999)).isNull();
+ }
+
@Test
public void find_profile_rules() {
Paging paging = Paging.create(10, 1);