import org.sonar.db.DbSession;
import org.sonar.db.rule.RuleDao;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.server.rule.index.RuleIndexer;
import static java.util.Objects.requireNonNull;
*/
public RuleDto persistAndIndex(DbSession dbSession, NewAdHocRule adHoc) {
RuleDao dao = dbClient.ruleDao();
- RuleMetadataDto metadata;
long now = system2.now();
RuleDto ruleDtoToUpdate = findOrCreateRuleDto(dbSession, adHoc, dao, now);
- metadata = ruleDtoToUpdate.getMetadata();
if (adHoc.hasDetails()) {
boolean changed = false;
- if (!Objects.equals(metadata.getAdHocName(), adHoc.getName())) {
- metadata.setAdHocName(substring(adHoc.getName(), 0, MAX_LENGTH_AD_HOC_NAME));
+ if (!Objects.equals(ruleDtoToUpdate.getAdHocName(), adHoc.getName())) {
+ ruleDtoToUpdate.setAdHocName(substring(adHoc.getName(), 0, MAX_LENGTH_AD_HOC_NAME));
changed = true;
}
- if (!Objects.equals(metadata.getAdHocDescription(), adHoc.getDescription())) {
- metadata.setAdHocDescription(substring(adHoc.getDescription(), 0, MAX_LENGTH_AD_HOC_DESC));
+ if (!Objects.equals(ruleDtoToUpdate.getAdHocDescription(), adHoc.getDescription())) {
+ ruleDtoToUpdate.setAdHocDescription(substring(adHoc.getDescription(), 0, MAX_LENGTH_AD_HOC_DESC));
changed = true;
}
- if (!Objects.equals(metadata.getAdHocSeverity(), adHoc.getSeverity())) {
- metadata.setAdHocSeverity(adHoc.getSeverity());
+ if (!Objects.equals(ruleDtoToUpdate.getAdHocSeverity(), adHoc.getSeverity())) {
+ ruleDtoToUpdate.setAdHocSeverity(adHoc.getSeverity());
changed = true;
}
RuleType ruleType = requireNonNull(adHoc.getRuleType(), "Rule type should not be null");
- if (!Objects.equals(metadata.getAdHocType(), ruleType.getDbConstant())) {
- metadata.setAdHocType(ruleType);
+ if (!Objects.equals(ruleDtoToUpdate.getAdHocType(), ruleType.getDbConstant())) {
+ ruleDtoToUpdate.setAdHocType(ruleType);
changed = true;
}
if (changed) {
private static final String ISSUE_STATUS_CLOSED = "CLOSED";
private static final String QUERY = "select" +
- " r.uuid, r.plugin_key, r.plugin_rule_key, r.plugin_name, r.name, r.status, r.rule_type, r.scope, rm.rule_uuid, rm.ad_hoc_name," +
- " rm.ad_hoc_description,rm.ad_hoc_severity, rm.ad_hoc_type" +
+ " r.uuid, r.plugin_key, r.plugin_rule_key, r.plugin_name, r.name, r.status, r.rule_type, r.scope, r.ad_hoc_name," +
+ " r.ad_hoc_description,r.ad_hoc_severity, r.ad_hoc_type" +
" from rules r" +
- " left join rules_metadata rm on rm.rule_uuid = r.uuid" +
" inner join issues i on r.uuid = i.rule_uuid and r.status <> ? and r.is_ad_hoc = ?" +
" left join components p on p.uuid = i.project_uuid" +
" left join project_branches pb on pb.uuid = p.uuid" +
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.scanner.protocol.Constants;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.server.es.EsTester;
assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty();
assertThat(rule.getSeverity()).isNull();
assertThat(rule.getType()).isZero();
- assertThat(rule.getMetadata().getAdHocName()).isNull();
- assertThat(rule.getMetadata().getAdHocDescription()).isNull();
- assertThat(rule.getMetadata().getAdHocSeverity()).isNull();
- assertThat(rule.getMetadata().getAdHocType()).isNull();
+ assertThat(rule.getAdHocName()).isNull();
+ assertThat(rule.getAdHocDescription()).isNull();
+ assertThat(rule.getAdHocSeverity()).isNull();
+ assertThat(rule.getAdHocType()).isNull();
}
@Test
assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty();
assertThat(rule.getSeverity()).isNull();
assertThat(rule.getType()).isZero();
- assertThat(rule.getMetadata().getAdHocName()).isEqualTo("No condition assigned");
- assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo("A description");
- assertThat(rule.getMetadata().getAdHocSeverity()).isEqualTo(Severity.BLOCKER);
- assertThat(rule.getMetadata().getAdHocType()).isEqualTo(RuleType.BUG.getDbConstant());
+ assertThat(rule.getAdHocName()).isEqualTo("No condition assigned");
+ assertThat(rule.getAdHocDescription()).isEqualTo("A description");
+ assertThat(rule.getAdHocSeverity()).isEqualTo(Severity.BLOCKER);
+ assertThat(rule.getAdHocType()).isEqualTo(RuleType.BUG.getDbConstant());
}
@Test
RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
- assertThat(rule.getMetadata().getAdHocName()).isEqualTo(repeat("a", 200));
- assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo(repeat("a", 16_777_215));
+ assertThat(rule.getAdHocName()).isEqualTo(repeat("a", 200));
+ assertThat(rule.getAdHocDescription()).isEqualTo(repeat("a", 16_777_215));
}
@Test
assertThat(ruleUpdated.getRuleDescriptionSectionDtos()).isEmpty();
assertThat(ruleUpdated.getSeverity()).isNull();
assertThat(ruleUpdated.getType()).isZero();
- assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo("No condition assigned updated");
- assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo("A description updated");
- assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(Severity.CRITICAL);
- assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
+ assertThat(ruleUpdated.getAdHocName()).isEqualTo("No condition assigned updated");
+ assertThat(ruleUpdated.getAdHocDescription()).isEqualTo("A description updated");
+ assertThat(ruleUpdated.getAdHocSeverity()).isEqualTo(Severity.CRITICAL);
+ assertThat(ruleUpdated.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
assertThat(ruleUpdated.getCreatedAt()).isEqualTo(creationDate);
assertThat(ruleUpdated.getUpdatedAt()).isGreaterThan(creationDate);
}
@Test
public void does_not_update_rule_when_no_change() {
RuleDto rule = db.rules().insert(r -> r.setRepositoryKey("external_eslint").setIsExternal(true).setIsAdHoc(true));
- RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule);
RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
.setEngineId("eslint")
.setRuleId(rule.getKey().rule())
- .setName(ruleMetadata.getAdHocName())
- .setDescription(ruleMetadata.getAdHocDescription())
- .setSeverity(Constants.Severity.valueOf(ruleMetadata.getAdHocSeverity()))
- .setType(ScannerReport.IssueType.forNumber(ruleMetadata.getAdHocType()))
+ .setName(rule.getAdHocName())
+ .setDescription(rule.getAdHocDescription())
+ .setSeverity(Constants.Severity.valueOf(rule.getAdHocSeverity()))
+ .setType(ScannerReport.IssueType.forNumber(rule.getAdHocType()))
.build()));
assertThat(ruleUpdated).isNotNull();
assertThat(ruleUpdated.getCreatedAt()).isEqualTo(rule.getCreatedAt());
assertThat(ruleUpdated.getUpdatedAt()).isEqualTo(rule.getUpdatedAt());
- assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo(ruleMetadata.getAdHocName());
- assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo(ruleMetadata.getAdHocDescription());
- assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(ruleMetadata.getAdHocSeverity());
- assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(ruleMetadata.getAdHocType());
+ assertThat(ruleUpdated.getAdHocName()).isEqualTo(rule.getAdHocName());
+ assertThat(ruleUpdated.getAdHocDescription()).isEqualTo(rule.getAdHocDescription());
+ assertThat(ruleUpdated.getAdHocSeverity()).isEqualTo(rule.getAdHocSeverity());
+ assertThat(ruleUpdated.getAdHocType()).isEqualTo(rule.getAdHocType());
}
}
dbTester.components().insertComponents(project, file);
RuleDto ruleDto = RuleTesting.newDto(ruleKey);
- dbTester.rules().insertRule(ruleDto);
+ dbTester.rules().insert(ruleDto);
ruleRepositoryRule.add(ruleKey);
IssueDto issue = IssueTesting.newDto(ruleDto, file, project)
dbTester.components().insertComponents(project, file);
RuleDto ruleDto = RuleTesting.newDto(ruleKey);
- dbTester.rules().insertRule(ruleDto);
+ dbTester.rules().insert(ruleDto);
ruleRepositoryRule.add(ruleKey);
IssueDto issue = IssueTesting.newDto(ruleDto, file, project)
import org.sonar.db.issue.IssueDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleMetadataDto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
.orElseThrow(() -> new RuntimeException("insertAdHocRule failed"));
}
- private void insertRuleMetadata(String ruleUuid, String adHocName) {
- dbTester.rules().insertOrUpdateMetadata(new RuleMetadataDto()
- .setRuleUuid(ruleUuid)
- .setAdHocName(adHocName));
- dbTester.commit();
- }
}
"saml_message_ids",
"rules",
"rule_desc_sections",
- "rules_metadata",
"rules_parameters",
"rules_profiles",
"rule_repositories",
*/
package org.sonar.db.rule;
-import com.google.common.annotations.VisibleForTesting;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
return Optional.ofNullable(mapper(session).selectByKey(key));
}
- public Optional<RuleMetadataDto> selectMetadataByKey(DbSession session, RuleKey key) {
- return Optional.ofNullable(mapper(session).selectMetadataByKey(key));
- }
-
public RuleDto selectOrFailByKey(DbSession session, RuleKey key) {
return Optional.ofNullable(mapper(session).selectByKey(key))
.orElseThrow(() -> new RowNotFoundException(String.format("Rule with key '%s' does not exist", key)));
checkNotNull(ruleDto.getUuid(), "RuleDto has no 'uuid'.");
RuleMapper mapper = mapper(session);
mapper.insertRule(ruleDto);
- insertOrUpdateRuleMetadata(session, ruleDto.getMetadata());
updateRuleDescriptionSectionDtos(ruleDto, mapper);
}
public void update(DbSession session, RuleDto ruleDto) {
RuleMapper mapper = mapper(session);
mapper.updateRule(ruleDto);
- insertOrUpdateRuleMetadata(session, ruleDto.getMetadata());
updateRuleDescriptionSectionDtos(ruleDto, mapper);
}
.forEach(section -> mapper.insertRuleDescriptionSection(ruleDto.getUuid(), section));
}
- @VisibleForTesting
- void insertOrUpdateRuleMetadata(DbSession session, RuleMetadataDto ruleMetadataDto) {
- if (ruleMetadataDto.isUndefined()) {
- mapper(session).deleteMetadata(ruleMetadataDto.getRuleUuid());
- } else if (mapper(session).countMetadata(ruleMetadataDto) > 0) {
- mapper(session).updateMetadata(ruleMetadataDto);
- } else {
- mapper(session).insertMetadata(ruleMetadataDto);
- }
- }
-
public void scrollIndexingRuleExtensionsByIds(DbSession dbSession, Collection<String> ruleExtensionIds, Consumer<RuleExtensionForIndexingDto> consumer) {
RuleMapper mapper = mapper(dbSession);
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
+import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
+import java.util.TreeSet;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
private static final Splitter SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
- private String uuid;
- private String repositoryKey;
- private String ruleKey;
+ private String uuid = null;
+ private String repositoryKey = null;
+ private String ruleKey = null;
private Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = new HashSet<>();
/**
* Description format can be null on external rule, otherwise it should never be null
*/
- private RuleDto.Format descriptionFormat;
- private RuleStatus status;
- private String name;
- private String configKey;
+ private RuleDto.Format descriptionFormat = null;
+ private RuleStatus status = null;
+ private String name = null;
+ private String configKey = null;
/**
* Severity can be null on external rule, otherwise it should never be null
*/
- private Integer severity;
+ private Integer severity = null;
- private boolean isTemplate;
+ private boolean isTemplate = false;
/**
* This flag specify that this is an external rule, meaning that generated issues from this rule will be provided by the analyzer without being activated on a quality profile.
*/
- private boolean isExternal;
+ private boolean isExternal = false;
/**
* When an external rule is defined as ad hoc, it means that it's not defined using {@link org.sonar.api.server.rule.RulesDefinition.Context#createExternalRepository(String, String)}.
* As the opposite, an external rule not being defined as ad hoc is declared by using {@link org.sonar.api.server.rule.RulesDefinition.Context#createExternalRepository(String, String)}.
* This flag is only used for external rules (it can only be set to true for when {@link #isExternal()} is true)
*/
- private boolean isAdHoc;
+ private boolean isAdHoc = false;
+
+ private String language = null;
+ private String templateUuid = null;
+ private String defRemediationFunction = null;
+ private String defRemediationGapMultiplier = null;
+ private String defRemediationBaseEffort = null;
+ private String gapDescription = null;
+ private String systemTagsField = null;
+ private String securityStandardsField = null;
+ private int type = 0;
+ private Scope scope = null;
+
+ private RuleKey key = null;
+
+ private String pluginKey = null;
+ private String noteData = null;
+ private String noteUserUuid = null;
+ private Long noteCreatedAt = null;
+ private Long noteUpdatedAt = null;
+ private String remediationFunction = null;
+ private String remediationGapMultiplier = null;
+ private String remediationBaseEffort = null;
+ private String tags = null;
- private String language;
- private String templateUuid;
- private String defRemediationFunction;
- private String defRemediationGapMultiplier;
- private String defRemediationBaseEffort;
- private String gapDescription;
- private String systemTagsField;
- private String securityStandardsField;
- private int type;
- private Scope scope;
-
- private RuleKey key;
-
- private String pluginKey;
-
- private long createdAt;
- private long updatedAt;
-
- private final RuleMetadataDto metadata;
+ /**
+ * Name of on ad hoc rule.
+ */
+ private String adHocName = null;
- public RuleDto() {
- this(new RuleMetadataDto());
- }
+ /**
+ * Optional description of on ad hoc rule.
+ */
+ private String adHocDescription = null;
- public RuleDto(RuleMetadataDto metadata) {
- this.metadata = metadata;
- }
+ /**
+ * Severity of on ad hoc rule.
+ * When {@link RuleDto#isAdHoc()} is true, this field should always be set
+ */
+ private String adHocSeverity = null;
- public RuleMetadataDto getMetadata() {
- return metadata;
- }
+ /**
+ * Type of on ad hoc rule.
+ * When {@link RuleDto#isAdHoc()} is true, this field should always be set
+ */
+ private Integer adHocType = null;
+ private long createdAt = 0;
+ private long updatedAt = 0;
public RuleKey getKey() {
if (key == null) {
public RuleDto setUuid(String uuid) {
this.uuid = uuid;
- metadata.setRuleUuid(uuid);
return this;
}
return this;
}
- @CheckForNull
- public String getAdHocName() {
- return metadata.getAdHocName();
- }
-
- public RuleDto setAdHocName(@Nullable String adHocName) {
- metadata.setAdHocName(adHocName);
- return this;
- }
-
- @CheckForNull
- public String getAdHocDescription() {
- return metadata.getAdHocDescription();
- }
-
- public RuleDto setAdHocDescription(@Nullable String adHocDescription) {
- metadata.setAdHocDescription(adHocDescription);
- return this;
- }
-
- @CheckForNull
- public String getAdHocSeverity() {
- return metadata.getAdHocSeverity();
- }
-
- public RuleDto setAdHocSeverity(@Nullable String adHocSeverity) {
- metadata.setAdHocSeverity(adHocSeverity);
- return this;
- }
-
- @CheckForNull
- public Integer getAdHocType() {
- return metadata.getAdHocType();
- }
-
- public RuleDto setAdHocType(@Nullable Integer type) {
- metadata.setAdHocType(type);
- return this;
- }
-
- public RuleDto setAdHocType(@Nullable RuleType adHocType) {
- metadata.setAdHocType(adHocType);
- return this;
- }
-
public boolean isTemplate() {
return isTemplate;
}
return this;
}
+
@CheckForNull
- public String getNoteData() {
- return metadata.getNoteData();
+ public String getDefRemediationFunction() {
+ return defRemediationFunction;
}
- public RuleDto setNoteData(@Nullable String s) {
- metadata.setNoteData(s);
+ public RuleDto setDefRemediationFunction(@Nullable String defaultRemediationFunction) {
+ this.defRemediationFunction = defaultRemediationFunction;
return this;
}
@CheckForNull
- public String getNoteUserUuid() {
- return metadata.getNoteUserUuid();
+ public String getDefRemediationGapMultiplier() {
+ return defRemediationGapMultiplier;
}
- public RuleDto setNoteUserUuid(@Nullable String noteUserUuid) {
- metadata.setNoteUserUuid(noteUserUuid);
+ public RuleDto setDefRemediationGapMultiplier(@Nullable String defaultRemediationGapMultiplier) {
+ this.defRemediationGapMultiplier = defaultRemediationGapMultiplier;
return this;
}
@CheckForNull
- public Long getNoteCreatedAt() {
- return metadata.getNoteCreatedAt();
+ public String getDefRemediationBaseEffort() {
+ return defRemediationBaseEffort;
}
- public RuleDto setNoteCreatedAt(@Nullable Long noteCreatedAt) {
- metadata.setNoteCreatedAt(noteCreatedAt);
+ public RuleDto setDefRemediationBaseEffort(@Nullable String defaultRemediationBaseEffort) {
+ this.defRemediationBaseEffort = defaultRemediationBaseEffort;
return this;
}
@CheckForNull
- public Long getNoteUpdatedAt() {
- return metadata.getNoteUpdatedAt();
+ public String getGapDescription() {
+ return gapDescription;
}
- public RuleDto setNoteUpdatedAt(@Nullable Long noteUpdatedAt) {
- metadata.setNoteUpdatedAt(noteUpdatedAt);
+ public RuleDto setGapDescription(@Nullable String s) {
+ this.gapDescription = s;
return this;
}
+ public static Set<String> deserializeTagsString(@Nullable String tags) {
+ return deserializeStringSet(tags);
+ }
+
+ public static Set<String> deserializeSecurityStandardsString(@Nullable String securityStandards) {
+ return deserializeStringSet(securityStandards);
+ }
+
+ private static Set<String> deserializeStringSet(@Nullable String str) {
+ if (str == null || str.isEmpty()) {
+ return emptySet();
+ }
+
+ return ImmutableSet.copyOf(SPLITTER.split(str));
+ }
@CheckForNull
- public String getDefRemediationFunction() {
- return defRemediationFunction;
+ public String getNoteData() {
+ return noteData;
}
- public RuleDto setDefRemediationFunction(@Nullable String defaultRemediationFunction) {
- this.defRemediationFunction = defaultRemediationFunction;
+ public RuleDto setNoteData(@Nullable String s) {
+ this.noteData = s;
return this;
}
@CheckForNull
- public String getDefRemediationGapMultiplier() {
- return defRemediationGapMultiplier;
+ public String getNoteUserUuid() {
+ return noteUserUuid;
}
- public RuleDto setDefRemediationGapMultiplier(@Nullable String defaultRemediationGapMultiplier) {
- this.defRemediationGapMultiplier = defaultRemediationGapMultiplier;
+ public RuleDto setNoteUserUuid(@Nullable String noteUserUuid) {
+ this.noteUserUuid = noteUserUuid;
return this;
}
@CheckForNull
- public String getDefRemediationBaseEffort() {
- return defRemediationBaseEffort;
+ public Long getNoteCreatedAt() {
+ return noteCreatedAt;
}
- public RuleDto setDefRemediationBaseEffort(@Nullable String defaultRemediationBaseEffort) {
- this.defRemediationBaseEffort = defaultRemediationBaseEffort;
+ public RuleDto setNoteCreatedAt(@Nullable Long noteCreatedAt) {
+ this.noteCreatedAt = noteCreatedAt;
return this;
}
@CheckForNull
- public String getGapDescription() {
- return gapDescription;
+ public Long getNoteUpdatedAt() {
+ return noteUpdatedAt;
}
- public RuleDto setGapDescription(@Nullable String s) {
- this.gapDescription = s;
+ public RuleDto setNoteUpdatedAt(@Nullable Long noteUpdatedAt) {
+ this.noteUpdatedAt = noteUpdatedAt;
return this;
}
@CheckForNull
public String getRemediationFunction() {
- return metadata.getRemediationFunction();
+ return remediationFunction;
}
public RuleDto setRemediationFunction(@Nullable String remediationFunction) {
- metadata.setRemediationFunction(remediationFunction);
+ this.remediationFunction = remediationFunction;
return this;
}
@CheckForNull
public String getRemediationGapMultiplier() {
- return metadata.getRemediationGapMultiplier();
+ return remediationGapMultiplier;
}
public RuleDto setRemediationGapMultiplier(@Nullable String remediationGapMultiplier) {
- metadata.setRemediationGapMultiplier(remediationGapMultiplier);
+ this.remediationGapMultiplier = remediationGapMultiplier;
return this;
}
@CheckForNull
public String getRemediationBaseEffort() {
- return metadata.getRemediationBaseEffort();
+ return remediationBaseEffort;
}
public RuleDto setRemediationBaseEffort(@Nullable String remediationBaseEffort) {
- metadata.setRemediationBaseEffort(remediationBaseEffort);
+ this.remediationBaseEffort = remediationBaseEffort;
return this;
}
public Set<String> getTags() {
- return metadata.getTags();
+ return tags == null ? new HashSet<>() : new TreeSet<>(Arrays.asList(StringUtils.split(tags, ',')));
}
- /**
- * Used in MyBatis mapping.
- */
- private void setTagsField(String s) {
- metadata.setTagsField(s);
+ String getTagsAsString() {
+ return tags;
}
- public static Set<String> deserializeTagsString(@Nullable String tags) {
- return deserializeStringSet(tags);
+ public RuleDto setTags(Set<String> tags) {
+ String raw = tags.isEmpty() ? null : String.join(",", tags);
+ checkArgument(raw == null || raw.length() <= 4000, "Rule tags are too long: %s", raw);
+ this.tags = raw;
+ return this;
}
- public static Set<String> deserializeSecurityStandardsString(@Nullable String securityStandards) {
- return deserializeStringSet(securityStandards);
+ private String getTagsField() {
+ return tags;
}
- private static Set<String> deserializeStringSet(@Nullable String str) {
- if (str == null || str.isEmpty()) {
- return emptySet();
- }
+ void setTagsField(String s) {
+ tags = s;
+ }
- return ImmutableSet.copyOf(SPLITTER.split(str));
+ @CheckForNull
+ public String getAdHocName() {
+ return adHocName;
}
- public RuleDto setTags(Set<String> tags) {
- this.metadata.setTags(tags);
+ public RuleDto setAdHocName(@Nullable String adHocName) {
+ this.adHocName = adHocName;
+ return this;
+ }
+
+ @CheckForNull
+ public String getAdHocDescription() {
+ return adHocDescription;
+ }
+
+ public RuleDto setAdHocDescription(@Nullable String adHocDescription) {
+ this.adHocDescription = adHocDescription;
+ return this;
+ }
+
+ @CheckForNull
+ public String getAdHocSeverity() {
+ return adHocSeverity;
+ }
+
+ public RuleDto setAdHocSeverity(@Nullable String adHocSeverity) {
+ this.adHocSeverity = adHocSeverity;
+ return this;
+ }
+
+ @CheckForNull
+ public Integer getAdHocType() {
+ return adHocType;
+ }
+
+ public RuleDto setAdHocType(@Nullable Integer adHocType) {
+ this.adHocType = adHocType;
+ return this;
+ }
+
+ public RuleDto setAdHocType(@Nullable RuleType adHocType) {
+ setAdHocType(adHocType != null ? adHocType.getDbConstant() : null);
return this;
}
private static String serializeStringSet(@Nullable Set<String> strings) {
- return strings == null || strings.isEmpty() ? null : StringUtils.join(strings, ',');
+ return strings == null || strings.isEmpty() ? null : String.join(",", strings);
}
@Override
ruleForIndexingDto.status = r.getStatus();
ruleForIndexingDto.isTemplate = r.isTemplate();
ruleForIndexingDto.systemTags = Sets.newHashSet(r.getSystemTags());
- ruleForIndexingDto.tags = r.getMetadata() != null ? Sets.newHashSet(r.getMetadata().getTags()) : Collections.emptySet();
+ ruleForIndexingDto.tags = r.getTags() != null ? Sets.newHashSet(r.getTags()) : Collections.emptySet();
ruleForIndexingDto.securityStandards = Sets.newHashSet(r.getSecurityStandards());
ruleForIndexingDto.internalKey = r.getConfigKey();
ruleForIndexingDto.language = r.getLanguage();
RuleDto selectByKey(@Param("ruleKey") RuleKey ruleKey);
- RuleMetadataDto selectMetadataByKey(@Param("ruleKey") RuleKey ruleKey);
-
List<RuleDto> selectByKeys(@Param("ruleKeys") List<RuleKey> keys);
List<RuleExtensionForIndexingDto> selectIndexingRuleExtensionsByIds(@Param("ruleExtensionIds") List<String> ruleExtensionIds);
void deleteRuleDescriptionSection(String ruleUuid);
- int countMetadata(RuleMetadataDto ruleMetadataDto);
-
- void insertMetadata(RuleMetadataDto ruleMetadataDto);
-
- void deleteMetadata(@Param("uuid") String uuid);
-
- void updateMetadata(RuleMetadataDto ruleMetadataDto);
-
List<RuleParamDto> selectParamsByRuleUuids(@Param("ruleUuids") List<String> ruleUuids);
List<RuleParamDto> selectParamsByRuleKey(RuleKey ruleKey);
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2022 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.db.rule;
-
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.TreeSet;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.rules.RuleType;
-
-import static com.google.common.base.Preconditions.checkArgument;
-
-public class RuleMetadataDto {
- private String ruleUuid;
- private String noteData;
- private String noteUserUuid;
- private Long noteCreatedAt;
- private Long noteUpdatedAt;
- private String remediationFunction;
- private String remediationGapMultiplier;
- private String remediationBaseEffort;
- private String tags;
-
- /**
- * Name of on ad hoc rule.
- */
- private String adHocName;
-
- /**
- * Optional description of on ad hoc rule.
- */
- private String adHocDescription;
-
- /**
- * Severity of on ad hoc rule.
- * When {@link RuleDto#isAdHoc()} is true, this field should always be set
- */
- private String adHocSeverity;
-
- /**
- * Type of on ad hoc rule.
- * When {@link RuleDto#isAdHoc()} is true, this field should always be set
- */
- private Integer adHocType;
-
- public RuleMetadataDto() {
- // nothing to do here
- }
-
- public String getRuleUuid() {
- return ruleUuid;
- }
-
- public RuleMetadataDto setRuleUuid(String ruleUuid) {
- this.ruleUuid = ruleUuid;
- return this;
- }
-
- @CheckForNull
- public String getNoteData() {
- return noteData;
- }
-
- public RuleMetadataDto setNoteData(@Nullable String s) {
- this.noteData = s;
- return this;
- }
-
- @CheckForNull
- public String getNoteUserUuid() {
- return noteUserUuid;
- }
-
- public RuleMetadataDto setNoteUserUuid(@Nullable String noteUserUuid) {
- this.noteUserUuid = noteUserUuid;
- return this;
- }
-
- @CheckForNull
- public Long getNoteCreatedAt() {
- return noteCreatedAt;
- }
-
- public RuleMetadataDto setNoteCreatedAt(@Nullable Long noteCreatedAt) {
- this.noteCreatedAt = noteCreatedAt;
- return this;
- }
-
- @CheckForNull
- public Long getNoteUpdatedAt() {
- return noteUpdatedAt;
- }
-
- public RuleMetadataDto setNoteUpdatedAt(@Nullable Long noteUpdatedAt) {
- this.noteUpdatedAt = noteUpdatedAt;
- return this;
- }
-
- @CheckForNull
- public String getRemediationFunction() {
- return remediationFunction;
- }
-
- public RuleMetadataDto setRemediationFunction(@Nullable String remediationFunction) {
- this.remediationFunction = remediationFunction;
- return this;
- }
-
- @CheckForNull
- public String getRemediationGapMultiplier() {
- return remediationGapMultiplier;
- }
-
- public RuleMetadataDto setRemediationGapMultiplier(@Nullable String remediationGapMultiplier) {
- this.remediationGapMultiplier = remediationGapMultiplier;
- return this;
- }
-
- @CheckForNull
- public String getRemediationBaseEffort() {
- return remediationBaseEffort;
- }
-
- public RuleMetadataDto setRemediationBaseEffort(@Nullable String remediationBaseEffort) {
- this.remediationBaseEffort = remediationBaseEffort;
- return this;
- }
-
- public Set<String> getTags() {
- return tags == null ? new HashSet<>() : new TreeSet<>(Arrays.asList(StringUtils.split(tags, ',')));
- }
-
- String getTagsAsString() {
- return tags;
- }
-
- public RuleMetadataDto setTags(Set<String> tags) {
- String raw = tags.isEmpty() ? null : StringUtils.join(tags, ',');
- checkArgument(raw == null || raw.length() <= 4000, "Rule tags are too long: %s", raw);
- this.tags = raw;
- return this;
- }
-
- private String getTagsField() {
- return tags;
- }
-
- void setTagsField(String s) {
- tags = s;
- }
-
- @CheckForNull
- public String getAdHocName() {
- return adHocName;
- }
-
- public RuleMetadataDto setAdHocName(@Nullable String adHocName) {
- this.adHocName = adHocName;
- return this;
- }
-
- @CheckForNull
- public String getAdHocDescription() {
- return adHocDescription;
- }
-
- public RuleMetadataDto setAdHocDescription(@Nullable String adHocDescription) {
- this.adHocDescription = adHocDescription;
- return this;
- }
-
- @CheckForNull
- public String getAdHocSeverity() {
- return adHocSeverity;
- }
-
- public RuleMetadataDto setAdHocSeverity(@Nullable String adHocSeverity) {
- this.adHocSeverity = adHocSeverity;
- return this;
- }
-
- @CheckForNull
- public Integer getAdHocType() {
- return adHocType;
- }
-
- public RuleMetadataDto setAdHocType(@Nullable Integer adHocType) {
- this.adHocType = adHocType;
- return this;
- }
-
- public RuleMetadataDto setAdHocType(@Nullable RuleType adHocType) {
- setAdHocType(adHocType != null ? adHocType.getDbConstant() : null);
- return this;
- }
-
-
-
-
-
- @Override
- public String toString() {
- return "RuleMetadataDto{" +
- "ruleUuid=" + ruleUuid +
- ", noteData='" + noteData + '\'' +
- ", noteUserUuid='" + noteUserUuid + '\'' +
- ", noteCreatedAt=" + noteCreatedAt +
- ", noteUpdatedAt=" + noteUpdatedAt +
- ", remediationFunction='" + remediationFunction + '\'' +
- ", remediationGapMultiplier='" + remediationGapMultiplier + '\'' +
- ", remediationBaseEffort='" + remediationBaseEffort + '\'' +
- ", tags='" + tags + '\'' +
- '}';
- }
-
- public boolean isUndefined() {
- return StringUtils.isEmpty(noteData) && StringUtils.isEmpty(noteUserUuid) && noteCreatedAt == null && noteUpdatedAt == null && StringUtils.isEmpty(remediationFunction) &&
- StringUtils.isEmpty(remediationGapMultiplier) && StringUtils.isEmpty(remediationBaseEffort) && StringUtils.isEmpty(tags)
- && StringUtils.isEmpty(adHocName) && StringUtils.isEmpty(adHocDescription) && StringUtils.isEmpty(adHocSeverity) && adHocType == null;
- }
-}
r.name,
r.description_format as "descriptionFormat",
r.rule_type as "type",
+ r.note_data as "extendedDescription",
+ r.tags,
rt.plugin_rule_key as "template",
- rm.note_data as "extendedDescription",
- rm.tags,
rds.content as "description"
</sql>
inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid
inner join rules r on r.uuid = a.rule_uuid and r.status != 'REMOVED'
left join rules rt on rt.uuid = r.template_uuid
- left join rules_metadata rm on rm.rule_uuid = r.uuid
<include refid="leftOuterJoinRulesDefaultDescriptionSection"/>
where oqp.uuid = #{id, jdbcType=VARCHAR}
</select>
r.scope,
r.created_at as "createdAt",
r.updated_at as "updatedAt",
- rm.note_data as "noteData",
- rm.note_user_uuid as "noteUserUuid",
- rm.note_created_at as "noteCreatedAt",
- rm.note_updated_at as "noteUpdatedAt",
- rm.remediation_function as "remediationFunction",
- rm.remediation_gap_mult as "remediationGapMultiplier",
- rm.remediation_base_effort as "remediationBaseEffort",
- rm.tags as "tagsField",
- rm.ad_hoc_name as "adHocName",
- rm.ad_hoc_description as "adHocDescription",
- rm.ad_hoc_severity as "adHocSeverity",
- rm.ad_hoc_type as "adHocType"
- </sql>
-
- <sql id="outerJoinRulesMetadata">
- left outer join rules_metadata rm on
- rm.rule_uuid = r.uuid
+ r.note_data as "noteData",
+ r.note_user_uuid as "noteUserUuid",
+ r.note_created_at as "noteCreatedAt",
+ r.note_updated_at as "noteUpdatedAt",
+ r.remediation_function as "remediationFunction",
+ r.remediation_gap_mult as "remediationGapMultiplier",
+ r.remediation_base_effort as "remediationBaseEffort",
+ r.tags as "tagsField",
+ r.ad_hoc_name as "adHocName",
+ r.ad_hoc_description as "adHocDescription",
+ r.ad_hoc_severity as "adHocSeverity",
+ r.ad_hoc_type as "adHocType"
</sql>
<select id="selectAll" resultMap="ruleResultMap">
<include refid="selectJoinedTablesColumns"/>
from
rules r
- <include refid="outerJoinRulesMetadata"/>
<include refid="leftOuterJoinRulesDescriptionSections"/>
</select>
<include refid="selectJoinedTablesColumns"/>
from
rules r
- <include refid="outerJoinRulesMetadata"/>
<include refid="leftOuterJoinRulesDescriptionSections"/>
where
r.status != 'REMOVED'
<include refid="selectJoinedTablesColumns"/>
from
rules r
- <include refid="outerJoinRulesMetadata"/>
<include refid="leftOuterJoinRulesDescriptionSections"/>
where
r.uuid=#{uuid,jdbcType=VARCHAR}
<include refid="selectJoinedTablesColumns"/>
from
rules r
- <include refid="outerJoinRulesMetadata"/>
<include refid="leftOuterJoinRulesDescriptionSections"/>
where
<foreach collection="uuids" index="index" item="uuid" open="" separator=" or " close="">
<include refid="selectJoinedTablesColumns"/>
from
rules r
- <include refid="outerJoinRulesMetadata"/>
<include refid="leftOuterJoinRulesDescriptionSections"/>
where
r.plugin_name=#{ruleKey.repository,jdbcType=VARCHAR}
r.uuid as "ruleUuid",
r.plugin_name as "pluginName",
r.plugin_rule_key as "pluginRuleKey",
- rm.tags as "tags"
+ r.tags as "tags"
from rules r
- inner join rules_metadata rm on rm.rule_uuid = r.uuid
where
- rm.tags is not null and
- rm.tags != ''
+ r.tags is not null and
+ r.tags != ''
</sql>
<sql id="sqlSelectIndexingRuleExtensions" databaseId="oracle">
r.uuid as "ruleUuid",
r.plugin_name as "pluginName",
r.plugin_rule_key as "pluginRuleKey",
- rm.tags as "tags"
+ r.tags as "tags"
from rules r
- inner join rules_metadata rm on rm.rule_uuid = r.uuid
where
- rm.tags is not null
+ r.tags is not null
</sql>
- <select id="selectMetadataByKey" parameterType="map" resultType="org.sonar.db.rule.RuleMetadataDto">
- select
- rm.rule_uuid as "ruleUuid",
- rm.note_data as "noteData",
- rm.note_user_uuid as "noteUserUuid",
- rm.note_created_at as "noteCreatedAt",
- rm.note_updated_at as "noteUpdatedAt",
- rm.remediation_function as "remediationFunction",
- rm.remediation_gap_mult as "remediationGapMultiplier",
- rm.remediation_base_effort as "remediationBaseEffort",
- rm.tags as "tagsField",
- rm.ad_hoc_name as "adHocName",
- rm.ad_hoc_description as "adHocDescription",
- rm.ad_hoc_severity as "adHocSeverity",
- rm.ad_hoc_type as "adHocType"
- from
- rules_metadata rm
- inner join rules r on rm.rule_uuid = r.uuid
- where
- r.plugin_name=#{ruleKey.repository,jdbcType=VARCHAR}
- and r.plugin_rule_key=#{ruleKey.rule,jdbcType=VARCHAR}
- </select>
-
<select id="selectByKeys" parameterType="map" resultMap="ruleResultMap">
select
<include refid="selectJoinedTablesColumns"/>
from
rules r
- <include refid="outerJoinRulesMetadata"/>
<include refid="leftOuterJoinRulesDescriptionSections"/>
where
<foreach collection="ruleKeys" index="index" item="ruleKey" open="" separator=" or " close="">
<include refid="selectJoinedTablesColumns"/>
from
rules r
- <include refid="outerJoinRulesMetadata"/>
<include refid="leftOuterJoinRulesDescriptionSections"/>
where
r.status != 'REMOVED'
<include refid="selectJoinedTablesColumns"/>
from
rules r
- <include refid="outerJoinRulesMetadata"/>
<include refid="leftOuterJoinRulesDescriptionSections"/>
where
r.status != 'REMOVED' and r.is_external=${_false} and r.is_template=${_false}
security_standards,
rule_type,
scope,
+ note_data,
+ note_user_uuid,
+ note_created_at,
+ note_updated_at,
+ remediation_function,
+ remediation_gap_mult,
+ remediation_base_effort,
+ tags,
+ ad_hoc_name,
+ ad_hoc_description,
+ ad_hoc_severity,
+ ad_hoc_type,
created_at,
updated_at
)
#{securityStandardsField,jdbcType=VARCHAR},
#{type,jdbcType=TINYINT},
#{scope,jdbcType=VARCHAR},
+ #{noteData,jdbcType=CLOB},
+ #{noteUserUuid,jdbcType=VARCHAR},
+ #{noteCreatedAt,jdbcType=BIGINT},
+ #{noteUpdatedAt,jdbcType=BIGINT},
+ #{remediationFunction,jdbcType=VARCHAR},
+ #{remediationGapMultiplier,jdbcType=VARCHAR},
+ #{remediationBaseEffort,jdbcType=VARCHAR},
+ #{tagsField,jdbcType=VARCHAR},
+ #{adHocName,jdbcType=VARCHAR},
+ #{adHocDescription,jdbcType=CLOB},
+ #{adHocSeverity,jdbcType=VARCHAR},
+ #{adHocType,jdbcType=TINYINT},
#{createdAt,jdbcType=BIGINT},
#{updatedAt,jdbcType=BIGINT}
)
security_standards=#{securityStandardsField,jdbcType=VARCHAR},
scope=#{scope,jdbcType=VARCHAR},
rule_type=#{type,jdbcType=TINYINT},
- updated_at=#{updatedAt,jdbcType=BIGINT}
- where
- uuid=#{uuid,jdbcType=VARCHAR}
- </update>
-
- <delete id="deleteRuleDescriptionSection" parameterType="String">
- delete from
- rule_desc_sections
- where
- rule_uuid=#{ruleUuid,jdbcType=VARCHAR}
- </delete>
-
- <select id="countMetadata" parameterType="org.sonar.db.rule.RuleMetadataDto" resultType="int">
- select
- count(1)
- from
- rules_metadata rm
- where
- rm.rule_uuid=#{ruleUuid,jdbcType=VARCHAR}
- </select>
-
- <insert id="insertMetadata" parameterType="org.sonar.db.rule.RuleMetadataDto">
- insert into rules_metadata (
- rule_uuid,
- note_data,
- note_user_uuid,
- note_created_at,
- note_updated_at,
- remediation_function,
- remediation_gap_mult,
- remediation_base_effort,
- tags,
- ad_hoc_name,
- ad_hoc_description,
- ad_hoc_severity,
- ad_hoc_type
- )
- values (
- #{ruleUuid,jdbcType=VARCHAR},
- #{noteData,jdbcType=CLOB},
- #{noteUserUuid,jdbcType=VARCHAR},
- #{noteCreatedAt,jdbcType=BIGINT},
- #{noteUpdatedAt,jdbcType=BIGINT},
- #{remediationFunction,jdbcType=VARCHAR},
- #{remediationGapMultiplier,jdbcType=VARCHAR},
- #{remediationBaseEffort,jdbcType=VARCHAR},
- #{tagsField,jdbcType=VARCHAR},
- #{adHocName,jdbcType=VARCHAR},
- #{adHocDescription,jdbcType=CLOB},
- #{adHocSeverity,jdbcType=VARCHAR},
- #{adHocType,jdbcType=TINYINT}
- )
- </insert>
-
- <update id="updateMetadata" parameterType="org.sonar.db.rule.RuleMetadataDto">
- update rules_metadata set
note_data=#{noteData,jdbcType=CLOB},
note_user_uuid=#{noteUserUuid,jdbcType=VARCHAR},
note_created_at=#{noteCreatedAt,jdbcType=BIGINT},
ad_hoc_name=#{adHocName,jdbcType=VARCHAR},
ad_hoc_description=#{adHocDescription,jdbcType=CLOB},
ad_hoc_severity=#{adHocSeverity,jdbcType=VARCHAR},
- ad_hoc_type=#{adHocType,jdbcType=TINYINT}
+ ad_hoc_type=#{adHocType,jdbcType=TINYINT},
+ updated_at=#{updatedAt,jdbcType=BIGINT}
where
- rule_uuid=#{ruleUuid,jdbcType=VARCHAR}
+ uuid=#{uuid,jdbcType=VARCHAR}
</update>
- <delete id="deleteMetadata" parameterType="String">
+ <delete id="deleteRuleDescriptionSection" parameterType="String">
delete from
- rules_metadata
+ rule_desc_sections
where
- rule_uuid=#{uuid,jdbcType=INTEGER}
+ rule_uuid=#{ruleUuid,jdbcType=VARCHAR}
</delete>
<delete id="deleteParams" parameterType="String">
}
private void prepareIssuesComponent() {
- db.rules().insertRule(RULE.setIsExternal(true));
+ db.rules().insert(RULE.setIsExternal(true));
ComponentDto projectDto = db.components().insertPrivateProject(t -> t.setUuid(PROJECT_UUID).setDbKey(PROJECT_KEY));
db.components().insertComponent(newFileDto(projectDto).setUuid(FILE_UUID).setDbKey(FILE_KEY));
}
file2 = ComponentTesting.newFileDto(project, null).setUuid("file2 uuid");
dbTester.getDbClient().componentDao().insert(dbSession, file2);
rule = RuleTesting.newXooX1();
- dbTester.rules().insertRule(rule);
+ dbTester.rules().insert(rule);
dbSession.commit();
}
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.db.rule.RuleParamDto;
import static org.assertj.core.api.Assertions.assertThat;
String language = "java";
RuleDto ruleTemplate = createRule(language);
RuleDto customRule = createRule(language, RuleStatus.READY, ruleTemplate.getUuid());
+ customRule.setNoteData("Extended description")
+ .setTags(Sets.newHashSet("tag1", "tag2", "tag3"));
+ db.rules().update(customRule);
+
var customRuleContent = customRule.getDefaultRuleDescriptionSection().getContent();
- RuleMetadataDto customRuleMetadata = createRuleMetadata(new RuleMetadataDto()
- .setRuleUuid(customRule.getUuid())
- .setNoteData("Extended description")
- .setTags(Sets.newHashSet("tag1", "tag2", "tag3")));
RuleDto rule = createRule(language, RuleStatus.READY, null);
var ruleContent = rule.getDefaultRuleDescriptionSection().getContent();
- RuleMetadataDto ruleMetadata = createRuleMetadata(new RuleMetadataDto()
- .setRuleUuid(rule.getUuid()));
QProfileDto profile = createProfile(language);
List<ActiveRuleDto> activeRules = activate(profile, customRule, rule);
assertThat(exportCustomRuleDto.isCustomRule()).isTrue();
assertThat(exportCustomRuleDto.getParams()).isEmpty();
assertThat(exportCustomRuleDto.getDescriptionOrThrow()).isEqualTo(customRuleContent);
- assertThat(exportCustomRuleDto.getExtendedDescription()).isEqualTo(customRuleMetadata.getNoteData());
+ assertThat(exportCustomRuleDto.getExtendedDescription()).isEqualTo(customRule.getNoteData());
assertThat(exportCustomRuleDto.getName()).isEqualTo(customRule.getName());
assertThat(exportCustomRuleDto.getRuleKey()).isEqualTo(customRule.getKey());
assertThat(exportCustomRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(customRule.getType()));
- assertThat(exportCustomRuleDto.getTags()).isEqualTo(String.join(",", customRuleMetadata.getTags()));
+ assertThat(exportCustomRuleDto.getTags()).isEqualTo(String.join(",", customRule.getTags()));
assertThat(exportCustomRuleDto.getTemplateRuleKey()).isEqualTo(ruleTemplate.getKey());
ActiveRuleDto activeCustomRule = activeRules.stream().filter(activeRuleDto -> activeRuleDto.getRuleKey().equals(customRule.getKey())).findFirst().get();
assertThat(exportRuleDto.isCustomRule()).isFalse();
assertThat(exportRuleDto.getParams()).isEmpty();
assertThat(exportRuleDto.getDescriptionOrThrow()).isEqualTo(ruleContent);
- assertThat(exportRuleDto.getExtendedDescription()).isEqualTo(ruleMetadata.getNoteData());
+ assertThat(exportRuleDto.getExtendedDescription()).isEqualTo(rule.getNoteData());
assertThat(exportRuleDto.getName()).isEqualTo(rule.getName());
assertThat(exportRuleDto.getRuleKey()).isEqualTo(rule.getKey());
assertThat(exportRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(rule.getType()));
.setTemplateUuid(templateUuid));
}
- private RuleMetadataDto createRuleMetadata(RuleMetadataDto metadataDto) {
- return db.rules().insertOrUpdateMetadata(metadataDto);
- }
-
private QProfileDto createProfile(String lanugage) {
return db.qualityProfiles().insert(p -> p.setLanguage(lanugage));
}
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.api.rule.RuleStatus.REMOVED;
-import static org.sonar.db.rule.RuleTesting.newRuleMetadata;
public class RuleDaoTest {
private static final String UNKNOWN_RULE_UUID = "unknown-uuid";
@Test
public void selectByKey() {
RuleDto ruleDto = db.rules().insert();
- RuleMetadataDto metadata = newRuleMetadata(ruleDto);
- db.rules().insertRule(ruleDto, metadata);
assertThat(underTest.selectByKey(db.getSession(), RuleKey.of("foo", "bar")))
.isEmpty();
- RuleDto rule = underTest.selectByKey(db.getSession(), ruleDto.getKey()).get();
- assertEquals(rule, ruleDto);
- verifyMetadata(rule.getMetadata(), metadata);
+ RuleDto actualRule = underTest.selectByKey(db.getSession(), ruleDto.getKey()).get();
+ assertEquals(actualRule, ruleDto);
}
@Test
assertThat(underTest.selectByKey(db.getSession(), ruleDto.getKey())).isNotEmpty();
}
- @Test
- public void selectByKey_returns_metadata() {
- RuleDto ruleDto = db.rules().insert();
- RuleMetadataDto ruleMetadata = newRuleMetadata(ruleDto);
- db.rules().insertRule(ruleDto, ruleMetadata);
-
- RuleDto rule = underTest.selectByKey(db.getSession(), ruleDto.getKey()).get();
- verifyMetadata(rule.getMetadata(), ruleMetadata);
- }
-
- @Test
- public void selectDefinitionByKey() {
- RuleDto rule = db.rules().insert();
-
- assertThat(underTest.selectByKey(db.getSession(), RuleKey.of("NOT", "FOUND"))).isEmpty();
-
- Optional<RuleDto> reloaded = underTest.selectByKey(db.getSession(), rule.getKey());
- assertThat(reloaded).isPresent();
- }
- @Test
- public void selectMetadataByKey() {
- RuleDto rule1 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule1);
-
- assertThat(underTest.selectByKeys(db.getSession(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty();
-
- Optional<RuleMetadataDto> rulesMetadata = underTest.selectMetadataByKey(db.getSession(), rule1.getKey());
- assertThat(rulesMetadata).isPresent();
- assertThat(rulesMetadata.get().getRuleUuid()).isEqualTo(rule1.getUuid());
- }
@Test
public void selectByUuid() {
RuleDto ruleDto = db.rules().insert();
- RuleMetadataDto metadata = newRuleMetadata(ruleDto);
- RuleDto expected = db.rules().insertRule(ruleDto, metadata);
- assertThat(underTest.selectByUuid(expected.getUuid() + 500, db.getSession())).isEmpty();
- RuleDto rule = underTest.selectByUuid(expected.getUuid(), db.getSession()).get();
- assertEquals(rule, ruleDto);
- verifyMetadata(rule.getMetadata(), metadata);
+ assertThat(underTest.selectByUuid(ruleDto.getUuid() + 500, db.getSession())).isEmpty();
+ RuleDto actualRule = underTest.selectByUuid(ruleDto.getUuid(), db.getSession()).get();
+ assertEquals(actualRule, ruleDto);
}
@Test
- public void selectByUuidWithDifferentsValuesOfBooleans() {
+ public void selectByUuidWithDifferentValuesOfBooleans() {
for (int i = 0; i < 3; i++) {
int indexBoolean = i;
RuleDto ruleDto = db.rules().insert((ruleDto1 -> {
ruleDto1.setIsExternal(indexBoolean == 1);
ruleDto1.setIsAdHoc(indexBoolean == 2);
}));
- RuleMetadataDto metadata = newRuleMetadata(ruleDto);
- RuleDto expected = db.rules().insertRule(ruleDto, metadata);
- assertThat(underTest.selectByUuid(expected.getUuid() + 500, db.getSession())).isEmpty();
- RuleDto rule = underTest.selectByUuid(expected.getUuid(), db.getSession()).get();
+ assertThat(underTest.selectByUuid(ruleDto.getUuid() + 500, db.getSession())).isEmpty();
+ RuleDto rule = underTest.selectByUuid(ruleDto.getUuid(), db.getSession()).get();
assertEquals(rule, ruleDto);
- verifyMetadata(rule.getMetadata(), metadata);
}
}
@Test
public void selectByUuids() {
RuleDto rule1 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule1);
RuleDto rule2 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule2);
RuleDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
- db.rules().insertOrUpdateMetadata(removedRule);
assertThat(underTest.selectByUuids(db.getSession(), singletonList(rule1.getUuid()))).hasSize(1);
assertThat(underTest.selectByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid()))).hasSize(2);
@Test
public void selectByKeys() {
RuleDto rule1 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule1);
- RuleDto rule2 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule2);
+ db.rules().insert();
assertThat(underTest.selectByKeys(db.getSession(), Collections.emptyList())).isEmpty();
assertThat(underTest.selectByKeys(db.getSession(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty();
.containsOnly(rule1.getUuid(), rule2.getUuid(), rule3.getUuid());
}
- @Test
- public void selectAll_returns_metadata() {
- RuleDto ruleDto = db.rules().insert();
- RuleMetadataDto expected = newRuleMetadata(ruleDto);
- db.rules().insertRule(ruleDto, expected);
-
- List<RuleDto> rules = underTest.selectAll(db.getSession());
- assertThat(rules).hasSize(1);
-
- verifyMetadata(rules.iterator().next().getMetadata(), expected);
- }
-
private void assertEquals(RuleDto actual, RuleDto expected) {
assertThat(actual.getUuid()).isEqualTo(expected.getUuid());
assertThat(actual.getRepositoryKey()).isEqualTo(expected.getRepositoryKey());
assertThat(actual.getSecurityStandards()).isEqualTo(expected.getSecurityStandards());
assertThat(actual.getType()).isEqualTo(expected.getType());
assertThat(actual.getDescriptionFormat()).isEqualTo(expected.getDescriptionFormat());
+ assertThat(actual.getRemediationBaseEffort()).isEqualTo(expected.getRemediationBaseEffort());
+ assertThat(actual.getRemediationFunction()).isEqualTo(expected.getRemediationFunction());
+ assertThat(actual.getRemediationGapMultiplier()).isEqualTo(expected.getRemediationGapMultiplier());
+ assertThat(actual.getTags()).isEqualTo(expected.getTags());
+ assertThat(actual.getNoteData()).isEqualTo(expected.getNoteData());
+ assertThat(actual.getNoteCreatedAt()).isEqualTo(expected.getNoteCreatedAt());
+ assertThat(actual.getNoteUpdatedAt()).isEqualTo(expected.getNoteUpdatedAt());
+ assertThat(actual.getAdHocName()).isEqualTo(expected.getAdHocName());
+ assertThat(actual.getAdHocDescription()).isEqualTo(expected.getAdHocDescription());
+ assertThat(actual.getAdHocSeverity()).isEqualTo(expected.getAdHocSeverity());
+ assertThat(actual.getAdHocType()).isEqualTo(expected.getAdHocType());
assertThat(actual.getRuleDescriptionSectionDtos()).usingRecursiveFieldByFieldElementComparator()
.isEqualTo(expected.getRuleDescriptionSectionDtos());
}
- private static void verifyMetadata(RuleMetadataDto metadata, RuleMetadataDto expected) {
- assertThat(metadata.getRemediationBaseEffort()).isEqualTo(expected.getRemediationBaseEffort());
- assertThat(metadata.getRemediationFunction()).isEqualTo(expected.getRemediationFunction());
- assertThat(metadata.getRemediationGapMultiplier()).isEqualTo(expected.getRemediationGapMultiplier());
- assertThat(metadata.getTags()).isEqualTo(expected.getTags());
- assertThat(metadata.getNoteData()).isEqualTo(expected.getNoteData());
- assertThat(metadata.getNoteCreatedAt()).isEqualTo(expected.getNoteCreatedAt());
- assertThat(metadata.getNoteUpdatedAt()).isEqualTo(expected.getNoteUpdatedAt());
- assertThat(metadata.getAdHocName()).isEqualTo(expected.getAdHocName());
- assertThat(metadata.getAdHocDescription()).isEqualTo(expected.getAdHocDescription());
- assertThat(metadata.getAdHocSeverity()).isEqualTo(expected.getAdHocSeverity());
- assertThat(metadata.getAdHocType()).isEqualTo(expected.getAdHocType());
- }
-
@Test
public void selectAllDefinitions() {
RuleDto rule1 = db.rules().insert();
.setConfigKey("S1")
.setType(RuleType.VULNERABILITY)
.setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1);
RuleDto rule2 = db.rules().insert(
r -> r.setKey(RuleKey.of("js", "S002"))
.setType(RuleType.SECURITY_HOTSPOT)
.setLanguage("js"));
- db.rules().insertOrUpdateMetadata(rule2);
assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
.extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
@Test
public void selectByTypeAndLanguages_return_nothing_when_no_rule_on_languages() {
- RuleDto rule1 = db.rules().insert(
+ db.rules().insert(
r -> r.setKey(RuleKey.of("java", "S001"))
.setConfigKey("S1")
.setType(RuleType.VULNERABILITY)
.setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1);
- RuleDto rule2 = db.rules().insert(
+ db.rules().insert(
r -> r.setKey(RuleKey.of("js", "S002"))
.setType(RuleType.VULNERABILITY)
.setLanguage("js"));
- db.rules().insertOrUpdateMetadata(rule2);
assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("cpp")))
.isEmpty();
@Test
public void selectByTypeAndLanguages_return_nothing_when_no_rule_with_type() {
- RuleDto rule1 = db.rules().insert(
+ db.rules().insert(
r -> r.setKey(RuleKey.of("java", "S001"))
.setConfigKey("S1")
.setType(RuleType.VULNERABILITY)
.setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1);
- RuleDto rule2 = db.rules().insert(
+ db.rules().insert(
r -> r.setKey(RuleKey.of("java", "S002"))
.setType(RuleType.SECURITY_HOTSPOT)
.setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule2);
- RuleDto rule3 = db.rules().insert(
+ db.rules().insert(
r -> r.setKey(RuleKey.of("java", "S003"))
.setType(RuleType.CODE_SMELL)
.setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule3);
assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.BUG.getDbConstant()), singletonList("java")))
.isEmpty();
@Test
public void selectByTypeAndLanguages_ignores_external_rules() {
- RuleDto rule1 = db.rules().insert(
+ db.rules().insert(
r -> r.setKey(RuleKey.of("java", "S001"))
.setConfigKey("S1")
.setType(RuleType.VULNERABILITY)
.setIsExternal(true)
.setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1);
assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
.extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
@Test
public void selectByTypeAndLanguages_ignores_template_rules() {
- RuleDto rule1 = db.rules().insert(
+ db.rules().insert(
r -> r.setKey(RuleKey.of("java", "S001"))
.setConfigKey("S1")
.setType(RuleType.VULNERABILITY)
.setIsTemplate(true)
.setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1);
assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
.extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
@Test
public void select_by_query() {
- RuleDto rule1 = db.rules().insert(r -> r.setKey(RuleKey.of("java", "S001")).setConfigKey("S1"));
- db.rules().insertOrUpdateMetadata(rule1);
- RuleDto rule2 = db.rules().insert(r -> r.setKey(RuleKey.of("java", "S002")));
- db.rules().insertOrUpdateMetadata(rule2);
- RuleDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
+ db.rules().insert(r -> r.setKey(RuleKey.of("java", "S001")).setConfigKey("S1"));
+ db.rules().insert(r -> r.setKey(RuleKey.of("java", "S002")));
+ db.rules().insert(r -> r.setStatus(REMOVED));
assertThat(underTest.selectByQuery(db.getSession(), RuleQuery.create())).hasSize(2);
assertThat(underTest.selectByQuery(db.getSession(), RuleQuery.create().withKey("S001"))).hasSize(1);
underTest.update(db.getSession(), rule);
db.commit();
- assertThat(db.countRowsOfTable("RULES_METADATA")).isOne();
RuleDto ruleDtoAfterUpdate = underTest.selectOrFailByKey(db.getSession(), rule.getKey());
- assertThat(ruleDtoAfterUpdate.getNoteData()).isNull();
- assertThat(ruleDtoAfterUpdate.getNoteUserUuid()).isNull();
- assertThat(ruleDtoAfterUpdate.getNoteCreatedAt()).isNull();
- assertThat(ruleDtoAfterUpdate.getNoteUpdatedAt()).isNull();
- assertThat(ruleDtoAfterUpdate.getRemediationFunction()).isNull();
- assertThat(ruleDtoAfterUpdate.getRemediationGapMultiplier()).isNull();
- assertThat(ruleDtoAfterUpdate.getRemediationBaseEffort()).isNull();
- assertThat(ruleDtoAfterUpdate.getTags()).isEmpty();
- assertThat(ruleDtoAfterUpdate.getAdHocName()).isNull();
+ assertThat(ruleDtoAfterUpdate.getNoteData()).isEqualTo(rule.getNoteData());
+ assertThat(ruleDtoAfterUpdate.getNoteUserUuid()).isEqualTo(rule.getNoteUserUuid());
+ assertThat(ruleDtoAfterUpdate.getNoteCreatedAt()).isEqualTo(rule.getNoteCreatedAt());
+ assertThat(ruleDtoAfterUpdate.getNoteUpdatedAt()).isEqualTo(rule.getNoteUpdatedAt());
+ assertThat(ruleDtoAfterUpdate.getRemediationFunction()).isEqualTo(rule.getRemediationFunction());
+ assertThat(ruleDtoAfterUpdate.getRemediationGapMultiplier()).isEqualTo(rule.getRemediationGapMultiplier());
+ assertThat(ruleDtoAfterUpdate.getRemediationBaseEffort()).isEqualTo(rule.getRemediationBaseEffort());
+ assertThat(ruleDtoAfterUpdate.getTags()).isEqualTo(rule.getTags());
+ assertThat(ruleDtoAfterUpdate.getAdHocName()).isEqualTo(rule.getAdHocName());
assertThat(ruleDtoAfterUpdate.getAdHocDescription()).isEqualTo("ad-hoc-desc");
- assertThat(ruleDtoAfterUpdate.getAdHocSeverity()).isNull();
- assertThat(ruleDtoAfterUpdate.getAdHocType()).isNull();
- assertThat(ruleDtoAfterUpdate.getSecurityStandards()).isEmpty();
+ assertThat(ruleDtoAfterUpdate.getAdHocSeverity()).isEqualTo(rule.getAdHocSeverity());
+ assertThat(ruleDtoAfterUpdate.getAdHocType()).isEqualTo(rule.getAdHocType());
+ assertThat(ruleDtoAfterUpdate.getSecurityStandards()).isEqualTo(rule.getSecurityStandards());
assertThat(ruleDtoAfterUpdate.getCreatedAt()).isEqualTo(ruleDtoBeforeUpdate.getCreatedAt());
assertThat(ruleDtoAfterUpdate.getUpdatedAt()).isEqualTo(4_000_000_000_000L);
RuleForIndexingDto secondRule = findRuleForIndexingWithUuid(accumulator, r2.getUuid());
assertRuleDefinitionFieldsAreEquals(r1, firstRule);
- assertRuleMetadataFieldsAreEquals(r1.getMetadata(), firstRule);
+ assertThat(r1.getTags()).isEqualTo(firstRule.getTags());
assertThat(firstRule.getTemplateRuleKey()).isNull();
assertThat(firstRule.getTemplateRepository()).isNull();
assertRuleDefinitionFieldsAreEquals(r2, secondRule);
assertThat(ruleForIndexing.getUpdatedAt()).isEqualTo(r1.getUpdatedAt());
}
- private static void assertRuleMetadataFieldsAreEquals(RuleMetadataDto r1Metadatas, RuleForIndexingDto firstRule) {
- assertThat(r1Metadatas.getTags()).isEqualTo(firstRule.getTags());
- }
-
@Test
public void scrollIndexingRulesByKeys_scrolls_nothing_if_key_does_not_exist() {
Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
@Test
public void scrollIndexingRuleExtensionsByIds() {
Accumulator<RuleExtensionForIndexingDto> accumulator = new Accumulator<>();
- RuleDto r1 = db.rules().insert();
- RuleMetadataDto r1Extension = db.rules().insertOrUpdateMetadata(r1, r -> r.setTagsField("t1,t2"));
- RuleDto r2 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(r2, r -> r.setTagsField("t1,t3"));
+ RuleDto r1 = db.rules().insert(ruleDto -> ruleDto.setTagsField("t1,t2"));
+ db.rules().insert(ruleDto -> ruleDto.setTagsField("t1,t3"));
underTest.scrollIndexingRuleExtensionsByIds(db.getSession(), singletonList(r1.getUuid()), accumulator);
assertThat(accumulator.list)
.extracting(RuleExtensionForIndexingDto::getRuleUuid, RuleExtensionForIndexingDto::getRuleKey, RuleExtensionForIndexingDto::getTags)
.containsExactlyInAnyOrder(
- tuple(r1.getUuid(), r1.getKey(), r1Extension.getTagsAsString()));
+ tuple(r1.getUuid(), r1.getKey(), r1.getTagsAsString()));
}
@Test
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
import java.util.Set;
+import java.util.TreeSet;
import org.junit.Test;
import org.sonar.core.util.Uuids;
assertThat(dto.getTags()).isEmpty();
}
+ @Test
+ public void tags_are_joined_with_comma() {
+ Set<String> tags = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
+ RuleDto dto = new RuleDto().setTags(tags);
+ assertThat(dto.getTags()).isEqualTo(tags);
+ assertThat(dto.getTagsAsString()).isEqualTo("first_tag,second_tag,third_tag");
+ }
+
+ @Test
+ public void system_tags_are_joined_with_comma() {
+ Set<String> systemTags = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
+ RuleDto dto = new RuleDto().setSystemTags(systemTags);
+ assertThat(dto.getSystemTags()).isEqualTo(systemTags);
+ }
+
+ @Test
+ public void security_standards_are_joined_with_comma() {
+ Set<String> securityStandards = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
+ RuleDto dto = new RuleDto().setSecurityStandards(securityStandards);
+ assertThat(dto.getSecurityStandards()).isEqualTo(securityStandards);
+ }
+
@Test
public void equals_is_based_on_uuid() {
String uuid = Uuids.createFast();
import org.sonar.api.rules.RuleType;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbTester;
-import org.sonar.db.user.UserDto;
import static java.util.Arrays.asList;
import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
return rule;
}
- @SafeVarargs
- public final RuleMetadataDto insertOrUpdateMetadata(RuleDto rule, Consumer<RuleMetadataDto>... populaters) {
- RuleMetadataDto dto = RuleTesting.newRuleMetadata(rule);
- asList(populaters).forEach(populater -> populater.accept(dto));
- return insertOrUpdateMetadata(dto);
- }
-
- @SafeVarargs
- public final RuleMetadataDto insertOrUpdateMetadata(RuleDto rule, UserDto noteUser, Consumer<RuleMetadataDto>... populaters) {
- RuleMetadataDto dto = RuleTesting.newRuleMetadata(rule, noteUser);
- asList(populaters).forEach(populater -> populater.accept(dto));
- return insertOrUpdateMetadata(dto);
- }
-
- public RuleMetadataDto insertOrUpdateMetadata(RuleMetadataDto metadata) {
- db.getDbClient().ruleDao().insertOrUpdateRuleMetadata(db.getSession(), metadata);
- db.commit();
- return db.getDbClient().ruleDao().selectByUuid(metadata.getRuleUuid(), db.getSession()).get().getMetadata();
- }
-
@SafeVarargs
public final RuleParamDto insertRuleParam(RuleDto rule, Consumer<RuleParamDto>... populaters) {
RuleParamDto param = RuleTesting.newRuleParam(rule);
return param;
}
- public RuleDto insertRule(RuleDto ruleDto) {
- if (ruleDto.getUuid() == null) {
- ruleDto.setUuid(Uuids.createFast());
- }
-
- insert(ruleDto);
- RuleMetadataDto metadata = ruleDto.getMetadata();
- db.getDbClient().ruleDao().insertOrUpdateRuleMetadata(db.getSession(), metadata.setRuleUuid(ruleDto.getUuid()));
- db.commit();
- return ruleDto;
- }
-
/**
* Create and persist a rule with random values.
*/
public final RuleDto insertRule(Consumer<RuleDto>... populaters) {
RuleDto ruleDto = newRuleDto();
asList(populaters).forEach(populater -> populater.accept(ruleDto));
- return insertRule(ruleDto);
+ return insert(ruleDto);
}
public RuleDto insertRule(Consumer<RuleDto> populateRuleDto) {
ruleDto.setUuid(Uuids.createFast());
}
- return insertRule(ruleDto);
+ return insert(ruleDto);
}
@SafeVarargs
return deprecatedRuleKeyDto;
}
- public RuleDto insertRule(RuleDto ruleDto, RuleMetadataDto ruleMetadata) {
- db.getDbClient().ruleDao().insertOrUpdateRuleMetadata(db.getSession(), ruleMetadata.setRuleUuid(ruleDto.getUuid()));
- db.commit();
- return db.getDbClient().ruleDao().selectOrFailByKey(db.getSession(), ruleDto.getKey());
- }
}
import org.sonar.core.util.UuidFactoryFast;
import org.sonar.core.util.Uuids;
import org.sonar.db.rule.RuleDto.Scope;
-import org.sonar.db.user.UserDto;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableSet.copyOf;
//voluntarily offset the remediation to be able to detect issues
.setDefRemediationGapMultiplier((nextInt(10) + 10) + "h")
.setDefRemediationFunction("LINEAR_OFFSET")
- .setCreatedAt(currentTimeMillis)
- .setUpdatedAt(currentTimeMillis + 5)
- .setScope(Scope.MAIN);
- }
-
- public static RuleMetadataDto newRuleMetadata() {
- return new RuleMetadataDto()
- .setRuleUuid("uuid_" + randomAlphanumeric(5))
.setRemediationBaseEffort(nextInt(10) + "h")
.setRemediationGapMultiplier(nextInt(10) + "h")
.setRemediationFunction("LINEAR_OFFSET")
.setAdHocName("adHocName_" + randomAlphanumeric(5))
.setAdHocDescription("adHocDescription_" + randomAlphanumeric(5))
.setAdHocSeverity(Severity.ALL.get(nextInt(Severity.ALL.size())))
- .setAdHocType(RuleType.values()[nextInt(RuleType.values().length - 1)]);
- }
-
- public static RuleMetadataDto newRuleMetadata(RuleDto rule) {
- return newRuleMetadata()
- .setRuleUuid(rule.getUuid());
- }
-
- public static RuleMetadataDto newRuleMetadata(RuleDto rule, UserDto noteUser) {
- return newRuleMetadata(rule).setNoteUserUuid(noteUser.getUuid());
+ .setAdHocType(RuleType.values()[nextInt(RuleType.values().length - 1)])
+ .setCreatedAt(currentTimeMillis)
+ .setUpdatedAt(currentTimeMillis + 5)
+ .setScope(Scope.MAIN);
}
public static RuleParamDto newRuleParam(RuleDto rule) {
return rule -> rule.setSystemTags(copyOf(tags));
}
- public static Consumer<RuleMetadataDto> setTags(String... tags) {
+ public static Consumer<RuleDto> setTags(String... tags) {
return rule -> rule.setTags(copyOf(tags));
}
*/
package org.sonar.server.rule;
-import com.google.common.collect.ImmutableSet;
+import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleDto.Scope;
+import static java.util.Collections.emptySet;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@Before
public void setup() {
- dbTester.rules().insertRule(rule1);
- dbTester.rules().insertRule(rule2);
- dbTester.rules().insertRule(rule3);
- dbTester.rules().insertRule(rule4);
+ dbTester.rules().insert(rule1);
+ dbTester.rules().insert(rule2);
+ dbTester.rules().insert(rule3);
+ dbTester.rules().insert(rule4);
session.commit();
}
@Test
public void findByKey_populates_system_tags_but_not_tags() {
RuleDto ruleDto = dbTester.rules()
- .insert(t -> t.setSystemTags(ImmutableSet.of(randomAlphanumeric(5), randomAlphanumeric(6))));
+ .insert(t -> t.setSystemTags(Set.of(randomAlphanumeric(5), randomAlphanumeric(6))).setTags(emptySet()));
dbTester.rules().insertRule();
Rule rule = underTest.findByKey(ruleDto.getKey());
assertThat(ruleDoc.status()).isEqualTo(ruleForIndexingDto.getStatus());
assertThat(ruleDoc.type().name()).isEqualTo(ruleForIndexingDto.getTypeAsRuleType().name());
assertThat(ruleDoc.createdAt()).isEqualTo(ruleForIndexingDto.getCreatedAt());
- assertThat(ruleDoc.getTags()).isEqualTo(ruleForIndexingDto.getSystemTags());
+ assertThat(ruleDoc.getTags())
+ .containsAll(ruleForIndexingDto.getSystemTags())
+ .containsAll(ruleForIndexingDto.getTags())
+ .hasSize(ruleForIndexingDto.getSystemTags().size() + ruleForIndexingDto.getTags().size());
assertThat(ruleDoc.updatedAt()).isEqualTo(ruleForIndexingDto.getUpdatedAt());
assertThat(ruleDoc.templateKey().repository()).isEqualTo(ruleForIndexingDto.getTemplateRepository());
assertThat(ruleDoc.templateKey().rule()).isEqualTo(ruleForIndexingDto.getTemplateRuleKey());
import org.sonar.db.DbTester;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.server.es.EsTester;
import org.sonar.server.es.Facets;
import org.sonar.server.es.SearchIdResult;
@Test
public void filter_by_tags() {
- RuleDto rule1 = createRule(setSystemTags("tag1s"));
- createRuleMetadata(rule1, setTags("tag1"));
- RuleDto rule2 = createRule(setSystemTags("tag2s"));
- createRuleMetadata(rule2, setTags("tag2"));
+ RuleDto rule1 = createRule(setSystemTags("tag1s"), setTags("tag1"));
+ RuleDto rule2 = createRule(setSystemTags("tag2s"), setTags("tag2"));
index();
assertThat(es.countDocuments(TYPE_RULE)).isEqualTo(2);
@Test
public void tags_facet_supports_selected_value_with_regexp_special_characters() {
- RuleDto rule = createRule();
- createRuleMetadata(rule, setTags("misra++"));
+ createRule(r -> r.setTags(Set.of("misra++")));
index();
RuleQuery query = new RuleQuery()
return createRule(r -> r.setLanguage("java"), consumer);
}
- @SafeVarargs
- private final RuleMetadataDto createRuleMetadata(RuleDto rule, Consumer<RuleMetadataDto>... populaters) {
- return db.rules().insertOrUpdateMetadata(rule, populaters);
- }
-
@Test
public void search_by_any_of_severities() {
createRule(setSeverity(BLOCKER));
@Test
public void listTags_should_return_tags() {
- RuleDto rule1 = createRule(setSystemTags("sys1", "sys2"));
- createRuleMetadata(rule1, setTags("tag1"));
-
- RuleDto rule2 = createRule(setSystemTags());
- createRuleMetadata(rule2, setTags("tag2"));
+ createRule(setSystemTags("sys1", "sys2"), setTags("tag1"));
+ createRule(setSystemTags(), setTags("tag2"));
index();
@Test
public void global_facet_on_repositories_and_tags() {
- createRule(setRepositoryKey("php"), setSystemTags("sysTag"));
- RuleDto rule1 = createRule(setRepositoryKey("php"), setSystemTags());
- createRuleMetadata(rule1, setTags("tag1"));
- RuleDto rule2 = createRule(setRepositoryKey("javascript"), setSystemTags());
- createRuleMetadata(rule2, setTags("tag1", "tag2"));
+ createRule(setRepositoryKey("php"), setSystemTags("sysTag"), setTags());
+ createRule(setRepositoryKey("php"), setSystemTags(), setTags("tag1"));
+ createRule(setRepositoryKey("javascript"), setSystemTags(), setTags("tag1", "tag2"));
index();
// should not have any facet!
}
private void setupStickyFacets() {
- createRule(setRepositoryKey("xoo"), setRuleKey("S001"), setLanguage("java"), setSystemTags(), setType(BUG));
- createRule(setRepositoryKey("xoo"), setRuleKey("S002"), setLanguage("java"), setSystemTags(), setType(CODE_SMELL));
- createRule(setRepositoryKey("xoo"), setRuleKey("S003"), setLanguage("java"), setSystemTags("T1", "T2"), setType(CODE_SMELL));
- createRule(setRepositoryKey("xoo"), setRuleKey("S011"), setLanguage("cobol"), setSystemTags(), setType(CODE_SMELL));
- createRule(setRepositoryKey("xoo"), setRuleKey("S012"), setLanguage("cobol"), setSystemTags(), setType(BUG));
- createRule(setRepositoryKey("foo"), setRuleKey("S013"), setLanguage("cobol"), setSystemTags("T3", "T4"),
+ createRule(setRepositoryKey("xoo"), setRuleKey("S001"), setLanguage("java"), setTags(), setSystemTags(), setType(BUG));
+ createRule(setRepositoryKey("xoo"), setRuleKey("S002"), setLanguage("java"), setTags(), setSystemTags(), setType(CODE_SMELL));
+ createRule(setRepositoryKey("xoo"), setRuleKey("S003"), setLanguage("java"), setTags(), setSystemTags("T1", "T2"), setType(CODE_SMELL));
+ createRule(setRepositoryKey("xoo"), setRuleKey("S011"), setLanguage("cobol"), setTags(), setSystemTags(), setType(CODE_SMELL));
+ createRule(setRepositoryKey("xoo"), setRuleKey("S012"), setLanguage("cobol"), setTags(), setSystemTags(), setType(BUG));
+ createRule(setRepositoryKey("foo"), setRuleKey("S013"), setLanguage("cobol"), setTags(), setSystemTags("T3", "T4"),
setType(VULNERABILITY));
- createRule(setRepositoryKey("foo"), setRuleKey("S111"), setLanguage("cpp"), setSystemTags(), setType(BUG));
- createRule(setRepositoryKey("foo"), setRuleKey("S112"), setLanguage("cpp"), setSystemTags(), setType(CODE_SMELL));
- createRule(setRepositoryKey("foo"), setRuleKey("S113"), setLanguage("cpp"), setSystemTags("T2", "T3"), setType(CODE_SMELL));
+ createRule(setRepositoryKey("foo"), setRuleKey("S111"), setLanguage("cpp"), setTags(), setSystemTags(), setType(BUG));
+ createRule(setRepositoryKey("foo"), setRuleKey("S112"), setLanguage("cpp"), setTags(), setSystemTags(), setType(CODE_SMELL));
+ createRule(setRepositoryKey("foo"), setRuleKey("S113"), setLanguage("cpp"), setTags(), setSystemTags("T2", "T3"), setType(CODE_SMELL));
index();
}
@Test
public void tags_facet_should_find_tags() {
- RuleDto rule = createRule(setSystemTags());
- createRuleMetadata(rule, setTags("bla"));
+ createRule(setSystemTags(), setTags("bla"));
index();
RuleQuery query = new RuleQuery();
ComponentDto module = db.components().insertComponent(newModuleDto(project));
ComponentDto file = db.components().insertComponent(newFileDto(project));
- RuleDto rule1 = ruleDbTester.insert();
- RuleDto rule2 = ruleDbTester.insert();
- ruleDbTester.insertOrUpdateMetadata(rule1, m -> m.setAdHocName(ruleAdHocName));
- ruleDbTester.insertOrUpdateMetadata(rule2, m -> m.setAdHocName(ruleAdHocName));
+ RuleDto rule1 = ruleDbTester.insert(r -> r.setAdHocName(ruleAdHocName));
+ RuleDto rule2 = ruleDbTester.insert(r -> r.setAdHocName(ruleAdHocName));
newRule(RuleKey.of("findbugs", "NullReference"));
SearchRequest request = new SearchRequest()
.setIssues(asList("anIssueKey"))
import org.sonar.db.rule.RuleDescriptionSectionDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleDto.Scope;
-import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.user.UserDto;
import org.sonar.markdown.Markdown;
import org.sonarqube.ws.Common.RuleScope;
import org.sonarqube.ws.Rules;
-import static java.util.stream.Collectors.joining;
import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.db.rule.RuleDto.Format.MARKDOWN;
return ruleResponse.build();
}
- public Rules.Rule toWsRule(RuleDto ruleDto, SearchResult result, Set<String> fieldsToReturn, RuleMetadataDto metadata,
- Map<String, UserDto> usersByUuid, Map<String, List<DeprecatedRuleKeyDto>> deprecatedRuleKeysByRuleUuid) {
+ public Rules.Rule toWsRule(RuleDto ruleDto, SearchResult result, Set<String> fieldsToReturn, Map<String, UserDto> usersByUuid,
+ Map<String, List<DeprecatedRuleKeyDto>> deprecatedRuleKeysByRuleUuid) {
Rules.Rule.Builder ruleResponse = Rules.Rule.newBuilder();
applyRuleDefinition(ruleResponse, ruleDto, result, fieldsToReturn, deprecatedRuleKeysByRuleUuid);
- applyRuleMetadata(ruleResponse, ruleDto, metadata, usersByUuid, fieldsToReturn);
- setDebtRemediationFunctionFields(ruleResponse, ruleDto, metadata, fieldsToReturn);
+ setDebtRemediationFunctionFields(ruleResponse, ruleDto, fieldsToReturn);
+ setNotesFields(ruleResponse, ruleDto, usersByUuid, fieldsToReturn);
return ruleResponse.build();
}
setEffortToFixDescription(ruleResponse, ruleDto, fieldsToReturn);
setScope(ruleResponse, ruleDto, fieldsToReturn);
setDeprecatedKeys(ruleResponse, ruleDto, fieldsToReturn, deprecatedRuleKeysByRuleUuid);
- return ruleResponse;
- }
- private void applyRuleMetadata(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, RuleMetadataDto metadata, Map<String, UserDto> usersByUuid,
- Set<String> fieldsToReturn) {
- setTags(ruleResponse, metadata, fieldsToReturn);
- setNotesFields(ruleResponse, metadata, usersByUuid, fieldsToReturn);
- setIsRemediationFunctionOverloaded(ruleResponse, metadata, fieldsToReturn);
+ setTags(ruleResponse, ruleDto, fieldsToReturn);
+ setIsRemediationFunctionOverloaded(ruleResponse, ruleDto, fieldsToReturn);
if (ruleDto.isAdHoc()) {
- setAdHocName(ruleResponse, metadata, fieldsToReturn);
- setAdHocDescription(ruleResponse, metadata, fieldsToReturn);
- setAdHocSeverity(ruleResponse, metadata, fieldsToReturn);
- setAdHocType(ruleResponse, metadata);
+ setAdHocName(ruleResponse, ruleDto, fieldsToReturn);
+ setAdHocDescription(ruleResponse, ruleDto, fieldsToReturn);
+ setAdHocSeverity(ruleResponse, ruleDto, fieldsToReturn);
+ setAdHocType(ruleResponse, ruleDto);
}
+ return ruleResponse;
}
- private static void setAdHocName(Rules.Rule.Builder ruleResponse, RuleMetadataDto metadata, Set<String> fieldsToReturn) {
- String adHocName = metadata.getAdHocName();
+ private static void setAdHocName(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set<String> fieldsToReturn) {
+ String adHocName = ruleDto.getAdHocName();
if (adHocName != null && shouldReturnField(fieldsToReturn, FIELD_NAME)) {
ruleResponse.setName(adHocName);
}
}
- private void setAdHocDescription(Rules.Rule.Builder ruleResponse, RuleMetadataDto metadata, Set<String> fieldsToReturn) {
- String adHocDescription = metadata.getAdHocDescription();
+ private void setAdHocDescription(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set<String> fieldsToReturn) {
+ String adHocDescription = ruleDto.getAdHocDescription();
if (adHocDescription != null && shouldReturnField(fieldsToReturn, FIELD_HTML_DESCRIPTION)) {
ruleResponse.setHtmlDesc(macroInterpreter.interpret(adHocDescription));
}
}
- private static void setAdHocSeverity(Rules.Rule.Builder ruleResponse, RuleMetadataDto metadata, Set<String> fieldsToReturn) {
- String severity = metadata.getAdHocSeverity();
+ private static void setAdHocSeverity(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set<String> fieldsToReturn) {
+ String severity = ruleDto.getAdHocSeverity();
if (shouldReturnField(fieldsToReturn, FIELD_SEVERITY) && severity != null) {
ruleResponse.setSeverity(severity);
}
}
- private static void setAdHocType(Rules.Rule.Builder ruleResponse, RuleMetadataDto metadata) {
- Integer ruleType = metadata.getAdHocType();
+ private static void setAdHocType(Rules.Rule.Builder ruleResponse, RuleDto ruleDto) {
+ Integer ruleType = ruleDto.getAdHocType();
if (ruleType != null) {
ruleResponse.setType(Common.RuleType.forNumber(ruleType));
}
}
}
- private static void setIsRemediationFunctionOverloaded(Rules.Rule.Builder ruleResponse, RuleMetadataDto ruleDto, Set<String> fieldsToReturn) {
+ private static void setIsRemediationFunctionOverloaded(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set<String> fieldsToReturn) {
if (shouldReturnField(fieldsToReturn, FIELD_DEBT_OVERLOADED) || shouldReturnField(fieldsToReturn, FIELD_REM_FUNCTION_OVERLOADED)) {
ruleResponse.setDebtOverloaded(isRemediationFunctionOverloaded(ruleDto));
ruleResponse.setRemFnOverloaded(isRemediationFunctionOverloaded(ruleDto));
}
}
- private static void setDebtRemediationFunctionFields(Rules.Rule.Builder ruleResponse, RuleDto ruleDefinitionDto, RuleMetadataDto ruleMetadataDto,
+ private static void setDebtRemediationFunctionFields(Rules.Rule.Builder ruleResponse, RuleDto ruleDto,
Set<String> fieldsToReturn) {
if (shouldReturnField(fieldsToReturn, FIELD_DEBT_REM_FUNCTION) || shouldReturnField(fieldsToReturn, FIELD_REM_FUNCTION)) {
- DebtRemediationFunction debtRemediationFunction = debtRemediationFunction(ruleDefinitionDto, ruleMetadataDto);
+ DebtRemediationFunction debtRemediationFunction = debtRemediationFunction(ruleDto);
if (debtRemediationFunction != null) {
if (debtRemediationFunction.type() != null) {
ruleResponse.setRemFnType(debtRemediationFunction.type().name());
}
}
- private static void setTags(Rules.Rule.Builder ruleResponse, RuleMetadataDto ruleDto, Set<String> fieldsToReturn) {
+ private static void setTags(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set<String> fieldsToReturn) {
if (shouldReturnField(fieldsToReturn, FIELD_TAGS)) {
ruleResponse.getTagsBuilder().addAllTags(ruleDto.getTags());
}
sectionDto.getContent();
}
- private void setNotesFields(Rules.Rule.Builder ruleResponse, RuleMetadataDto ruleDto, Map<String, UserDto> usersByUuid, Set<String> fieldsToReturn) {
+ private void setNotesFields(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Map<String, UserDto> usersByUuid, Set<String> fieldsToReturn) {
String noteData = ruleDto.getNoteData();
if (shouldReturnField(fieldsToReturn, "htmlNote") && noteData != null) {
ruleResponse.setHtmlNote(macroInterpreter.interpret(Markdown.convertToHtml(noteData)));
return fieldsToReturn.isEmpty() || fieldsToReturn.contains(fieldName);
}
- private static boolean isRemediationFunctionOverloaded(RuleMetadataDto rule) {
+ private static boolean isRemediationFunctionOverloaded(RuleDto rule) {
return rule.getRemediationFunction() != null;
}
}
@CheckForNull
- private static DebtRemediationFunction debtRemediationFunction(RuleDto ruleDefinitionDto, RuleMetadataDto ruleMetadataDto) {
- final String function = ruleMetadataDto.getRemediationFunction();
+ private static DebtRemediationFunction debtRemediationFunction(RuleDto ruleDto) {
+ final String function = ruleDto.getRemediationFunction();
if (function == null || function.isEmpty()) {
- return defaultDebtRemediationFunction(ruleDefinitionDto);
+ return defaultDebtRemediationFunction(ruleDto);
} else {
return new DefaultDebtRemediationFunction(
DebtRemediationFunction.Type.valueOf(function.toUpperCase(Locale.ENGLISH)),
- ruleMetadataDto.getRemediationGapMultiplier(),
- ruleMetadataDto.getRemediationBaseEffort());
+ ruleDto.getRemediationGapMultiplier(),
+ ruleDto.getRemediationBaseEffort());
}
}
private void writeRules(DbSession dbSession, SearchResponse.Builder response, SearchResult result, SearchOptions context) {
Map<String, UserDto> usersByUuid = ruleWsSupport.getUsersByUuid(dbSession, result.rules);
Map<String, List<DeprecatedRuleKeyDto>> deprecatedRuleKeysByRuleUuid = getDeprecatedRuleKeysByRuleUuid(dbSession, result.rules, context);
- result.rules.forEach(rule -> response.addRules(mapper.toWsRule(rule, result, context.getFields(), rule.getMetadata(), usersByUuid,
+ result.rules.forEach(rule -> response.addRules(mapper.toWsRule(rule, result, context.getFields(), usersByUuid,
deprecatedRuleKeysByRuleUuid)));
}
private ShowResponse buildResponse(DbSession dbSession, Request request, SearchAction.SearchResult searchResult) {
ShowResponse.Builder responseBuilder = ShowResponse.newBuilder();
RuleDto rule = searchResult.getRules().get(0);
- responseBuilder.setRule(mapper.toWsRule(rule, searchResult, Collections.emptySet(), rule.getMetadata(),
+ responseBuilder.setRule(mapper.toWsRule(rule, searchResult, Collections.emptySet(),
ruleWsSupport.getUsersByUuid(dbSession, searchResult.getRules()), emptyMap()));
if (request.mandatoryParamAsBoolean(PARAM_ACTIVES)) {
activeRuleCompleter.completeShow(dbSession, rule).forEach(responseBuilder::addActives);
.setRuleParameters(ruleParameters)
.setTotal(1L);
responseBuilder
- .setRule(mapper.toWsRule(rule, searchResult, Collections.emptySet(), rule.getMetadata(),
+ .setRule(mapper.toWsRule(rule, searchResult, Collections.emptySet(),
ruleWsSupport.getUsersByUuid(dbSession, singletonList(rule)), emptyMap()));
return responseBuilder.build();
}
private IssueDto insertIssue() {
- RuleDto rule = ruleDbTester.insertRule(newRuleDto());
+ RuleDto rule = ruleDbTester.insert(newRuleDto());
ComponentDto project = componentDbTester.insertPrivateProject();
ComponentDto file = componentDbTester.insertComponent(newFileDto(project));
return issueDbTester.insert(newDto(rule, file, project));
}
private IssueDto newIssue() {
- RuleDto rule = db.rules().insertRule(newRuleDto());
+ RuleDto rule = db.rules().insert(newRuleDto());
ComponentDto project = db.components().insertPrivateProject();
ComponentDto file = db.components().insertComponent(newFileDto(project));
return newDto(rule, file, project);
}
private IssueDto newIssue() {
- RuleDto rule = db.rules().insertRule(newRuleDto());
+ RuleDto rule = db.rules().insert(newRuleDto());
ComponentDto project = db.components().insertPrivateProject();
ComponentDto file = db.components().insertComponent(newFileDto(project));
return newDto(rule, file, project);
@Test
public void changelog_of_file_move_contains_file_names() {
- RuleDto rule = db.rules().insertRule(newRuleDto());
+ RuleDto rule = db.rules().insert(newRuleDto());
ComponentDto project = db.components().insertPrivateProject();
ComponentDto file1 = db.components().insertComponent(newFileDto(project));
ComponentDto file2 = db.components().insertComponent(newFileDto(project));
import org.sonar.db.protobuf.DbCommons;
import org.sonar.db.protobuf.DbIssues;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.db.user.UserDto;
import org.sonar.server.es.EsTester;
ComponentDto project = db.components().insertPublicProject();
indexPermissions();
ComponentDto file = db.components().insertComponent(newFileDto(project));
- RuleDto rule = db.rules().insertIssueRule(RuleTesting.EXTERNAL_XOO, r -> r.setIsExternal(true).setLanguage("xoo").setIsAdHoc(true));
- RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule, m -> m.setAdHocName("different_rule_name"));
+ RuleDto rule = db.rules().insertIssueRule(RuleTesting.EXTERNAL_XOO,
+ r -> r
+ .setIsExternal(true)
+ .setLanguage("xoo")
+ .setIsAdHoc(true)
+ .setAdHocName("different_rule_name"));
IssueDto issue = db.issues().insertIssue(rule, project, file);
indexIssues();
assertThat(response.getRules().getRulesList())
.extracting(Common.Rule::getKey, Common.Rule::getName)
- .containsExactlyInAnyOrder(tuple(rule.getKey().toString(), ruleMetadata.getAdHocName()));
+ .containsExactlyInAnyOrder(tuple(rule.getKey().toString(), rule.getAdHocName()));
}
@Test
assertThat(ws.newRequest()
.setMultiParam("author", singletonList("unknown"))
.executeProtobuf(SearchWsResponse.class).getIssuesList())
- .isEmpty();
+ .isEmpty();
}
@Test
.setCreatedAt(new Date().getTime())
.setUpdatedAt(new Date().getTime());
dbTester.rules().insert(templateRule);
- dbTester.rules().insertOrUpdateMetadata(templateRule.getMetadata().setRuleUuid(templateRule.getUuid()));
dbTester.rules().insertRuleParam(templateRule, param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"));
ruleIndexer.commitAndIndex(dbTester.getSession(), templateRule.getUuid());
return templateRule;
.setRemediationGapMultiplier("1d")
.setRemediationBaseEffort("5min");
db.rules().insert(ruleDto);
- db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleUuid(ruleDto.getUuid()));
dbSession.commit();
RuleUpdate update = createForPluginRule(RULE_KEY);
.setRemediationGapMultiplier("1d")
.setRemediationBaseEffort("5min");
db.rules().insert(ruleDto);
- db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleUuid(ruleDto.getUuid()));
dbSession.commit();
RuleUpdate update = createForPluginRule(RULE_KEY)
.setNoteData("my *note*")
.setNoteUserUuid("me");
db.rules().insert(ruleDto);
- db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleUuid(ruleDto.getUuid()));
dbSession.commit();
RuleUpdate update = createForPluginRule(RULE_KEY)
.setTags(Sets.newHashSet("security"))
.setSystemTags(Sets.newHashSet("java8", "javadoc"));
db.rules().insert(ruleDto);
- db.rules().insertOrUpdateMetadata(ruleDto.getMetadata());
dbSession.commit();
RuleUpdate update = createForPluginRule(RULE_KEY)
.setRemediationGapMultiplier(null)
.setRemediationBaseEffort("1min");
db.rules().insert(ruleDto);
- db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleUuid(ruleDto.getUuid()));
dbSession.commit();
RuleUpdate update = createForPluginRule(RULE_KEY)
// Template rule
RuleDto templateRule = newTemplateRule(RuleKey.of("java", "S001")).setType(CODE_SMELL);
db.rules().insert(templateRule);
- db.rules().insertOrUpdateMetadata(templateRule.getMetadata().setRuleUuid(templateRule.getUuid()));
db.rules().insertRuleParam(templateRule, param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"));
String result = ws.newRequest()
import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.assertj.core.api.iterable.Extractor;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.es.EsTester;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
+import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
@Test
public void return_all_rules() {
- RuleDto rule1 = db.rules().insert(r1 -> r1.setLanguage("java"));
- RuleDto rule2 = db.rules().insert(r1 -> r1.setLanguage("java"));
+ RuleDto rule1 = db.rules().insert(r1 -> r1.setLanguage("java").setNoteUserUuid(null));
+ RuleDto rule2 = db.rules().insert(r1 -> r1.setLanguage("java").setNoteUserUuid(null));
indexRules();
verify(r -> {
@Test
public void return_note_login() {
UserDto user1 = db.users().insertUser();
- RuleDto rule1 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule1, user1);
+ RuleDto rule1 = db.rules().insert(r -> r.setNoteUserUuid(user1.getUuid()));
UserDto disableUser = db.users().insertDisabledUser();
- RuleDto rule2 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule2, disableUser);
+ RuleDto rule2 = db.rules().insert(r -> r.setNoteUserUuid(disableUser.getUuid()));
indexRules();
SearchResponse result = ws.newRequest()
@Test
public void filter_by_rule_key() {
- RuleDto rule1 = db.rules().insert(r1 -> r1.setLanguage("java"));
- RuleDto rule2 = db.rules().insert(r1 -> r1.setLanguage("java"));
+ RuleDto rule1 = db.rules().insert(r1 -> r1.setLanguage("java").setNoteUserUuid(null));
+ db.rules().insert(r1 -> r1.setLanguage("java").setNoteUserUuid(null));
indexRules();
verify(r -> r.setParam(PARAM_RULE_KEY, rule1.getKey().toString()), rule1);
@Test
public void filter_by_rule_name() {
- RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever"));
- RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff"));
+ RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever").setNoteUserUuid(null));
+ RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff").setNoteUserUuid(null));
indexRules();
verify(r -> r.setParam("q", "Be"), rule1);
@Test
public void filter_by_rule_name_requires_all_words_to_match() {
- RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever"));
- RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff"));
+ RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever").setNoteUserUuid(null));
+ RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff").setNoteUserUuid(null));
indexRules();
verify(r -> r.setParam("q", "Best other"));
@Test
public void filter_by_rule_name_does_not_interpret_query() {
- RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule for-ever"));
- RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff"));
+ RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule for-ever").setNoteUserUuid(null));
+ RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff").setNoteUserUuid(null));
indexRules();
// do not interpret "-" as a "not"
@Test
public void filter_by_rule_description() {
RuleDto rule1 = db.rules()
- .insert(r1 -> r1.addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "This is the <bold>best</bold> rule now&for<b>ever</b>")));
- RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff"));
+ .insert(
+ r1 -> r1
+ .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "This is the <bold>best</bold> rule now&for<b>ever</b>"))
+ .setNoteUserUuid(null));
+ db.rules().insert(r1 -> r1.setName("Some other stuff").setNoteUserUuid(null));
indexRules();
verify(r -> r.setParam("q", "Best "), rule1);
@Test
public void filter_by_rule_name_or_descriptions_requires_all_words_to_match_anywhere() {
- RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever")
+ RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever").setNoteUserUuid(null)
.addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "This is a good rule")));
- RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Another thing")
+ db.rules().insert(r1 -> r1.setName("Another thing").setNoteUserUuid(null)
.addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "Another thing")));
indexRules();
@Test
public void return_all_rule_fields_by_default() {
- RuleDto rule = db.rules().insert(r -> r.setLanguage("java"));
+ RuleDto rule = db.rules().insert(r -> r.setLanguage("java").setNoteUserUuid(null));
indexRules();
Rules.SearchResponse response = ws.newRequest().executeProtobuf(Rules.SearchResponse.class);
@Test
public void should_filter_on_specific_tags() {
- RuleDto rule1 = db.rules().insert(r -> r.setLanguage("java"));
- RuleMetadataDto metadata1 = insertMetadata(rule1, setTags("tag1", "tag2"));
- RuleDto rule2 = db.rules().insert(r -> r.setLanguage("java"));
- RuleMetadataDto metadata2 = insertMetadata(rule2);
+ RuleDto rule1 = db.rules().insert(r -> r.setLanguage("java").setTags(Set.of("tag1", "tag2")));
+ db.rules().insert(r -> r.setLanguage("java"));
indexRules();
Consumer<TestRequest> request = r -> r
.setParam("f", "repo,name")
- .setParam("tags", metadata1.getTags().stream().collect(Collectors.joining(",")));
+ .setParam("tags", rule1.getTags().stream().collect(Collectors.joining(",")));
verify(request, rule1);
}
@Test
public void when_searching_for_several_tags_combine_them_with_OR() {
- RuleDto bothTagsRule = db.rules().insert(r -> r.setLanguage("java"));
- insertMetadata(bothTagsRule, setTags("tag1", "tag2"));
- RuleDto oneTagRule = db.rules().insert(r -> r.setLanguage("java"));
- insertMetadata(oneTagRule, setTags("tag1"));
- RuleDto otherTagRule = db.rules().insert(r -> r.setLanguage("java"));
- insertMetadata(otherTagRule, setTags("tag2"));
- RuleDto noTagRule = db.rules().insert(r -> r.setLanguage("java"));
- insertMetadata(noTagRule, setTags());
+ RuleDto bothTagsRule = db.rules().insert(r -> r.setLanguage("java"), setTags("tag1", "tag2"));
+ RuleDto oneTagRule = db.rules().insert(r -> r.setLanguage("java"), setTags("tag1"));
+ RuleDto otherTagRule = db.rules().insert(r -> r.setLanguage("java"), setTags("tag2"));
+ db.rules().insert(r -> r.setLanguage("java"), setTags());
indexRules();
Consumer<TestRequest> request = r -> r
@Test
public void should_list_tags_in_tags_facet() {
String[] tags = get101Tags();
- RuleDto rule = db.rules().insert(setSystemTags("x"));
- insertMetadata(rule, setTags(tags));
+ db.rules().insert(setSystemTags("x"), setTags(tags));
indexRules();
SearchResponse result = ws.newRequest()
@Test
public void should_list_tags_ordered_by_count_then_by_name_in_tags_facet() {
- RuleDto rule = db.rules().insert(setSystemTags("tag7", "tag5", "tag3", "tag1", "tag9"));
- insertMetadata(rule, setTags("tag2", "tag4", "tag6", "tag8", "tagA"));
- db.rules().insert(setSystemTags("tag2"));
+ db.rules().insert(setSystemTags("tag7", "tag5", "tag3", "tag1", "tag9"), setTags("tag2", "tag4", "tag6", "tag8", "tagA"));
+ db.rules().insert(setSystemTags("tag2"), setTags());
indexRules();
SearchResponse result = ws.newRequest()
@Test
public void should_include_selected_matching_tag_in_facet() {
- RuleDto rule = db.rules().insert(setSystemTags("tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tagA", "x"));
+ db.rules().insert(
+ setSystemTags("tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tagA", "x"),
+ r -> r.setNoteUserUuid(null));
indexRules();
SearchResponse result = ws.newRequest()
@Test
public void should_return_specific_tags() {
- RuleDto rule = db.rules().insert(r -> r.setLanguage("java"));
- RuleMetadataDto metadata = insertMetadata(rule, setTags("tag1", "tag2"));
+ RuleDto rule = db.rules().insert(r -> r.setLanguage("java"), setTags("tag1", "tag2"));
indexRules();
SearchResponse result = ws.newRequest()
assertThat(result.getRulesList()).extracting(Rule::getKey).containsExactly(rule.getKey().toString());
assertThat(result.getRulesList())
.extracting(Rule::getTags).flatExtracting(Rules.Tags::getTagsList)
- .containsExactly(metadata.getTags().toArray(new String[0]));
+ .containsExactly(rule.getTags().toArray(new String[0]));
}
@Test
@Test
public void search_debt_rules_with_default_and_overridden_debt_values() {
- RuleDto rule = db.rules().insert(r -> r.setLanguage("java")
+ db.rules().insert(r -> r.setLanguage("java")
.setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
.setDefRemediationGapMultiplier("1h")
- .setDefRemediationBaseEffort("15min"));
-
- RuleMetadataDto metadata = insertMetadata(rule,
- r -> r.setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
- .setRemediationGapMultiplier("2h")
- .setRemediationBaseEffort("25min"));
+ .setDefRemediationBaseEffort("15min")
+ .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setRemediationGapMultiplier("2h")
+ .setRemediationBaseEffort("25min"));
indexRules();
@Test
public void search_debt_rules_with_default_linear_offset_and_overridden_constant_debt() {
- RuleDto rule = db.rules().insert(r -> r.setLanguage("java")
+ db.rules().insert(r -> r.setLanguage("java")
.setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
.setDefRemediationGapMultiplier("1h")
- .setDefRemediationBaseEffort("15min"));
-
- RuleMetadataDto metadata = insertMetadata(rule,
- r -> r.setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name())
- .setRemediationGapMultiplier(null)
- .setRemediationBaseEffort("5min"));
+ .setDefRemediationBaseEffort("15min")
+ .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name())
+ .setRemediationGapMultiplier(null)
+ .setRemediationBaseEffort("5min"));
indexRules();
@Test
public void search_debt_rules_with_default_linear_offset_and_overridden_linear_debt() {
- RuleDto rule = db.rules().insert(r -> r.setLanguage("java")
+ db.rules().insert(r -> r.setLanguage("java")
.setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
.setDefRemediationGapMultiplier("1h")
- .setDefRemediationBaseEffort("15min"));
-
- RuleMetadataDto metadata = insertMetadata(rule,
- r -> r.setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name())
- .setRemediationGapMultiplier("1h")
- .setRemediationBaseEffort(null));
+ .setDefRemediationBaseEffort("15min")
+ .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name())
+ .setRemediationGapMultiplier("1h")
+ .setRemediationBaseEffort(null));
indexRules();
@Test
public void search_all_active_rules() {
QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage("java"));
- RuleDto rule = db.rules().insert(r -> r.setLanguage("java"));
+ RuleDto rule = db.rules().insert(r -> r.setLanguage("java").setNoteUserUuid(null));
RuleActivation activation = RuleActivation.create(rule.getUuid(), BLOCKER, null);
qProfileRules.activateAndCommit(db.getSession(), profile, singleton(activation));
.setLanguage(profile.getLanguage())
.setRepositoryKey("repositoryKey1")
.setSystemTags(new HashSet<>(singletonList("tag1")))
+ .setTags(emptySet())
.setSeverity("CRITICAL")
+ .setNoteUserUuid(null)
.setStatus(RuleStatus.BETA)
.setType(RuleType.CODE_SMELL));
.setLanguage(profile.getLanguage())
.setRepositoryKey("repositoryKey2")
.setSystemTags(new HashSet<>(singletonList("tag2")))
+ .setTags(emptySet())
.setSeverity("MAJOR")
+ .setNoteUserUuid(null)
.setStatus(RuleStatus.DEPRECATED)
.setType(RuleType.VULNERABILITY));
RuleActivation activation = RuleActivation.create(rule2.getUuid(), null, null);
.setLanguage("language3")
.setRepositoryKey("repositoryKey3")
.setSystemTags(new HashSet<>(singletonList("tag3")))
+ .setTags(emptySet())
+ .setNoteUserUuid(null)
.setSeverity("BLOCKER")
.setStatus(RuleStatus.READY)
.setType(RuleType.BUG));
assertThat(result.getRulesList()).extracting(responseExtractor).containsExactly(expected);
}
- @SafeVarargs
- private final RuleMetadataDto insertMetadata(RuleDto rule, Consumer<RuleMetadataDto>... populaters) {
- RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(rule, populaters);
- ruleIndexer.commitAndIndex(db.getSession(), rule.getUuid());
- return metadata;
- }
-
private void verifyNoResults(Consumer<TestRequest> requestPopulator) {
verify(requestPopulator);
}
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.rule.RuleDescriptionSectionDto;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.rule.RuleDescriptionFormatter;
new ShowAction(db.getDbClient(), new RuleMapper(languages, macroInterpreter, new RuleDescriptionFormatter()),
new ActiveRuleCompleter(db.getDbClient(), languages),
new RuleWsSupport(db.getDbClient(), userSession)));
+ private UserDto userDto;
@Before
public void before() {
+ userDto = db.users().insertUser();
doReturn(INTERPRETED).when(macroInterpreter).interpret(anyString());
}
@Test
public void show_rule_key() {
- RuleDto rule = db.rules().insert();
+ RuleDto rule = db.rules().insert(r -> r.setNoteUserUuid(userDto.getUuid()));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, rule.getKey().toString())
@Test
public void show_rule_with_basic_info() {
- RuleDto rule = db.rules().insert();
+ RuleDto rule = db.rules().insert(r -> r.setNoteUserUuid(userDto.getUuid()));
RuleParamDto ruleParam = db.rules().insertRuleParam(rule);
ShowResponse result = ws.newRequest()
@Test
public void show_rule_tags() {
- RuleDto rule = db.rules().insert();
- RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(rule, setTags("tag1", "tag2"), m -> m.setNoteData(null).setNoteUserUuid(null));
+ RuleDto rule = db.rules().insert(setTags("tag1", "tag2"), r -> r.setNoteData(null).setNoteUserUuid(null));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, rule.getKey().toString())
.executeProtobuf(ShowResponse.class);
assertThat(result.getRule().getTags().getTagsList())
- .containsExactly(metadata.getTags().toArray(new String[0]));
+ .containsExactly(rule.getTags().toArray(new String[0]));
}
@Test
public void show_rule_with_note_login() {
- RuleDto rule = db.rules().insert();
UserDto user = db.users().insertUser();
- db.rules().insertOrUpdateMetadata(rule, user);
+ RuleDto rule = db.rules().insert(r -> r.setNoteUserUuid(user.getUuid()));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, rule.getKey().toString())
.setDefRemediationFunction("LINEAR_OFFSET")
.setDefRemediationGapMultiplier("5d")
.setDefRemediationBaseEffort("10h")
- .setGapDescription("gap desc"));
+ .setGapDescription("gap desc")
+ .setNoteUserUuid(userDto.getUuid())
+ .setRemediationFunction(null)
+ .setRemediationGapMultiplier(null)
+ .setRemediationBaseEffort(null));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, rule.getKey().toString())
RuleDto rule = db.rules().insert(r -> r
.setDefRemediationFunction(null)
.setDefRemediationGapMultiplier(null)
- .setDefRemediationBaseEffort(null));
- db.rules().insertOrUpdateMetadata(rule,
- m -> m.setNoteData(null).setNoteUserUuid(null),
- m -> m
- .setRemediationFunction("LINEAR_OFFSET")
- .setRemediationGapMultiplier("5d")
- .setRemediationBaseEffort("10h"));
+ .setDefRemediationBaseEffort(null)
+ .setNoteData(null)
+ .setNoteUserUuid(null)
+ .setRemediationFunction("LINEAR_OFFSET")
+ .setRemediationGapMultiplier("5d")
+ .setRemediationBaseEffort("10h"));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, rule.getKey().toString())
RuleDto rule = db.rules().insert(r -> r
.setDefRemediationFunction("LINEAR_OFFSET")
.setDefRemediationGapMultiplier("5d")
- .setDefRemediationBaseEffort("10h"));
- db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null),
- m -> m
- .setRemediationFunction("CONSTANT_ISSUE")
- .setRemediationGapMultiplier(null)
- .setRemediationBaseEffort("15h"));
+ .setDefRemediationBaseEffort("10h")
+ .setNoteData(null)
+ .setNoteUserUuid(null)
+ .setRemediationFunction("CONSTANT_ISSUE")
+ .setRemediationGapMultiplier(null)
+ .setRemediationBaseEffort("15h"));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, rule.getKey().toString())
RuleDto rule = db.rules().insert(r -> r
.setDefRemediationFunction(null)
.setDefRemediationGapMultiplier(null)
- .setDefRemediationBaseEffort(null));
- db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null),
- m -> m
- .setRemediationFunction(null)
- .setRemediationGapMultiplier(null)
- .setRemediationBaseEffort(null));
+ .setDefRemediationBaseEffort(null)
+ .setNoteData(null)
+ .setNoteUserUuid(null)
+ .setRemediationFunction(null)
+ .setRemediationGapMultiplier(null)
+ .setRemediationBaseEffort(null));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, rule.getKey().toString())
.setDefRemediationFunction("LINEAR_OFFSET")
.setDefRemediationGapMultiplier("5d")
.setDefRemediationBaseEffort("10h")
- .setGapDescription("gap desc"));
- db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null),
- m -> m
- .setRemediationFunction("CONSTANT_ISSUE")
- .setRemediationGapMultiplier(null)
- .setRemediationBaseEffort("15h"));
+ .setGapDescription("gap desc")
+ .setNoteData(null)
+ .setNoteUserUuid(null)
+ .setRemediationFunction("CONSTANT_ISSUE")
+ .setRemediationGapMultiplier(null)
+ .setRemediationBaseEffort("15h"));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, rule.getKey().toString())
// Custom rule
RuleDto customRule = newCustomRule(templateRule)
.addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "<div>line1\nline2</div>"))
- .setDescriptionFormat(MARKDOWN);
+ .setDescriptionFormat(MARKDOWN)
+ .setNoteUserUuid(userDto.getUuid());
db.rules().insert(customRule);
doReturn("<div>line1<br/>line2</div>").when(macroInterpreter).interpret("<div>line1\nline2</div>");
public void show_external_rule() {
RuleDto externalRule = db.rules().insert(r -> r
.setIsExternal(true)
- .setName("ext rule name"));
+ .setName("ext rule name")
+ .setNoteUserUuid(userDto.getUuid()));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, externalRule.getKey().toString())
public void show_adhoc_rule() {
RuleDto externalRule = db.rules().insert(r -> r
.setIsExternal(true)
- .setIsAdHoc(true));
- RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(externalRule, m -> m
+ .setIsAdHoc(true)
.setAdHocName("adhoc name")
.setAdHocDescription("<div>desc</div>")
.setAdHocSeverity(Severity.BLOCKER)
.setAdHocType(RuleType.VULNERABILITY)
.setNoteData(null)
.setNoteUserUuid(null));
- doReturn("<div>desc2</div>").when(macroInterpreter).interpret(metadata.getAdHocDescription());
+ doReturn("<div>desc2</div>").when(macroInterpreter).interpret(externalRule.getAdHocDescription());
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, externalRule.getKey().toString())
RuleDto rule = createRuleWithDescriptionSections(section1, section2, section3);
rule.setType(RuleType.SECURITY_HOTSPOT);
+ rule.setNoteUserUuid(userDto.getUuid());
db.rules().insert(rule);
ShowResponse result = ws.newRequest()
RuleDto rule = createRuleWithDescriptionSections(section);
rule.setDescriptionFormat(MARKDOWN);
+ rule.setNoteUserUuid(userDto.getUuid());
db.rules().insert(rule);
ShowResponse result = ws.newRequest()
.setName("predefined name")
.addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "<div>predefined desc</div>"))
.setSeverity(Severity.BLOCKER)
- .setType(RuleType.VULNERABILITY));
- RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(externalRule, m -> m
+ .setType(RuleType.VULNERABILITY)
.setAdHocName("adhoc name")
.setAdHocDescription("<div>adhoc desc</div>")
.setAdHocSeverity(Severity.MAJOR)
.setAdHocType(RuleType.CODE_SMELL)
.setNoteData(null)
.setNoteUserUuid(null));
- doReturn("<div>adhoc desc</div>").when(macroInterpreter).interpret(metadata.getAdHocDescription());
+ doReturn("<div>adhoc desc</div>").when(macroInterpreter).interpret(externalRule.getAdHocDescription());
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, externalRule.getKey().toString())
.setName(null)
.setDescriptionFormat(null)
.setSeverity((String) null)
- .setType(0));
+ .setNoteData(null)
+ .setNoteUserUuid(null)
+ .setAdHocDescription(null)
+ .setType(0)
+ .setAdHocSeverity(null)
+ .setAdHocName(null)
+ .setAdHocType(0));
ShowResponse result = ws.newRequest()
.setParam(PARAM_KEY, externalRule.getKey().toString())
@Test
public void show_rule_with_activation() {
- RuleDto rule = db.rules().insert();
+ RuleDto rule = db.rules().insert(r -> r.setNoteData(null).setNoteUserUuid(null));
RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setType("STRING").setDescription("Reg *exp*").setDefaultValue(".*"));
- db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null));
QProfileDto qProfile = db.qualityProfiles().insert();
ActiveRuleDto activeRule = db.qualityProfiles().activateRule(qProfile, rule);
db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, new ActiveRuleParamDto()
@Test
public void show_rule_without_activation() {
- RuleDto rule = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null));
+ RuleDto rule = db.rules().insert(r -> r.setNoteData(null).setNoteUserUuid(null));
QProfileDto qProfile = db.qualityProfiles().insert();
db.qualityProfiles().activateRule(qProfile, rule);
@Test
public void system_tag() {
- RuleDto r = db.rules().insert(setSystemTags("tag"));
+ RuleDto r = db.rules().insert(setSystemTags("tag"), setTags());
ruleIndexer.commitAndIndex(db.getSession(), r.getUuid());
String result = ws.newRequest().execute().getInput();
@Test
public void tag() {
- RuleDto r = db.rules().insert(setSystemTags());
+ RuleDto r = db.rules().insert(setSystemTags(), setTags("tag"));
ruleIndexer.commitAndIndex(db.getSession(), r.getUuid());
- db.rules().insertOrUpdateMetadata(r, setTags("tag"));
ruleIndexer.commitAndIndex(db.getSession(), r.getUuid());
String result = ws.newRequest().execute().getInput();
import org.sonar.db.DbTester;
import org.sonar.db.rule.RuleDescriptionSectionDto;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.es.EsClient;
import org.sonar.server.es.EsTester;
RuleDto templateRule = db.rules().insert(
r -> r.setRuleKey(RuleKey.of("java", "S001")),
r -> r.setIsTemplate(true),
+ r -> r.setNoteUserUuid(null),
r -> r.setCreatedAt(PAST),
r -> r.setUpdatedAt(PAST));
db.rules().insertRuleParam(templateRule, param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"));
r -> r.setStatus(RuleStatus.BETA),
r -> r.setTemplateUuid(templateRule.getUuid()),
r -> r.setLanguage("js"),
+ r -> r.setNoteUserUuid(null),
r -> r.setCreatedAt(PAST),
r -> r.setUpdatedAt(PAST));
db.rules().insertRuleParam(customRule, param -> param.setName("regex").setType("a").setDescription("Reg ex"));
public void update_tags() {
logInAsQProfileAdministrator();
- RuleDto rule = db.rules().insert(setSystemTags("stag1", "stag2"));
- db.rules().insertOrUpdateMetadata(rule, setTags("tag1", "tag2"), m -> m.setNoteData(null).setNoteUserUuid(null));
+ RuleDto rule = db.rules().insert(setSystemTags("stag1", "stag2"), setTags("tag1", "tag2"), r -> r.setNoteData(null).setNoteUserUuid(null));
Rules.UpdateResponse result = ws.newRequest().setMethod("POST")
.setParam(PARAM_KEY, rule.getKey().toString())
RuleDto rule = db.rules().insert(
r -> r.setDefRemediationFunction(LINEAR.toString()),
r -> r.setDefRemediationGapMultiplier("10d"),
- r -> r.setDefRemediationBaseEffort(null));
+ r -> r.setDefRemediationBaseEffort(null),
+ r -> r.setNoteUserUuid(null));
String newOffset = LINEAR_OFFSET.toString();
String newMultiplier = "15d";
assertThat(updatedRule.getRemFnBaseEffort()).isEqualTo(newEffort);
// check database
- RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey())
+ RuleDto updatedRuleDto = db.getDbClient().ruleDao().selectByKey(db.getSession(), rule.getKey())
.orElseThrow(() -> new IllegalStateException("Cannot load metadata"));
- assertThat(metadataOfSpecificOrg.getRemediationFunction()).isEqualTo(newOffset);
- assertThat(metadataOfSpecificOrg.getRemediationGapMultiplier()).isEqualTo(newMultiplier);
- assertThat(metadataOfSpecificOrg.getRemediationBaseEffort()).isEqualTo(newEffort);
+ assertThat(updatedRuleDto.getRemediationFunction()).isEqualTo(newOffset);
+ assertThat(updatedRuleDto.getRemediationGapMultiplier()).isEqualTo(newMultiplier);
+ assertThat(updatedRuleDto.getRemediationBaseEffort()).isEqualTo(newEffort);
}
@Test
public void update_note() {
- RuleDto rule = db.rules().insert();
UserDto userHavingUpdatingNote = db.users().insertUser();
- db.rules().insertOrUpdateMetadata(rule, userHavingUpdatingNote, m -> m.setNoteData("old data"));
+ RuleDto rule = db.rules().insert(m -> m.setNoteData("old data").setNoteUserUuid(userHavingUpdatingNote.getUuid()));
UserDto userAuthenticated = db.users().insertUser();
userSession.logIn(userAuthenticated).addPermission(ADMINISTER_QUALITY_PROFILES);
assertThat(updatedRule.getNoteLogin()).isEqualTo(userAuthenticated.getLogin());
// check database
- RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey()).get();
- assertThat(metadataOfSpecificOrg.getNoteData()).isEqualTo("new data");
- assertThat(metadataOfSpecificOrg.getNoteUserUuid()).isEqualTo(userAuthenticated.getUuid());
+ RuleDto updatedRuleDto = db.getDbClient().ruleDao().selectByKey(db.getSession(), rule.getKey()).get();
+ assertThat(updatedRuleDto.getNoteData()).isEqualTo("new data");
+ assertThat(updatedRuleDto.getNoteUserUuid()).isEqualTo(userAuthenticated.getUuid());
}
@Test