import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.sonar.api.rules.ActiveRuleParam;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.ValidationMessages;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleParamDto;
-import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
@ServerSide
public class QProfileReset {
private final DbClient db;
private final QProfileFactory factory;
private final RuleActivator activator;
+ private final ActiveRuleIndexer activeRuleIndexer;
private final BuiltInProfiles builtInProfiles;
private final ProfileDefinition[] definitions;
- public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory, ProfileDefinition[] definitions) {
+ public QProfileReset(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, BuiltInProfiles builtInProfiles, QProfileFactory factory,
+ ProfileDefinition[] definitions) {
this.db = db;
this.activator = activator;
+ this.activeRuleIndexer = activeRuleIndexer;
this.builtInProfiles = builtInProfiles;
this.factory = factory;
this.definitions = definitions;
}
- public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory) {
- this(db, activator, builtInProfiles, factory, new ProfileDefinition[0]);
+ public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory, ActiveRuleIndexer activeRuleIndexer) {
+ this(db, activator, activeRuleIndexer, builtInProfiles, factory, new ProfileDefinition[0]);
}
public Collection<String> builtInProfileNamesForLanguage(String language) {
activation.setParameter(param.getParamKey(), param.getValue());
}
} else {
- for (RuleParamDto param : db.deprecatedRuleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
+ for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
activation.setParameter(param.getName(), param.getDefaultValue());
}
}
}
}
doReset(dbSession, profile, activations);
- dbSession.commit();
}
} finally {
dbSession.close();
try {
QualityProfileDto profile = factory.getOrCreate(dbSession, profileName);
BulkChangeResult result = doReset(dbSession, profile, activations);
- dbSession.commit();
return result;
} finally {
dbSession.close();
}
}
+ List<ActiveRuleChange> changes = new ArrayList<>();
+ changes.addAll(result.getChanges());
for (RuleKey ruleKey : ruleToBeDeactivated) {
try {
- activator.deactivate(dbSession, ActiveRuleKey.of(profile.getKee(), ruleKey));
+ changes.addAll(activator.deactivate(dbSession, ActiveRuleKey.of(profile.getKee(), ruleKey)));
} catch (BadRequestException e) {
// ignore, probably a rule inherited from parent that can't be deactivated
}
}
+ dbSession.commit();
+ activeRuleIndexer.index(changes);
return result;
}
*/
package org.sonar.server.qualityprofile;
-import com.google.common.collect.ImmutableMap;
+import java.util.List;
+import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.sonar.api.profiles.ProfileDefinition;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleParam;
import org.sonar.api.rules.RulePriority;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.ValidationMessages;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.ActiveRuleDao;
+import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey;
+import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QualityProfileDao;
import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.db.DbClient;
import org.sonar.server.platform.Platform;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex2;
import org.sonar.server.tester.ServerTester;
-
-import javax.annotation.Nullable;
import org.sonar.server.tester.UserSessionRule;
import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.rule.Severity.BLOCKER;
+import static org.sonar.api.rule.Severity.CRITICAL;
+import static org.sonar.api.rule.Severity.MAJOR;
+import static org.sonar.api.rule.Severity.MINOR;
+// TODO Replace ServerTester by EsTester and DbTester
public class QProfileResetMediumTest {
static final XooRulesDefinition RULE_DEFS = new XooRulesDefinition();
static final XooProfileDefinition PROFILE_DEFS = new XooProfileDefinition();
@ClassRule
- public static ServerTester tester = new ServerTester().addXoo().addComponents(PROFILE_DEFS, RULE_DEFS);
+ public static ServerTester tester = new ServerTester()
+ .withEsIndexes()
+ .addXoo().addComponents(PROFILE_DEFS, RULE_DEFS);
+
@Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
RulesDefinition.NewRule x1 = repository.createRule("x1")
.setName("x1 name")
.setHtmlDescription("x1 desc")
- .setSeverity(Severity.MINOR);
+ .setSeverity(MINOR);
x1.createParam("acceptWhitespace")
.setDefaultValue("false")
.setType(RuleParamType.BOOLEAN)
// Change the severity and the value of the parameter in the active rule
tester.get(RuleActivator.class).activate(dbSession,
- new RuleActivation(ruleKey).setSeverity(Severity.BLOCKER)
+ new RuleActivation(ruleKey).setSeverity(BLOCKER)
.setParameter("acceptWhitespace", "false"),
profile.getKey()
);
dbSession.commit();
+
// Verify severity and param has changed
- ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
- assertThat(activeRule.severity()).isEqualTo(Severity.BLOCKER);
- assertThat(activeRule.params()).isEqualTo(ImmutableMap.of("acceptWhitespace", "false"));
+ ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
+ assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER);
+ List<ActiveRuleParamDto> activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleKey);
+ assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
+ assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("false");
reset.resetLanguage(ServerTester.Xoo.KEY);
+ dbSession.commit();
// Severity and parameter value come back to origin after reset
- activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
- assertThat(activeRule.severity()).isEqualTo(Severity.CRITICAL);
- assertThat(activeRule.params()).isEqualTo(ImmutableMap.of("acceptWhitespace", "true"));
+ activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
+ assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL);
+ ActiveRule activeRule = tester.get(ActiveRuleIndex2.class).getNullableByKey(activeRuleKey);
+ assertThat(activeRule.severity()).isEqualTo(CRITICAL);
+
+ activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleKey);
+ assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
+ assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("true");
}
@Test
RulesDefinition.NewRule x1 = repository.createRule("x1")
.setName("x1 name")
.setHtmlDescription("x1 desc")
- .setSeverity(Severity.MAJOR);
+ .setSeverity(MAJOR);
x1.createParam("acceptWhitespace")
.setDefaultValue("false")
.setType(RuleParamType.BOOLEAN)
RulesDefinition.NewRule x1 = repository.createRule("x1")
.setName("x1 name")
.setHtmlDescription("x1 desc")
- .setSeverity(Severity.MAJOR);
+ .setSeverity(MAJOR);
x1.createParam("acceptWhitespace")
.setDefaultValue("true")
.setType(RuleParamType.BOOLEAN)
reset.resetLanguage(ServerTester.Xoo.KEY);
// Parameter value come back to origin after reset
- ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
- assertThat(activeRule.params()).isEqualTo(ImmutableMap.of("acceptWhitespace", "true"));
+ List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleKey);
+ assertThat(params).hasSize(1);
+ assertThat(params.get(0).getKey()).isEqualTo("acceptWhitespace");
+ assertThat(params.get(0).getValue()).isEqualTo("true");
}
interface Rules {