package org.sonar.core.qualityprofile.db;
+import com.google.common.base.Preconditions;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
}
public static ActiveRuleDto createFor(QualityProfileDto profileDto, RuleDto ruleDto) {
+ Preconditions.checkArgument(profileDto.getId()!=null, "Profile is not persisted");
+ Preconditions.checkArgument(ruleDto.getId()!=null, "Rule is not persisted");
ActiveRuleDto dto = new ActiveRuleDto();
dto.setProfileId(profileDto.getId());
dto.setRuleId(ruleDto.getId());
package org.sonar.core.qualityprofile.db;
+import com.google.common.base.Preconditions;
import org.sonar.core.rule.RuleParamDto;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
public class ActiveRuleParamDto {
private Integer id;
}
public static ActiveRuleParamDto createFor(RuleParamDto param) {
- return new ActiveRuleParamDto()
+ Preconditions.checkArgument(param.getId() != null, "Parameter is not persisted");
+ return new ActiveRuleParamDto()
.setKey(param.getName())
.setRulesParameterId(param.getId());
}
- public static ActiveRuleParamDto createFrom(ActiveRuleParamDto parentParam) {
- return new ActiveRuleParamDto()
- .setKey(parentParam.getKey())
- .setRulesParameterId(parentParam.getId());
+ public static Map<String,ActiveRuleParamDto> groupByKey(Collection<ActiveRuleParamDto> params) {
+ Map<String, ActiveRuleParamDto> result = new HashMap<String, ActiveRuleParamDto>();
+ for (ActiveRuleParamDto param : params) {
+ result.put(param.getKey(), param);
+ }
+ return result;
}
}
import com.google.common.base.Preconditions;
import org.apache.ibatis.session.SqlSession;
-import org.sonar.core.persistence.DaoComponent;
import org.sonar.api.ServerComponent;
import org.sonar.core.component.ComponentDto;
+import org.sonar.core.persistence.DaoComponent;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
this.mybatis = mybatis;
}
+ @CheckForNull
+ public QualityProfileDto getByKey(QualityProfileKey key, DbSession session) {
+ return session.getMapper(QualityProfileMapper.class).selectByNameAndLanguage(key.name(), key.lang());
+ }
+
+ public List<QualityProfileDto> findAll(DbSession session) {
+ return session.getMapper(QualityProfileMapper.class).selectAll();
+ }
+
public void insert(QualityProfileDto dto, SqlSession session) {
session.getMapper(QualityProfileMapper.class).insert(dto);
}
/**
* @deprecated use {@link #delete(QualityProfileDto, DbSession)}
- * @param id
- * @param session
*/
@Deprecated
public void delete(int id, SqlSession session) {
}
/**
- * @deprecated use {@link #delete(QualityProfileDto, DbSession)}
* @param id
+ * @deprecated use {@link #delete(QualityProfileDto, DbSession)}
*/
@Deprecated
public void delete(int id) {
* @deprecated use QualityProfileDto.createFor instead
*/
@Deprecated
- public QualityProfileDto(){
+ public QualityProfileDto() {
}
@CheckForNull
public QualityProfileKey getParentKey() {
- if(getParent() != null && !getParent().isEmpty()) {
+ if (getParent() != null && !getParent().isEmpty()) {
return QualityProfileKey.of(this.getParent(), this.getLanguage());
} else {
return null;
return this;
}
- public static QualityProfileDto createFor(String name, String language){
+ public static QualityProfileDto createFor(String name, String language) {
return new QualityProfileDto()
.setName(name)
.setLanguage(language);
}
+
+ public static QualityProfileDto createFor(QualityProfileKey key) {
+ return createFor(key.name(), key.lang());
+ }
}
public class RuleParamDto {
- private int id;
+ private Integer id;
private Integer ruleId;
private String name;
private String type;
}
- public int getId() {
+ public Integer getId() {
return id;
}
- public RuleParamDto setId(int id) {
+ public RuleParamDto setId(Integer id) {
this.id = id;
return this;
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 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.core.qualityprofile.db;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class ActiveRuleParamDtoTest {
+
+ @Test
+ public void groupByKey() throws Exception {
+ assertThat(ActiveRuleParamDto.groupByKey(Collections.<ActiveRuleParamDto>emptyList())).isEmpty();
+
+ Collection<ActiveRuleParamDto> dtos = Arrays.asList(
+ new ActiveRuleParamDto().setKey("foo"), new ActiveRuleParamDto().setKey("bar")
+ );
+ Map<String, ActiveRuleParamDto> group = ActiveRuleParamDto.groupByKey(dtos);
+ assertThat(group.keySet()).containsOnly("foo", "bar");
+ }
+}
import org.sonar.api.utils.MessageException;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import javax.persistence.*;
import java.util.ArrayList;
/**
* @param optionalSeverity if null, then the default rule severity is used
*/
- public ActiveRule activateRule(final Rule rule, RulePriority optionalSeverity) {
+ public ActiveRule activateRule(final Rule rule, @Nullable RulePriority optionalSeverity) {
if (Iterables.any(activeRules, new Predicate<ActiveRule>() {
@Override
public boolean apply(ActiveRule input) {
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.ActiveRuleKey;
import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
-import org.sonar.core.qualityprofile.db.QualityProfileKey;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
return index.get(ActiveRuleIndex.class).getByKey(key);
}
- public List<ActiveRule> findByRuleKey(RuleKey key){
+ public List<ActiveRule> findByRuleKey(RuleKey key) {
return index.get(ActiveRuleIndex.class).findByRule(key);
}
*/
public List<ActiveRuleChange> activate(RuleActivation activation) {
verifyPermission(UserSession.get());
-
DbSession dbSession = db.openSession(false);
- List<ActiveRuleChange> changes = Lists.newArrayList();
try {
- RuleActivationContext context = contextFactory.create(activation.getKey(), dbSession);
- ActiveRuleChange change;
- if (context.activeRule() == null) {
- change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, activation.getKey());
- } else {
- change = new ActiveRuleChange(ActiveRuleChange.Type.UPDATED, activation.getKey());
- }
- change.setSeverity(StringUtils.defaultIfEmpty(activation.getSeverity(), context.defaultSeverity()));
- for (RuleParamDto ruleParamDto : context.ruleParams()) {
- String value = activation.getParameters().get(ruleParamDto.getName());
- verifyParam(ruleParamDto, value);
- change.setParameter(ruleParamDto.getName(), StringUtils.defaultIfEmpty(value, ruleParamDto.getDefaultValue()));
+ List<ActiveRuleChange> changes = activate(activation, dbSession);
+ if (!changes.isEmpty()) {
+ dbSession.commit();
+ previewCache.reportGlobalModification();
}
- changes.add(change);
- // TODO filter changes without any differences
-
- persist(changes, context, dbSession);
-
return changes;
-
} finally {
dbSession.close();
}
}
+ /**
+ * Activate the rule WITHOUT committing db session and WITHOUT checking permissions
+ */
+ List<ActiveRuleChange> activate(RuleActivation activation, DbSession dbSession) {
+ List<ActiveRuleChange> changes = Lists.newArrayList();
+ RuleActivationContext context = contextFactory.create(activation.getKey(), dbSession);
+ ActiveRuleChange change;
+ if (context.activeRule() == null) {
+ change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, activation.getKey());
+ } else {
+ change = new ActiveRuleChange(ActiveRuleChange.Type.UPDATED, activation.getKey());
+ }
+ change.setSeverity(StringUtils.defaultIfEmpty(activation.getSeverity(), context.defaultSeverity()));
+ for (RuleParamDto ruleParamDto : context.ruleParams()) {
+ String value = activation.getParameters().get(ruleParamDto.getName());
+ verifyParam(ruleParamDto, value);
+ change.setParameter(ruleParamDto.getName(), StringUtils.defaultIfEmpty(value, ruleParamDto.getDefaultValue()));
+ }
+ changes.add(change);
+ // TODO filter changes without any differences
+
+ persist(changes, context, dbSession);
+ return changes;
+ }
+
private void persist(Collection<ActiveRuleChange> changes, RuleActivationContext context, DbSession dbSession) {
ActiveRuleDao dao = db.activeRuleDao();
for (ActiveRuleChange change : changes) {
}
}
}
- if (!changes.isEmpty()) {
- dbSession.commit();
- previewCache.reportGlobalModification();
- }
}
/**
change = new ActiveRuleChange(ActiveRuleChange.Type.DEACTIVATED, key);
changes.add(change);
persist(changes, context, dbSession);
+ dbSession.commit();
return changes;
} finally {
import com.google.common.collect.Multimaps;
import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
-import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.ServerComponent;
import org.sonar.api.profiles.ProfileDefinition;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.ActiveRuleParam;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.api.utils.ValidationMessages;
-import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleKey;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.core.rule.RuleDto;
+import org.sonar.core.qualityprofile.db.QualityProfileKey;
import org.sonar.core.template.LoadedTemplateDto;
-import org.sonar.jpa.session.DatabaseSessionFactory;
import org.sonar.server.db.DbClient;
-import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.platform.PersistentSettings;
-import org.sonar.server.user.UserSession;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
-public class RegisterQualityProfiles {
+/**
+ * Synchronize Quality profiles during server startup
+ */
+public class RegisterQualityProfiles implements ServerComponent {
private static final Logger LOGGER = LoggerFactory.getLogger(RegisterQualityProfiles.class);
private static final String DEFAULT_PROFILE_NAME = "Sonar way";
private final PersistentSettings settings;
private final List<ProfileDefinition> definitions;
private final DefaultProfilesCache defaultProfilesCache;
- private final DatabaseSessionFactory sessionFactory;
private final DbClient dbClient;
+ private final ActiveRuleService activeRuleService;
- public RegisterQualityProfiles(DatabaseSessionFactory sessionFactory,
- PersistentSettings settings,
- DefaultProfilesCache defaultProfilesCache, DbClient dbClient) {
- this(sessionFactory, settings, defaultProfilesCache, dbClient,
- Collections.<ProfileDefinition>emptyList());
+ /**
+ * To be kept when no ProfileDefinition are injected
+ */
+ public RegisterQualityProfiles(PersistentSettings settings,
+ DefaultProfilesCache defaultProfilesCache,
+ DbClient dbClient,
+ ActiveRuleService activeRuleService) {
+ this(settings, defaultProfilesCache, dbClient, activeRuleService, Collections.<ProfileDefinition>emptyList());
}
- public RegisterQualityProfiles(DatabaseSessionFactory sessionFactory,
- PersistentSettings settings,
+ public RegisterQualityProfiles(PersistentSettings settings,
DefaultProfilesCache defaultProfilesCache,
DbClient dbClient,
+ ActiveRuleService activeRuleService,
List<ProfileDefinition> definitions) {
- this.sessionFactory = sessionFactory;
this.settings = settings;
this.defaultProfilesCache = defaultProfilesCache;
- this.definitions = definitions;
this.dbClient = dbClient;
+ this.activeRuleService = activeRuleService;
+ this.definitions = definitions;
}
public void start() {
TimeProfiler profiler = new TimeProfiler(LOGGER).start("Register Quality Profiles");
- // Hibernate session can contain an invalid cache of rules.
- // As long ProfileDefinition API will be used, then we'll have to use this commit as Hibernate is used by plugin to load rules when creating their profiles.
- sessionFactory.getSession().commit();
-
DbSession session = dbClient.openSession(false);
try {
ListMultimap<String, RulesProfile> profilesByLanguage = profilesByLanguage();
verifyLanguage(language, profiles);
for (Map.Entry<String, Collection<RulesProfile>> entry : profilesByName(profiles).entrySet()) {
- String name = entry.getKey();
- if (shouldRegister(language, name, session)) {
- register(language, name, entry.getValue(), session);
+ String profileName = entry.getKey();
+ QualityProfileKey profileKey = QualityProfileKey.of(profileName, language);
+ if (shouldRegister(profileKey, session)) {
+ register(profileKey, entry.getValue(), session);
}
- defaultProfilesCache.put(language, name);
+ defaultProfilesCache.put(language, profileName);
}
setDefault(language, profiles, session);
}
}
}
- private void checkPermission(UserSession userSession) {
- userSession.checkLoggedIn();
- userSession.checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- }
+ private void register(QualityProfileKey key, Collection<RulesProfile> profiles, DbSession session) {
+ LOGGER.info("Register profile " + key);
- private QualityProfileDto newQualityProfileDto(String name, String language, DbSession session) {
- checkPermission(UserSession.get());
- if (dbClient.qualityProfileDao().selectByNameAndLanguage(name, language, session) != null) {
- throw BadRequestException.ofL10n("quality_profiles.profile_x_already_exists", name);
+ QualityProfileDto profileDto = dbClient.qualityProfileDao().getByKey(key, session);
+ if (profileDto != null) {
+ // cleanup
+ cleanUp(key, profileDto, session);
}
+ insertNewProfile(key, session);
- QualityProfileDto profile = QualityProfileDto.createFor(name, language)
- .setVersion(1).setUsed(false);
- dbClient.qualityProfileDao().insert(profile, session);
- return profile;
- }
-
- private void register(String language, String name, Collection<RulesProfile> profiles, DbSession session) {
- LOGGER.info("Register " + language + " profile: " + name);
-
- QualityProfileDto profile = dbClient.qualityProfileDao().selectByNameAndLanguage(name, language, session);
- if (profile != null) {
- dbClient.activeRuleDao().deleteByProfileKey(profile.getKey(), session);
- dbClient.qualityProfileDao().delete(profile, session);
- }
- profile = newQualityProfileDto(name, language, session);
-
- for (RulesProfile currentRulesProfile : profiles) {
- //TODO trapped on legacy Hibernate object.
- for (org.sonar.api.rules.ActiveRule activeRule : currentRulesProfile.getActiveRules()) {
+ for (RulesProfile profile : profiles) {
+ for (org.sonar.api.rules.ActiveRule activeRule : profile.getActiveRules()) {
RuleKey ruleKey = RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey());
- RuleDto rule = dbClient.ruleDao().getByKey(ruleKey, session);
- if (rule == null) {
- throw new NotFoundException(String.format("Rule '%s' does not exists.", ruleKey));
- }
-
- ActiveRuleDto activeRuleDto = dbClient.activeRuleDao().createActiveRule(
- profile.getKey(), ruleKey, activeRule.getSeverity().name(), session);
-
- //TODO trapped on legacy Hibernate object.
+ RuleActivation activation = new RuleActivation(ActiveRuleKey.of(key, ruleKey));
+ activation.setSeverity(activeRule.getSeverity() != null ? activeRule.getSeverity().name() : null);
for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
- String paramKey = param.getKey();
- String value = param.getValue();
- ActiveRuleParamDto paramDto = dbClient.activeRuleDao()
- .getParamsByActiveRuleAndKey(activeRuleDto, paramKey, session);
- if (value != null && !paramDto.getValue().equals(value)) {
- paramDto.setValue(value);
- dbClient.activeRuleDao().updateParam(activeRuleDto, paramDto, session);
- }
+ activation.setParameter(param.getKey(), param.getValue());
}
+ activeRuleService.activate(activation, session);
}
}
- dbClient.loadedTemplateDao()
- .insert(new LoadedTemplateDto(templateKey(language, name), LoadedTemplateDto.QUALITY_PROFILE_TYPE), session);
+ LoadedTemplateDto template = new LoadedTemplateDto(templateKey(key), LoadedTemplateDto.QUALITY_PROFILE_TYPE);
+ dbClient.loadedTemplateDao().insert(template, session);
+ }
+
+ private void cleanUp(QualityProfileKey key, QualityProfileDto profileDto, DbSession session) {
+ dbClient.activeRuleDao().deleteByProfileKey(key, session);
+ dbClient.qualityProfileDao().delete(profileDto, session);
+ session.commit();
+ }
+
+ private void insertNewProfile(QualityProfileKey key, DbSession session) {
+ QualityProfileDto profile = QualityProfileDto.createFor(key).setVersion(1).setUsed(false);
+ dbClient.qualityProfileDao().insert(profile, session);
+ session.commit();
}
- private void setDefault(String language, List<RulesProfile> profiles, SqlSession session) {
+ private void setDefault(String language, List<RulesProfile> profiles, DbSession session) {
String propertyKey = "sonar.profile." + language;
if (settings.getString(propertyKey) == null) {
String defaultProfileName = defaultProfileName(profiles);
LOGGER.info("Set default " + language + " profile: " + defaultProfileName);
settings.saveProperty(propertyKey, defaultProfileName);
+ } else {
+ //TODO check that the declared default profile exists, else fix
}
}
return names;
}
- private boolean shouldRegister(String language, String profileName, SqlSession session) {
+ private boolean shouldRegister(QualityProfileKey key, DbSession session) {
return dbClient.loadedTemplateDao()
- .countByTypeAndKey(LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey(language, profileName), session) == 0;
+ .countByTypeAndKey(LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey(key), session) == 0;
}
- private static String templateKey(String language, String profileName) {
- return StringUtils.lowerCase(language) + ":" + profileName;
+ private static String templateKey(QualityProfileKey key) {
+ return StringUtils.lowerCase(key.lang()) + ":" + key.name();
}
}
QualityProfileDto profileDto = profileDao.selectByNameAndLanguage(profileKey.name(), profileKey.lang(), session);
ActiveRuleDto activeRule = ActiveRuleDto.createFor(profileDto, ruleDto)
.setSeverity(severity);
- this.insert(activeRule, session);
-
- List<RuleParamDto> ruleParams = ruleDao.findRuleParamsByRuleKey(ruleKey, session);
- List<ActiveRuleParamDto> activeRuleParams = newArrayList();
- for (RuleParamDto ruleParam : ruleParams) {
- ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(ruleParam)
- .setKey(ruleParam.getName())
- .setValue(ruleParam.getDefaultValue());
- activeRuleParams.add(activeRuleParam);
- this.addParam(activeRule, activeRuleParam, session);
- }
+ insert(activeRule, session);
+
+// List<RuleParamDto> ruleParams = ruleDao.findRuleParamsByRuleKey(ruleKey, session);
+// List<ActiveRuleParamDto> activeRuleParams = newArrayList();
+// for (RuleParamDto ruleParam : ruleParams) {
+// ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(ruleParam)
+// .setKey(ruleParam.getName())
+// .setValue(ruleParam.getDefaultValue());
+// activeRuleParams.add(activeRuleParam);
+// addParam(activeRule, activeRuleParam, session);
+// }
return activeRule;
}
//TODO test params
// verify es
-
ActiveRule activeRule = index.getByKey(key);
assertThat(activeRule).isNull();
}
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.ValidationMessages;
import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
import org.sonar.core.properties.PropertiesDao;
import org.sonar.core.properties.PropertyDto;
import org.sonar.core.properties.PropertyQuery;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.ActiveRuleKey;
+import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
import org.sonar.core.qualityprofile.db.QualityProfileKey;
-import org.sonar.core.rule.RuleDto;
+import org.sonar.server.db.DbClient;
import org.sonar.server.qualityprofile.persistence.ActiveRuleDao;
-import org.sonar.server.rule2.persistence.RuleDao;
import org.sonar.server.tester.ServerTester;
import java.util.List;
+import java.util.Map;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
@org.junit.Rule
public ServerTester serverTester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
+ DbClient dbClient;
DbSession session;
@Before
- public void setup(){
- session = serverTester.get(MyBatis.class).openSession(false);
+ public void setup() {
+ dbClient = serverTester.get(DbClient.class);
+ session = dbClient.openSession(false);
}
@After
}
@Test
- public void register_profile_at_startup() throws Exception {
+ public void register_profile_definitions() throws Exception {
+ QualityProfileKey qualityProfileKey = QualityProfileKey.of("Basic", "xoo");
- QualityProfileKey qualityProfileKey = QualityProfileKey.of("Basic","xoo");
+ // Check Profile in DB
+ QualityProfileDao qualityProfileDao = dbClient.qualityProfileDao();
+ assertThat(qualityProfileDao.findAll(session)).hasSize(1);
+ assertThat(qualityProfileDao.getByKey(qualityProfileKey, session)).isNotNull();
- // 1. Check Profile in DB
- QualityProfileDao qualityProfileDao = serverTester.get(QualityProfileDao.class);
- assertThat(qualityProfileDao.selectAll()).hasSize(1);
- assertThat(qualityProfileDao.selectByNameAndLanguage(qualityProfileKey.name(), qualityProfileKey.lang()))
- .isNotNull();
-
- // 2. Check Default Profile
- PropertiesDao propertiesDao = serverTester.get(PropertiesDao.class);
+ // Check Default Profile
+ PropertiesDao propertiesDao = dbClient.propertiesDao();
List<PropertyDto> xooDefault = propertiesDao.selectByQuery(PropertyQuery.builder().setKey("sonar.profile.xoo").build(), session);
assertThat(xooDefault).hasSize(1);
assertThat(xooDefault.get(0).getValue()).isEqualTo("Basic");
- // 3. Check ActiveRules in DB
- ActiveRuleDao activeRuleDao = serverTester.get(ActiveRuleDao.class);
-
+ // Check ActiveRules in DB
+ ActiveRuleDao activeRuleDao = dbClient.activeRuleDao();
assertThat(activeRuleDao.findByProfileKey(qualityProfileKey, session)).hasSize(2);
RuleKey ruleKey = RuleKey.of("xoo", "x1");
- RuleDto rule = serverTester.get(RuleDao.class).getByKey(ruleKey, session);
-
-
- ActiveRuleDto activeRule = activeRuleDao.getByKey(ActiveRuleKey.of(qualityProfileKey,ruleKey), session);
+ ActiveRuleDto activeRule = activeRuleDao.getByKey(ActiveRuleKey.of(qualityProfileKey, ruleKey), session);
assertThat(activeRule.getKey().qProfile()).isEqualTo(qualityProfileKey);
- assertThat(activeRule.getKey().ruleKey()).isEqualTo(rule.getKey());
- assertThat(activeRule.getSeverityString()).isEqualToIgnoringCase("MAJOR");
+ assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
+ assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
+
+ // Check ActiveRuleParameters in DB
+ Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.findParamsByActiveRule(activeRule, session));
+ assertThat(params).hasSize(2);
+ // set by profile
+ assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
+ // default value
+ assertThat(params.get("max").getValue()).isEqualTo("10");
}
public static class XooProfileDefinition extends ProfileDefinition {
final RulesProfile profile = RulesProfile.create("Basic", "xoo");
ActiveRule activeRule1 = profile.activateRule(
org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))),
- RulePriority.MAJOR);
+ RulePriority.CRITICAL);
activeRule1.setParameter("acceptWhitespace", "true");
- profile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x2"), RulePriority.BLOCKER);
+ profile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x2"), RulePriority.INFO);
return profile;
}
}
@Override
public void define(Context context) {
NewRepository repository = context.createRepository("xoo", "xoo").setName("Xoo Repo");
- repository.createRule("x1")
+ NewRule x1 = repository.createRule("x1")
.setName("x1 name")
.setHtmlDescription("x1 desc")
- .setSeverity(Severity.MINOR)
- .createParam("acceptWhitespace")
+ .setSeverity(Severity.MINOR);
+ x1.createParam("acceptWhitespace")
.setDefaultValue("false")
.setType(RuleParamType.BOOLEAN)
.setDescription("Accept whitespaces on the line");
+ x1.createParam("max")
+ .setDefaultValue("10")
+ .setType(RuleParamType.INTEGER)
+ .setDescription("Maximum");
repository.createRule("x2")
.setName("x2 name")
.setHtmlDescription("x2 desc")
- .setSeverity(Severity.MAJOR);
+ .setSeverity(Severity.INFO);
repository.done();
}
}
-
- private QualityProfileKey profileKey(QProfile profile){
- return QualityProfileKey.of(profile.name(), profile.language());
- }
}