import static java.lang.String.format;
public class RuleExtensionId {
+ private final int ruleId;
private final String organizationUuid;
- private final String repositoryName;
- private final String ruleKey;
private final String id;
- private static final Splitter ID_SPLITTER = Splitter.on(CharMatcher.anyOf(":|"));
+ private static final Splitter ID_SPLITTER = Splitter.on(CharMatcher.anyOf("|"));
- public RuleExtensionId(String organizationUuid, String repositoryName, String ruleKey) {
+ public RuleExtensionId(String organizationUuid, int ruleId) {
this.organizationUuid = organizationUuid;
- this.repositoryName = repositoryName;
- this.ruleKey = ruleKey;
- this.id = format("%s:%s|%s",repositoryName,ruleKey,organizationUuid);
+ this.ruleId = ruleId;
+ this.id = format("%s|%s", ruleId, organizationUuid);
}
public RuleExtensionId(String ruleExtensionId) {
List<String> splittedId = ID_SPLITTER.splitToList(ruleExtensionId);
- checkArgument(splittedId.size() == 3, "Incorrect Id %s", ruleExtensionId);
+ checkArgument(splittedId.size() == 2, "Incorrect Id %s", ruleExtensionId);
this.id = ruleExtensionId;
- this.repositoryName = splittedId.get(0);
- this.ruleKey = splittedId.get(1);
- this.organizationUuid = splittedId.get(2);
+ this.ruleId = Integer.parseInt(splittedId.get(0));
+ this.organizationUuid = splittedId.get(1);
}
- public String getOrganizationUuid() {
- return organizationUuid;
- }
-
- public String getRepositoryName() {
- return repositoryName;
+ public int getRuleId() {
+ return ruleId;
}
- public String getRuleKey() {
- return ruleKey;
+ public String getOrganizationUuid() {
+ return organizationUuid;
}
public String getId() {
public class IndexedActiveRuleDto {
private long id;
+ private int ruleId;
private int severity;
private String inheritance;
private String repository;
return id;
}
+ public int getRuleId() {
+ return ruleId;
+ }
+
public int getSeverity() {
return severity;
}
});
}
- public void scrollIndexingRulesByKeys(DbSession dbSession, Collection<RuleKey> ruleKeys, Consumer<RuleForIndexingDto> consumer) {
+ public void scrollIndexingRulesByKeys(DbSession dbSession, Collection<Integer> ruleIds, Consumer<RuleForIndexingDto> consumer) {
RuleMapper mapper = mapper(dbSession);
- executeLargeInputsWithoutOutput(ruleKeys,
- pageOfRuleKeys -> mapper
- .selectIndexingRulesByKeys(pageOfRuleKeys)
+ executeLargeInputsWithoutOutput(ruleIds,
+ pageOfRuleIds -> mapper
+ .selectIndexingRulesByIds(pageOfRuleIds)
.forEach(consumer));
}
return executeLargeInputs(ruleKeys, mapper(session)::selectParamsByRuleKeys);
}
- public List<RuleParamDto> selectRuleParamsByRuleIds(DbSession dbSession, List<Integer> ruleIds) {
+ public List<RuleParamDto> selectRuleParamsByRuleIds(DbSession dbSession, Collection<Integer> ruleIds) {
return executeLargeInputs(ruleIds, mapper(dbSession)::selectParamsByRuleIds);
}
private static final Splitter TAGS_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
+ private int ruleId;
private String pluginName;
private String pluginRuleKey;
private String organizationUuid;
return this;
}
+ public int getRuleId() {
+ return ruleId;
+ }
+
+ public void setRuleId(int ruleId) {
+ this.ruleId = ruleId;
+ }
+
public String getOrganizationUuid() {
return organizationUuid;
}
void scrollIndexingRules(ResultHandler<RuleForIndexingDto> handler);
- List<RuleForIndexingDto> selectIndexingRulesByKeys(@Param("ruleKeys") List<RuleKey> keys);
+ List<RuleForIndexingDto> selectIndexingRulesByIds(@Param("ruleIds") List<Integer> ruleIds);
void scrollIndexingRuleExtensions(ResultHandler<RuleExtensionForIndexingDto> handler);
ar.id as "id",
ar.failure_level as "severity",
ar.inheritance as "inheritance",
+ r.id as "ruleId",
r.plugin_name as "repository",
r.plugin_rule_key as "key",
rp.kee as "ruleProfileUuid"
<include refid="sqlSelectIndexingRuleExtensions" />
and
<foreach collection="ruleExtensionIds" index="index" item="ruleExtId" open="" separator=" or " close="">
- ( r.plugin_name = #{ruleExtId.repositoryName, jdbcType=VARCHAR} and
- r.plugin_rule_key = #{ruleExtId.ruleKey, jdbcType=VARCHAR} and
+ ( r.id = #{ruleExtId.ruleId, jdbcType=INTEGER} and
rm.organization_uuid = #{ruleExtId.organizationUuid, jdbcType=VARCHAR} )
</foreach>
</select>
<sql id="sqlSelectIndexingRuleExtensions">
select
+ r.id as "ruleId",
r.plugin_name as "pluginName",
r.plugin_rule_key as "pluginRuleKey",
rm.organization_uuid as "organizationUuid",
<sql id="sqlSelectIndexingRuleExtensions" databaseId="oracle">
select
+ r.id as "ruleId",
r.plugin_name as "pluginName",
r.plugin_rule_key as "pluginRuleKey",
rm.organization_uuid as "organizationUuid",
</foreach>
</select>
- <select id="selectIndexingRulesByKeys" parameterType="map" resultType="org.sonar.db.rule.RuleForIndexingDto">
+ <select id="selectIndexingRulesByIds" parameterType="map" resultType="org.sonar.db.rule.RuleForIndexingDto">
<include refid="sqlSelectIndexingRules"/>
where
- <foreach collection="ruleKeys" index="index" item="ruleKey" open="" separator=" or " close="">
- (r.plugin_name=#{ruleKey.repository,jdbcType=VARCHAR} and r.plugin_rule_key=#{ruleKey.rule,jdbcType=VARCHAR})
+ <foreach collection="ruleIds" index="index" item="ruleId" open="" separator=" or " close="">
+ r.id=#{ruleId,jdbcType=INTEGER}
</foreach>
</select>
Accumulator accumulator = new Accumulator();
underTest.scrollAllForIndexing(dbSession, accumulator);
assertThat(accumulator.list)
- .extracting(IndexedActiveRuleDto::getId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey, IndexedActiveRuleDto::getRuleProfileUuid,
+ .extracting(IndexedActiveRuleDto::getId,
+ IndexedActiveRuleDto::getRuleId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey,
+ IndexedActiveRuleDto::getRuleProfileUuid,
IndexedActiveRuleDto::getSeverity, IndexedActiveRuleDto::getInheritance)
.containsExactlyInAnyOrder(
- tuple((long) ar1.getId(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity(), ar1.getInheritance()),
- tuple((long) ar2.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity(), ar2.getInheritance()),
- tuple((long) ar3.getId(), ar3.getRuleKey().repository(), ar3.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar3.getSeverity(), ar3.getInheritance()));
+ tuple((long)ar1.getId(), rule1.getId(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity(), ar1.getInheritance()),
+ tuple((long)ar2.getId(), rule1.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity(), ar2.getInheritance()),
+ tuple((long)ar3.getId(), rule2.getId(), ar3.getRuleKey().repository(), ar3.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar3.getSeverity(), ar3.getInheritance()));
}
@Test
Accumulator accumulator = new Accumulator();
underTest.scrollByIdsForIndexing(dbSession, asList((long) ar1.getId(), (long) ar2.getId()), accumulator);
assertThat(accumulator.list)
- .extracting(IndexedActiveRuleDto::getId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey, IndexedActiveRuleDto::getRuleProfileUuid,
- IndexedActiveRuleDto::getSeverity)
+ .extracting(IndexedActiveRuleDto::getId,
+ IndexedActiveRuleDto::getRuleId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey,
+ IndexedActiveRuleDto::getRuleProfileUuid, IndexedActiveRuleDto::getSeverity)
.containsExactlyInAnyOrder(
- tuple((long) ar1.getId(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity()),
- tuple((long) ar2.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity()));
+ tuple((long)ar1.getId(), rule1.getId(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity()),
+ tuple((long)ar2.getId(), rule1.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity()));
}
@Test
RuleDefinitionDto r1 = db.rules().insert();
db.rules().insert();
- underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(r1.getKey()), accumulator);
+ underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(r1.getId()), accumulator);
assertThat(accumulator.list)
.extracting(RuleForIndexingDto::getId, RuleForIndexingDto::getRuleKey)
RuleDefinitionDto r1 = db.rules().insert();
RuleDefinitionDto r2 = db.rules().insert(rule -> rule.setTemplateId(r1.getId()));
- underTest.scrollIndexingRulesByKeys(db.getSession(), Arrays.asList(r1.getKey(), r2.getKey()), accumulator);
+ underTest.scrollIndexingRulesByKeys(db.getSession(), Arrays.asList(r1.getId(), r2.getId()), accumulator);
assertThat(accumulator.list).hasSize(2);
RuleForIndexingDto firstRule = accumulator.list.stream().filter(t -> t.getId().equals(r1.getId())).findFirst().get();
public void scrollIndexingRulesByKeys_scrolls_nothing_if_key_does_not_exist() {
Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
db.rules().insert();
+ int nonExistingRuleId = 42;
- underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(RuleKey.of("does", "not exist")), accumulator);
+ underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(nonExistingRuleId), accumulator);
assertThat(accumulator.list).isEmpty();
}
underTest.scrollIndexingRuleExtensions(db.getSession(), accumulator);
assertThat(accumulator.list)
- .extracting(RuleExtensionForIndexingDto::getRuleKey, RuleExtensionForIndexingDto::getOrganizationUuid, RuleExtensionForIndexingDto::getTags)
+ .extracting(RuleExtensionForIndexingDto::getRuleId,
+ RuleExtensionForIndexingDto::getRuleKey,
+ RuleExtensionForIndexingDto::getOrganizationUuid, RuleExtensionForIndexingDto::getTags)
.containsExactlyInAnyOrder(
- tuple(r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()),
- tuple(r2.getKey(), organization.getUuid(), r2Extension.getTagsAsString()));
+ tuple(r1.getId(), r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()),
+ tuple(r2.getId(), r2.getKey(), organization.getUuid(), r2Extension.getTagsAsString()));
}
@Test
Accumulator<RuleExtensionForIndexingDto> accumulator = new Accumulator<>();
RuleDefinitionDto r1 = db.rules().insert();
RuleMetadataDto r1Extension = db.rules().insertOrUpdateMetadata(r1, organization, r -> r.setTagsField("t1,t2"));
- RuleExtensionId r1ExtensionId = new RuleExtensionId(organization.getUuid(), r1.getRepositoryKey(), r1.getRuleKey());
+ RuleExtensionId r1ExtensionId = new RuleExtensionId(organization.getUuid(), r1.getId());
RuleDefinitionDto r2 = db.rules().insert();
db.rules().insertOrUpdateMetadata(r2, organization, r -> r.setTagsField("t1,t3"));
underTest.scrollIndexingRuleExtensionsByIds(db.getSession(), singletonList(r1ExtensionId), accumulator);
assertThat(accumulator.list)
- .extracting(RuleExtensionForIndexingDto::getRuleKey, RuleExtensionForIndexingDto::getOrganizationUuid, RuleExtensionForIndexingDto::getTags)
+ .extracting(RuleExtensionForIndexingDto::getRuleId,
+ RuleExtensionForIndexingDto::getRuleKey,
+ RuleExtensionForIndexingDto::getOrganizationUuid, RuleExtensionForIndexingDto::getTags)
.containsExactlyInAnyOrder(
- tuple(r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()));
+ tuple(r1.getId(), r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()));
}
@Test
.setOldRuleKey(ruleKey));
}
- private static class Accumulator<T> implements Consumer<T> {
+ private static class Accumulator<T> implements Consumer<T> {
private final List<T> list = new ArrayList<>();
@Override
package org.sonar.server.organization.ws;
import java.util.List;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
if (isSupportDisabled(dbSession)) {
flagCurrentUserAsRoot(dbSession);
createDefaultMembersGroup(dbSession);
- List<RuleKey> disabledTemplateAndCustomRuleKeys = disableTemplateRulesAndCustomRules(dbSession);
+ List<Integer> disabledTemplateAndCustomRuleIds = disableTemplateRulesAndCustomRules(dbSession);
enableFeature(dbSession);
- ruleIndexer.commitAndIndex(dbSession, disabledTemplateAndCustomRuleKeys);
+ ruleIndexer.commitAndIndex(dbSession, disabledTemplateAndCustomRuleIds);
}
}
response.noContent();
permissionTemplateGroup.getTemplateId(), membersGroup.getId(), permissionTemplateGroup.getPermission()));
}
- public List<RuleKey> disableTemplateRulesAndCustomRules(DbSession dbSession) {
+ public List<Integer> disableTemplateRulesAndCustomRules(DbSession dbSession) {
List<RuleDefinitionDto> rules = dbClient.ruleDao().selectAllDefinitions(dbSession).stream()
.filter(r -> r.isTemplate() || r.isCustomRule())
.collect(toList());
r.setStatus(RuleStatus.REMOVED);
dbClient.ruleDao().update(dbSession, r);
});
- return rules.stream().map(RuleDefinitionDto::getKey).collect(toList());
+ return rules.stream().map(RuleDefinitionDto::getId).collect(toList());
}
private void enableFeature(DbSession dbSession) {
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
public List<ActiveRuleChange> update(DbSession dbSession, BuiltInQProfile builtIn, RulesProfileDto rulesProfile) {
// Keep reference to all the activated rules before update
- Set<RuleKey> deactivatedKeys = dbClient.activeRuleDao().selectByRuleProfile(dbSession, rulesProfile)
+ Set<Integer> deactivatedRuleIds = dbClient.activeRuleDao().selectByRuleProfile(dbSession, rulesProfile)
.stream()
- .map(ActiveRuleDto::getRuleKey)
+ .map(ActiveRuleDto::getRuleId)
.collect(MoreCollectors.toHashSet());
- Set<RuleKey> ruleKeys = Stream.concat(
- deactivatedKeys.stream(),
- builtIn.getActiveRules().stream().map(BuiltInQProfile.ActiveRule::getRuleKey))
+ Set<Integer> ruleKeys = Stream.concat(
+ deactivatedRuleIds.stream(),
+ builtIn.getActiveRules().stream().map(BuiltInQProfile.ActiveRule::getRuleId))
.collect(toSet());
RuleActivationContext context = ruleActivator.createContextForBuiltInProfile(dbSession, rulesProfile, ruleKeys);
for (BuiltInQProfile.ActiveRule ar : builtIn.getActiveRules()) {
RuleActivation activation = convert(ar);
activations.add(activation);
- deactivatedKeys.remove(activation.getRuleKey());
+ deactivatedRuleIds.remove(activation.getRuleId());
}
List<ActiveRuleChange> changes = new ArrayList<>();
}
// these rules are not part of the built-in profile anymore
- deactivatedKeys.forEach(ruleKey -> changes.addAll(ruleActivator.deactivate(dbSession, context, ruleKey, false)));
+ deactivatedRuleIds.forEach(ruleKey -> changes.addAll(ruleActivator.deactivate(dbSession, context, ruleKey, false)));
activeRuleIndexer.commitAndIndex(dbSession, changes);
return changes;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
checkArgument(!profile.isBuiltIn(), "Operation forbidden for built-in Quality Profile '%s'", profile.getKee());
BulkChangeResult result = new BulkChangeResult();
- Set<RuleKey> rulesToBeDeactivated = new HashSet<>();
+ Set<Integer> rulesToBeDeactivated = new HashSet<>();
// Keep reference to all the activated rules before backup restore
for (ActiveRuleDto activeRuleDto : db.activeRuleDao().selectByProfile(dbSession, profile)) {
if (activeRuleDto.getInheritance() == null) {
// inherited rules can't be deactivated
- rulesToBeDeactivated.add(activeRuleDto.getRuleKey());
+ rulesToBeDeactivated.add(activeRuleDto.getRuleId());
}
}
- Set<RuleKey> ruleKeys = new HashSet<>(rulesToBeDeactivated);
- activations.forEach(a -> ruleKeys.add(a.getRuleKey()));
+ Set<Integer> ruleKeys = new HashSet<>(rulesToBeDeactivated);
+ activations.forEach(a -> ruleKeys.add(a.getRuleId()));
RuleActivationContext context = activator.createContextForUserProfile(dbSession, profile, ruleKeys);
for (RuleActivation activation : activations) {
try {
List<ActiveRuleChange> changes = activator.activate(dbSession, activation, context);
- rulesToBeDeactivated.remove(activation.getRuleKey());
+ rulesToBeDeactivated.remove(activation.getRuleId());
result.incrementSucceeded();
result.addChanges(changes);
} catch (BadRequestException e) {
}
List<ActiveRuleChange> changes = new ArrayList<>(result.getChanges());
- for (RuleKey ruleKey : rulesToBeDeactivated) {
+ for (Integer ruleId : rulesToBeDeactivated) {
try {
- changes.addAll(activator.deactivate(dbSession, context, ruleKey, false));
+ changes.addAll(activator.deactivate(dbSession, context, ruleId, false));
} catch (BadRequestException e) {
// ignore, probably a rule inherited from parent that can't be deactivated
}
import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.QProfileDto;
*/
BulkChangeResult bulkActivateAndCommit(DbSession dbSession, QProfileDto profile, RuleQuery ruleQuery, @Nullable String severity);
- List<ActiveRuleChange> deactivateAndCommit(DbSession dbSession, QProfileDto profile, Collection<RuleKey> ruleKeys);
+ List<ActiveRuleChange> deactivateAndCommit(DbSession dbSession, QProfileDto profile, Collection<Integer> ruleIds);
BulkChangeResult bulkDeactivateAndCommit(DbSession dbSession, QProfileDto profile, RuleQuery ruleQuery);
import java.util.Set;
import java.util.function.BiFunction;
import javax.annotation.Nullable;
-import org.sonar.api.rule.RuleKey;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
public List<ActiveRuleChange> activateAndCommit(DbSession dbSession, QProfileDto profile, Collection<RuleActivation> activations) {
verifyNotBuiltIn(profile);
- Set<RuleKey> ruleKeys = activations.stream().map(RuleActivation::getRuleKey).collect(MoreCollectors.toHashSet(activations.size()));
- RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleKeys);
+ Set<Integer> ruleIds = activations.stream().map(RuleActivation::getRuleId).collect(MoreCollectors.toHashSet(activations.size()));
+ RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleIds);
List<ActiveRuleChange> changes = new ArrayList<>();
for (RuleActivation activation : activations) {
}
@Override
- public List<ActiveRuleChange> deactivateAndCommit(DbSession dbSession, QProfileDto profile, Collection<RuleKey> ruleKeys) {
+ public List<ActiveRuleChange> deactivateAndCommit(DbSession dbSession, QProfileDto profile, Collection<Integer> ruleIds) {
verifyNotBuiltIn(profile);
- RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleKeys);
+ RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleIds);
List<ActiveRuleChange> changes = new ArrayList<>();
- for (RuleKey ruleKey : ruleKeys) {
- changes.addAll(ruleActivator.deactivate(dbSession, context, ruleKey, false));
+ for (Integer ruleId : ruleIds) {
+ changes.addAll(ruleActivator.deactivate(dbSession, context, ruleId, false));
}
activeRuleIndexer.commitAndIndex(dbSession, changes);
return changes;
@Override
public BulkChangeResult bulkDeactivateAndCommit(DbSession dbSession, QProfileDto profile, RuleQuery ruleQuery) {
verifyNotBuiltIn(profile);
- return doBulk(dbSession, profile, ruleQuery, (context, ruleDefinition) -> ruleActivator.deactivate(dbSession, context, ruleDefinition.getKey(), false));
+ return doBulk(dbSession, profile, ruleQuery, (context, ruleDefinition) -> ruleActivator.deactivate(dbSession, context, ruleDefinition.getId(), false));
}
@Override
private BulkChangeResult doBulk(DbSession dbSession, QProfileDto profile, RuleQuery ruleQuery, BiFunction<RuleActivationContext, RuleDefinitionDto, List<ActiveRuleChange>> fn) {
BulkChangeResult result = new BulkChangeResult();
- Collection<RuleKey> ruleKeys = Sets.newHashSet(ruleIndex.searchAll(ruleQuery));
- RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleKeys);
+ Collection<Integer> ruleIds = Sets.newHashSet(ruleIndex.searchAll(ruleQuery));
+ RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleIds);
- for (RuleKey ruleKey : ruleKeys) {
+ for (Integer ruleId : ruleIds) {
try {
- context.reset(ruleKey);
+ context.reset(ruleId);
RuleDefinitionDto ruleDefinition = context.getRule().get();
List<ActiveRuleChange> changes = fn.apply(context, ruleDefinition);
result.addChanges(changes);
import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.System2;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
db.qualityProfileDao().update(dbSession, profile);
List<OrgActiveRuleDto> parentActiveRules = db.activeRuleDao().selectByProfile(dbSession, parent);
- Collection<RuleKey> ruleKeys = parentActiveRules.stream().map(ActiveRuleDto::getRuleKey).collect(MoreCollectors.toArrayList());
- RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleKeys);
+ Collection<Integer> ruleIds = parentActiveRules.stream().map(ActiveRuleDto::getRuleId).collect(MoreCollectors.toArrayList());
+ RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleIds);
for (ActiveRuleDto parentActiveRule : parentActiveRules) {
try {
db.qualityProfileDao().update(dbSession, profile);
List<OrgActiveRuleDto> activeRules = db.activeRuleDao().selectByProfile(dbSession, profile);
- Collection<RuleKey> ruleKeys = activeRules.stream().map(ActiveRuleDto::getRuleKey).collect(MoreCollectors.toArrayList());
- RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleKeys);
+ Collection<Integer> ruleIds = activeRules.stream().map(ActiveRuleDto::getRuleId).collect(MoreCollectors.toArrayList());
+ RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleIds);
for (OrgActiveRuleDto activeRule : activeRules) {
if (ActiveRuleDto.INHERITED.equals(activeRule.getInheritance())) {
- changes.addAll(ruleActivator.deactivate(dbSession, context, activeRule.getRuleKey(), true));
+ changes.addAll(ruleActivator.deactivate(dbSession, context, activeRule.getRuleId(), true));
} else if (ActiveRuleDto.OVERRIDES.equals(activeRule.getInheritance())) {
context.reset(activeRule.getRuleKey());
// the rules
private final Map<RuleKey, RuleWrapper> rulesByKey;
+ private final Map<Integer, RuleWrapper> rulesById;
private final Map<ActiveRuleKey, ActiveRuleWrapper> activeRulesByKey;
// cursor, moved in the tree of profiles
// rules
this.rulesByKey = Maps.newHashMapWithExpectedSize(builder.rules.size());
+ this.rulesById = Maps.newHashMapWithExpectedSize(builder.rules.size());
ListMultimap<Integer, RuleParamDto> paramsByRuleId = builder.ruleParams.stream().collect(index(RuleParamDto::getRuleId));
for (RuleDefinitionDto rule : builder.rules) {
RuleWrapper wrapper = new RuleWrapper(rule, paramsByRuleId.get(rule.getId()));
rulesByKey.put(rule.getKey(), wrapper);
+ rulesById.put(rule.getId(), wrapper);
}
// profiles
doSwitch(this.baseProfile, this.baseRulesProfile, ruleKey);
}
+ public void reset(Integer ruleId) {
+ this.cascading = false;
+ RuleWrapper ruleWrapper = rulesById.get(ruleId);
+ checkRequest(ruleWrapper != null, "Rule not found: %s", ruleId);
+ doSwitch(this.baseProfile, this.baseRulesProfile, ruleWrapper.get().getKey());
+ }
+
/**
* Moves cursor to a child profile
*/
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ServerSide;
import org.sonar.api.server.rule.RuleParamType;
return activeRule.get();
}
- public List<ActiveRuleChange> deactivate(DbSession dbSession, RuleActivationContext context, RuleKey ruleKey, boolean force) {
- context.reset(ruleKey);
+ public List<ActiveRuleChange> deactivate(DbSession dbSession, RuleActivationContext context, int ruleId, boolean force) {
+ context.reset(ruleId);
return doDeactivate(dbSession, context, force);
}
return value;
}
- public RuleActivationContext createContextForBuiltInProfile(DbSession dbSession, RulesProfileDto builtInProfile, Collection<RuleKey> ruleKeys) {
+ public RuleActivationContext createContextForBuiltInProfile(DbSession dbSession, RulesProfileDto builtInProfile, Collection<Integer> ruleIds) {
checkArgument(builtInProfile.isBuiltIn(), "Rules profile with UUID %s is not built-in", builtInProfile.getKee());
RuleActivationContext.Builder builder = new RuleActivationContext.Builder();
// load rules
- List<RuleDefinitionDto> rules = completeWithRules(dbSession, builder, ruleKeys);
+ List<RuleDefinitionDto> rules = completeWithRules(dbSession, builder, ruleIds);
// load profiles
List<QProfileDto> aliasedBuiltInProfiles = db.qualityProfileDao().selectQProfilesByRuleProfile(dbSession, builtInProfile);
return builder.build();
}
- public RuleActivationContext createContextForUserProfile(DbSession dbSession, QProfileDto profile, Collection<RuleKey> ruleKeys) {
+ public RuleActivationContext createContextForUserProfile(DbSession dbSession, QProfileDto profile, Collection<Integer> ruleIds) {
checkArgument(!profile.isBuiltIn(), "Profile with UUID %s is built-in", profile.getKee());
RuleActivationContext.Builder builder = new RuleActivationContext.Builder();
// load rules
- List<RuleDefinitionDto> rules = completeWithRules(dbSession, builder, ruleKeys);
+ List<RuleDefinitionDto> rules = completeWithRules(dbSession, builder, ruleIds);
// load descendant profiles
List<QProfileDto> profiles = new ArrayList<>(db.qualityProfileDao().selectDescendants(dbSession, singleton(profile)));
return builder.build();
}
- private List<RuleDefinitionDto> completeWithRules(DbSession dbSession, RuleActivationContext.Builder builder, Collection<RuleKey> ruleKeys) {
- List<RuleDefinitionDto> rules = db.ruleDao().selectDefinitionByKeys(dbSession, ruleKeys);
+ private List<RuleDefinitionDto> completeWithRules(DbSession dbSession, RuleActivationContext.Builder builder, Collection<Integer> ruleIds) {
+ List<RuleDefinitionDto> rules = db.ruleDao().selectDefinitionByIds(dbSession, ruleIds);
builder.setRules(rules);
- builder.setRuleParams(db.ruleDao().selectRuleParamsByRuleKeys(dbSession, ruleKeys));
+ builder.setRuleParams(db.ruleDao().selectRuleParamsByRuleIds(dbSession, ruleIds));
return rules;
}
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_UUID;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_REPOSITORY;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_RULE_ID;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_RULE_KEY;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY;
setField(FIELD_ACTIVE_RULE_ID, String.valueOf(id));
}
- public ActiveRuleDoc(Map<String,Object> source) {
+ public ActiveRuleDoc(Map<String, Object> source) {
super(source);
}
@Override
public String getRouting() {
- return getRuleKeyAsString();
+ return getRuleIdAsString();
}
@Override
public String getParent() {
- return getRuleKey().toString();
+ return getRuleIdAsString();
}
RuleKey getRuleKey() {
return getField(FIELD_ACTIVE_RULE_REPOSITORY);
}
+ ActiveRuleDoc setRuleKey(RuleKey ruleKey) {
+ setField(FIELD_ACTIVE_RULE_RULE_KEY, ruleKey.toString());
+ setField(FIELD_ACTIVE_RULE_REPOSITORY, ruleKey.repository());
+ return this;
+ }
+
+ private String getRuleIdAsString() {
+ return getField(FIELD_ACTIVE_RULE_RULE_ID);
+ }
+
+ ActiveRuleDoc setRuleId(int ruleId) {
+ setField(FIELD_ACTIVE_RULE_RULE_ID, String.valueOf(ruleId));
+ return this;
+ }
+
String getSeverity() {
return getNullableField(FIELD_ACTIVE_RULE_SEVERITY);
}
return this;
}
- ActiveRuleDoc setRuleKey(RuleKey ruleKey) {
- setField(FIELD_ACTIVE_RULE_RULE_KEY, ruleKey.toString());
- setField(FIELD_ACTIVE_RULE_REPOSITORY, ruleKey.repository());
- return this;
- }
-
String getRuleProfileUuid() {
return getField(FIELD_ACTIVE_RULE_PROFILE_UUID);
}
public void commitAndIndex(DbSession dbSession, Collection<ActiveRuleChange> changes) {
List<EsQueueDto> items = changes.stream()
.map(ActiveRuleChange::getActiveRule)
- .map(ar -> newQueueDto(String.valueOf(ar.getId()), ID_TYPE_ACTIVE_RULE_ID, ar.getRuleKey().toString()))
+ .map(ar -> newQueueDto(String.valueOf(ar.getId()), ID_TYPE_ACTIVE_RULE_ID, String.valueOf(ar.getRuleId())))
.collect(toArrayList());
dbClient.esQueueDao().insert(dbSession, items);
}
private static IndexRequest newIndexRequest(IndexedActiveRuleDto dto) {
- ActiveRuleDoc doc = new ActiveRuleDoc(dto.getId());
- doc.setRuleProfileUuid(dto.getRuleProfileUuid());
- doc.setSeverity(SeverityUtil.getSeverityFromOrdinal(dto.getSeverity()));
- doc.setRuleKey(RuleKey.of(dto.getRepository(), dto.getKey()));
+ ActiveRuleDoc doc = new ActiveRuleDoc(dto.getId())
+ .setRuleId(dto.getRuleId())
+ .setRuleProfileUuid(dto.getRuleProfileUuid())
+ .setSeverity(SeverityUtil.getSeverityFromOrdinal(dto.getSeverity()))
+ .setRuleKey(RuleKey.of(dto.getRepository(), dto.getKey()));
// all the fields must be present, even if value is null
String inheritance = dto.getInheritance();
doc.setInheritance(inheritance == null ? ActiveRule.Inheritance.NONE.name() : inheritance);
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualityprofile.QProfileDto;
+import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.server.qualityprofile.QProfileRules;
import org.sonar.server.user.UserSession;
String qualityProfileKey = request.mandatoryParam(PARAM_KEY);
userSession.checkLoggedIn();
try (DbSession dbSession = dbClient.openSession(false)) {
+ RuleDefinitionDto rule = wsSupport.getRule(dbSession, ruleKey);
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(qualityProfileKey));
OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
wsSupport.checkCanEdit(dbSession, organization, profile);
- ruleActivator.deactivateAndCommit(dbSession, profile, singletonList(ruleKey));
+ ruleActivator.deactivateAndCommit(dbSession, profile, singletonList(rule.getId()));
}
response.noContent();
}
import java.util.Optional;
import javax.annotation.Nullable;
+import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.api.server.ws.WebService.NewParam;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.OrganizationPermission;
import org.sonar.db.qualityprofile.QProfileDto;
+import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.organization.DefaultOrganizationProvider;
"No organization with key '%s'", organizationOrDefaultKey);
}
+ public RuleDefinitionDto getRule(DbSession dbSession, RuleKey ruleKey) {
+ Optional<RuleDefinitionDto> ruleDefinitionDto = dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey);
+ return checkFoundWithOptional(ruleDefinitionDto, "Rule with key '%s' not found", ruleKey);
+ }
+
/**
* Get the Quality profile specified by the reference {@code ref}.
*
checkArgument(dbClient.organizationMemberDao().select(dbSession, organization.getUuid(), user.getId()).isPresent(),
"User '%s' is not member of organization '%s'", user.getLogin(), organization.getKey());
}
-
}
Profiler profiler = Profiler.create(LOG).startInfo("Register rules");
try (DbSession dbSession = dbClient.openSession(false)) {
Map<RuleKey, RuleDefinitionDto> allRules = loadRules(dbSession);
+ List<Integer> ruleIdsToIndex = new ArrayList<>();
Map<Integer, Set<SingleDeprecatedRuleKey>> existingDeprecatedRuleKeys = loadDeprecatedRuleKeys(dbSession);
RulesDefinition.Context context = defLoader.load();
List<RulesDefinition.ExtendedRepository> repositories = getRepositories(context);
- List<RuleKey> keysToIndex = new ArrayList<>();
boolean orgsEnabled = organizationFlags.isEnabled(dbSession);
for (RulesDefinition.ExtendedRepository repoDef : repositories) {
}
continue;
}
- boolean relevantForIndex = registerRule(ruleDef, allRules, existingDeprecatedRuleKeys, dbSession);
- if (relevantForIndex) {
- keysToIndex.add(ruleKey);
- }
+
+ registerRule(ruleDef, allRules, existingDeprecatedRuleKeys, dbSession)
+ .ifPresent(ruleIdsToIndex::add);
}
dbSession.commit();
}
List<RuleDefinitionDto> removedRules = processRemainingDbRules(allRules.values(), dbSession);
List<ActiveRuleChange> changes = removeActiveRulesOnStillExistingRepositories(dbSession, removedRules, context);
dbSession.commit();
- keysToIndex.addAll(removedRules.stream().map(RuleDefinitionDto::getKey).collect(Collectors.toList()));
+ ruleIdsToIndex.addAll(removedRules.stream().map(RuleDefinitionDto::getId).collect(Collectors.toList()));
persistRepositories(dbSession, context.repositories());
// FIXME lack of resiliency, active rules index is corrupted if rule index fails
// to be updated. Only a single DB commit should be executed.
- ruleIndexer.commitAndIndex(dbSession, keysToIndex);
+ ruleIndexer.commitAndIndex(dbSession, ruleIdsToIndex);
activeRuleIndexer.commitAndIndex(dbSession, changes);
profiler.stopDebug();
dbSession.commit();
}
- private boolean registerRule(RulesDefinition.Rule ruleDef, Map<RuleKey, RuleDefinitionDto> allRules, Map<Integer,
- Set<SingleDeprecatedRuleKey>> existingDeprecatedRuleKeys, DbSession session) {
+ /**
+ * @return the id of the rule if it's just been created or if it's been updated.
+ */
+ private Optional<Integer> registerRule(RulesDefinition.Rule ruleDef, Map<RuleKey, RuleDefinitionDto> allRules,
+ Map<Integer, Set<SingleDeprecatedRuleKey>> existingDeprecatedRuleKeys, DbSession session) {
RuleKey ruleKey = RuleKey.of(ruleDef.repository().key(), ruleDef.key());
RuleDefinitionDto existingRule = allRules.remove(ruleKey);
newRule = false;
}
- boolean executeUpdate = mergeRule(ruleDef, rule);
- executeUpdate |= mergeDebtDefinitions(ruleDef, rule);
- executeUpdate |= mergeTags(ruleDef, rule);
+ boolean executeUpdate = false;
+ if (mergeRule(ruleDef, rule)) {
+ executeUpdate = true;
+ }
+
+ if (mergeDebtDefinitions(ruleDef, rule)) {
+ executeUpdate = true;
+ }
+
+ if (mergeTags(ruleDef, rule)) {
+ executeUpdate = true;
+ }
if (executeUpdate) {
update(session, rule);
mergeParams(ruleDef, rule, session);
updateDeprecatedKeys(ruleDef, rule, existingDeprecatedRuleKeys, session);
- return newRule || executeUpdate;
+ if (newRule || executeUpdate) {
+ return Optional.of(rule.getId());
+ }
+ return Optional.empty();
}
private Map<RuleKey, RuleDefinitionDto> loadRules(DbSession session) {
.collect(Collectors.groupingBy(SingleDeprecatedRuleKey::getRuleId, toSet()));
}
-
private List<RulesDefinition.ExtendedRepository> getRepositories(RulesDefinition.Context context) {
List<RulesDefinition.ExtendedRepository> repositories = new ArrayList<>();
for (RulesDefinition.Repository repoDef : context.repositories()) {
deprecatedRuleKeysToBeCreated
.forEach(r -> dbClient.ruleDao().insert(dbSession, new DeprecatedRuleKeyDto()
- .setUuid(uuidFactory.create())
- .setRuleId(rule.getId())
- .setOldRepositoryKey(r.getOldRepositoryKey())
- .setOldRuleKey(r.getOldRuleKey())
- .setCreatedAt(system2.now())
- )
- );
+ .setUuid(uuidFactory.create())
+ .setRuleId(rule.getId())
+ .setOldRepositoryKey(r.getOldRepositoryKey())
+ .setOldRuleKey(r.getOldRuleKey())
+ .setCreatedAt(system2.now())));
}
private List<RuleDefinitionDto> processRemainingDbRules(Collection<RuleDefinitionDto> existingRules, DbSession session) {
validateCustomRule(newRule, dbSession, templateKey);
RuleKey customRuleKey = RuleKey.of(templateRule.getRepositoryKey(), newRule.ruleKey());
+ Optional<RuleDefinitionDto> definition = loadRule(dbSession, customRuleKey);
+ int customRuleId = definition.map(d -> updateExistingRule(d, newRule, dbSession))
+ .orElseGet(() -> createCustomRule(customRuleKey, newRule, templateRule, dbSession));
- Optional<RuleDefinitionDto> definition = loadRule(customRuleKey, dbSession);
- if (definition.isPresent()) {
- updateExistingRule(definition.get(), newRule, dbSession);
- } else {
- createCustomRule(customRuleKey, newRule, templateRule, dbSession);
- }
-
- ruleIndexer.commitAndIndex(dbSession, customRuleKey);
+ ruleIndexer.commitAndIndex(dbSession, customRuleId);
return customRuleKey;
}
}
}
- private Optional<RuleDefinitionDto> loadRule(RuleKey ruleKey, DbSession dbSession) {
+ private Optional<RuleDefinitionDto> loadRule(DbSession dbSession, RuleKey ruleKey) {
return dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey);
}
- private RuleKey createCustomRule(RuleKey ruleKey, NewCustomRule newRule, RuleDto templateRuleDto, DbSession dbSession) {
+ private int createCustomRule(RuleKey ruleKey, NewCustomRule newRule, RuleDto templateRuleDto, DbSession dbSession) {
RuleDefinitionDto ruleDefinition = new RuleDefinitionDto()
.setRuleKey(ruleKey)
.setPluginKey(templateRuleDto.getPluginKey())
String customRuleParamValue = Strings.emptyToNull(newRule.parameter(templateRuleParamDto.getName()));
createCustomRuleParams(customRuleParamValue, ruleDefinition, templateRuleParamDto, dbSession);
}
- return ruleKey;
+ return ruleDefinition.getId();
}
private void createCustomRuleParams(@Nullable String paramValue, RuleDefinitionDto ruleDto, RuleParamDto templateRuleParam, DbSession dbSession) {
dbClient.ruleDao().insertRuleParam(dbSession, ruleDto, ruleParamDto);
}
- private RuleKey updateExistingRule(RuleDefinitionDto ruleDto, NewCustomRule newRule, DbSession dbSession) {
+ private int updateExistingRule(RuleDefinitionDto ruleDto, NewCustomRule newRule, DbSession dbSession) {
if (ruleDto.getStatus().equals(RuleStatus.REMOVED)) {
if (newRule.isPreventReactivation()) {
throw new ReactivationException(format("A removed rule with the key '%s' already exists", ruleDto.getKey().rule()), ruleDto.getKey());
} else {
throw new IllegalArgumentException(format("A rule with the key '%s' already exists", ruleDto.getKey().rule()));
}
- return ruleDto.getKey();
+ return ruleDto.getId();
}
}
apply(update, rule, userSession);
update(dbSession, rule);
updateParameters(dbSession, organization, update, rule);
- ruleIndexer.commitAndIndex(dbSession, rule.getKey(), organization);
+ ruleIndexer.commitAndIndex(dbSession, rule.getId(), organization);
return true;
}
@Override
public String getId() {
- return key().toString();
+ return idAsString();
+ }
+
+ private String idAsString() {
+ return getField(RuleIndexDefinition.FIELD_RULE_ID);
+ }
+
+ public RuleDoc setId(int ruleId) {
+ setField(RuleIndexDefinition.FIELD_RULE_ID, String.valueOf(ruleId));
+ return this;
}
@Override
public String getRouting() {
- return keyAsString();
+ return idAsString();
}
@Override
public static RuleDoc of(RuleForIndexingDto dto) {
RuleDoc ruleDoc = new RuleDoc()
+ .setId(dto.getId())
.setKey(dto.getRuleKey().toString())
.setRepository(dto.getRepository())
.setInternalKey(dto.getInternalKey())
@Override
public String getId() {
- return idOf(getRuleKey(), getScope());
+ return idOf(getRuleId(), getScope());
}
@Override
public String getRouting() {
- return getRuleKey().toString();
+ return ruleIdAsString();
}
@Override
public String getParent() {
- return getRuleKey().toString();
+ return ruleIdAsString();
+ }
+
+ public int getRuleId() {
+ return Integer.valueOf(ruleIdAsString());
+ }
+
+ private String ruleIdAsString() {
+ return getField(RuleIndexDefinition.FIELD_RULE_EXTENSION_RULE_ID);
+ }
+
+ public RuleExtensionDoc setRuleId(int ruleId) {
+ setField(RuleIndexDefinition.FIELD_RULE_EXTENSION_RULE_ID, String.valueOf(ruleId));
+ return this;
}
public RuleKey getRuleKey() {
public static RuleExtensionDoc of(RuleForIndexingDto rule) {
return new RuleExtensionDoc()
+ .setRuleId(rule.getId())
.setRuleKey(rule.getRuleKey())
.setScope(RuleExtensionScope.system())
.setTags(rule.getSystemTagsAsSet());
public static RuleExtensionDoc of(RuleExtensionForIndexingDto rule) {
return new RuleExtensionDoc()
+ .setRuleId(rule.getRuleId())
.setRuleKey(rule.getRuleKey())
.setScope(RuleExtensionScope.organization(rule.getOrganizationUuid()))
.setTags(rule.getTagsAsSet());
}
- public static String idOf(RuleKey ruleKey, RuleExtensionScope scope) {
- return ruleKey + "|" + scope.getScope();
+ public static String idOf(int ruleId, RuleExtensionScope scope) {
+ return ruleId + "|" + scope.getScope();
}
@Override
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
this.system2 = system2;
}
- public SearchIdResult<RuleKey> search(RuleQuery query, SearchOptions options) {
+ public SearchIdResult<Integer> search(RuleQuery query, SearchOptions options) {
SearchRequestBuilder esSearch = client
.prepareSearch(INDEX_TYPE_RULE);
}
esSearch.setQuery(boolQuery().must(qb).filter(fb));
- return new SearchIdResult<>(esSearch.get(), RuleKey::parse, system2.getDefaultTimeZone());
+ return new SearchIdResult<>(esSearch.get(), Integer::parseInt, system2.getDefaultTimeZone());
}
/**
- * Return all keys matching the search query, without pagination nor facets
+ * Return all rule ids matching the search query, without pagination nor facets
*/
- public Iterator<RuleKey> searchAll(RuleQuery query) {
+ public Iterator<Integer> searchAll(RuleQuery query) {
SearchRequestBuilder esSearch = client
.prepareSearch(INDEX_TYPE_RULE)
.setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
esSearch.setQuery(boolQuery().must(qb).filter(fb));
SearchResponse response = esSearch.get();
- return scrollIds(client, response, RuleKey::parse);
+ return scrollIds(client, response, Integer::parseInt);
}
/* Build main query (search based) */
static final String INDEX = "rules";
public static final IndexType INDEX_TYPE_RULE = new IndexType(INDEX, "rule");
+ public static final String FIELD_RULE_ID = "id";
public static final String FIELD_RULE_KEY = "key";
public static final String FIELD_RULE_REPOSITORY = "repo";
public static final String FIELD_RULE_RULE_KEY = "ruleKey";
public static final IndexType INDEX_TYPE_RULE_EXTENSION = new IndexType(INDEX, "ruleExtension");
/** The uuid of a {@link RuleExtensionScope} */
public static final String FIELD_RULE_EXTENSION_SCOPE = "scope";
+ public static final String FIELD_RULE_EXTENSION_RULE_ID = "ruleId";
public static final String FIELD_RULE_EXTENSION_RULE_KEY = "ruleKey";
public static final String FIELD_RULE_EXTENSION_TAGS = "tags";
// Active rule fields
public static final IndexType INDEX_TYPE_ACTIVE_RULE = new IndexType(INDEX, "activeRule");
public static final String FIELD_ACTIVE_RULE_ID = "id";
+ public static final String FIELD_ACTIVE_RULE_RULE_ID = "ruleId";
public static final String FIELD_ACTIVE_RULE_REPOSITORY = "repo";
public static final String FIELD_ACTIVE_RULE_INHERITANCE = "inheritance";
public static final String FIELD_ACTIVE_RULE_PROFILE_UUID = "ruleProfile";
activeRuleMapping.setAttribute("_parent", ImmutableMap.of("type", INDEX_TYPE_RULE.getType()));
activeRuleMapping.keywordFieldBuilder(FIELD_ACTIVE_RULE_ID).disableNorms().build();
+ activeRuleMapping.keywordFieldBuilder(FIELD_ACTIVE_RULE_RULE_ID).disableNorms().build();
activeRuleMapping.keywordFieldBuilder(FIELD_ACTIVE_RULE_RULE_KEY).addSubFields(SORTABLE_ANALYZER).build();
activeRuleMapping.keywordFieldBuilder(FIELD_ACTIVE_RULE_REPOSITORY).build();
activeRuleMapping.keywordFieldBuilder(FIELD_ACTIVE_RULE_PROFILE_UUID).disableNorms().build();
NewIndex.NewIndexType ruleMapping = index.createType(INDEX_TYPE_RULE.getType());
ruleMapping.setEnableSource(enableSource);
+ ruleMapping.keywordFieldBuilder(FIELD_RULE_ID).disableNorms().build();
ruleMapping.keywordFieldBuilder(FIELD_RULE_KEY).addSubFields(SORTABLE_ANALYZER).build();
ruleMapping.keywordFieldBuilder(FIELD_RULE_RULE_KEY).addSubFields(SORTABLE_ANALYZER).build();
ruleMapping.keywordFieldBuilder(FIELD_RULE_REPOSITORY).build();
import java.util.Objects;
import java.util.Set;
import org.elasticsearch.action.index.IndexRequest;
-import org.sonar.api.rule.RuleKey;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
// index all organization extensions
if (uninitializedIndexTypes.contains(INDEX_TYPE_RULE_EXTENSION)) {
- dbClient.ruleDao().scrollIndexingRuleExtensions(dbSession, dto ->
- bulk.add(newRuleExtensionDocIndexRequest(dto))
- );
+ dbClient.ruleDao().scrollIndexingRuleExtensions(dbSession, dto -> bulk.add(newRuleExtensionDocIndexRequest(dto)));
}
bulk.stop();
}
}
- public void commitAndIndex(DbSession dbSession, RuleKey ruleKey) {
- commitAndIndex(dbSession, singletonList(ruleKey));
+ public void commitAndIndex(DbSession dbSession, int ruleId) {
+ commitAndIndex(dbSession, singletonList(ruleId));
}
- public void commitAndIndex(DbSession dbSession, Collection<RuleKey> ruleKeys) {
- List<EsQueueDto> items = ruleKeys.stream()
+ public void commitAndIndex(DbSession dbSession, Collection<Integer> ruleIds) {
+ List<EsQueueDto> items = ruleIds.stream()
.map(RuleIndexer::createQueueDtoForRule)
.collect(MoreCollectors.toArrayList());
/**
* Commit a change on a rule and its extension on the given organization
*/
- public void commitAndIndex(DbSession dbSession, RuleKey ruleKey, OrganizationDto organization) {
- List<EsQueueDto> items = asList(createQueueDtoForRule(ruleKey), createQueueDtoForRuleExtension(ruleKey, organization));
+ public void commitAndIndex(DbSession dbSession, int ruleId, OrganizationDto organization) {
+ List<EsQueueDto> items = asList(createQueueDtoForRule(ruleId), createQueueDtoForRuleExtension(ruleId, organization));
dbClient.esQueueDao().insert(dbSession, items);
dbSession.commit();
postCommit(dbSession, items);
BulkIndexer bulkIndexer = createBulkIndexer(Size.REGULAR, new OneToOneResilientIndexingListener(dbClient, dbSession, items));
bulkIndexer.start();
- Set<RuleKey> ruleKeys = items
+ Set<Integer> ruleIds = items
.stream()
- .map(i -> RuleKey.parse(i.getDocId()))
+ .map(i -> Integer.parseInt(i.getDocId()))
.collect(toHashSet(items.size()));
- dbClient.ruleDao().scrollIndexingRulesByKeys(dbSession, ruleKeys,
+ dbClient.ruleDao().scrollIndexingRulesByKeys(dbSession, ruleIds,
r -> {
bulkIndexer.add(newRuleDocIndexRequest(r));
bulkIndexer.add(newRuleExtensionDocIndexRequest(r));
- ruleKeys.remove(r.getRuleKey());
+ ruleIds.remove(r.getId());
});
// the remaining items reference rows that don't exist in db. They must
// be deleted from index.
- ruleKeys.forEach(ruleKey -> {
- bulkIndexer.addDeletion(INDEX_TYPE_RULE, ruleKey.toString(), ruleKey.toString());
- bulkIndexer.addDeletion(INDEX_TYPE_RULE_EXTENSION, RuleExtensionDoc.idOf(ruleKey, RuleExtensionScope.system()), ruleKey.toString());
+ ruleIds.forEach(ruleId -> {
+ bulkIndexer.addDeletion(INDEX_TYPE_RULE, ruleId.toString(), ruleId.toString());
+ bulkIndexer.addDeletion(INDEX_TYPE_RULE_EXTENSION, RuleExtensionDoc.idOf(ruleId, RuleExtensionScope.system()), ruleId.toString());
});
return bulkIndexer.stop();
// only index requests, no deletion requests.
// Deactivated users are not deleted but updated.
r -> {
- RuleExtensionId docId = new RuleExtensionId(r.getOrganizationUuid(), r.getPluginName(), r.getPluginRuleKey());
+ RuleExtensionId docId = new RuleExtensionId(r.getOrganizationUuid(), r.getRuleId());
docIds.remove(docId);
bulkIndexer.add(newRuleExtensionDocIndexRequest(r));
});
// the remaining items reference rows that don't exist in db. They must
// be deleted from index.
- docIds.forEach(docId -> {
- RuleKey ruleKey = RuleKey.of(docId.getRepositoryName(), docId.getRuleKey());
- bulkIndexer.addDeletion(INDEX_TYPE_RULE_EXTENSION, docId.getId(), ruleKey.toString());
- });
+ docIds.forEach(docId -> bulkIndexer.addDeletion(INDEX_TYPE_RULE_EXTENSION, docId.getId(), docId.getId()));
return bulkIndexer.stop();
}
RuleDoc doc = RuleDoc.of(ruleForIndexingDto);
return new IndexRequest(INDEX_TYPE_RULE.getIndex(), INDEX_TYPE_RULE.getType())
- .id(doc.key().toString())
+ .id(doc.getId())
.routing(doc.getRouting())
.source(doc.getFields());
}
return new RuleExtensionId(esQueueDto.getDocId());
}
- private static EsQueueDto createQueueDtoForRule(RuleKey ruleKey) {
- return EsQueueDto.create("rules/rule", ruleKey.toString(), null, ruleKey.toString());
+ private static EsQueueDto createQueueDtoForRule(int ruleId) {
+ String docId = String.valueOf(ruleId);
+ return EsQueueDto.create("rules/rule", docId, null, docId);
}
- private static EsQueueDto createQueueDtoForRuleExtension(RuleKey ruleKey, OrganizationDto organization) {
- String docId = RuleExtensionDoc.idOf(ruleKey, RuleExtensionScope.organization(organization));
- return EsQueueDto.create("rules/ruleExtension", docId, null, ruleKey.toString());
+ private static EsQueueDto createQueueDtoForRuleExtension(int ruleId, OrganizationDto organization) {
+ String docId = RuleExtensionDoc.idOf(ruleId, RuleExtensionScope.organization(organization));
+ return EsQueueDto.create("rules/ruleExtension", docId, null, String.valueOf(ruleId));
}
}
rule.setUpdatedAt(system2.now());
dbClient.ruleDao().update(dbSession, rule);
- ruleIndexer.commitAndIndex(dbSession, ruleKey);
+ ruleIndexer.commitAndIndex(dbSession, rule.getId());
}
}
}
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
}
private SearchResult doSearch(DbSession dbSession, RuleQuery query, SearchOptions context) {
- SearchIdResult<RuleKey> result = ruleIndex.search(query, context);
- List<RuleKey> ruleKeys = result.getIds();
+ SearchIdResult<Integer> result = ruleIndex.search(query, context);
+ List<Integer> ruleIds = result.getIds();
// rule order is managed by ES
- Map<RuleKey, RuleDto> rulesByRuleKey = Maps.uniqueIndex(
- dbClient.ruleDao().selectByKeys(dbSession, query.getOrganization(), ruleKeys), RuleDto::getKey);
+ Map<Integer, RuleDto> rulesByRuleKey = Maps.uniqueIndex(
+ dbClient.ruleDao().selectByIds(dbSession, query.getOrganization().getUuid(), ruleIds), RuleDto::getId);
List<RuleDto> rules = new ArrayList<>();
- for (RuleKey ruleKey : ruleKeys) {
- RuleDto rule = rulesByRuleKey.get(ruleKey);
+ for (Integer ruleId : ruleIds) {
+ RuleDto rule = rulesByRuleKey.get(ruleId);
if (rule != null) {
rules.add(rule);
}
}
- List<Integer> ruleIds = rules.stream().map(RuleDto::getId).collect(MoreCollectors.toList());
List<Integer> templateRuleIds = rules.stream()
.map(RuleDto::getTemplateId)
.filter(Objects::nonNull)
public void list_tags() {
RuleDefinitionDto r1 = db.rules().insert();
RuleDefinitionDto r2 = db.rules().insert();
- ruleIndexer.commitAndIndex(db.getSession(), asList(r1.getKey(), r2.getKey()));
+ ruleIndexer.commitAndIndex(db.getSession(), asList(r1.getId(), r2.getId()));
OrganizationDto org = db.organizations().insert();
OrganizationDto anotherOrg = db.organizations().insert();
public void return_tags_from_rules() {
userSession.logIn();
RuleDefinitionDto r = dbTester.rules().insert(setSystemTags("tag1"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), r.getKey());
+ ruleIndexer.commitAndIndex(dbTester.getSession(), r.getId());
dbTester.rules().insertOrUpdateMetadata(r, organization, setTags("tag2"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), r.getKey(), organization);
+ ruleIndexer.commitAndIndex(dbTester.getSession(), r.getId(), organization);
RuleDefinitionDto r2 = dbTester.rules().insert(setSystemTags("tag3"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), r2.getKey());
+ ruleIndexer.commitAndIndex(dbTester.getSession(), r2.getId());
dbTester.rules().insertOrUpdateMetadata(r2, organization, setTags("tag4", "tag5"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), r2.getKey(), organization);
+ ruleIndexer.commitAndIndex(dbTester.getSession(), r2.getId(), organization);
String result = ws.newRequest()
.setParam("organization", organization.getKey())
insertIssueWithBrowsePermission(insertRuleWithoutTags(), "tag3", "tag4", "tag5");
RuleDefinitionDto r = dbTester.rules().insert(setSystemTags("tag6"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), r.getKey());
+ ruleIndexer.commitAndIndex(dbTester.getSession(), r.getId());
dbTester.rules().insertOrUpdateMetadata(r, organization, setTags("tag7"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), r.getKey(), organization);
+ ruleIndexer.commitAndIndex(dbTester.getSession(), r.getId(), organization);
String result = ws.newRequest()
.setParam("organization", organization.getKey())
insertIssueWithBrowsePermission(insertRuleWithoutTags(), "convention");
RuleDefinitionDto r = dbTester.rules().insert(setSystemTags("cwe"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), r.getKey());
+ ruleIndexer.commitAndIndex(dbTester.getSession(), r.getId());
dbTester.rules().insertOrUpdateMetadata(r, organization, setTags("security"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), r.getKey(), organization);
+ ruleIndexer.commitAndIndex(dbTester.getSession(), r.getId(), organization);
String result = ws.newRequest()
.setParam("organization", organization.getKey())
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.sonar.api.config.internal.MapSettings;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbTester;
tuple(custom.getKey(), RuleStatus.REMOVED));
@SuppressWarnings("unchecked")
- Class<ArrayList<RuleKey>> listClass = (Class<ArrayList<RuleKey>>) (Class) ArrayList.class;
- ArgumentCaptor<ArrayList<RuleKey>> indexedRuleKeys = ArgumentCaptor.forClass(listClass);
+ Class<ArrayList<Integer>> listClass = (Class<ArrayList<Integer>>) (Class) ArrayList.class;
+ ArgumentCaptor<ArrayList<Integer>> indexedRuleKeys = ArgumentCaptor.forClass(listClass);
verify(ruleIndexer).commitAndIndex(any(), indexedRuleKeys.capture());
- assertThat(indexedRuleKeys.getValue()).containsExactlyInAnyOrder(template.getKey(), custom.getKey());
+ assertThat(indexedRuleKeys.getValue()).containsExactlyInAnyOrder(template.getId(), custom.getId());
}
@Test
*/
package org.sonar.server.platform;
+import java.util.Random;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
}
private static RuleDoc newRuleDoc() {
- return new RuleDoc().setKey(RuleTesting.XOO_X1.toString()).setRepository(RuleTesting.XOO_X1.repository());
+ return new RuleDoc().setId(new Random().nextInt(942)).setKey(RuleTesting.XOO_X1.toString()).setRepository(RuleTesting.XOO_X1.repository());
}
private ComponentDoc newComponentDoc() {
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.qualityprofile.QProfileDto;
-import org.sonar.db.rule.RuleDto;
+import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.es.EsTester;
private QProfileRules qProfileRules;
private QProfileComparison comparison;
- private RuleDto xooRule1;
- private RuleDto xooRule2;
+ private RuleDefinitionDto xooRule1;
+ private RuleDefinitionDto xooRule2;
private QProfileDto left;
private QProfileDto right;
qProfileRules = new QProfileRulesImpl(db, ruleActivator, ruleIndex, activeRuleIndexer);
comparison = new QProfileComparison(db);
- xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR");
- xooRule2 = RuleTesting.newXooX2().setSeverity("MAJOR");
- db.ruleDao().insert(dbSession, xooRule1.getDefinition());
- db.ruleDao().insert(dbSession, xooRule2.getDefinition());
- db.ruleDao().insertRuleParam(dbSession, xooRule1.getDefinition(), RuleParamDto.createFor(xooRule1.getDefinition())
+ xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR").getDefinition();
+ xooRule2 = RuleTesting.newXooX2().setSeverity("MAJOR").getDefinition();
+ db.ruleDao().insert(dbSession, xooRule1);
+ db.ruleDao().insert(dbSession, xooRule2);
+ db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
.setName("max").setType(RuleParamType.INTEGER.type()));
- db.ruleDao().insertRuleParam(dbSession, xooRule1.getDefinition(), RuleParamDto.createFor(xooRule1.getDefinition())
+ db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
.setName("min").setType(RuleParamType.INTEGER.type()));
left = QProfileTesting.newXooP1("org-123");
@Test
public void compare_modified_severity() {
qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getId(), xooRule1.getKey(), Severity.CRITICAL, null)));
- qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule2.getId(), xooRule1.getKey(), Severity.BLOCKER, null)));
+ qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getId(), xooRule1.getKey(), Severity.BLOCKER, null)));
QProfileComparisonResult result = comparison.compare(dbSession, left, right);
assertThat(result.left().getKee()).isEqualTo(left.getKee());
assertThat(changes).hasSize(0);
}
- @Test
- public void deactivation_fails_if_rule_does_not_exist() {
- RuleDefinitionDto rule = createRule();
- QProfileDto profile = createProfile(rule);
- RuleKey ruleKey = RuleKey.parse("unknown:xxx");
-
- expectFailure("Rule not found: " + ruleKey, () -> underTest.deactivateAndCommit(db.getSession(), profile, singleton(ruleKey)));
- }
-
@Test
public void deactivate_rule_that_has_REMOVED_status() {
RuleDefinitionDto rule = createRule();
}
private List<ActiveRuleChange> deactivate(QProfileDto profile, RuleDefinitionDto rule) {
- return underTest.deactivateAndCommit(db.getSession(), profile, singleton(rule.getKey()));
+ return underTest.deactivateAndCommit(db.getSession(), profile, singleton(rule.getId()));
}
private List<ActiveRuleChange> activate(QProfileDto profile, RuleActivation activation) {
import org.sonar.server.qualityprofile.ActiveRuleChange;
import org.sonar.server.rule.index.RuleIndexDefinition;
+import static java.lang.String.valueOf;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.Collections.emptySet;
commitAndIndex(rule1, ar);
- EsQueueDto expectedItem = EsQueueDto.create(INDEX_TYPE_ACTIVE_RULE.format(), "" + ar.getId(), "activeRuleId", ar.getRuleKey().toString());
+ EsQueueDto expectedItem = EsQueueDto.create(INDEX_TYPE_ACTIVE_RULE.format(), "" + ar.getId(), "activeRuleId", valueOf(ar.getRuleId()));
assertThatEsQueueContainsExactly(expectedItem);
}
RuleDefinitionDto rule1 = createRule();
createActiveRule(rule1, parent1);
- ruleIndexer.commitAndIndex(dbSession, rule1.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule1.getId());
activeRuleIndexer.indexOnStartup(emptySet());
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, child.getKee())).isEmpty();
RuleDefinitionDto rule2 = createRule();
createActiveRule(rule1, parent1);
createActiveRule(rule2, parent2);
- ruleIndexer.commitAndIndex(dbSession, asList(rule1.getKey(), rule2.getKey()));
+ ruleIndexer.commitAndIndex(dbSession, asList(rule1.getId(), rule2.getId()));
activeRuleIndexer.indexOnStartup(emptySet());
// Set parent 1
RuleDefinitionDto rule1 = createRule();
createActiveRule(rule1, parent);
- ruleIndexer.commitAndIndex(dbSession, rule1.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule1.getId());
activeRuleIndexer.indexOnStartup(emptySet());
// Set parent
RuleDefinitionDto rule2 = createRule();
createActiveRule(rule1, parent1);
createActiveRule(rule2, parent2);
- ruleIndexer.commitAndIndex(dbSession, rule1.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule1.getId());
activeRuleIndexer.indexOnStartup(emptySet());
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, child.getKee())).isEmpty();
RuleDefinitionDto rule1 = createRule();
createActiveRule(rule1, parent);
- ruleIndexer.commitAndIndex(dbSession, rule1.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule1.getId());
activeRuleIndexer.indexOnStartup(emptySet());
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, child.getKee())).isEmpty();
RuleDefinitionDto rule2 = createRule();
createActiveRule(rule1, parent1);
createActiveRule(rule2, parent2);
- ruleIndexer.commitAndIndex(dbSession, asList(rule1.getKey(), rule2.getKey()));
+ ruleIndexer.commitAndIndex(dbSession, asList(rule1.getId(), rule2.getId()));
activeRuleIndexer.indexOnStartup(emptySet());
// Set parent 1
qProfileTree.setParentAndCommit(dbSession, child, parent1);
private void insertRule(RuleDefinitionDto ruleDto) {
dbClient.ruleDao().insert(dbSession, ruleDto);
dbSession.commit();
- ruleIndexer.commitAndIndex(dbSession, ruleDto.getKey());
+ ruleIndexer.commitAndIndex(dbSession, ruleDto.getId());
}
private CreateWsResponse executeRequest(String name, String language) {
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.OrganizationPermission;
import org.sonar.db.qualityprofile.QProfileDto;
+import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.BadRequestException;
public void deactivate_rule_in_default_organization() {
userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization);
- RuleKey ruleKey = RuleTesting.randomRuleKey();
+ RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
TestRequest request = ws.newRequest()
.setMethod("POST")
- .setParam(PARAM_RULE, ruleKey.toString())
+ .setParam(PARAM_RULE, rule.getKey().toString())
.setParam(PARAM_KEY, qualityProfile.getKee());
TestResponse response = request.execute();
assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
- Class<Collection<RuleKey>> collectionClass = (Class<Collection<RuleKey>>) (Class) Collection.class;
- ArgumentCaptor<Collection<RuleKey>> ruleKeyCaptor = ArgumentCaptor.forClass(collectionClass);
+ ArgumentCaptor<Collection<Integer>> ruleIdCaptor = ruleIdCollectionCaptor();
ArgumentCaptor<QProfileDto> qProfileDtoCaptor = ArgumentCaptor.forClass(QProfileDto.class);
- verify(qProfileRules).deactivateAndCommit(any(DbSession.class), qProfileDtoCaptor.capture(), ruleKeyCaptor.capture());
- assertThat(ruleKeyCaptor.getValue()).containsExactly(ruleKey);
+ verify(qProfileRules).deactivateAndCommit(any(DbSession.class), qProfileDtoCaptor.capture(), ruleIdCaptor.capture());
+ assertThat(ruleIdCaptor.getValue()).containsExactly(rule.getId());
assertThat(qProfileDtoCaptor.getValue().getKee()).isEqualTo(qualityProfile.getKee());
}
public void deactivate_rule_in_specific_organization() {
userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
- RuleKey ruleKey = RuleTesting.randomRuleKey();
+ RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
TestRequest request = ws.newRequest()
.setMethod("POST")
.setParam("organization", organization.getKey())
- .setParam(PARAM_RULE, ruleKey.toString())
+ .setParam(PARAM_RULE, rule.getKey().toString())
.setParam(PARAM_KEY, qualityProfile.getKee());
TestResponse response = request.execute();
assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
- Class<Collection<RuleKey>> collectionClass = (Class<Collection<RuleKey>>) (Class) Collection.class;
- ArgumentCaptor<Collection<RuleKey>> ruleKeyCaptor = ArgumentCaptor.forClass(collectionClass);
+ ArgumentCaptor<Collection<Integer>> ruleIdCaptor = ruleIdCollectionCaptor();
ArgumentCaptor<QProfileDto> qProfileDtoCaptor = ArgumentCaptor.forClass(QProfileDto.class);
- verify(qProfileRules).deactivateAndCommit(any(DbSession.class), qProfileDtoCaptor.capture(), ruleKeyCaptor.capture());
- assertThat(ruleKeyCaptor.getValue()).containsExactly(ruleKey);
+ verify(qProfileRules).deactivateAndCommit(any(DbSession.class), qProfileDtoCaptor.capture(), ruleIdCaptor.capture());
+ assertThat(ruleIdCaptor.getValue()).containsExactly(rule.getId());
assertThat(qProfileDtoCaptor.getValue().getKee()).isEqualTo(qualityProfile.getKee());
}
db.qualityProfiles().addUserPermission(qualityProfile, user);
userSession.logIn(user);
RuleKey ruleKey = RuleTesting.randomRuleKey();
+ db.rules().insert(ruleKey);
ws.newRequest()
.setMethod("POST")
@Test
public void fail_if_not_organization_quality_profile_administrator() {
+ RuleDefinitionDto rule = db.rules().insert();
userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
TestRequest request = ws.newRequest()
.setMethod("POST")
- .setParam(PARAM_RULE, RuleTesting.newRuleDto().getKey().toString())
+ .setParam(PARAM_RULE, rule.getKey().toString())
.setParam(PARAM_KEY, qualityProfile.getKee());
expectedException.expect(ForbiddenException.class);
@Test
public void fail_deactivate_if_built_in_profile() {
+ RuleDefinitionDto rule = db.rules().insert();
userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization, profile -> profile.setIsBuiltIn(true));
TestRequest request = ws.newRequest()
.setMethod("POST")
- .setParam(PARAM_RULE, RuleTesting.newRuleDto().getKey().toString())
+ .setParam(PARAM_RULE, rule.getKey().toString())
.setParam(PARAM_KEY, qualityProfile.getKee());
expectedException.expect(BadRequestException.class);
request.execute();
}
+
+ private static ArgumentCaptor<Collection<Integer>> ruleIdCollectionCaptor() {
+ Class<Collection<Integer>> collectionClass = (Class<Collection<Integer>>) (Class) Collection.class;
+ return ArgumentCaptor.forClass(collectionClass);
+ }
}
RuleDefinitionDto rule1 = dbTester.rules().insert();
RuleDefinitionDto rule2 = dbTester.rules().insert();
RuleDefinitionDto rule3 = dbTester.rules().insert();
- ruleIndexer.commitAndIndex(dbTester.getSession(), asList(rule1.getKey(), rule2.getKey(), rule3.getKey()));
+ ruleIndexer.commitAndIndex(dbTester.getSession(), asList(rule1.getId(), rule2.getId(), rule3.getId()));
QProfileDto parent = dbTester.qualityProfiles().insert(organization);
dbTester.qualityProfiles().activateRule(parent, rule1);
@Test
public void inheritance_ignores_removed_rules() throws Exception {
RuleDefinitionDto rule = dbTester.rules().insert(r -> r.setStatus(RuleStatus.REMOVED));
- ruleIndexer.commitAndIndex(dbTester.getSession(), rule.getKey());
+ ruleIndexer.commitAndIndex(dbTester.getSession(), rule.getId());
QProfileDto profile = dbTester.qualityProfiles().insert(organization);
dbTester.qualityProfiles().activateRule(profile, rule);
.setUpdatedAt(now)
.setCreatedAt(now);
dbClient.ruleDao().insert(dbSession, rule);
- ruleIndexer.commitAndIndex(dbSession, rule.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule.getId());
return rule;
}
QProfileDto profile = createProfile("java");
RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto");
createActiveRule(rule, profile);
- ruleIndexer.commitAndIndex(dbSession, rule.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule.getId());
activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());
// 0. Assert No Active Rule for profile
public void activate_rule() {
QProfileDto profile = createProfile("java");
RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto");
- ruleIndexer.commitAndIndex(dbSession, rule.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule.getId());
// 0. Assert No Active Rule for profile
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();
public void activate_rule_diff_languages() {
QProfileDto profile = createProfile("java");
RuleDefinitionDto rule = createRule("php", "toto");
- ruleIndexer.commitAndIndex(dbSession, rule.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule.getId());
// 0. Assert No Active Rule for profile
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();
public void activate_rule_override_severity() {
QProfileDto profile = createProfile("java");
RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto");
- ruleIndexer.commitAndIndex(dbSession, rule.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule.getId());
// 0. Assert No Active Rule for profile
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();
.setSeverity(Severity.BLOCKER)
.setStatus(RuleStatus.READY);
dbClient.ruleDao().insert(dbSession, rule);
- ruleIndexer.commitAndIndex(dbSession, rule.getKey());
+ ruleIndexer.commitAndIndex(dbSession, rule.getId());
return rule;
}
import org.sonar.server.rule.index.RuleQuery;
import static com.google.common.collect.Sets.newHashSet;
+import static java.lang.String.valueOf;
import static java.util.Collections.singletonList;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
assertThat(param.getDefaultValue()).isEqualTo("default1");
// verify index
- assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY2);
+ RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), dbTester.getDefaultOrganization(), RULE_KEY2);
+ assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(rule1.getId(), rule2.getId());
// verify repositories
assertThat(dbClient.ruleRepositoryDao().selectAll(dbTester.getSession())).extracting(RuleRepositoryDto::getKey).containsOnly("fake");
// register one rule
execute(context -> {
- NewRepository repo = context.createRepository("fake", "java");
+ RulesDefinition.NewRepository repo = context.createRepository("fake", "java");
repo.createRule(ruleKey)
.setName(randomAlphanumeric(5))
.setHtmlDescription(randomAlphanumeric(20));
});
// verify db
- assertThat(dbClient.ruleDao().selectAllDefinitions(dbTester.getSession()))
+ List<RuleDefinitionDto> rules = dbClient.ruleDao().selectAllDefinitions(dbTester.getSession());
+ assertThat(rules)
.extracting(RuleDefinitionDto::getKey)
.extracting(RuleKey::rule)
.containsExactly(ruleKey);
+ RuleDefinitionDto rule = rules.iterator().next();
// verify index
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds())
- .extracting(RuleKey::rule)
- .containsExactly(ruleKey);
+ .containsExactly(rule.getId());
// register no rule
execute(context -> context.createRepository("fake", "java").done());
// verify index
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds())
- .extracting(RuleKey::rule)
.isEmpty();
}
public void update_and_remove_rules_on_changes() {
execute(new FakeRepositoryV1());
assertThat(dbClient.ruleDao().selectAllDefinitions(dbTester.getSession())).hasSize(2);
- assertThat(esTester.getIds(RuleIndexDefinition.INDEX_TYPE_RULE)).containsOnly(RULE_KEY1.toString(), RULE_KEY2.toString());
+ RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY1);
+ RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY2);
+ assertThat(esTester.getIds(RuleIndexDefinition.INDEX_TYPE_RULE)).containsOnly(valueOf(rule1.getId()), valueOf(rule2.getId()));
// user adds tags and sets markdown note
- OrganizationDto defaultOrganization = dbTester.getDefaultOrganization();
- RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY1);
rule1.setTags(newHashSet("usertag1", "usertag2"));
rule1.setNoteData("user *note*");
rule1.setNoteUserLogin("marius");
assertThat(param.getDefaultValue()).isEqualTo("default1 v2");
// rule2 has been removed -> status set to REMOVED but db row is not deleted
- RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY2);
+ rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY2);
assertThat(rule2.getStatus()).isEqualTo(RuleStatus.REMOVED);
assertThat(rule2.getUpdatedAt()).isEqualTo(DATE2.getTime());
assertThat(rule3.getStatus()).isEqualTo(RuleStatus.READY);
// verify index
- assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY3);
+ assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(rule1.getId(), rule3.getId());
// verify repositories
assertThat(dbClient.ruleRepositoryDao().selectAll(dbTester.getSession())).extracting(RuleRepositoryDto::getKey).containsOnly("fake");
public void do_not_update_already_removed_rules() {
execute(new FakeRepositoryV1());
assertThat(dbClient.ruleDao().selectAllDefinitions(dbTester.getSession())).hasSize(2);
- assertThat(esTester.getIds(RuleIndexDefinition.INDEX_TYPE_RULE)).containsOnly(RULE_KEY1.toString(), RULE_KEY2.toString());
-
+ RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY1);
RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY2);
+ assertThat(esTester.getIds(RuleIndexDefinition.INDEX_TYPE_RULE)).containsOnly(valueOf(rule1.getId()), valueOf(rule2.getId()));
+
assertThat(rule2.getStatus()).isEqualTo(RuleStatus.READY);
when(system.now()).thenReturn(DATE2.getTime());
execute(new FakeRepositoryV2());
// On MySQL, need to update a rule otherwise rule2 will be seen as READY, but why ???
- dbClient.ruleDao().update(dbTester.getSession(), dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY1).getDefinition());
+ dbClient.ruleDao().update(dbTester.getSession(), rule1.getDefinition());
dbTester.getSession().commit();
// rule2 is removed
rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY2);
+ RuleDto rule3 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), defaultOrganization, RULE_KEY3);
assertThat(rule2.getStatus()).isEqualTo(RuleStatus.REMOVED);
- assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY3);
+ assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(rule1.getId(), rule3.getId());
when(system.now()).thenReturn(DATE3.getTime());
execute(new FakeRepositoryV2());
assertThat(rule2.getStatus()).isEqualTo(RuleStatus.REMOVED);
assertThat(rule2.getUpdatedAt()).isEqualTo(DATE2.getTime());
- assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY3);
+ assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(rule1.getId(), rule3.getId());
}
@Test
@Override
public void define(Context context) {
NewRepository repo = context.createRepository("fake", "java");
- NewRule rule1 = repo.createRule("rule1")
+ NewRule rule1 = repo.createRule(RULE_KEY1.rule())
.setName("One")
.setHtmlDescription("Description of One")
.setSeverity(BLOCKER)
rule1.createParam("param1").setDescription("parameter one").setDefaultValue("default1");
rule1.createParam("param2").setDescription("parameter two").setDefaultValue("default2");
- repo.createRule("rule2")
+ repo.createRule(RULE_KEY2.rule())
.setName("Two")
.setHtmlDescription("Minimal rule");
repo.done();
NewRepository repo = context.createRepository("fake", "java");
// almost all the attributes of rule1 are changed
- NewRule rule1 = repo.createRule("rule1")
+ NewRule rule1 = repo.createRule(RULE_KEY1.rule())
.setName("One v2")
.setHtmlDescription("Description of One v2")
.setSeverity(INFO)
rule1.createParam("param2").setDescription("parameter two v2").setDefaultValue("default2 v2");
// rule2 is dropped, rule3 is new
- repo.createRule("rule3")
+ repo.createRule(RULE_KEY3.rule())
.setName("Three")
.setHtmlDescription("Rule Three");
repo.done();
// From user
assertThat(param.getDefaultValue()).isEqualTo("a.*");
- assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(customRuleKey, templateRule.getKey());
+ assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(rule.getId(), templateRule.getId());
}
@Test
dbTester.rules().insert(templateRule.getDefinition());
dbTester.rules().insertOrUpdateMetadata(templateRule.getMetadata().setRuleId(templateRule.getId()));
dbTester.rules().insertRuleParam(templateRule.getDefinition(), param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), templateRule.getDefinition().getKey());
+ ruleIndexer.commitAndIndex(dbTester.getSession(), templateRule.getDefinition().getId());
return templateRule;
}
dbTester.rules().insert(templateRule);
dbTester.rules().insertRuleParam(templateRule,
param -> param.setName("myIntegers").setType("INTEGER,multiple=true,values=1;2;3").setDescription("My Integers").setDefaultValue("1"));
- ruleIndexer.commitAndIndex(dbTester.getSession(), templateRule.getKey());
+ ruleIndexer.commitAndIndex(dbTester.getSession(), templateRule.getId());
return templateRule;
}
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
-import org.assertj.core.api.ThrowableAssertAlternative;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.es.EsTester;
import org.sonar.server.es.SearchOptions;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualityprofile.QProfileTesting;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.tester.UserSessionRule;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
-import static org.sonar.api.rule.RuleStatus.REMOVED;
import static org.sonar.api.rule.Severity.CRITICAL;
import static org.sonar.db.rule.RuleTesting.newRule;
import static org.sonar.server.rule.RuleUpdate.createForCustomRule;
assertThat(params).extracting(RuleParamDto::getDefaultValue).containsOnly("b.*", null);
// Verify in index
- assertThat(ruleIndex.search(new RuleQuery().setQueryText("New name"), new SearchOptions()).getIds()).containsOnly(customRule.getKey());
- assertThat(ruleIndex.search(new RuleQuery().setQueryText("New description"), new SearchOptions()).getIds()).containsOnly(customRule.getKey());
+ assertThat(ruleIndex.search(new RuleQuery().setQueryText("New name"), new SearchOptions()).getIds()).containsOnly(customRule.getId());
+ assertThat(ruleIndex.search(new RuleQuery().setQueryText("New description"), new SearchOptions()).getIds()).containsOnly(customRule.getId());
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Old name"), new SearchOptions()).getTotal()).isZero();
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Old description"), new SearchOptions()).getTotal()).isZero();
import static org.sonar.process.ProcessProperties.Property.CLUSTER_ENABLED;
import static org.sonar.server.es.DefaultIndexSettingsElement.ENGLISH_HTML_ANALYZER;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_HTML_DESCRIPTION;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_ID;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_KEY;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_REPOSITORY;
import static org.sonar.server.rule.index.RuleIndexDefinition.INDEX_TYPE_RULE;
// the following method fails if PUT fails
tester.putDocuments(INDEX_TYPE_RULE, new RuleDoc(ImmutableMap.of(
+ FIELD_RULE_ID, "123",
FIELD_RULE_HTML_DESCRIPTION, longText,
FIELD_RULE_REPOSITORY, "squid",
FIELD_RULE_KEY, "squid:S001")));
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
+import org.elasticsearch.search.SearchHit;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
// key
RuleQuery query = new RuleQuery().setQueryText("X001");
- assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(js1.getKey(), cobol1.getKey());
+ assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(js1.getId(), cobol1.getId());
// partial key does not match
query = new RuleQuery().setQueryText("X00");
// repo:key -> nice-to-have !
query = new RuleQuery().setQueryText("javascript:X001");
- assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(js1.getKey());
+ assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(js1.getId());
}
@Test
index();
RuleQuery query = new RuleQuery().setQueryText("x001");
- assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(ruleDto.getKey());
+ assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(ruleDto.getId());
}
@Test
index();
RuleQuery protectedCharsQuery = new RuleQuery().setQueryText(rule.getName());
- List<RuleKey> results = underTest.search(protectedCharsQuery, new SearchOptions()).getIds();
- assertThat(results).containsOnly(rule.getKey());
+ List<Integer> results = underTest.search(protectedCharsQuery, new SearchOptions()).getIds();
+ assertThat(results).containsOnly(rule.getId());
}
@Test
index();
// partial match at word boundary
- assertThat(underTest.search(new RuleQuery().setQueryText("CWE"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule1.getKey(), rule2.getKey(), rule3.getKey());
+ assertThat(underTest.search(new RuleQuery().setQueryText("CWE"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule1.getId(), rule2.getId(), rule3.getId());
// full match
- assertThat(underTest.search(new RuleQuery().setQueryText("CWE-123"), new SearchOptions()).getIds()).containsExactly(rule1.getKey());
+ assertThat(underTest.search(new RuleQuery().setQueryText("CWE-123"), new SearchOptions()).getIds()).containsExactly(rule1.getId());
// match somewhere else in the text
- assertThat(underTest.search(new RuleQuery().setQueryText("CWE-1000"), new SearchOptions()).getIds()).containsExactly(rule3.getKey());
- assertThat(underTest.search(new RuleQuery().setQueryText("CWE 1000"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule3.getKey(), rule1.getKey());
+ assertThat(underTest.search(new RuleQuery().setQueryText("CWE-1000"), new SearchOptions()).getIds()).containsExactly(rule3.getId());
+ assertThat(underTest.search(new RuleQuery().setQueryText("CWE 1000"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule3.getId(), rule1.getId());
// several words
- assertThat(underTest.search(new RuleQuery().setQueryText("great rule"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule1.getKey(), rule2.getKey(), rule3.getKey());
- assertThat(underTest.search(new RuleQuery().setQueryText("rule Another"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule2.getKey(), rule3.getKey());
+ assertThat(underTest.search(new RuleQuery().setQueryText("great rule"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule1.getId(), rule2.getId(), rule3.getId());
+ assertThat(underTest.search(new RuleQuery().setQueryText("rule Another"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule2.getId(), rule3.getId());
// no matches
assertThat(underTest.search(new RuleQuery().setQueryText("unexisting"), new SearchOptions()).getIds()).isEmpty();
// html
assertThat(underTest.search(new RuleQuery().setQueryText("h1"), new SearchOptions()).getIds()).isEmpty();
assertThat(underTest.search(new RuleQuery().setQueryText("style"), new SearchOptions()).getIds()).isEmpty();
- assertThat(underTest.search(new RuleQuery().setQueryText("special"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule4.getKey());
- assertThat(underTest.search(new RuleQuery().setQueryText("geeks formatting inside tables"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule4.getKey());
+ assertThat(underTest.search(new RuleQuery().setQueryText("special"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule4.getId());
+ assertThat(underTest.search(new RuleQuery().setQueryText("geeks formatting inside tables"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule4.getId());
// long words
- assertThat(underTest.search(new RuleQuery().setQueryText("missunderstand"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule5.getKey());
- assertThat(underTest.search(new RuleQuery().setQueryText("missunderstandings"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule5.getKey());
- assertThat(underTest.search(new RuleQuery().setQueryText("alsdkjfnadklsjfnadkdfnsksdjfn"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule5.getKey());
- assertThat(underTest.search(new RuleQuery().setQueryText("internationalization"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule5.getKey());
+ assertThat(underTest.search(new RuleQuery().setQueryText("missunderstand"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule5.getId());
+ assertThat(underTest.search(new RuleQuery().setQueryText("missunderstandings"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule5.getId());
+ assertThat(underTest.search(new RuleQuery().setQueryText("alsdkjfnadklsjfnadkdfnsksdjfn"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule5.getId());
+ assertThat(underTest.search(new RuleQuery().setQueryText("internationalization"), new SearchOptions()).getIds()).containsExactlyInAnyOrder(rule5.getId());
assertThat(underTest.search(new RuleQuery().setQueryText("internationalizationBlaBla"), new SearchOptions()).getIds()).isEmpty();
}
RuleQuery query = new RuleQuery().setRepositories(asList("checkstyle", "pmd"));
SearchIdResult results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsExactly(pmd.getKey());
+ assertThat(results.getIds()).containsExactly(pmd.getId());
// no results
query = new RuleQuery().setRepositories(singletonList("checkstyle"));
// empty list => no filter
query = new RuleQuery().setRepositories(emptyList());
- assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(findbugs.getKey(), pmd.getKey());
+ assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(findbugs.getId(), pmd.getId());
}
@Test
// type3 in filter
query = new RuleQuery().setTypes(of(VULNERABILITY));
- assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(vulnerability.getKey());
+ assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(vulnerability.getId());
query = new RuleQuery().setTypes(of(BUG));
- assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(bug1.getKey(), bug2.getKey());
+ assertThat(underTest.search(query, new SearchOptions()).getIds()).containsOnly(bug1.getId(), bug2.getId());
// types in query => nothing
query = new RuleQuery().setQueryText("code smell bug vulnerability");
// Only template
query = new RuleQuery().setIsTemplate(true);
results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsOnly(ruleIsTemplate.getKey());
+ assertThat(results.getIds()).containsOnly(ruleIsTemplate.getId());
// Only not template
query = new RuleQuery().setIsTemplate(false);
results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsOnly(ruleNoTemplate.getKey());
+ assertThat(results.getIds()).containsOnly(ruleNoTemplate.getId());
// null => no filter
query = new RuleQuery().setIsTemplate(null);
results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsOnly(ruleIsTemplate.getKey(), ruleNoTemplate.getKey());
+ assertThat(results.getIds()).containsOnly(ruleIsTemplate.getId(), ruleNoTemplate.getId());
}
@Test
// Only custom rule
query = new RuleQuery().setTemplateKey(template.getKey().toString());
results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsOnly(customRule.getKey());
+ assertThat(results.getIds()).containsOnly(customRule.getId());
// null => no filter
query = new RuleQuery().setTemplateKey(null);
RuleQuery query = new RuleQuery().setLanguages(asList("cobol", "js"));
SearchIdResult results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsOnly(javascript.getKey());
+ assertThat(results.getIds()).containsOnly(javascript.getId());
// no results
query = new RuleQuery().setLanguages(singletonList("cpp"));
RuleQuery query = new RuleQuery().setSeverities(asList(INFO, MINOR));
SearchIdResult results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsOnly(info.getKey());
+ assertThat(results.getIds()).containsOnly(info.getId());
// no results
query = new RuleQuery().setSeverities(singletonList(MINOR));
index();
RuleQuery query = new RuleQuery().setStatuses(asList(RuleStatus.DEPRECATED, RuleStatus.READY));
- SearchIdResult<RuleKey> results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsOnly(ready.getKey());
+ SearchIdResult<Integer> results = underTest.search(query, new SearchOptions());
+ assertThat(results.getIds()).containsOnly(ready.getId());
// no results
query = new RuleQuery().setStatuses(singletonList(RuleStatus.DEPRECATED));
}
private void verifySearch(RuleQuery query, RuleDefinitionDto... expectedRules) {
- SearchIdResult<RuleKey> result = underTest.search(query, new SearchOptions());
+ SearchIdResult<Integer> result = underTest.search(query, new SearchOptions());
assertThat(result.getTotal()).isEqualTo((long) expectedRules.length);
assertThat(result.getIds()).hasSize(expectedRules.length);
for (RuleDefinitionDto expectedRule : expectedRules) {
- assertThat(result.getIds()).contains(expectedRule.getKey());
+ assertThat(result.getIds()).contains(expectedRule.getId());
}
}
}
private void verifyFacet(RuleQuery query, String facet, Map.Entry<String, Long>... expectedBuckets) {
- SearchIdResult<RuleKey> result = underTest.search(query, new SearchOptions().addFacets(facet));
+ SearchIdResult<Integer> result = underTest.search(query, new SearchOptions().addFacets(facet));
assertThat(result.getFacets().get(facet))
.containsOnly(expectedBuckets);
}
private void verifyNoFacet(RuleQuery query, String facet) {
- SearchIdResult<RuleKey> result = underTest.search(query, new SearchOptions().addFacets(facet));
+ SearchIdResult<Integer> result = underTest.search(query, new SearchOptions().addFacets(facet));
assertThat(result.getFacets().get(facet)).isNull();
}
// ascending
RuleQuery query = new RuleQuery().setSortField(RuleIndexDefinition.FIELD_RULE_NAME);
- SearchIdResult<RuleKey> results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsExactly(abc.getKey(), abcd.getKey(), fgh.getKey());
+ SearchIdResult<Integer> results = underTest.search(query, new SearchOptions());
+ assertThat(results.getIds()).containsExactly(abc.getId(), abcd.getId(), fgh.getId());
// descending
query = new RuleQuery().setSortField(RuleIndexDefinition.FIELD_RULE_NAME).setAscendingSort(false);
results = underTest.search(query, new SearchOptions());
- assertThat(results.getIds()).containsExactly(fgh.getKey(), abcd.getKey(), abc.getKey());
+ assertThat(results.getIds()).containsExactly(fgh.getId(), abcd.getId(), abc.getId());
}
@Test
RuleDefinitionDto older = createRule(setCreatedAt(1000L), setUpdatedAt(2000L));
index();
- SearchIdResult<RuleKey> results = underTest.search(new RuleQuery(), new SearchOptions());
- assertThat(results.getIds()).containsExactly(oldest.getKey(), older.getKey(), old.getKey());
+ SearchIdResult<Integer> results = underTest.search(new RuleQuery(), new SearchOptions());
+ assertThat(results.getIds()).containsExactly(oldest.getId(), older.getId(), old.getId());
}
@Test
index();
// inactive rules on profile
+ List<SearchHit> ruleDocs = es.getDocuments(INDEX_TYPE_RULE);
+ List<SearchHit> activeRuleDocs = es.getDocuments(INDEX_TYPE_ACTIVE_RULE);
assertThat(underTest.searchAll(new RuleQuery().setActivation(false).setQProfile(profile2)))
- .containsOnly(rule2.getKey(), rule3.getKey());
+ .containsOnly(rule2.getId(), rule3.getId());
// active rules on profile
assertThat(underTest.searchAll(new RuleQuery().setActivation(true).setQProfile(profile2)))
- .containsOnly(rule1.getKey());
+ .containsOnly(rule1.getId());
}
}
@Test
public void index() {
dbClient.ruleDao().insert(dbSession, rule);
- underTest.commitAndIndex(dbSession, rule.getKey());
+ underTest.commitAndIndex(dbSession, rule.getId());
assertThat(esTester.countDocuments(RuleIndexDefinition.INDEX_TYPE_RULE)).isEqualTo(1);
}
// Create and Index rule
dbClient.ruleDao().insert(dbSession, rule.setStatus(RuleStatus.READY));
dbSession.commit();
- underTest.commitAndIndex(dbTester.getSession(), rule.getKey());
+ underTest.commitAndIndex(dbTester.getSession(), rule.getId());
assertThat(esTester.countDocuments(RuleIndexDefinition.INDEX_TYPE_RULE)).isEqualTo(1);
// Remove rule
dbTester.getDbClient().ruleDao().update(dbTester.getSession(), rule.setStatus(RuleStatus.READY).setUpdatedAt(2000000000000L));
- underTest.commitAndIndex(dbTester.getSession(), rule.getKey());
+ underTest.commitAndIndex(dbTester.getSession(), rule.getId());
assertThat(esTester.countDocuments(RuleIndexDefinition.INDEX_TYPE_RULE)).isEqualTo(1);
}
@Test
public void index_rule_extension_with_long_id() {
RuleDefinitionDto rule = dbTester.rules().insert(r -> r.setRuleKey(RuleTesting.randomRuleKeyOfMaximumLength()));
- underTest.commitAndIndex(dbTester.getSession(), rule.getKey());
+ underTest.commitAndIndex(dbTester.getSession(), rule.getId());
OrganizationDto organization = dbTester.organizations().insert();
dbTester.rules().insertOrUpdateMetadata(rule, organization, m -> m.setTags(ImmutableSet.of("bla")));
- underTest.commitAndIndex(dbTester.getSession(), rule.getKey(), organization);
+ underTest.commitAndIndex(dbTester.getSession(), rule.getId(), organization);
RuleExtensionDoc doc = new RuleExtensionDoc()
+ .setRuleId(rule.getId())
.setRuleKey(rule.getKey())
.setScope(RuleExtensionScope.organization(organization.getUuid()));
assertThat(
public void index_long_rule_description() {
String description = IntStream.range(0, 100000).map(i -> i % 100).mapToObj(Integer::toString).collect(Collectors.joining(" "));
RuleDefinitionDto rule = dbTester.rules().insert(r -> r.setDescription(description));
- underTest.commitAndIndex(dbTester.getSession(), rule.getKey());
+ underTest.commitAndIndex(dbTester.getSession(), rule.getId());
assertThat(esTester.countDocuments(RuleIndexDefinition.INDEX_TYPE_RULE)).isEqualTo(1);
}
.setParam("key", customRule.getKey().toString())
.execute();
- verify(ruleIndexer).commitAndIndex(any(), eq(customRule.getKey()));
+ verify(ruleIndexer).commitAndIndex(any(), eq(customRule.getId()));
// Verify custom rule has status REMOVED
RuleDefinitionDto customRuleReloaded = dbClient.ruleDao().selectOrFailDefinitionByKey(dbSession, customRule.getKey());
@SafeVarargs
private final RuleMetadataDto insertMetadata(OrganizationDto organization, RuleDefinitionDto rule, Consumer<RuleMetadataDto>... populaters) {
RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(rule, organization, populaters);
- ruleIndexer.commitAndIndex(db.getSession(), rule.getKey(), organization);
+ ruleIndexer.commitAndIndex(db.getSession(), rule.getId(), organization);
return metadata;
}
*/
package org.sonar.server.rule.ws;
-import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
DbSession session = dbTester.getSession();
ruleDao.insert(session, ruleDto);
session.commit();
- ruleIndexer.commitAndIndex(session, ruleDto.getKey());
+ ruleIndexer.commitAndIndex(session, ruleDto.getId());
RuleParamDto regexParam = RuleParamDto.createFor(ruleDto).setName("regex").setType("STRING").setDescription("Reg *exp*").setDefaultValue(".*");
ruleDao.insertRuleParam(session, ruleDto, regexParam);
private RuleDefinitionDto insertRule() {
RuleDefinitionDto rule = dbTester.rules().insert();
- ruleIndexer.commitAndIndex(dbTester.getSession(), rule.getKey());
+ ruleIndexer.commitAndIndex(dbTester.getSession(), rule.getId());
return rule;
}
@SafeVarargs
private final RuleMetadataDto insertMetadata(OrganizationDto organization, RuleDefinitionDto rule, Consumer<RuleMetadataDto>... populaters) {
RuleMetadataDto metadata = dbTester.rules().insertOrUpdateMetadata(rule, organization, populaters);
- ruleIndexer.commitAndIndex(dbTester.getSession(), rule.getKey(), organization);
+ ruleIndexer.commitAndIndex(dbTester.getSession(), rule.getId(), organization);
return metadata;
}
}
@Test
public void system_tag() {
RuleDefinitionDto r = db.rules().insert(setSystemTags("tag"));
- ruleIndexer.commitAndIndex(db.getSession(), r.getKey());
+ ruleIndexer.commitAndIndex(db.getSession(), r.getId());
String result = ws.newRequest().execute().getInput();
assertJson(result).isSimilarTo("{\"tags\":[\"tag\"]}");
@Test
public void tag() {
RuleDefinitionDto r = db.rules().insert(setSystemTags());
- ruleIndexer.commitAndIndex(db.getSession(), r.getKey());
+ ruleIndexer.commitAndIndex(db.getSession(), r.getId());
db.rules().insertOrUpdateMetadata(r, organization, setTags("tag"));
- ruleIndexer.commitAndIndex(db.getSession(), r.getKey(), organization);
+ ruleIndexer.commitAndIndex(db.getSession(), r.getId(), organization);
String result = ws.newRequest().setParam("organization", organization.getKey()).execute().getInput();
assertJson(result).isSimilarTo("{\"tags\":[\"tag\"]}");