diff options
author | Jacek <52388493+jacek-poreda-sonarsource@users.noreply.github.com> | 2019-09-17 09:19:44 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-09-18 09:51:47 +0200 |
commit | 2be53fd8bc0bff878bd4c7daebbca61720aee9c2 (patch) | |
tree | e5b898474c48ae80a08cf821e14ed12784120852 /server/sonar-db-dao | |
parent | 81f6a5ff6a1b372fa401d31e98de2e50600a7cbe (diff) | |
download | sonarqube-2be53fd8bc0bff878bd4c7daebbca61720aee9c2.tar.gz sonarqube-2be53fd8bc0bff878bd4c7daebbca61720aee9c2.zip |
SONAR-5366 - restore custom rules (#2060)
- restore custom rules
- reduce SQL query executions
- code readability improvements
Diffstat (limited to 'server/sonar-db-dao')
10 files changed, 503 insertions, 2 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index 3f833346883..93a5d217467 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -71,6 +71,7 @@ import org.sonar.db.qualityprofile.QProfileChangeDao; import org.sonar.db.qualityprofile.QProfileEditGroupsDao; import org.sonar.db.qualityprofile.QProfileEditUsersDao; import org.sonar.db.qualityprofile.QualityProfileDao; +import org.sonar.db.qualityprofile.QualityProfileExportDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; import org.sonar.db.schemamigration.SchemaMigrationDao; @@ -139,6 +140,7 @@ public class DaoModule extends Module { QualityGateConditionDao.class, QualityGateDao.class, QualityProfileDao.class, + QualityProfileExportDao.class, RoleDao.class, RuleDao.class, RuleRepositoryDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index eb220ed54c0..27f1832a2bb 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -69,6 +69,7 @@ import org.sonar.db.qualityprofile.QProfileChangeDao; import org.sonar.db.qualityprofile.QProfileEditGroupsDao; import org.sonar.db.qualityprofile.QProfileEditUsersDao; import org.sonar.db.qualityprofile.QualityProfileDao; +import org.sonar.db.qualityprofile.QualityProfileExportDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; import org.sonar.db.schemamigration.SchemaMigrationDao; @@ -94,6 +95,7 @@ public class DbClient { private final OrganizationDao organizationDao; private final OrganizationMemberDao organizationMemberDao; private final QualityProfileDao qualityProfileDao; + private final QualityProfileExportDao qualityProfileExportDao; private final PropertiesDao propertiesDao; private final AlmAppInstallDao almAppInstallDao; private final ProjectAlmBindingDao projectAlmBindingDao; @@ -167,6 +169,7 @@ public class DbClient { organizationDao = getDao(map, OrganizationDao.class); organizationMemberDao = getDao(map, OrganizationMemberDao.class); qualityProfileDao = getDao(map, QualityProfileDao.class); + qualityProfileExportDao = getDao(map, QualityProfileExportDao.class); propertiesDao = getDao(map, PropertiesDao.class); internalPropertiesDao = getDao(map, InternalPropertiesDao.class); snapshotDao = getDao(map, SnapshotDao.class); @@ -267,6 +270,10 @@ public class DbClient { return qualityProfileDao; } + public QualityProfileExportDao qualityProfileExportDao() { + return qualityProfileExportDao; + } + public PropertiesDao propertiesDao() { return propertiesDao; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index f449a0eeabf..391ee1c0933 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -48,10 +48,10 @@ import org.sonar.db.ce.CeTaskMessageMapper; import org.sonar.db.component.AnalysisPropertiesMapper; import org.sonar.db.component.BranchMapper; import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentWithModuleUuidDto; import org.sonar.db.component.ComponentDtoWithSnapshotId; import org.sonar.db.component.ComponentKeyUpdaterMapper; import org.sonar.db.component.ComponentMapper; +import org.sonar.db.component.ComponentWithModuleUuidDto; import org.sonar.db.component.FilePathWithHashDto; import org.sonar.db.component.KeyWithUuidDto; import org.sonar.db.component.ProjectLinkMapper; @@ -121,6 +121,7 @@ import org.sonar.db.qualityprofile.DefaultQProfileMapper; import org.sonar.db.qualityprofile.QProfileChangeMapper; import org.sonar.db.qualityprofile.QProfileEditGroupsMapper; import org.sonar.db.qualityprofile.QProfileEditUsersMapper; +import org.sonar.db.qualityprofile.QualityProfileExportMapper; import org.sonar.db.qualityprofile.QualityProfileMapper; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleMapper; @@ -270,6 +271,7 @@ public class MyBatis implements Startable { QualityGateConditionMapper.class, QualityGateMapper.class, QualityProfileMapper.class, + QualityProfileExportMapper.class, RoleMapper.class, RuleMapper.class, RuleRepositoryMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java new file mode 100644 index 00000000000..9e0c123cef5 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java @@ -0,0 +1,92 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.qualityprofile; + +import java.util.LinkedList; +import java.util.List; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rules.RuleType; +import org.sonar.db.rule.SeverityUtil; + +public class ExportRuleDto { + private Integer activeRuleId; + private String repository; + private String rule; + private String name; + private String description; + private String extendedDescription; + private String template; + private Integer severity; + private Integer type; + private String tags; + + private List<ExportRuleParamDto> params; + + public boolean isCustomRule() { + return template != null; + } + + public Integer getActiveRuleId() { + return activeRuleId; + } + + public RuleKey getRuleKey() { + return RuleKey.of(repository, rule); + } + + public RuleKey getTemplateRuleKey() { + return RuleKey.of(repository, template); + } + + public String getSeverityString() { + return SeverityUtil.getSeverityFromOrdinal(severity); + } + + public String getExtendedDescription() { + return extendedDescription; + } + + public RuleType getRuleType() { + return RuleType.valueOf(type); + } + + public String getTags() { + return tags; + } + + public String getDescription() { + return description; + } + + public String getName() { + return name; + } + + public List<ExportRuleParamDto> getParams() { + if (params == null) { + params = new LinkedList<>(); + } + return params; + } + + void setParams(List<ExportRuleParamDto> params) { + this.params = params; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java new file mode 100644 index 00000000000..1222480dd5a --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.qualityprofile; + +public class ExportRuleParamDto { + private Integer activeRuleId; + private String kee; + private String value; + + public Integer getActiveRuleId() { + return activeRuleId; + } + + public String getKey() { + return kee; + } + + public String getValue() { + return value; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportDao.java new file mode 100644 index 00000000000..5f2ff75ff8f --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportDao.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.qualityprofile; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +import static org.sonar.db.DatabaseUtils.executeLargeInputs; + +public class QualityProfileExportDao implements Dao { + + public List<ExportRuleDto> selectRulesByProfile(DbSession dbSession, QProfileDto profile) { + List<ExportRuleDto> exportRules = mapper(dbSession).selectByProfileUuid(profile.getKee()); + + Set<Integer> activeRuleIds = exportRules.stream().map(ExportRuleDto::getActiveRuleId).collect(Collectors.toSet()); + + Map<Integer, List<ExportRuleParamDto>> activeRulesParam = selectParamsByActiveRuleIds(dbSession, activeRuleIds); + + exportRules.forEach(exportRuleDto -> + exportRuleDto.setParams(activeRulesParam.get(exportRuleDto.getActiveRuleId())) + ); + return exportRules; + } + + private Map<Integer, List<ExportRuleParamDto>> selectParamsByActiveRuleIds(DbSession dbSession, Collection<Integer> activeRuleIds) { + return executeLargeInputs(activeRuleIds, ids -> mapper(dbSession).selectParamsByActiveRuleIds(ids)) + .stream() + .collect(Collectors.groupingBy(ExportRuleParamDto::getActiveRuleId)); + } + + private static QualityProfileExportMapper mapper(DbSession dbSession) { + return dbSession.getMapper(QualityProfileExportMapper.class); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportMapper.java new file mode 100644 index 00000000000..3a2bb304b07 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportMapper.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.qualityprofile; + +import java.util.Collection; +import java.util.List; +import org.apache.ibatis.annotations.Param; + +public interface QualityProfileExportMapper { + List<ExportRuleDto> selectByProfileUuid(String uuid); + + List<ExportRuleParamDto> selectParamsByActiveRuleIds(@Param("activeRuleIds") Collection<Integer> activeRuleIds); +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileExportMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileExportMapper.xml new file mode 100644 index 00000000000..ec8fe7a81e3 --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileExportMapper.xml @@ -0,0 +1,47 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd"> + +<mapper namespace="org.sonar.db.qualityprofile.QualityProfileExportMapper"> + + <sql id="exportRuleColumns"> + a.id as "activeRuleId", + a.failure_level as "severity", + r.plugin_rule_key as "rule", + r.plugin_name as "repository", + r.priority as "defaultSeverity", + r.name, + r.description, + r.description_format as "descriptionFormat", + r.rule_type as "type", + rt.plugin_rule_key as "template", + rm.note_data as "extendedDescription", + rm.tags + </sql> + + <sql id="exportRuleParamColumns"> + p.active_rule_id as activeRuleId, + p.rules_parameter_key as kee, + p.value as value + </sql> + + <select id="selectByProfileUuid" parameterType="string" resultType="org.sonar.db.qualityprofile.ExportRuleDto"> + select + <include refid="exportRuleColumns"/> + from active_rules a + inner join rules_profiles rp on rp.id = a.profile_id + inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.kee + inner join rules r on r.id = a.rule_id and r.status != 'REMOVED' + left join rules rt on rt.id = r.template_id + left join rules_metadata rm on rm.rule_id = r.id + where oqp.uuid = #{id, jdbcType=VARCHAR} + </select> + + <select id="selectParamsByActiveRuleIds" parameterType="map" resultType="org.sonar.db.qualityprofile.ExportRuleParamDto"> + select + <include refid="exportRuleParamColumns"/> + from active_rule_parameters p + <where> + p.active_rule_id in <foreach collection="activeRuleIds" open="(" close=")" item="activeRuleId" separator=",">#{activeRuleId,jdbcType=INTEGER}</foreach> + </where> + </select> +</mapper> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java index 184f56ad80d..cb8424da879 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java @@ -30,6 +30,6 @@ public class DaoModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new DaoModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 61); + assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 62); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java new file mode 100644 index 00000000000..7b33fb41526 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java @@ -0,0 +1,227 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.qualityprofile; + +import com.google.common.collect.Sets; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import javax.annotation.Nullable; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; +import org.sonar.api.rule.RuleStatus; +import org.sonar.api.rules.RuleType; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.rule.RuleDefinitionDto; +import org.sonar.db.rule.RuleMetadataDto; +import org.sonar.db.rule.RuleParamDto; + +import static org.assertj.core.api.Assertions.assertThat; + +public class QualityProfileExportDaoTest { + + @Rule + public DbTester db = DbTester.create(AlwaysIncreasingSystem2.INSTANCE); + + private DbSession dbSession = db.getSession(); + private QualityProfileExportDao underTest = db.getDbClient().qualityProfileExportDao(); + + @Test + public void selectRulesByProfile_ready_rules_only() { + String language = "java"; + RuleDefinitionDto rule1 = createRule(language); + RuleDefinitionDto rule2 = createRule(language); + RuleDefinitionDto rule3 = createRule(language); + createRule(language, RuleStatus.REMOVED); + + QProfileDto profile = createProfile(language); + + activate(profile, rule1, rule2, rule3); + + List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile); + assertThat(results).isNotNull(); + assertThat(results).asList() + .extracting("ruleKey") + .containsOnly(rule1.getKey(), rule2.getKey(), rule3.getKey()); + } + + @Test + public void selectRulesByProfile_verify_columns() { + String language = "java"; + RuleDefinitionDto ruleTemplate = createRule(language); + RuleDefinitionDto rule = createRule(language, RuleStatus.READY, ruleTemplate.getId()); + RuleMetadataDto ruleMetadata = createRuleMetadata(new RuleMetadataDto() + .setRuleId(rule.getId()) + .setOrganizationUuid(db.getDefaultOrganization().getUuid()) + .setNoteData("Extended description") + .setTags(Sets.newHashSet("tag1", "tag2", "tag3"))); + + QProfileDto profile = createProfile(language); + + List<ActiveRuleDto> activeRules = activate(profile, rule); + + List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile); + assertThat(results).isNotNull(); + assertThat(results).asList().isNotEmpty(); + + ExportRuleDto exportRuleDto = results.get(0); + assertThat(exportRuleDto).isNotNull(); + assertThat(exportRuleDto.getParams()).asList().isEmpty(); + assertThat(exportRuleDto.getDescription()).isEqualTo(rule.getDescription()); + assertThat(exportRuleDto.getExtendedDescription()).isEqualTo(ruleMetadata.getNoteData()); + assertThat(exportRuleDto.getName()).isEqualTo(rule.getName()); + assertThat(exportRuleDto.getRuleKey()).isEqualTo(rule.getKey()); + assertThat(exportRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(rule.getType())); + assertThat(exportRuleDto.getSeverityString()).isEqualTo(activeRules.get(0).getSeverityString()); + assertThat(exportRuleDto.getTags()).isEqualTo(String.join(",", ruleMetadata.getTags())); + assertThat(exportRuleDto.getTemplateRuleKey()).isEqualTo(ruleTemplate.getKey()); + } + + @Test + public void selectRulesByProfile_verify_rows_over_1000() { + String language = "java"; + int numberOfParamsToCreate = 1005; + RuleDefinitionDto rule = createRule(language); + List<RuleParamDto> ruleParams = addParamsToRule(rule, numberOfParamsToCreate); + + QProfileDto profile = createProfile(language); + ActiveRuleDto activatedRule = activate(profile, rule, ruleParams); + + List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile); + + assertThat(results) + .extracting("activeRuleId") + .containsOnly(activatedRule.getId()); + + assertThat(results.get(0).getParams()) + .extracting("key") + .containsOnly(ruleParams.stream().map(RuleParamDto::getName).toArray()); + } + + @Test + public void selectRulesByProfile_params_assigned_correctly() { + String language = "java"; + RuleDefinitionDto firstRule = createRule(language); + List<RuleParamDto> ruleParamsOfFirstRule = addParamsToRule(firstRule, 2); + + RuleDefinitionDto secondRule = createRule(language); + List<RuleParamDto> ruleParamsOfSecondRule = addParamsToRule(secondRule, 3); + + String otherLanguage = "js"; + RuleDefinitionDto thirdRule = createRule(otherLanguage); + List<RuleParamDto> ruleParamsOfThirdRule = addParamsToRule(thirdRule, 4); + + QProfileDto profile = createProfile(language); + QProfileDto otherProfile = createProfile(otherLanguage); + + ActiveRuleDto firstActivatedRule = activate(profile, firstRule, ruleParamsOfFirstRule); + ActiveRuleDto secondActivatedRule = activate(profile, secondRule, ruleParamsOfSecondRule); + + ActiveRuleDto thirdActivatedRule = activate(otherProfile, thirdRule, ruleParamsOfThirdRule); + + List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile); + List<ExportRuleDto> otherProfileResults = underTest.selectRulesByProfile(dbSession, otherProfile); + + assertThat(results) + .extracting("activeRuleId") + .containsOnly(firstActivatedRule.getId(), secondActivatedRule.getId()); + + assertThat(otherProfileResults).asList() + .extracting("activeRuleId") + .containsOnly(thirdActivatedRule.getId()); + + ExportRuleDto firstExportedRule = findExportedRuleById(firstActivatedRule.getId(), results); + assertThat(firstExportedRule.getParams()) + .extracting("key") + .containsOnly(ruleParamsOfFirstRule.stream().map(RuleParamDto::getName).toArray()); + + ExportRuleDto secondExportedRule = findExportedRuleById(secondActivatedRule.getId(), results); + assertThat(secondExportedRule.getParams()) + .extracting("key") + .containsOnly(ruleParamsOfSecondRule.stream().map(RuleParamDto::getName).toArray()); + + ExportRuleDto thirdExportedRule = findExportedRuleById(thirdActivatedRule.getId(), otherProfileResults); + assertThat(thirdExportedRule.getParams()) + .extracting("key") + .containsOnly(ruleParamsOfThirdRule.stream().map(RuleParamDto::getName).toArray()); + } + + + private ExportRuleDto findExportedRuleById(Integer id, List<ExportRuleDto> results) { + Optional<ExportRuleDto> found = results.stream().filter(exportRuleDto -> id.equals(exportRuleDto.getActiveRuleId())).findFirst(); + if (!found.isPresent()) { + Assert.fail(); + } + return found.get(); + } + + private List<RuleParamDto> addParamsToRule(RuleDefinitionDto firstRule, int numberOfParams) { + return IntStream.range(0, numberOfParams) + .mapToObj(value -> db.rules().insertRuleParam(firstRule, + ruleParamDto -> ruleParamDto.setName("name_" + firstRule.getId() + "_" + value))) + .collect(Collectors.toList()); + } + + private RuleDefinitionDto createRule(String language) { + return createRule(language, RuleStatus.READY); + } + + private RuleDefinitionDto createRule(String language, RuleStatus status) { + return createRule(language, status, null); + } + + private RuleDefinitionDto createRule(String language, RuleStatus status, @Nullable Integer templateId) { + return db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto.setRepositoryKey("repoKey").setLanguage(language).setStatus(status).setTemplateId(templateId)); + } + + private RuleMetadataDto createRuleMetadata(RuleMetadataDto metadataDto) { + return db.rules().insertOrUpdateMetadata(metadataDto); + } + + private QProfileDto createProfile(String lanugage) { + return db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(lanugage)); + } + + private List<ActiveRuleDto> activate(QProfileDto profile, RuleDefinitionDto... rules) { + return Stream.of(rules) + .map(ruleDefinitionDto -> db.qualityProfiles().activateRule(profile, ruleDefinitionDto)) + .collect(Collectors.toList()); + } + + private ActiveRuleDto activate(QProfileDto profile, RuleDefinitionDto rule, Collection<RuleParamDto> params) { + ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile, rule); + + params.forEach(ruleParamDto -> { + ActiveRuleParamDto dto = ActiveRuleParamDto.createFor(ruleParamDto) + .setKey(ruleParamDto.getName()) + .setValue("20") + .setActiveRuleId(activeRule.getId()); + db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, dto); + }); + + return activeRule; + } +} |