import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.persistence.Transient;
-import java.util.Date;
public class ActiveRuleDto extends Dto<ActiveRuleKey> {
private Integer ruleId;
private Integer severity;
private String inheritance;
- private Date noteCreatedAt;
- private Date noteUpdatedAt;
- private String noteUserLogin;
- private String noteData;
@Deprecated
public ActiveRuleDto setKey(ActiveRuleKey key) {
}
public ActiveRuleKey getKey() {
- return ActiveRuleKey.of(QualityProfileKey.of(this.profile, this.language),
- RuleKey.of(this.repository, this.rule));
+ return ActiveRuleKey.of(QualityProfileKey.of(this.profile, this.language),
+ RuleKey.of(this.repository, this.rule));
}
// This field do not exists in db, it's only retrieve by joins
return this;
}
- @CheckForNull
- public Date getNoteCreatedAt() {
- return noteCreatedAt;
- }
-
- public ActiveRuleDto setNoteCreatedAt(@Nullable Date noteCreatedAt) {
- this.noteCreatedAt = noteCreatedAt;
- return this;
- }
-
- @CheckForNull
- public Date getNoteUpdatedAt() {
- return noteUpdatedAt;
- }
-
- public ActiveRuleDto setNoteUpdatedAt(@Nullable Date noteUpdatedAt) {
- this.noteUpdatedAt = noteUpdatedAt;
- return this;
- }
-
- @CheckForNull
- public String getNoteUserLogin() {
- return noteUserLogin;
- }
-
- public ActiveRuleDto setNoteUserLogin(@Nullable String noteUserLogin) {
- this.noteUserLogin = noteUserLogin;
- return this;
- }
-
- @CheckForNull
- public String getNoteData() {
- return noteData;
- }
-
- public ActiveRuleDto setNoteData(@Nullable String noteData) {
- this.noteData = noteData;
- return this;
- }
-
-
@CheckForNull
public Integer getParentId() {
return parentId;
private final MyBatis myBatis;
private final Map<Class<?>, DaoComponent> daoComponents;
-
public DbClient(Database db, MyBatis myBatis, DaoComponent... daoComponents) {
this.db = db;
this.myBatis = myBatis;
// TODO apply changes to children
- persist(changes, dbSession);
+ persist(changes, context, dbSession);
dbSession.commit();
// TODO filter changes without any differences
}
}
- private void persist(Collection<ActiveRuleChange> changes, DbSession dbSession) {
+ private void persist(Collection<ActiveRuleChange> changes, RuleActivationContext context, DbSession dbSession) {
for (ActiveRuleChange change : changes) {
if (change.getType() == ActiveRuleChange.Type.ACTIVATED) {
- ActiveRuleDto activeRule = ActiveRuleDto.createFor(null, null /* TODO */)
- .setKey(change.getKey())
+ ActiveRuleDto activeRule = ActiveRuleDto.createFor(context.profile(), context.rule())
.setSeverity(change.getSeverity());
db.activeRuleDao().insert(activeRule, dbSession);
db.activeRuleDao().deleteByKey(change.getKey(), dbSession);
} else if (change.getType() == ActiveRuleChange.Type.UPDATED) {
-
+ ActiveRuleDto activeRule = context.activeRule();
+ activeRule.setSeverity(change.getSeverity());
+ db.activeRuleDao().update(activeRule, dbSession);
+ // TODO insert activeruelparams
}
}
}
}
}
- public void updateActiveRuleNote(int activeRuleId, String note, UserSession userSession) {
- validatePermission(userSession);
- DbSession session = myBatis.openSession(false);
-
- try {
- ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId, session);
- String sanitizedNote = Strings.emptyToNull(note);
- if (sanitizedNote != null) {
- Date now = new Date(system.now());
- if (activeRule.getNoteData() == null) {
- activeRule.setNoteCreatedAt(now);
- activeRule.setNoteUserLogin(userSession.login());
- }
- activeRule.setNoteUpdatedAt(now);
- activeRule.setNoteData(note);
- activeRuleDao.update(activeRule, session);
- session.commit();
-
- reindexActiveRule(activeRule, session);
- }
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public void deleteActiveRuleNote(int activeRuleId, UserSession userSession) {
- validatePermission(userSession);
-
- DbSession session = myBatis.openSession(false);
- try {
- ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId, session);
-
- activeRule.setNoteData(null);
- activeRule.setNoteUserLogin(null);
- activeRule.setNoteCreatedAt(null);
- activeRule.setNoteUpdatedAt(null);
- activeRuleDao.update(activeRule, session);
- session.commit();
-
- reindexActiveRule(activeRule, session);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
private void notifyParamsDeleted(ActiveRuleDto activeRule, List<ActiveRuleParamDto> params, SqlSession session, UserSession userSession) {
ProfilesManager.RuleInheritanceActions actions = new ProfilesManager.RuleInheritanceActions();
for (ActiveRuleParamDto activeRuleParam : params) {
}
private void defineActiveRuleKeyParameters(WebService.NewAction action) {
- action.createParam("profile_lang")
- .setDescription("Profile language")
+ action.createParam("profile_key")
+ .setDescription("Key of Quality profile")
.setRequired(true)
- .setExampleValue("java");
-
- action.createParam("profile_name")
- .setDescription("Profile name")
- .setRequired(true)
- .setExampleValue("My profile");
-
- action.createParam("rule_repo")
- .setDescription("Rule repository")
- .setRequired(true)
- .setExampleValue("squid");
+ .setExampleValue("Sonar way:java");
action.createParam("rule_key")
- .setDescription("Rule key")
+ .setDescription("Key of the rule to activate")
.setRequired(true)
- .setExampleValue("AvoidCycles");
+ .setExampleValue("squid:AvoidCycles");
}
private void activate(Request request, Response response) throws Exception {
private ActiveRuleKey readKey(Request request) {
return ActiveRuleKey.of(
- QualityProfileKey.of(request.mandatoryParam("profile_name"), request.mandatoryParam("profile_lang")),
- RuleKey.of(request.mandatoryParam("rule_repo"), request.mandatoryParam("rule_key")));
+ QualityProfileKey.parse(request.mandatoryParam("profile_key")),
+ RuleKey.parse(request.mandatoryParam("rule_key")));
}
}
*/
package org.sonar.server.qualityprofile;
-import org.junit.ClassRule;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+import org.sonar.api.server.rule.RuleParamType;
+import org.sonar.api.server.rule.RulesDefinition;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleKey;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
+import org.sonar.core.qualityprofile.db.QualityProfileKey;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.tester.ServerTester;
+import org.sonar.server.user.MockUserSession;
+
+import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
public class ActiveRuleServiceMediumTest {
- @ClassRule
- public static ServerTester tester = new ServerTester();
+ @Rule
+ public ServerTester tester = new ServerTester().addComponents(XooRulesDefinition.class);
+
+ DbClient dbClient;
+ DbSession dbSession;
+ ActiveRuleService service;
+
+ @Before
+ public void before() {
+ dbClient = tester.get(DbClient.class);
+ dbSession = dbClient.openSession(false);
+ service = tester.get(ActiveRuleService.class);
+
+ // create quality profile
+ dbClient.qualityProfileDao().insert(QualityProfileDto.createFor("MyProfile", "xoo"), dbSession);
+ dbSession.commit();
+ }
@Test
public void activate() throws Exception {
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("marius");
+ QualityProfileKey profileKey = QualityProfileKey.of("MyProfile", "xoo");
+ RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1")));
+ activation.setSeverity(Severity.BLOCKER);
+ service.activate(activation);
+
+ // verify db
+ List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().findByProfileKey(profileKey, dbSession);
+ assertThat(activeRuleDtos).hasSize(1);
+ assertThat(activeRuleDtos.get(0).getSeverityString()).isEqualTo(Severity.BLOCKER);
+ assertThat(activeRuleDtos.get(0).getInheritance()).isNull();
+
+ // verify es
+ ActiveRuleIndex index = tester.get(ActiveRuleIndex.class);
+ index.refresh();
+ ActiveRule activeRule = index.getByKey(activation.getKey());
+ assertThat(activeRule).isNotNull();
+ assertThat(activeRule.severity()).isEqualTo(Severity.BLOCKER);
+ assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+ }
+
+ @Test
+ public void activate_with_default_severity() throws Exception {
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("marius");
+ QualityProfileKey profileKey = QualityProfileKey.of("MyProfile", "xoo");
+ RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1")));
+ service.activate(activation);
+
+ // verify db
+ List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().findByProfileKey(profileKey, dbSession);
+ assertThat(activeRuleDtos).hasSize(1);
+ assertThat(activeRuleDtos.get(0).getSeverityString()).isEqualTo(Severity.MINOR);
+ assertThat(activeRuleDtos.get(0).getInheritance()).isNull();
+
+ // verify es
+ ActiveRuleIndex index = tester.get(ActiveRuleIndex.class);
+ index.refresh();
+ ActiveRule activeRule = index.getByKey(activation.getKey());
+ assertThat(activeRule).isNotNull();
+ assertThat(activeRule.severity()).isEqualTo(Severity.MINOR);
+ assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+ }
+
+ @Test
+ public void update_activation_severity_and_parameters() throws Exception {
+ // initial activation
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("marius");
+ QualityProfileKey profileKey = QualityProfileKey.of("MyProfile", "xoo");
+ RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1")));
+ activation.setSeverity(Severity.BLOCKER);
+ service.activate(activation);
+
+ // update
+ RuleActivation update = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1")));
+ update.setSeverity(Severity.CRITICAL);
+ service.activate(update);
+
+ // verify db
+ List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().findByProfileKey(profileKey, dbSession);
+ assertThat(activeRuleDtos).hasSize(1);
+ assertThat(activeRuleDtos.get(0).getSeverityString()).isEqualTo(Severity.CRITICAL);
+ assertThat(activeRuleDtos.get(0).getInheritance()).isNull();
+
+ // verify es
+ ActiveRuleIndex index = tester.get(ActiveRuleIndex.class);
+ index.refresh();
+ ActiveRule activeRule = index.getByKey(activation.getKey());
+ assertThat(activeRule).isNotNull();
+ assertThat(activeRule.severity()).isEqualTo(Severity.CRITICAL);
+ assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+ }
+ @Test
+ @Ignore
+ public void activate_with_params() throws Exception {
+
+ }
+
+ @Test
+ @Ignore
+ public void activate_with_default_params() throws Exception {
+
+ }
+
+ @Test
+ @Ignore
+ public void fail_to_activate_if_not_granted() throws Exception {
+
+ }
+
+ @Test
+ @Ignore
+ public void fail_to_activate_if_different_languages() throws Exception {
+ // profile and rule have different languages
+ }
+
+ @Test
+ @Ignore
+ public void fail_to_activate_if_invalid_parameter() throws Exception {
+
+ }
+ public static class XooRulesDefinition implements RulesDefinition {
+ @Override
+ public void define(Context context) {
+ NewRepository repository = context.createRepository("xoo", "xoo").setName("Xoo Repo");
+ repository.createRule("x1")
+ .setName("x1 name")
+ .setHtmlDescription("x1 desc")
+ .setSeverity(Severity.MINOR)
+ .createParam("max")
+ .setDefaultValue("10")
+ .setType(RuleParamType.INTEGER)
+ .setDescription("Maximum");
+ repository.done();
+ }
}
}
@Test
public void bulk_index_active_rules_checking_into_db() throws IOException {
- List<ActiveRuleDto> activeRules = newArrayList(new ActiveRuleDto().setId(1).setProfileId(10).setRuleId(1).setSeverity(Severity.MAJOR).setParentId(5)
- .setNoteData("polop").setNoteCreatedAt(new Date()).setNoteUserLogin("godin"));
+ List<ActiveRuleDto> activeRules = newArrayList(new ActiveRuleDto().setId(1).setProfileId(10).setRuleId(1).setSeverity(Severity.MAJOR).setParentId(5));
DbSession session = mock(DbSession.class);
when(myBatis.openSession(false)).thenReturn(session);
@org.junit.Rule
public ServerTester serverTester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
- private DbSession session;
+ DbSession session;
@Before
public void setup(){
@Before
public void before() {
- tester.clearDataStores();
+ tester.clearDbAndEs();
dbSession = myBatis.openSession(false);
}
WebService.Action restoreProfiles = controller.action("activate_rule");
assertThat(restoreProfiles).isNotNull();
assertThat(restoreProfiles.isPost()).isTrue();
- assertThat(restoreProfiles.params()).hasSize(6);
+ assertThat(restoreProfiles.params()).hasSize(4);
}
@Test
WebService.Action restoreProfiles = controller.action("deactivate_rule");
assertThat(restoreProfiles).isNotNull();
assertThat(restoreProfiles.isPost()).isTrue();
- assertThat(restoreProfiles.params()).hasSize(4);
+ assertThat(restoreProfiles.params()).hasSize(2);
}
}
.setType(RuleParamType.BOOLEAN)
.setDescription("Accept whitespaces on the line");
-
-
repository.createRule("x2")
.setName("x2 name")
.setHtmlDescription("x2 desc")
@Before
public void before() {
- tester.clearDataStores();
+ tester.clearDbAndEs();
dbSession = tester.get(MyBatis.class).openSession(false);
}
@Before
public void before() {
- tester.clearDataStores();
+ tester.clearDbAndEs();
dbSession = tester.get(MyBatis.class).openSession(false);
}
@Before
public void before() {
- tester.clearDataStores();
+ tester.clearDbAndEs();
dbSession = myBatis.openSession(false);
}
@Before
public void setUp() throws Exception {
- tester.clearDataStores();
+ tester.clearDbAndEs();
ruleDao = tester.get(RuleDao.class);
ws = tester.get(RulesWebService.class);
wsTester = new WsTester(ws);
/**
* Truncate all db tables and es indices. Can be executed only if ServerTester is started.
*/
- public void clearDataStores() {
+ public void clearDbAndEs() {
checkStarted();
get(DataStoreCleanup.class).clearAll();
}