Browse Source

SONAR-7330 Replace usage of deprecateActiveRuleDao by activeRuleDao

tags/5.5-M6
Julien Lancelot 8 years ago
parent
commit
b7cd51fb8d

+ 1
- 1
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java View File

@@ -116,7 +116,7 @@ public class QProfileFactory {

private void doDelete(DbSession session, QualityProfileDto profile) {
db.qualityProfileDao().deleteAllProjectProfileAssociation(profile.getKey(), session);
db.deprecatedActiveRuleDao().deleteByProfileKey(session, profile.getKey());
db.activeRuleDao().deleteByProfileKey(session, profile.getKey());
db.qualityProfileDao().delete(session, profile);
}


+ 1
- 1
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java View File

@@ -128,7 +128,7 @@ public class QProfileReset {
BulkChangeResult result = new BulkChangeResult(profile);
Set<RuleKey> ruleToBeDeactivated = Sets.newHashSet();
// Keep reference to all the activated rules before backup restore
for (ActiveRuleDto activeRuleDto : db.deprecatedActiveRuleDao().selectByProfileKey(dbSession, profile.getKee())) {
for (ActiveRuleDto activeRuleDto : db.activeRuleDao().selectByProfileKey(dbSession, profile.getKee())) {
if (activeRuleDto.getInheritance() == null) {
// inherited rules can't be deactivated
ruleToBeDeactivated.add(activeRuleDto.getKey().ruleKey());

+ 18
- 17
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java View File

@@ -24,30 +24,28 @@ import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Sets;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.server.ServerSide;
import org.sonar.api.profiles.ProfileDefinition;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Languages;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.ActiveRuleParam;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.ValidationMessages;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.loadedtemplate.LoadedTemplateDto;
import org.sonar.db.DbClient;
import org.sonar.server.platform.PersistentSettings;

import javax.annotation.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;

/**
* Synchronize Quality profiles during server startup
@@ -64,24 +62,26 @@ public class RegisterQualityProfiles {
private final QProfileFactory profileFactory;
private final RuleActivator ruleActivator;
private final Languages languages;
private final ActiveRuleIndexer activeRuleIndexer;

/**
* To be kept when no ProfileDefinition are injected
*/
public RegisterQualityProfiles(PersistentSettings settings, BuiltInProfiles builtInProfiles,
DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator, Languages languages) {
this(settings, builtInProfiles, dbClient, profileFactory, ruleActivator, Collections.<ProfileDefinition>emptyList(), languages);
public RegisterQualityProfiles(BuiltInProfiles builtInProfiles,
DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
this(builtInProfiles, dbClient, profileFactory, ruleActivator, Collections.<ProfileDefinition>emptyList(), languages, activeRuleIndexer);
}

public RegisterQualityProfiles(PersistentSettings settings, BuiltInProfiles builtInProfiles,
public RegisterQualityProfiles(BuiltInProfiles builtInProfiles,
DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator,
List<ProfileDefinition> definitions, Languages languages) {
List<ProfileDefinition> definitions, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
this.builtInProfiles = builtInProfiles;
this.dbClient = dbClient;
this.profileFactory = profileFactory;
this.ruleActivator = ruleActivator;
this.definitions = definitions;
this.languages = languages;
this.activeRuleIndexer = activeRuleIndexer;
}

public void start() {
@@ -95,6 +95,7 @@ public class RegisterQualityProfiles {
registerProfilesForLanguage(session, language, defs);
}
}
activeRuleIndexer.setEnabled(true).index();
profiler.stopDebug();

} finally {

+ 19
- 12
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java View File

@@ -29,7 +29,10 @@ import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.server.ServerSide;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.api.utils.System2;
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;
@@ -37,9 +40,7 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.activity.ActivityService;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.qualityprofile.db.ActiveRuleDao;
import org.sonar.server.rule.Rule;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleNormalizer;
@@ -58,6 +59,7 @@ import static com.google.common.collect.Lists.newArrayList;
@ServerSide
public class RuleActivator {

private final System2 system2;
private final DbClient db;
private final TypeValidations typeValidations;
private final RuleActivatorContextFactory contextFactory;
@@ -65,9 +67,10 @@ public class RuleActivator {
private final ActivityService activityService;
private final UserSession userSession;

public RuleActivator(DbClient db, IndexClient index,
public RuleActivator(System2 system2, DbClient db, IndexClient index,
RuleActivatorContextFactory contextFactory, TypeValidations typeValidations,
ActivityService activityService, UserSession userSession) {
this.system2 = system2;
this.db = db;
this.index = index;
this.contextFactory = contextFactory;
@@ -233,21 +236,21 @@ public class RuleActivator {
ActiveRuleDto activeRule = null;
if (change.getType() == ActiveRuleChange.Type.ACTIVATED) {
activeRule = doInsert(change, context, dbSession);

} else if (change.getType() == ActiveRuleChange.Type.DEACTIVATED) {
ActiveRuleDao dao = db.deprecatedActiveRuleDao();
dao.deleteByKey(dbSession, change.getKey());
ActiveRuleDao dao = db.activeRuleDao();
dao.delete(dbSession, change.getKey());

} else if (change.getType() == ActiveRuleChange.Type.UPDATED) {
activeRule = doUpdate(change, context, dbSession);
}

activityService.save(change.toActivity());
return activeRule;
}

private ActiveRuleDto doInsert(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
ActiveRuleDto activeRule;
ActiveRuleDao dao = db.deprecatedActiveRuleDao();
ActiveRuleDao dao = db.activeRuleDao();
activeRule = ActiveRuleDto.createFor(context.profile(), context.rule());
String severity = change.getSeverity();
if (severity != null) {
@@ -257,6 +260,8 @@ public class RuleActivator {
if (inheritance != null) {
activeRule.setInheritance(inheritance.name());
}
activeRule.setUpdatedAtInMs(system2.now());
activeRule.setCreatedAtInMs(system2.now());
dao.insert(dbSession, activeRule);
for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
if (param.getValue() != null) {
@@ -269,7 +274,7 @@ public class RuleActivator {
}

private ActiveRuleDto doUpdate(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
ActiveRuleDao dao = db.deprecatedActiveRuleDao();
ActiveRuleDao dao = db.activeRuleDao();
ActiveRuleDto activeRule = context.activeRule();
if (activeRule != null) {
String severity = change.getSeverity();
@@ -280,6 +285,7 @@ public class RuleActivator {
if (inheritance != null) {
activeRule.setInheritance(inheritance.name());
}
activeRule.setUpdatedAtInMs(system2.now());
dao.update(dbSession, activeRule);

for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
@@ -331,7 +337,7 @@ public class RuleActivator {
*/
public List<ActiveRuleChange> deactivate(DbSession dbSession, RuleDto ruleDto) {
List<ActiveRuleChange> changes = Lists.newArrayList();
List<ActiveRuleDto> activeRules = db.deprecatedActiveRuleDao().selectByRule(dbSession, ruleDto);
List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByRule(dbSession, ruleDto);
for (ActiveRuleDto activeRule : activeRules) {
changes.addAll(deactivate(dbSession, activeRule.getKey(), true));
}
@@ -478,7 +484,7 @@ public class RuleActivator {
// set new parent
profile.setParentKee(parentKey);
db.qualityProfileDao().update(dbSession, profile);
for (ActiveRuleDto parentActiveRule : db.deprecatedActiveRuleDao().selectByProfileKey(dbSession, parentKey)) {
for (ActiveRuleDto parentActiveRule : db.activeRuleDao().selectByProfileKey(dbSession, parentKey)) {
try {
RuleActivation activation = new RuleActivation(parentActiveRule.getKey().ruleKey());
activate(dbSession, activation, profileKey);
@@ -497,12 +503,13 @@ public class RuleActivator {
if (profileDto.getParentKee() != null) {
profileDto.setParentKee(null);
db.qualityProfileDao().update(dbSession, profileDto);
for (ActiveRuleDto activeRule : db.deprecatedActiveRuleDao().selectByProfileKey(dbSession, profileDto.getKey())) {
for (ActiveRuleDto activeRule : db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey())) {
if (ActiveRuleDto.INHERITED.equals(activeRule.getInheritance())) {
deactivate(dbSession, activeRule.getKey(), true);
} else if (ActiveRuleDto.OVERRIDES.equals(activeRule.getInheritance())) {
activeRule.setInheritance(null);
db.deprecatedActiveRuleDao().update(dbSession, activeRule);
activeRule.setUpdatedAtInMs(system2.now());
db.activeRuleDao().update(dbSession, activeRule);
}
}
}

+ 6
- 5
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivatorContextFactory.java View File

@@ -19,6 +19,7 @@
*/
package org.sonar.server.qualityprofile;

import com.google.common.base.Optional;
import java.util.Collection;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
@@ -86,16 +87,16 @@ public class RuleActivatorContextFactory {

private void initActiveRules(String profileKey, RuleKey ruleKey, RuleActivatorContext context, DbSession session, boolean parent) {
ActiveRuleKey key = ActiveRuleKey.of(profileKey, ruleKey);
ActiveRuleDto activeRule = db.deprecatedActiveRuleDao().getNullableByKey(session, key);
Optional<ActiveRuleDto> activeRule = db.activeRuleDao().selectByKey(session, key);
Collection<ActiveRuleParamDto> activeRuleParams = null;
if (activeRule != null) {
activeRuleParams = db.deprecatedActiveRuleDao().selectParamsByActiveRuleKey(session, key);
if (activeRule.isPresent()) {
activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleKey(session, key);
}
if (parent) {
context.setParentActiveRule(activeRule);
context.setParentActiveRule(activeRule.orNull());
context.setParentActiveRuleParams(activeRuleParams);
} else {
context.setActiveRule(activeRule);
context.setActiveRule(activeRule.orNull());
context.setActiveRuleParams(activeRuleParams);
}
}

+ 4
- 4
server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java View File

@@ -46,13 +46,13 @@ import org.sonar.api.utils.System2;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleDto.Format;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.qualityprofile.RuleActivator;
import org.sonar.server.rule.index.RuleIndexer;

@@ -284,7 +284,7 @@ public class RegisterRules implements Startable {
for (RuleParamDto paramDto : paramDtos) {
RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
if (paramDef == null) {
dbClient.deprecatedActiveRuleDao().deleteParamsByRuleParam(session, rule, paramDto.getName());
dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule, paramDto.getName());
dbClient.ruleDao().deleteRuleParam(session, paramDto.getId());
} else {
if (mergeParam(paramDto, paramDef)) {
@@ -306,9 +306,9 @@ public class RegisterRules implements Startable {
dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
if (!StringUtils.isEmpty(param.defaultValue())) {
// Propagate the default value to existing active rules
for (ActiveRuleDto activeRule : dbClient.deprecatedActiveRuleDao().selectByRule(session, rule)) {
for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRule(session, rule)) {
ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
dbClient.deprecatedActiveRuleDao().insertParam(session, activeRule, activeParam);
dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
}
}
}

+ 5
- 5
server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java View File

@@ -251,9 +251,9 @@ public class RuleUpdater {
// Load active rules and its parameters in cache
Multimap<RuleDto, ActiveRuleDto> activeRules = ArrayListMultimap.create();
Multimap<ActiveRuleDto, ActiveRuleParamDto> activeRuleParams = ArrayListMultimap.create();
for (ActiveRuleDto activeRuleDto : dbClient.deprecatedActiveRuleDao().selectByRule(dbSession, customRule)) {
for (ActiveRuleDto activeRuleDto : dbClient.activeRuleDao().selectByRule(dbSession, customRule)) {
activeRules.put(customRule, activeRuleDto);
for (ActiveRuleParamDto activeRuleParamDto : dbClient.deprecatedActiveRuleDao().selectParamsByActiveRuleKey(dbSession, activeRuleDto.getKey())) {
for (ActiveRuleParamDto activeRuleParamDto : dbClient.activeRuleDao().selectParamsByActiveRuleKey(dbSession, activeRuleDto.getKey())) {
activeRuleParams.put(activeRuleDto, activeRuleParamDto);
}
}
@@ -278,9 +278,9 @@ public class RuleUpdater {
for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) {
for (ActiveRuleParamDto activeRuleParamDto : activeRuleParams.get(activeRuleDto)) {
if (activeRuleParamDto.getKey().equals(key)) {
dbClient.deprecatedActiveRuleDao().updateParam(dbSession, activeRuleDto, activeRuleParamDto.setValue(value));
dbClient.activeRuleDao().updateParam(dbSession, activeRuleDto, activeRuleParamDto.setValue(value));
} else {
dbClient.deprecatedActiveRuleDao().insertParam(dbSession, activeRuleDto, ActiveRuleParamDto.createFor(ruleParamDto).setValue(value));
dbClient.activeRuleDao().insertParam(dbSession, activeRuleDto, ActiveRuleParamDto.createFor(ruleParamDto).setValue(value));
}
}
}
@@ -289,7 +289,7 @@ public class RuleUpdater {
for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) {
for (ActiveRuleParamDto activeRuleParamDto : activeRuleParams.get(activeRuleDto)) {
if (activeRuleParamDto.getKey().equals(key)) {
dbClient.deprecatedActiveRuleDao().deleteParam(dbSession, activeRuleDto, activeRuleParamDto);
dbClient.activeRuleDao().deleteParam(dbSession, activeRuleDto, activeRuleParamDto);
}
}
}

+ 25
- 18
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java View File

@@ -32,21 +32,24 @@ 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.loadedtemplate.LoadedTemplateDto;
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.es.SearchOptions;
import org.sonar.server.platform.Platform;
import org.sonar.server.qualityprofile.db.ActiveRuleDao;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.rule.index.RuleIndex2;
import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.tester.ServerTester;

import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;

public class RegisterQualityProfilesMediumTest {

@@ -76,34 +79,38 @@ public class RegisterQualityProfilesMediumTest {
assertThat(profile).isNotNull();

// Check ActiveRules in DB
ActiveRuleDao activeRuleDao = dbClient().deprecatedActiveRuleDao();
ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);

RuleKey ruleKey = RuleKey.of("xoo", "x1");
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();

// Check in ES
assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);

// 0. Check and clear ES
assertThat(tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey)).isNotNull();
tester.clearIndexes();
assertThat(tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey)).isNull();
tester.get(Platform.class).restart();
assertThat(tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey)).isNotNull();

// Check ActiveRules in ES
org.sonar.server.qualityprofile.ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
assertThat(activeRule.key().qProfile()).isEqualTo(profile.getKee());
assertThat(activeRule.key().ruleKey()).isEqualTo(ruleKey);
assertThat(activeRule.severity()).isEqualTo(Severity.CRITICAL);
assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();

// Check ActiveRules
ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, activeRuleKey).get();
assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKee());
assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);

// Check in ES
assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);

// TODO
// Check ActiveRuleParameters in DB
Map<String, ActiveRuleParamDto> params =
ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.key()));
ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
assertThat(params).hasSize(2);
// set by profile
assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
// default value
assertThat(params.get("max").getValue()).isEqualTo("10");

}

@Test
@@ -122,11 +129,11 @@ public class RegisterQualityProfilesMediumTest {
verifyDefaultProfile("xoo", "Basic");

// Check ActiveRules in DB
ActiveRuleDao activeRuleDao = dbClient().deprecatedActiveRuleDao();
ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
RuleKey ruleKey = RuleKey.of("xoo", "x1");

ActiveRuleDto activeRule = activeRuleDao.getNullableByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey));
ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey)).get();
assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKey());
assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);

+ 2
- 2
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexerTest.java View File

@@ -158,7 +158,7 @@ public class ActiveRuleIndexerTest {
dbTester.getDbClient().qualityProfileDao().insert(dbTester.getSession(), profile);
ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule).setSeverity(Severity.BLOCKER)
.setCreatedAtInMs(yesterday).setUpdatedAtInMs(yesterday);
// dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule);
dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule);
dbTester.getSession().commit();

indexer.index();
@@ -170,7 +170,7 @@ public class ActiveRuleIndexerTest {
dbTester.getDbClient().ruleDao().insert(dbTester.getSession(), rule2);
ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(profile, rule2).setSeverity(Severity.CRITICAL)
.setCreatedAtInMs(now).setUpdatedAtInMs(now);
// dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule2);
dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule2);
dbTester.getSession().commit();

indexer.index(singletonList(ActiveRuleChange.createFor(ACTIVATED, activeRule2.getKey())));

+ 27
- 47
server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java View File

@@ -19,14 +19,16 @@
*/
package org.sonar.server.rule;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
@@ -35,21 +37,19 @@ import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.rule.RuleDao;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.db.DbClient;
import org.sonar.server.es.SearchIdResult;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.platform.Platform;
import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.qualityprofile.QProfileService;
import org.sonar.server.qualityprofile.QProfileTesting;
import org.sonar.server.qualityprofile.RuleActivation;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.rule.index.RuleIndex2;
import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.tester.ServerTester;
@@ -57,16 +57,15 @@ import org.sonar.server.tester.UserSessionRule;

import static com.google.common.collect.Sets.newHashSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

// TODO tests should be moved to RegisterRulesTest
// TODO remaining tests should be moved to RegisterRulesTest
public class RegisterRulesMediumTest {

static final XooRulesDefinition RULE_DEFS = new XooRulesDefinition();

@ClassRule
public static final ServerTester TESTER = new ServerTester().addXoo().addComponents(RULE_DEFS);
public static final RuleKey X1_KEY = RuleKey.of("xoo", "x1");
@org.junit.Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(TESTER);

@@ -74,8 +73,6 @@ public class RegisterRulesMediumTest {
DbSession dbSession = TESTER.get(DbClient.class).openSession(false);

RuleIndex2 ruleIndex = TESTER.get(RuleIndex2.class);
ActiveRuleIndex activeRuleIndex = TESTER.get(ActiveRuleIndex.class);

RuleDao ruleDao = db.ruleDao();

@Before
@@ -103,36 +100,6 @@ public class RegisterRulesMediumTest {
ruleIndex = TESTER.get(RuleIndex2.class);
}

/**
* Use-case:
* 1. start server
* 2. stop server
* 3. drop elasticsearch index: rm -rf data/es
* 4. start server -> db is up-to-date (no changes) but rules must be re-indexed
*/
@Test
@Ignore
public void index_rules_even_if_no_changes() {
Rules rules = new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
repository.createRule("x1")
.setName("x1 name")
.setHtmlDescription("x1 desc");
}
};
register(rules);

// clear ES but keep db
TESTER.clearIndexes();
register(rules);

// verify that rules are indexed
SearchIdResult<RuleKey> searchResult = ruleIndex.search(new RuleQuery().setKey("xoo:x1"), new SearchOptions());
assertThat(searchResult.getTotal()).isEqualTo(1);
assertThat(searchResult.getIds()).containsOnly(RuleKey.of("xoo", "x1"));
}

@Test
public void deactivate_removed_rules_only_if_repository_still_exists() {
register(new Rules() {
@@ -159,7 +126,7 @@ public class RegisterRulesMediumTest {
});
assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(0);
assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X2.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).hasSize(0);
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).isEmpty();
}

@Test
@@ -182,13 +149,16 @@ public class RegisterRulesMediumTest {

// Restart without xoo
register(null);
dbSession.commit();
dbSession.clearCache();

assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(0);
assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).isEmpty();
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).hasSize(1);

// Re-install
register(rules);
assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).hasSize(1);
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).hasSize(1);
}

@Test
@@ -224,15 +194,16 @@ public class RegisterRulesMediumTest {
}
});

ActiveRule activeRule = activeRuleIndex.getByKey(ActiveRuleKey.of(QProfileTesting.XOO_P1_KEY, RuleTesting.XOO_X1));
Map<String, String> params = activeRule.params();
List<ActiveRuleParamDto> params = db.activeRuleDao().selectParamsByActiveRuleKey(dbSession, ActiveRuleKey.of(QProfileTesting.XOO_P1_KEY, RuleTesting.XOO_X1));
assertThat(params).hasSize(2);

Map<String, ActiveRuleParamDto> parmsByKey = FluentIterable.from(params).uniqueIndex(ActiveRuleParamToKey.INSTANCE);

// do not change default value on existing active rules -> keep min=5
assertThat(params.get("min")).isEqualTo("5");
assertThat(parmsByKey.get("min").getValue()).isEqualTo("5");

// new param with default value
assertThat(params.get("max")).isEqualTo("10");
assertThat(parmsByKey.get("max").getValue()).isEqualTo("10");
}

@Test
@@ -485,4 +456,13 @@ public class RegisterRulesMediumTest {
}
}

private enum ActiveRuleParamToKey implements Function<ActiveRuleParamDto, String> {
INSTANCE;

@Override
public String apply(@Nonnull ActiveRuleParamDto input) {
return input.getKey();
}
}

}

+ 3
- 11
server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java View File

@@ -35,16 +35,13 @@ import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.qualityprofile.QualityProfileDao;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.es.EsTester;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.qualityprofile.RuleActivator;
import org.sonar.server.qualityprofile.db.ActiveRuleDao;
import org.sonar.server.rule.db.RuleDao;
import org.sonar.server.rule.index.RuleIndex2;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.rule.index.RuleIndexer;
@@ -68,7 +65,7 @@ public class RegisterRulesTest {
static final RuleKey RULE_KEY2 = RuleKey.of("fake", "rule2");
static final RuleKey RULE_KEY3 = RuleKey.of("fake", "rule3");

System2 system;
System2 system = mock(System2.class);;

@org.junit.Rule
public DbTester dbTester = DbTester.create(system);
@@ -78,7 +75,7 @@ public class RegisterRulesTest {

RuleActivator ruleActivator = mock(RuleActivator.class);

DbClient dbClient;
DbClient dbClient = dbTester.getDbClient();

RuleIndexer ruleIndexer;

@@ -87,12 +84,7 @@ public class RegisterRulesTest {
@Before
public void before() {
esTester.truncateIndices();
system = mock(System2.class);
when(system.now()).thenReturn(DATE1.getTime());
RuleDao ruleDao = new RuleDao(system);
ActiveRuleDao activeRuleDao = new ActiveRuleDao(new QualityProfileDao(dbTester.myBatis(), system), ruleDao, system);
dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), ruleDao, activeRuleDao, new org.sonar.db.rule.RuleDao(),
new QualityProfileDao(dbTester.myBatis(), system));
ruleIndexer = new RuleIndexer(dbClient, esTester.client());
ruleIndexer.setEnabled(true);
ruleIndex = new RuleIndex2(esTester.client());

Loading…
Cancel
Save