import org.sonar.core.qualityprofile.db.ActiveRuleDao;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
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;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.util.Date;
this.system = system;
}
-// public ProfileRuleChanged activateRule(int profileId, int ruleId, String severity) {
-// QProfile profile = profileLookup.profile(profileId);
-// RuleDto rule = findRuleNotNull(ruleId);
-// ActiveRuleDto activeRule = findActiveRule(qualityProfile, rule);
-// if (activeRule == null) {
-// activeRule = activeRuleOperations.createActiveRule(qualityProfile, rule, severity, UserSession.get());
-// } else {
-// activeRuleOperations.updateSeverity(activeRule, severity, UserSession.get());
-// }
-// return activeRuleChanged(qualityProfile, activeRule);
-// }
-
- public ActiveRuleDto createActiveRule(QualityProfileDto qualityProfile, RuleDto rule, String severity, UserSession userSession) {
+ public ProfileRuleChanged activateRule(int profileId, int ruleId, String severity, UserSession userSession) {
validatePermission(userSession);
validateSeverity(severity);
+
+ QProfile profile = findProfileNotNull(profileId);
+ QProfileRule rule = findRuleNotNull(ruleId);
+ ActiveRuleDto activeRule = findActiveRule(profileId, ruleId);
+ if (activeRule == null) {
+ activeRule = createActiveRule(profile, rule, severity, userSession);
+ } else {
+ updateSeverity(activeRule, severity, userSession);
+ }
+ return activeRuleChanged(profile, activeRule.getId());
+ }
+
+ private ActiveRuleDto createActiveRule(QProfile profile, QProfileRule rule, String severity, UserSession userSession) {
SqlSession session = myBatis.openSession();
try {
ActiveRuleDto activeRule = new ActiveRuleDto()
- .setProfileId(qualityProfile.getId())
- .setRuleId(rule.getId())
+ .setProfileId(profile.id())
+ .setRuleId(rule.id())
.setSeverity(getSeverityOrdinal(severity));
activeRuleDao.insert(activeRule, session);
- List<RuleParamDto> ruleParams = ruleDao.selectParameters(rule.getId(), session);
+ List<RuleParamDto> ruleParams = ruleDao.selectParameters(rule.id(), session);
List<ActiveRuleParamDto> activeRuleParams = newArrayList();
for (RuleParamDto ruleParam : ruleParams) {
ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto()
}
session.commit();
- ProfilesManager.RuleInheritanceActions actions = profilesManager.activated(qualityProfile.getId(), activeRule.getId(), userSession.name());
+ ProfilesManager.RuleInheritanceActions actions = profilesManager.activated(profile.id(), activeRule.getId(), userSession.name());
reindexInheritanceResult(actions, session);
return activeRule;
}
}
- public void updateSeverity(ActiveRuleDto activeRule, String newSeverity, UserSession userSession) {
- validatePermission(userSession);
- validateSeverity(newSeverity);
+ private void updateSeverity(ActiveRuleDto activeRule, String newSeverity, UserSession userSession) {
SqlSession session = myBatis.openSession();
try {
Integer oldSeverity = activeRule.getSeverity();
}
}
- public void deactivateRule(ActiveRuleDto activeRule, UserSession userSession) {
+ public ProfileRuleChanged deactivateRule(int profileId, int ruleId, UserSession userSession) {
validatePermission(userSession);
+ QProfile profile = findProfileNotNull(profileId);
+ ActiveRuleDto activeRule = findActiveRule(profileId, ruleId);
+ deactivateRule(activeRule, userSession);
+ return ruleChanged(profile, ruleId);
+ }
+
+ private void deactivateRule(ActiveRuleDto activeRule, UserSession userSession) {
SqlSession session = myBatis.openSession();
try {
ProfilesManager.RuleInheritanceActions actions = profilesManager.deactivated(activeRule.getProfileId(), activeRule.getId(), userSession.name());
}
}
- public void createActiveRuleParam(ActiveRuleDto activeRule, String key, String value, UserSession userSession) {
+ public ProfileRuleChanged updateActiveRuleParam(int profileId, int activeRuleId, String key, @Nullable String value, UserSession userSession) {
validatePermission(userSession);
+ String sanitizedValue = Strings.emptyToNull(value);
+ QProfile profile = findProfileNotNull(profileId);
+ ActiveRuleParamDto activeRuleParam = findActiveRuleParam(activeRuleId, key);
+ ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId);
+ if (activeRuleParam == null && sanitizedValue != null) {
+ createActiveRuleParam(activeRule, key, value, userSession);
+ } else if (activeRuleParam != null && sanitizedValue == null) {
+ deleteActiveRuleParam(activeRule, activeRuleParam, userSession);
+ } else if (activeRuleParam != null) {
+ updateActiveRuleParam(activeRule, activeRuleParam, value, userSession);
+ }
+ // If no active rule param and no value -> do nothing
+ return activeRuleChanged(profile, activeRuleId);
+ }
+
+ private void createActiveRuleParam(ActiveRuleDto activeRule, String key, String value, UserSession userSession) {
SqlSession session = myBatis.openSession();
try {
RuleParamDto ruleParam = findRuleParamNotNull(activeRule.getRulId(), key, session);
}
}
- public void deleteActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, UserSession userSession) {
- validatePermission(userSession);
-
+ private void deleteActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, UserSession userSession) {
SqlSession session = myBatis.openSession();
try {
activeRuleDao.deleteParameter(activeRuleParam.getId(), session);
}
}
- public void updateActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, String value, UserSession userSession) {
- validatePermission(userSession);
-
+ private void updateActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, String value, UserSession userSession) {
SqlSession session = myBatis.openSession();
try {
RuleParamDto ruleParam = findRuleParamNotNull(activeRule.getRulId(), activeRuleParam.getKey(), session);
}
}
- public void revertActiveRule(ActiveRuleDto activeRule, UserSession userSession) {
+ public ProfileRuleChanged revertActiveRule(int profileId, int activeRuleId, UserSession userSession) {
validatePermission(userSession);
+ QProfile profile = findProfileNotNull(profileId);
+ ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId);
+ revertActiveRule(activeRule, userSession);
+ return activeRuleChanged(profile, activeRuleId);
+ }
+
+ private void revertActiveRule(ActiveRuleDto activeRule, UserSession userSession) {
if (activeRule.doesOverride()) {
SqlSession session = myBatis.openSession();
try {
return ruleParam;
}
+ private QProfile findProfileNotNull(int profileId) {
+ QProfile profile = profileLookup.profile(profileId);
+ QProfileValidations.checkProfileIsNotNull(profile);
+ return profile;
+ }
+
+ private QProfileRule findRuleNotNull(int ruleId) {
+ QProfileRule rule = rulesLookup.findByRuleId(ruleId);
+ QProfileValidations.checkRuleIsNotNull(rule);
+ return rule;
+ }
+
+ @CheckForNull
+ private ActiveRuleDto findActiveRule(int profileId, int ruleId) {
+ return activeRuleDao.selectByProfileAndRule(profileId, ruleId);
+ }
+
+ private ActiveRuleDto findActiveRuleNotNull(int profileId, int ruleId) {
+ ActiveRuleDto activeRuleDto = findActiveRule(profileId, ruleId);
+ if (activeRuleDto == null) {
+ throw new BadRequestException("No rule has been activated on this profile.");
+ }
+ return activeRuleDto;
+ }
+
+ private ActiveRuleDto findActiveRuleNotNull(int activeRuleId) {
+ ActiveRuleDto activeRule = activeRuleDao.selectById(activeRuleId);
+ if (activeRule == null) {
+ throw new NotFoundException("This active rule does not exists.");
+ }
+ return activeRule;
+ }
+
+ @CheckForNull
+ private ActiveRuleParamDto findActiveRuleParam(int activeRuleId, String key) {
+ return activeRuleDao.selectParamByActiveRuleAndKey(activeRuleId, key);
+ }
+
private static String getSeverityFromOrdinal(int ordinal) {
return Severity.ALL.get(ordinal);
}
return Severity.ALL.indexOf(severity);
}
+ private ProfileRuleChanged activeRuleChanged(QProfile profile, int activeRuleId) {
+ return new ProfileRuleChanged(profile, profileLookup.parent(profile), rulesLookup.findByActiveRuleId(activeRuleId));
+ }
+
+ private ProfileRuleChanged ruleChanged(QProfile profile, int ruleId) {
+ return new ProfileRuleChanged(profile, profileLookup.parent(profile), rulesLookup.findByRuleId(ruleId));
+ }
+
public static class ProfileRuleChanged {
private QProfile profile;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.server.exceptions.NotFoundException;
import javax.annotation.CheckForNull;
return dao.selectByNameAndLanguage(name, language);
}
- private QualityProfileDto checkNotNull(QualityProfileDto qualityProfile) {
- if (qualityProfile == null) {
- throw new NotFoundException("This quality profile does not exists.");
- }
- return qualityProfile;
- }
-
}
import org.sonar.core.qualityprofile.db.*;
import org.sonar.server.configuration.ProfilesManager;
import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.rule.RuleRegistry;
import org.sonar.server.user.UserSession;
private QProfile findNotNull(int profileId) {
QProfile profile = profileLookup.profile(profileId);
- if (profile == null) {
- throw new NotFoundException("This quality profile does not exists.");
- }
+ QProfileValidations.checkProfileIsNotNull(profile);
return profile;
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.qualityprofile;
+
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
+import org.sonar.core.rule.RuleDto;
+import org.sonar.server.exceptions.NotFoundException;
+
+public class QProfileValidations {
+
+ public static void checkProfileIsNotNull(QProfile profile) {
+ if (profile == null) {
+ throw new NotFoundException("This quality profile does not exists.");
+ }
+ }
+
+ public static void checkProfileIsNotNull(QualityProfileDto profile) {
+ if (profile == null) {
+ throw new NotFoundException("This quality profile does not exists.");
+ }
+ }
+
+ public static void checkRuleIsNotNull(QProfileRule rule) {
+ if (rule == null) {
+ throw new NotFoundException("This rule does not exists.");
+ }
+ }
+
+ public static void checkRuleIsNotNull(RuleDto rule) {
+ if (rule == null) {
+ throw new NotFoundException("This rule does not exists.");
+ }
+ }
+}
import org.sonar.api.ServerComponent;
import org.sonar.api.component.Component;
import org.sonar.core.component.ComponentDto;
-import org.sonar.core.qualityprofile.db.*;
+import org.sonar.core.qualityprofile.db.ActiveRuleDao;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.QualityProfileDao;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.core.resource.ResourceDao;
import org.sonar.core.rule.RuleDao;
import org.sonar.core.rule.RuleDto;
return rules.countProfileRules(ProfileRuleQuery.create(profile.id()).setInheritance(QProfileRule.OVERRIDES));
}
- public ProfileRuleChanged activateRule(int profileId, int ruleId, String severity) {
- QualityProfileDto qualityProfile = findNotNull(profileId);
- RuleDto rule = findRuleNotNull(ruleId);
- ActiveRuleDto activeRule = findActiveRule(qualityProfile, rule);
- if (activeRule == null) {
- activeRule = activeRuleOperations.createActiveRule(qualityProfile, rule, severity, UserSession.get());
- } else {
- activeRuleOperations.updateSeverity(activeRule, severity, UserSession.get());
- }
- return activeRuleChanged(qualityProfile, activeRule);
+ public QProfileActiveRuleOperations.ProfileRuleChanged activateRule(int profileId, int ruleId, String severity) {
+ return activeRuleOperations.activateRule(profileId, ruleId, severity, UserSession.get());
}
- public ProfileRuleChanged deactivateRule(int profileId, int ruleId) {
- QualityProfileDto qualityProfile = findNotNull(profileId);
- RuleDto rule = findRuleNotNull(ruleId);
- ActiveRuleDto activeRule = findActiveRuleNotNull(qualityProfile, rule);
- activeRuleOperations.deactivateRule(activeRule, UserSession.get());
- return activeRuleChanged(qualityProfile, rule);
+ public QProfileActiveRuleOperations.ProfileRuleChanged deactivateRule(int profileId, int ruleId) {
+ return activeRuleOperations.deactivateRule(profileId, ruleId, UserSession.get());
}
- public ProfileRuleChanged updateActiveRuleParam(int profileId, int activeRuleId, String key, @Nullable String value) {
- String sanitizedValue = Strings.emptyToNull(value);
- QualityProfileDto qualityProfile = findNotNull(profileId);
- ActiveRuleParamDto activeRuleParam = findActiveRuleParam(activeRuleId, key);
- ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId);
- UserSession userSession = UserSession.get();
- if (activeRuleParam == null && sanitizedValue != null) {
- activeRuleOperations.createActiveRuleParam(activeRule, key, value, userSession);
- } else if (activeRuleParam != null && sanitizedValue == null) {
- activeRuleOperations.deleteActiveRuleParam(activeRule, activeRuleParam, userSession);
- } else if (activeRuleParam != null) {
- activeRuleOperations.updateActiveRuleParam(activeRule, activeRuleParam, value, userSession);
- }
- // If no active rule param and no value -> do nothing
-
- return activeRuleChanged(qualityProfile, activeRule);
+ public QProfileActiveRuleOperations.ProfileRuleChanged updateActiveRuleParam(int profileId, int activeRuleId, String key, @Nullable String value) {
+ return activeRuleOperations.updateActiveRuleParam(profileId, activeRuleId, key, value, UserSession.get());
}
- public ProfileRuleChanged revertActiveRule(int profileId, int activeRuleId) {
- QualityProfileDto qualityProfile = findNotNull(profileId);
- ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId);
- activeRuleOperations.revertActiveRule(activeRule, UserSession.get());
- return activeRuleChanged(qualityProfile, activeRule);
+ public QProfileActiveRuleOperations.ProfileRuleChanged revertActiveRule(int profileId, int activeRuleId) {
+ return activeRuleOperations.revertActiveRule(profileId, activeRuleId, UserSession.get());
}
public QProfileRule updateActiveRuleNote(int activeRuleId, String note) {
private QualityProfileDto findNotNull(int id) {
QualityProfileDto qualityProfile = findQualityProfile(id);
- return checkNotNull(qualityProfile);
- }
-
- private QualityProfileDto findNotNull(String name, String language) {
- QualityProfileDto qualityProfile = findQualityProfile(name, language);
- return checkNotNull(qualityProfile);
- }
-
- private QualityProfileDto checkNotNull(QualityProfileDto qualityProfile) {
- if (qualityProfile == null) {
- throw new NotFoundException("This quality profile does not exists.");
- }
+ QProfileValidations.checkProfileIsNotNull(qualityProfile);
return qualityProfile;
}
private RuleDto findRuleNotNull(int ruleId) {
RuleDto rule = ruleDao.selectById(ruleId);
- if (rule == null) {
- throw new NotFoundException("This rule does not exists.");
- }
+ QProfileValidations.checkRuleIsNotNull(rule);
return rule;
}
return activeRule;
}
- private ActiveRuleDto findActiveRuleNotNull(QualityProfileDto qualityProfile, RuleDto rule) {
- ActiveRuleDto activeRuleDto = findActiveRule(qualityProfile, rule);
- if (activeRuleDto == null) {
- throw new BadRequestException("No rule has been activated on this profile.");
- }
- return activeRuleDto;
- }
-
- @CheckForNull
- private ActiveRuleDto findActiveRule(QualityProfileDto qualityProfile, RuleDto rule) {
- return activeRuleDao.selectByProfileAndRule(qualityProfile.getId(), rule.getId());
- }
-
- @CheckForNull
- private ActiveRuleParamDto findActiveRuleParam(int activeRuleId, String key) {
- return activeRuleDao.selectParamByActiveRuleAndKey(activeRuleId, key);
- }
-
- @CheckForNull
- private QProfile findParent(QProfile profile) {
- QualityProfileDto parent = findQualityProfile(profile.parent(), profile.language());
- if (parent != null) {
- return QProfile.from(parent);
- }
- return null;
- }
-
- private ProfileRuleChanged activeRuleChanged(QualityProfileDto qualityProfile, ActiveRuleDto activeRule) {
- QProfile profile = QProfile.from(qualityProfile);
- return new ProfileRuleChanged(profile, findParent(profile), rules.findByActiveRuleId(activeRule.getId()));
- }
-
- private ProfileRuleChanged activeRuleChanged(QualityProfileDto qualityProfile, RuleDto rule) {
- QProfile profile = QProfile.from(qualityProfile);
- return new ProfileRuleChanged(profile, findParent(profile), rules.findByRuleId(rule.getId()));
- }
-
- public static class ProfileRuleChanged {
-
- private QProfile profile;
- private QProfile parentProfile;
- private QProfileRule rule;
-
- public ProfileRuleChanged(QProfile profile, @Nullable QProfile parentProfile, QProfileRule rule) {
- this.profile = profile;
- this.parentProfile = parentProfile;
- this.rule = rule;
- }
-
- public QProfile profile() {
- return profile;
- }
-
- public QProfile parentProfile() {
- return parentProfile;
- }
-
- public QProfileRule rule() {
- return rule;
- }
- }
-
}
}
@CheckForNull
- public QProfileRule findByRuleId(Integer ruleId) {
+ public QProfileRule findByRuleId(int ruleId) {
Map<String, Object> ruleSource = index.client().prepareGet(INDEX_RULES, TYPE_RULE, Integer.toString(ruleId))
.execute().actionGet().getSourceAsMap();
if (ruleSource != null) {
import org.mockito.stubbing.Answer;
import org.sonar.api.PropertyType;
import org.sonar.api.rule.Severity;
-import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
import org.sonar.core.qualityprofile.db.ActiveRuleDao;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
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;
@Test
public void fail_to_activate_rule_without_profile_admin_permission() throws Exception {
- QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java");
- RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle");
+ 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.createActiveRule(qualityProfile, rule, Severity.CRITICAL, unauthorizedUserSession);
+ operations.activateRule(1, 10, Severity.CRITICAL, unauthorizedUserSession);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(ForbiddenException.class);
@Test
public void activate_rule() throws Exception {
- QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java");
- RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle");
+ 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(ruleDao.selectParameters(eq(10), eq(session))).thenReturn(newArrayList(new RuleParamDto().setId(20).setName("max").setDefaultValue("10")));
final int idActiveRuleToUpdate = 42;
final int idActiveRuleToDelete = 24;
.addToDelete(idActiveRuleToDelete);
when(profilesManager.activated(eq(1), anyInt(), eq("Nicolas"))).thenReturn(inheritanceActions);
- ActiveRuleDto result = operations.createActiveRule(qualityProfile, rule, Severity.CRITICAL, authorizedUserSession);
- assertThat(result).isNotNull();
+ operations.activateRule(1, 10, Severity.CRITICAL, authorizedUserSession);
ArgumentCaptor<ActiveRuleDto> activeRuleArgument = ArgumentCaptor.forClass(ActiveRuleDto.class);
verify(activeRuleDao).insert(activeRuleArgument.capture(), eq(session));
@Test
public void update_severity() throws Exception {
- Rule rule = Rule.create().setRepositoryKey("squid").setKey("AvoidCycle");
- rule.setId(10);
+ 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);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
+ when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule);
+
when(profilesManager.ruleSeverityChanged(eq(1), eq(5), eq(RulePriority.MINOR), eq(RulePriority.MAJOR), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions());
- operations.updateSeverity(activeRule, Severity.MAJOR, authorizedUserSession);
+ operations.activateRule(1, 10, Severity.MAJOR, authorizedUserSession);
verify(activeRuleDao).update(eq(activeRule), eq(session));
verify(session).commit();
@Test
public void fail_to_update_severity_on_invalid_severity() throws Exception {
- Rule rule = Rule.create().setRepositoryKey("squid").setKey("AvoidCycle");
- rule.setId(10);
+ 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);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
+ when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule);
try {
- operations.updateSeverity(activeRule, "Unknown", authorizedUserSession);
+ operations.activateRule(1, 10, "Unknown", authorizedUserSession);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(BadRequestException.class);
@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);
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());
- operations.deactivateRule(activeRule, authorizedUserSession);
+ operations.deactivateRule(1, 10, authorizedUserSession);
verify(activeRuleDao).delete(eq(5), eq(session));
verify(activeRuleDao).deleteParameters(eq(5), eq(session));
@Test
public void create_active_rule_param() 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);
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());
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 ProfilesManager.RuleInheritanceActions());
- operations.createActiveRuleParam(activeRule, "max", "30", authorizedUserSession);
+ operations.updateActiveRuleParam(1, 5, "max", "30", authorizedUserSession);
ArgumentCaptor<ActiveRuleParamDto> argumentCaptor = ArgumentCaptor.forClass(ActiveRuleParamDto.class);
verify(activeRuleDao).insert(argumentCaptor.capture(), eq(session));
@Test
public void fail_to_create_active_rule_if_no_rule_param() 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);
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);
+
try {
- operations.createActiveRuleParam(activeRule, "max", "30", authorizedUserSession);
+ operations.updateActiveRuleParam(1, 5, "max", "30", authorizedUserSession);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
@Test
public void fail_to_create_active_rule_if_type_is_invalid() 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);
+ 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());
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);
+ operations.updateActiveRuleParam(1, 5, "max", "invalid integer", authorizedUserSession);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(BadRequestException.class);
@Test
public void update_active_rule_param() 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);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
- ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20");
-
+ when(activeRuleDao.selectById(5)).thenReturn(activeRule);
RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name());
when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam);
+ ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20");
+ when(activeRuleDao.selectParamByActiveRuleAndKey(5, "max")).thenReturn(activeRuleParam);
when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("max"), eq("20"), eq("30"), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions());
- operations.updateActiveRuleParam(activeRule, activeRuleParam, "30", authorizedUserSession);
+ operations.updateActiveRuleParam(1, 5, "max", "30", authorizedUserSession);
ArgumentCaptor<ActiveRuleParamDto> argumentCaptor = ArgumentCaptor.forClass(ActiveRuleParamDto.class);
verify(activeRuleDao).update(argumentCaptor.capture(), eq(session));
@Test
public void remove_active_rule_param() 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);
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());
+ when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam);
ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20");
+ when(activeRuleDao.selectParamByActiveRuleAndKey(5, "max")).thenReturn(activeRuleParam);
+
when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("max"), eq("20"), eq((String) null), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions());
- operations.deleteActiveRuleParam(activeRule, activeRuleParam, authorizedUserSession);
+ operations.updateActiveRuleParam(1, 5, "max", null, authorizedUserSession);
verify(session).commit();
verify(activeRuleDao).deleteParameter(100, session);
@Test
public void revert_active_rule_with_severity_to_update() throws Exception {
+ QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
+ when(profileLookup.profile(1)).thenReturn(profile);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4);
+ when(activeRuleDao.selectById(5)).thenReturn(activeRule);
ActiveRuleDto parent = new ActiveRuleDto().setId(4).setProfileId(1).setRuleId(10).setSeverity(2);
when(activeRuleDao.selectById(4, session)).thenReturn(parent);
when(profilesManager.ruleSeverityChanged(eq(1), eq(5), eq(RulePriority.MINOR), eq(RulePriority.MAJOR), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions());
- operations.revertActiveRule(activeRule, authorizedUserSession);
+ operations.revertActiveRule(1, 5, authorizedUserSession);
ArgumentCaptor<ActiveRuleDto> argumentCaptor = ArgumentCaptor.forClass(ActiveRuleDto.class);
verify(activeRuleDao, times(2)).update(argumentCaptor.capture(), eq(session));
@Test
public void fail_to_revert_active_rule_if_no_parent() throws Exception {
+ QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
+ when(profileLookup.profile(1)).thenReturn(profile);
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4);
+ when(activeRuleDao.selectById(5)).thenReturn(activeRule);
when(activeRuleDao.selectById(4, session)).thenReturn(null);
when(profilesManager.ruleSeverityChanged(eq(1), eq(5), eq(RulePriority.MINOR), eq(RulePriority.MAJOR), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions());
try {
- operations.revertActiveRule(activeRule, authorizedUserSession);
+ operations.revertActiveRule(1, 5, authorizedUserSession);
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class);
}
@Test
public void revert_active_rule_with_param_to_update() throws Exception {
+ QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
+ when(profileLookup.profile(1)).thenReturn(profile);
+
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4);
+ when(activeRuleDao.selectById(5)).thenReturn(activeRule);
when(activeRuleDao.selectParamsByActiveRuleId(eq(5), eq(session))).thenReturn(newArrayList(
new ActiveRuleParamDto().setId(102).setActiveRuleId(5).setKey("max").setValue("20")
));
when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("max"), eq("20"), eq("15"), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions());
- operations.revertActiveRule(activeRule, authorizedUserSession);
+ operations.revertActiveRule(1, 5, authorizedUserSession);
ArgumentCaptor<ActiveRuleDto> argumentCaptor = ArgumentCaptor.forClass(ActiveRuleDto.class);
verify(activeRuleDao).update(argumentCaptor.capture(), eq(session));
@Test
public void revert_active_rule_with_param_to_delete() throws Exception {
+ QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
+ when(profileLookup.profile(1)).thenReturn(profile);
+
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4);
+ when(activeRuleDao.selectById(5)).thenReturn(activeRule);
when(activeRuleDao.selectParamsByActiveRuleId(eq(5), eq(session))).thenReturn(newArrayList(
new ActiveRuleParamDto().setId(103).setActiveRuleId(5).setKey("format").setValue("abc"))
);
when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("format"), eq("abc"), eq((String) null), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions());
- operations.revertActiveRule(activeRule, authorizedUserSession);
+ operations.revertActiveRule(1, 5, authorizedUserSession);
ArgumentCaptor<ActiveRuleDto> argumentCaptor = ArgumentCaptor.forClass(ActiveRuleDto.class);
verify(activeRuleDao).update(argumentCaptor.capture(), eq(session));
@Test
public void revert_active_rule_with_param_to_create() throws Exception {
+ QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java");
+ when(profileLookup.profile(1)).thenReturn(profile);
+
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4);
+ when(activeRuleDao.selectById(5)).thenReturn(activeRule);
ActiveRuleDto parent = new ActiveRuleDto().setId(4).setProfileId(1).setRuleId(10).setSeverity(1);
when(activeRuleDao.selectById(4, session)).thenReturn(parent);
when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("minimum"), eq((String) null), eq("2"), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions());
- operations.revertActiveRule(activeRule, authorizedUserSession);
+ operations.revertActiveRule(1, 5, authorizedUserSession);
ArgumentCaptor<ActiveRuleDto> argumentCaptor = ArgumentCaptor.forClass(ActiveRuleDto.class);
verify(activeRuleDao).update(argumentCaptor.capture(), eq(session));
@Test
public void no_revert_when_active_rule_do_not_override() throws Exception {
ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(null);
+ when(activeRuleDao.selectById(5)).thenReturn(activeRule);
- operations.revertActiveRule(activeRule, authorizedUserSession);
+ when(activeRuleDao.selectById(5)).thenReturn(activeRule);
verifyZeroInteractions(activeRuleDao);
verifyZeroInteractions(session);
import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.rule.Severity;
import org.sonar.core.component.ComponentDto;
-import org.sonar.core.qualityprofile.db.*;
+import org.sonar.core.qualityprofile.db.ActiveRuleDao;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.QualityProfileDao;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.core.resource.ResourceDao;
import org.sonar.core.rule.RuleDao;
import org.sonar.core.rule.RuleDto;
@Test
public void activate_rule() throws Exception {
- QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java");
- when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile);
- RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle");
- when(ruleDao.selectById(10)).thenReturn(rule);
-
- when(activeRuleOperations.createActiveRule(eq(qualityProfile), eq(rule), eq(Severity.BLOCKER), any(UserSession.class)))
- .thenReturn(new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1));
-
qProfiles.activateRule(1, 10, Severity.BLOCKER);
-
- verify(activeRuleOperations).createActiveRule(eq(qualityProfile), eq(rule), eq(Severity.BLOCKER), any(UserSession.class));
- }
-
- @Test
- public void activate_rule_with_parent_profile() throws Exception {
- QualityProfileDto parentProfile = new QualityProfileDto().setId(2).setName("Parent").setLanguage("java");
- when(qualityProfileDao.selectByNameAndLanguage("Parent", "java")).thenReturn(parentProfile);
- QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java").setParent("Parent");
- when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile);
- RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle");
- when(ruleDao.selectById(10)).thenReturn(rule);
-
- when(activeRuleOperations.createActiveRule(eq(qualityProfile), eq(rule), eq(Severity.BLOCKER), any(UserSession.class)))
- .thenReturn(new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1));
-
- QProfiles.ProfileRuleChanged result = qProfiles.activateRule(1, 10, Severity.BLOCKER);
-
- assertThat(result.parentProfile()).isNotNull();
- assertThat(result.parentProfile().id()).isEqualTo(2);
- }
-
- @Test
- public void fail_to_activate_rule_if_rule_not_found() throws Exception {
- QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java");
- when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile);
- QProfileRule rule = mock(QProfileRule.class);
- when(rule.id()).thenReturn(10);
-
- try {
- qProfiles.activateRule(1, 10, Severity.BLOCKER);
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(NotFoundException.class);
- }
- verifyZeroInteractions(service);
- }
-
- @Test
- public void update_severity() throws Exception {
- QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java");
- when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile);
-
- ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
- when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule);
-
- RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle");
- when(ruleDao.selectById(10)).thenReturn(rule);
-
- qProfiles.activateRule(1, 10, Severity.BLOCKER);
-
- verify(activeRuleOperations).updateSeverity(eq(activeRule), eq(Severity.BLOCKER), any(UserSession.class));
+ verify(activeRuleOperations).activateRule(eq(1), eq(10), eq(Severity.BLOCKER), any(UserSession.class));
}
@Test
public void deactivate_rule() throws Exception {
- QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java");
- when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile);
- RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle");
- when(ruleDao.selectById(10)).thenReturn(rule);
- ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1);
- when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule);
-
qProfiles.deactivateRule(1, 10);
-
- verify(activeRuleOperations).deactivateRule(eq(activeRule), any(UserSession.class));
- }
-
- @Test
- public void fail_to_deactivate_rule_if_no_active_rule_on_profile() throws Exception {
- QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java");
- when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile);
- RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle");
- when(ruleDao.selectById(10)).thenReturn(rule);
- when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(null);
-
- try {
- qProfiles.deactivateRule(1, 10);
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(BadRequestException.class);
- }
+ verify(activeRuleOperations).deactivateRule(eq(1), eq(10), any(UserSession.class));
}
@Test
public void update_active_rule_param() throws Exception {
- when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
-
- ActiveRuleDto activeRule = new ActiveRuleDto().setId(50);
- when(activeRuleDao.selectById(50)).thenReturn(activeRule);
-
- ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20");
- when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(activeRuleParam);
-
qProfiles.updateActiveRuleParam(1, 50, "max", "20");
-
- verify(activeRuleOperations).updateActiveRuleParam(eq(activeRule), eq(activeRuleParam), eq("20"), any(UserSession.class));
- }
-
- @Test
- public void fail_to_update_active_rule_param_if_active_rule_not_found() throws Exception {
- when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
- ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20");
- when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(activeRuleParam);
-
- when(activeRuleDao.selectById(50)).thenReturn(null);
-
- try {
- qProfiles.updateActiveRuleParam(1, 50, "max", "20");
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(NotFoundException.class);
- }
- verifyZeroInteractions(service);
- }
-
- @Test
- public void create_active_rule_param() throws Exception {
- when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
- ActiveRuleDto activeRule = new ActiveRuleDto().setId(50);
- when(activeRuleDao.selectById(50)).thenReturn(activeRule);
- when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(null);
-
- qProfiles.updateActiveRuleParam(1, 50, "max", "20");
-
- verify(activeRuleOperations).createActiveRuleParam(eq(activeRule), eq("max"), eq("20"), any(UserSession.class));
- }
-
- @Test
- public void delete_active_rule_param() throws Exception {
- when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
- ActiveRuleDto activeRule = new ActiveRuleDto().setId(50);
- when(activeRuleDao.selectById(50)).thenReturn(activeRule);
-
- ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20");
- when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(activeRuleParam);
-
- qProfiles.updateActiveRuleParam(1, 50, "max", "");
-
- verify(activeRuleOperations).deleteActiveRuleParam(eq(activeRule), eq(activeRuleParam), any(UserSession.class));
- }
-
- @Test
- public void do_nothing_when_updating_active_rule_param_with_no_param_and_empty_value() throws Exception {
- when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
- ActiveRuleDto activeRule = new ActiveRuleDto().setId(50);
- when(activeRuleDao.selectById(50)).thenReturn(activeRule);
- when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(null);
-
- qProfiles.updateActiveRuleParam(1, 50, "max", "");
-
- verifyZeroInteractions(service);
+ verify(activeRuleOperations).updateActiveRuleParam(eq(1), eq(50), eq("max"), eq("20"), any(UserSession.class));
}
@Test
public void revert_active_rule() throws Exception {
- when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
- ActiveRuleDto activeRule = new ActiveRuleDto().setId(50);
- when(activeRuleDao.selectById(50)).thenReturn(activeRule);
-
qProfiles.revertActiveRule(1, 50);
-
- verify(activeRuleOperations).revertActiveRule(eq(activeRule), any(UserSession.class));
+ verify(activeRuleOperations).revertActiveRule(eq(1), eq(50), any(UserSession.class));
}
@Test
when(ruleOperations.createRule(eq(rule), eq("Rule name"), eq(Severity.MAJOR), eq("My note"), eq(paramsByKey), any(UserSession.class))).thenReturn(newRule);
try {
- qProfiles.createRule( 10, "", "", "", paramsByKey);
+ qProfiles.createRule(10, "", "", "", paramsByKey);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(BadRequestException.class);