diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-05-31 10:59:33 +0200 |
---|---|---|
committer | Eric Hartmann <hartmann.eric@gmail.com> | 2017-06-14 15:43:12 +0200 |
commit | 3e26c9b9b44ebabbc2c81e134c26799ce3f4bc3b (patch) | |
tree | 548b40695d3fb85cdcd42c889bb1ae470cbbbb14 /server/sonar-db-dao | |
parent | d4133c63eb8e7b6acc54195da3206f1b3a5f7689 (diff) | |
download | sonarqube-3e26c9b9b44ebabbc2c81e134c26799ce3f4bc3b.tar.gz sonarqube-3e26c9b9b44ebabbc2c81e134c26799ce3f4bc3b.zip |
SONAR-9304 rename table qprofiles to org_qprofiles
Diffstat (limited to 'server/sonar-db-dao')
27 files changed, 838 insertions, 877 deletions
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 d55857343c3..e99aec847ea 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 @@ -97,7 +97,6 @@ import org.sonar.db.qualityprofile.ActiveRuleMapper; import org.sonar.db.qualityprofile.ActiveRuleParamDto; import org.sonar.db.qualityprofile.DefaultQProfileMapper; import org.sonar.db.qualityprofile.QProfileChangeMapper; -import org.sonar.db.qualityprofile.RulesProfileDto; import org.sonar.db.qualityprofile.QualityProfileMapper; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleMapper; @@ -166,7 +165,6 @@ public class MyBatis implements Startable { confBuilder.loadAlias("PurgeableAnalysis", PurgeableAnalysisDto.class); confBuilder.loadAlias("QualityGateCondition", QualityGateConditionDto.class); confBuilder.loadAlias("QualityGate", QualityGateDto.class); - confBuilder.loadAlias("RulesProfile", RulesProfileDto.class); confBuilder.loadAlias("RequirementMigration", RequirementMigrationDto.class); confBuilder.loadAlias("Resource", ResourceDto.class); confBuilder.loadAlias("RuleParam", RuleParamDto.class); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java index ff3719a8810..415c17564ec 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java @@ -142,7 +142,7 @@ public class ActiveRuleDto { return this; } - public static ActiveRuleDto createFor(RulesProfileDto profileDto, RuleDefinitionDto ruleDto) { + public static ActiveRuleDto createFor(QProfileDto profileDto, RuleDefinitionDto ruleDto) { requireNonNull(profileDto.getId(), "Profile is not persisted"); requireNonNull(ruleDto.getId(), "Rule is not persisted"); ActiveRuleDto dto = new ActiveRuleDto(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileDto.java index e97e7803665..4474af22cf9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileDto.java @@ -52,7 +52,7 @@ public class DefaultQProfileDto { return this; } - public static DefaultQProfileDto from(RulesProfileDto profile) { + public static DefaultQProfileDto from(QProfileDto profile) { return new DefaultQProfileDto() .setOrganizationUuid(profile.getOrganizationUuid()) .setLanguage(profile.getLanguage()) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeDao.java index b98e7b24d29..783f1580563 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeDao.java @@ -40,9 +40,7 @@ public class QProfileChangeDao implements Dao { } public void insert(DbSession dbSession, QProfileChangeDto dto) { - checkState(dto.getKey() == null, "Key of QProfileChangeDto must be set by DAO only. Got %s.", dto.getKey()); checkState(dto.getCreatedAt() == 0L, "Date of QProfileChangeDto must be set by DAO only. Got %s.", dto.getCreatedAt()); - dto.setKey(uuidFactory.create()); dto.setCreatedAt(system2.now()); mapper(dbSession).insert(dto); @@ -52,13 +50,13 @@ public class QProfileChangeDao implements Dao { return mapper(dbSession).selectByQuery(query); } - public int countForProfileKey(DbSession dbSession, String profileKey) { - return mapper(dbSession).countForProfileKey(profileKey); + public int countForProfileUuid(DbSession dbSession, String profileUuid) { + return mapper(dbSession).countForProfileUuid(profileUuid); } - public void deleteByProfileKeys(DbSession dbSession, Collection<String> profileKeys) { + public void deleteByProfileKeys(DbSession dbSession, Collection<String> profileUuids) { QProfileChangeMapper mapper = mapper(dbSession); - DatabaseUtils.executeLargeUpdates(profileKeys, mapper::deleteByProfileKeys); + DatabaseUtils.executeLargeUpdates(profileUuids, mapper::deleteByProfileUuids); } private static QProfileChangeMapper mapper(DbSession dbSession) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeMapper.java index 24d46605d9e..6d14638fcd2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeMapper.java @@ -29,7 +29,7 @@ public interface QProfileChangeMapper { List<QProfileChangeDto> selectByQuery(@Param("query") QProfileChangeQuery query); - int countForProfileKey(@Param("profileKey") String profileKey); + int countForProfileUuid(@Param("profileUuid") String profileUuid); - void deleteByProfileKeys(@Param("profileKeys") Collection<String> profileKeys); + void deleteByProfileUuids(@Param("profileUuids") Collection<String> profileUuids); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeQuery.java index 8bbcbd88584..8de9d798c0f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeQuery.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeQuery.java @@ -28,18 +28,18 @@ import static java.util.Objects.requireNonNull; public class QProfileChangeQuery { - private final String profileKey; + private final String profileUuid; private Long fromIncluded; private Long toExcluded; private int offset = 0; private int limit = 100; - public QProfileChangeQuery(String profileKey) { - this.profileKey = requireNonNull(profileKey); + public QProfileChangeQuery(String profileUuid) { + this.profileUuid = requireNonNull(profileUuid); } - public String getProfileKey() { - return profileKey; + public String getProfileUuid() { + return profileUuid; } @CheckForNull diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/RulesProfileDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileDto.java index 0afffd0f7aa..92f47a6d3c0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/RulesProfileDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileDto.java @@ -26,9 +26,9 @@ import org.sonar.core.util.UtcDateUtils; import org.sonar.db.organization.OrganizationDto; /** - * Represents the table "rules_profiles" + * Represents the join of "org_qprofiles" and "rules_profiles" */ -public class RulesProfileDto { +public class QProfileDto { private Integer id; /** @@ -50,12 +50,12 @@ public class RulesProfileDto { return organizationUuid; } - public RulesProfileDto setOrganizationUuid(String organizationUuid) { + public QProfileDto setOrganizationUuid(String organizationUuid) { this.organizationUuid = organizationUuid; return this; } - public RulesProfileDto setKey(String s) { + public QProfileDto setKey(String s) { return setKee(s); } @@ -63,7 +63,7 @@ public class RulesProfileDto { return kee; } - public RulesProfileDto setKee(String s) { + public QProfileDto setKee(String s) { this.kee = s; return this; } @@ -72,7 +72,7 @@ public class RulesProfileDto { return id; } - public RulesProfileDto setId(Integer id) { + public QProfileDto setId(Integer id) { this.id = id; return this; } @@ -81,7 +81,7 @@ public class RulesProfileDto { return name; } - public RulesProfileDto setName(String name) { + public QProfileDto setName(String name) { this.name = name; return this; } @@ -90,7 +90,7 @@ public class RulesProfileDto { return language; } - public RulesProfileDto setLanguage(String language) { + public QProfileDto setLanguage(String language) { this.language = language; return this; } @@ -100,7 +100,7 @@ public class RulesProfileDto { return parentKee; } - public RulesProfileDto setParentKee(@Nullable String s) { + public QProfileDto setParentKee(@Nullable String s) { this.parentKee = s; return this; } @@ -109,12 +109,12 @@ public class RulesProfileDto { return rulesUpdatedAt; } - public RulesProfileDto setRulesUpdatedAt(String s) { + public QProfileDto setRulesUpdatedAt(String s) { this.rulesUpdatedAt = s; return this; } - public RulesProfileDto setRulesUpdatedAtAsDate(Date d) { + public QProfileDto setRulesUpdatedAtAsDate(Date d) { this.rulesUpdatedAt = UtcDateUtils.formatDateTime(d); return this; } @@ -124,7 +124,7 @@ public class RulesProfileDto { return lastUsed; } - public RulesProfileDto setLastUsed(@Nullable Long lastUsed) { + public QProfileDto setLastUsed(@Nullable Long lastUsed) { this.lastUsed = lastUsed; return this; } @@ -134,7 +134,7 @@ public class RulesProfileDto { return userUpdatedAt; } - public RulesProfileDto setUserUpdatedAt(@Nullable Long userUpdatedAt) { + public QProfileDto setUserUpdatedAt(@Nullable Long userUpdatedAt) { this.userUpdatedAt = userUpdatedAt; return this; } @@ -143,12 +143,12 @@ public class RulesProfileDto { return isBuiltIn; } - public RulesProfileDto setIsBuiltIn(boolean b) { + public QProfileDto setIsBuiltIn(boolean b) { this.isBuiltIn = b; return this; } - public static RulesProfileDto createFor(String key) { - return new RulesProfileDto().setKee(key); + public static QProfileDto createFor(String key) { + return new QProfileDto().setKee(key); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java index 7dd416bd086..e23cc4d980a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java @@ -20,7 +20,7 @@ package org.sonar.db.qualityprofile; import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; +import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; @@ -49,84 +49,87 @@ public class QualityProfileDao implements Dao { } @CheckForNull - public RulesProfileDto selectByKey(DbSession session, String key) { - return mapper(session).selectByKey(key); + public QProfileDto selectByUuid(DbSession session, String uuid) { + return mapper(session).selectByUuid(uuid); } - public RulesProfileDto selectOrFailByKey(DbSession session, String key) { - RulesProfileDto dto = selectByKey(session, key); + public QProfileDto selectOrFailByUuid(DbSession session, String uuid) { + QProfileDto dto = selectByUuid(session, uuid); if (dto == null) { - throw new RowNotFoundException("Quality profile not found: " + key); + throw new RowNotFoundException("Quality profile not found: " + uuid); } return dto; } - public List<RulesProfileDto> selectByKeys(DbSession session, List<String> keys) { - return executeLargeInputs(keys, mapper(session)::selectByKeys); + public List<QProfileDto> selectByUuids(DbSession session, List<String> uuids) { + return executeLargeInputs(uuids, mapper(session)::selectByUuids); } - public List<RulesProfileDto> selectAll(DbSession session, OrganizationDto organization) { + public List<QProfileDto> selectAll(DbSession session, OrganizationDto organization) { return mapper(session).selectAll(organization.getUuid()); } - public void insert(DbSession session, RulesProfileDto profile, RulesProfileDto... otherProfiles) { + public void insert(DbSession session, QProfileDto profile, QProfileDto... otherProfiles) { QualityProfileMapper mapper = mapper(session); doInsert(mapper, profile); - for (RulesProfileDto other : otherProfiles) { + for (QProfileDto other : otherProfiles) { doInsert(mapper, other); } } - private void doInsert(QualityProfileMapper mapper, RulesProfileDto profile) { + private void doInsert(QualityProfileMapper mapper, QProfileDto profile) { Preconditions.checkArgument(profile.getId() == null, "Quality profile is already persisted (got id %d)", profile.getId()); - mapper.insert(profile, new Date(system.now())); + long now = system.now(); + mapper.insertRulesProfile(profile, new Date(now)); + mapper.insertOrgQProfile(profile, now); } - public void update(DbSession session, RulesProfileDto profile, RulesProfileDto... otherProfiles) { + public void update(DbSession session, QProfileDto profile, QProfileDto... otherProfiles) { QualityProfileMapper mapper = mapper(session); - doUpdate(mapper, profile); - for (RulesProfileDto otherProfile : otherProfiles) { - doUpdate(mapper, otherProfile); + long now = system.now(); + doUpdate(mapper, profile, now); + for (QProfileDto otherProfile : otherProfiles) { + doUpdate(mapper, otherProfile, now); } } - private void doUpdate(QualityProfileMapper mapper, RulesProfileDto profile) { - Preconditions.checkArgument(profile.getId() != null, "Quality profile is not persisted"); - mapper.update(profile, new Date(system.now())); + private void doUpdate(QualityProfileMapper mapper, QProfileDto profile, long now) { + mapper.updateRulesProfile(profile, new Date(now)); + mapper.updateOrgQProfile(profile, now); } - public List<RulesProfileDto> selectDefaultProfiles(DbSession session, OrganizationDto organization, Collection<String> languageKeys) { - return mapper(session).selectDefaultProfiles(organization.getUuid(), languageKeys); + public List<QProfileDto> selectDefaultProfiles(DbSession session, OrganizationDto organization, Collection<String> languages) { + return mapper(session).selectDefaultProfiles(organization.getUuid(), languages); } @CheckForNull - public RulesProfileDto selectDefaultProfile(DbSession session, OrganizationDto organization, String language) { + public QProfileDto selectDefaultProfile(DbSession session, OrganizationDto organization, String language) { return mapper(session).selectDefaultProfile(organization.getUuid(), language); } @CheckForNull - public RulesProfileDto selectAssociatedToProjectAndLanguage(DbSession session, ComponentDto project, String language) { + public QProfileDto selectAssociatedToProjectAndLanguage(DbSession session, ComponentDto project, String language) { return mapper(session).selectAssociatedToProjectUuidAndLanguage(project.getOrganizationUuid(), project.projectUuid(), language); } - public List<RulesProfileDto> selectAssociatedToProjectUuidAndLanguages(DbSession session, ComponentDto project, Collection<String> languages) { + public List<QProfileDto> selectAssociatedToProjectUuidAndLanguages(DbSession session, ComponentDto project, Collection<String> languages) { return mapper(session).selectAssociatedToProjectUuidAndLanguages(project.getOrganizationUuid(), project.uuid(), languages); } - public List<RulesProfileDto> selectByLanguage(DbSession dbSession, OrganizationDto organization, String language) { + public List<QProfileDto> selectByLanguage(DbSession dbSession, OrganizationDto organization, String language) { return mapper(dbSession).selectByLanguage(organization.getUuid(), language); } - public List<RulesProfileDto> selectChildren(DbSession session, String key) { - return mapper(session).selectChildren(key); + public List<QProfileDto> selectChildren(DbSession session, String uuid) { + return mapper(session).selectChildren(uuid); } /** * All descendants, in the top-down order. */ - public List<RulesProfileDto> selectDescendants(DbSession session, String key) { - List<RulesProfileDto> descendants = Lists.newArrayList(); - for (RulesProfileDto child : selectChildren(session, key)) { + public List<QProfileDto> selectDescendants(DbSession session, String uuid) { + List<QProfileDto> descendants = new ArrayList<>(); + for (QProfileDto child : selectChildren(session, uuid)) { descendants.add(child); descendants.addAll(selectDescendants(session, child.getKee())); } @@ -134,63 +137,69 @@ public class QualityProfileDao implements Dao { } @CheckForNull - public RulesProfileDto selectByNameAndLanguage(OrganizationDto organization, String name, String language, DbSession session) { + public QProfileDto selectByNameAndLanguage(DbSession session, OrganizationDto organization, String name, String language) { return mapper(session).selectByNameAndLanguage(organization.getUuid(), name, language); } - public List<RulesProfileDto> selectByNameAndLanguages(OrganizationDto organization, String name, Collection<String> languageKeys, DbSession session) { - return mapper(session).selectByNameAndLanguages(organization.getUuid(), name, languageKeys); + public List<QProfileDto> selectByNameAndLanguages(DbSession session, OrganizationDto organization, String name, Collection<String> languages) { + return mapper(session).selectByNameAndLanguages(organization.getUuid(), name, languages); } - public Map<String, Long> countProjectsByProfileKey(DbSession dbSession, OrganizationDto organization) { - return KeyLongValue.toMap(mapper(dbSession).countProjectsByProfileKey(organization.getUuid())); + public Map<String, Long> countProjectsByProfileUuid(DbSession dbSession, OrganizationDto organization) { + return KeyLongValue.toMap(mapper(dbSession).countProjectsByProfileUuid(organization.getUuid())); } - public void insertProjectProfileAssociation(DbSession dbSession, ComponentDto project, RulesProfileDto profile) { + public void insertProjectProfileAssociation(DbSession dbSession, ComponentDto project, QProfileDto profile) { mapper(dbSession).insertProjectProfileAssociation(project.uuid(), profile.getKee()); } - public void deleteProjectProfileAssociation(DbSession dbSession, ComponentDto project, RulesProfileDto profile) { + public void deleteProjectProfileAssociation(DbSession dbSession, ComponentDto project, QProfileDto profile) { mapper(dbSession).deleteProjectProfileAssociation(project.uuid(), profile.getKee()); } - public void updateProjectProfileAssociation(DbSession dbSession, ComponentDto project, String newProfileKey, String oldProfileKey) { - mapper(dbSession).updateProjectProfileAssociation(project.uuid(), newProfileKey, oldProfileKey); + public void updateProjectProfileAssociation(DbSession dbSession, ComponentDto project, String newProfileUuid, String oldProfileUuid) { + mapper(dbSession).updateProjectProfileAssociation(project.uuid(), newProfileUuid, oldProfileUuid); } - public void deleteProjectAssociationsByProfileKeys(DbSession dbSession, Collection<String> profileKeys) { + public void deleteProjectAssociationsByProfileUuids(DbSession dbSession, Collection<String> profileUuids) { QualityProfileMapper mapper = mapper(dbSession); - DatabaseUtils.executeLargeUpdates(profileKeys, mapper::deleteProjectAssociationByProfileKeys); + DatabaseUtils.executeLargeUpdates(profileUuids, mapper::deleteProjectAssociationByProfileUuids); } - public List<ProjectQprofileAssociationDto> selectSelectedProjects(OrganizationDto organization, String profileKey, @Nullable String query, DbSession session) { + public List<ProjectQprofileAssociationDto> selectSelectedProjects(DbSession session, OrganizationDto organization, QProfileDto profile, @Nullable String query) { String nameQuery = sqlQueryString(query); - return mapper(session).selectSelectedProjects(organization.getUuid(), profileKey, nameQuery); + return mapper(session).selectSelectedProjects(organization.getUuid(), profile.getKee(), nameQuery); } - public List<ProjectQprofileAssociationDto> selectDeselectedProjects(OrganizationDto organization, String profileKey, @Nullable String query, DbSession session) { + public List<ProjectQprofileAssociationDto> selectDeselectedProjects(DbSession session, OrganizationDto organization, QProfileDto profile, @Nullable String query) { String nameQuery = sqlQueryString(query); - return mapper(session).selectDeselectedProjects(organization.getUuid(), profileKey, nameQuery); + return mapper(session).selectDeselectedProjects(organization.getUuid(), profile.getKee(), nameQuery); } - public List<ProjectQprofileAssociationDto> selectProjectAssociations(OrganizationDto organization, String profileKey, @Nullable String query, DbSession session) { + public List<ProjectQprofileAssociationDto> selectProjectAssociations(DbSession session, OrganizationDto organization, QProfileDto profile, @Nullable String query) { String nameQuery = sqlQueryString(query); - return mapper(session).selectProjectAssociations(organization.getUuid(), profileKey, nameQuery); + return mapper(session).selectProjectAssociations(organization.getUuid(), profile.getKee(), nameQuery); } - public Collection<String> selectOutdatedProfiles(DbSession dbSession, String language, String name) { - return mapper(dbSession).selectOutdatedProfiles(language, name); + public Collection<String> selectUuidsOfCustomRulesProfiles(DbSession dbSession, String language, String name) { + return mapper(dbSession).selectUuidsOfCustomQProfiles(language, name); } - public void renameAndCommit(DbSession dbSession, Collection<String> keys, String newName) { + public void renameRulesProfilesAndCommit(DbSession dbSession, Collection<String> rulesProfileUuids, String newName) { QualityProfileMapper mapper = mapper(dbSession); Date now = new Date(system.now()); - executeLargeUpdates(keys, partition -> { - mapper.rename(newName, now, partition); + executeLargeUpdates(rulesProfileUuids, partition -> { + mapper.renameRulesProfiles(newName, now, partition); dbSession.commit(); }); } + public void deleteByUuids(DbSession dbSession, Collection<String> profileUuids) { + QualityProfileMapper mapper = mapper(dbSession); + DatabaseUtils.executeLargeUpdates(profileUuids, mapper::deleteOrgQProfilesByUuids); + DatabaseUtils.executeLargeUpdates(profileUuids, mapper::deleteRulesProfilesByUuids); + } + private static String sqlQueryString(@Nullable String query) { if (query == null) { return "%"; @@ -201,9 +210,4 @@ public class QualityProfileDao implements Dao { private static QualityProfileMapper mapper(DbSession session) { return session.getMapper(QualityProfileMapper.class); } - - public void deleteByKeys(DbSession dbSession, Collection<String> profileKeys) { - QualityProfileMapper mapper = mapper(dbSession); - DatabaseUtils.executeLargeUpdates(profileKeys, mapper::deleteByKeys); - } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java index 9a514215385..cf289bc2ce9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java @@ -28,51 +28,62 @@ import org.sonar.db.KeyLongValue; public interface QualityProfileMapper { - void insert(@Param("dto") RulesProfileDto dto, @Param("now") Date now); + void insertOrgQProfile(@Param("dto") QProfileDto dto, @Param("now") long now); - void update(@Param("dto") RulesProfileDto dto, @Param("now") Date now); + void insertRulesProfile(@Param("dto") QProfileDto dto, @Param("now") Date now); - void deleteByKeys(@Param("profileKeys") Collection<String> profileKeys); + void updateRulesProfile(@Param("dto") QProfileDto dto, @Param("now") Date now); - List<RulesProfileDto> selectAll(@Param("organizationUuid") String organizationUuid); + void updateOrgQProfile(@Param("dto") QProfileDto dto, @Param("now") long now); + + void deleteRulesProfilesByUuids(@Param("uuids") Collection<String> uuids); + + void deleteOrgQProfilesByUuids(@Param("uuids") Collection<String> uuids); + + List<QProfileDto> selectAll(@Param("organizationUuid") String organizationUuid); @CheckForNull - RulesProfileDto selectDefaultProfile(@Param("organizationUuid") String organizationUuid, @Param("language") String language); + QProfileDto selectDefaultProfile(@Param("organizationUuid") String organizationUuid, @Param("language") String language); - List<RulesProfileDto> selectDefaultProfiles( + List<QProfileDto> selectDefaultProfiles( @Param("organizationUuid") String organizationUuid, @Param("languages") Collection<String> languages); @CheckForNull - RulesProfileDto selectByNameAndLanguage(@Param("organizationUuid") String organizationUuid, @Param("name") String name, @Param("language") String language); + QProfileDto selectByNameAndLanguage( + @Param("organizationUuid") String organizationUuid, + @Param("name") String name, + @Param("language") String language); - List<RulesProfileDto> selectByNameAndLanguages( + List<QProfileDto> selectByNameAndLanguages( @Param("organizationUuid") String organizationUuid, @Param("name") String name, @Param("languages") Collection<String> languages); @CheckForNull - RulesProfileDto selectByKey(String key); + QProfileDto selectByUuid(String uuid); - List<RulesProfileDto> selectByLanguage(@Param("organizationUuid") String organizationUuid, @Param("language") String language); + List<QProfileDto> selectByUuids(@Param("uuids") Collection<String> uuids); - List<RulesProfileDto> selectByKeys(@Param("keys") List<String> keys); + List<QProfileDto> selectByLanguage( + @Param("organizationUuid") String organizationUuid, + @Param("language") String language); // INHERITANCE - List<RulesProfileDto> selectChildren(String key); + List<QProfileDto> selectChildren(String uuid); // PROJECTS - List<KeyLongValue> countProjectsByProfileKey(@Param("organizationUuid") String organizationUuid); + List<KeyLongValue> countProjectsByProfileUuid(@Param("organizationUuid") String organizationUuid); @CheckForNull - RulesProfileDto selectAssociatedToProjectUuidAndLanguage( + QProfileDto selectAssociatedToProjectUuidAndLanguage( @Param("organizationUuid") String organizationUuid, @Param("projectUuid") String projectUuid, @Param("language") String language); - List<RulesProfileDto> selectAssociatedToProjectUuidAndLanguages( + List<QProfileDto> selectAssociatedToProjectUuidAndLanguages( @Param("organizationUuid") String organizationUuid, @Param("projectUuid") String projectUuid, @Param("languages") Collection<String> languages); @@ -86,26 +97,26 @@ public interface QualityProfileMapper { @Param("profileUuid") String profileUuid, @Param("oldProfileUuid") String oldProfileUuid); - void deleteProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey); + void deleteProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileUuid") String profileUuid); - void deleteProjectAssociationByProfileKeys(@Param("profileKeys") Collection<String> profileKeys); + void deleteProjectAssociationByProfileUuids(@Param("profileUuids") Collection<String> profileUuids); List<ProjectQprofileAssociationDto> selectSelectedProjects( @Param("organizationUuid") String organizationUuid, - @Param("profileKey") String profileKey, + @Param("profileUuid") String profileUuid, @Param("nameQuery") String nameQuery); List<ProjectQprofileAssociationDto> selectDeselectedProjects( @Param("organizationUuid") String organizationUuid, - @Param("profileKey") String profileKey, + @Param("profileUuid") String profileUuid, @Param("nameQuery") String nameQuery); List<ProjectQprofileAssociationDto> selectProjectAssociations( @Param("organizationUuid") String organizationUuid, - @Param("profileKey") String profileKey, + @Param("profileUuid") String profileUuid, @Param("nameQuery") String nameQuery); - List<String> selectOutdatedProfiles(@Param("language") String language, @Param("name") String name); + List<String> selectUuidsOfCustomQProfiles(@Param("language") String language, @Param("name") String name); - void rename(@Param("newName") String newName, @Param("updatedAt") Date updatedAt, @Param("profileKeys") Collection<String> profileKeys); + void renameRulesProfiles(@Param("newName") String newName, @Param("updatedAt") Date updatedAt, @Param("uuids") Collection<String> uuids); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml index feacf5db7a3..5d80727e51e 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml @@ -135,7 +135,8 @@ from organizations org where not exists ( select 1 from rules_profiles p - where p.organization_uuid = org.uuid + inner join org_qprofiles oqp on oqp.rules_profile_uuid = p.kee + where oqp.organization_uuid = org.uuid and p.language = #{profileLanguage, jdbcType=VARCHAR} and p.name = #{profileName, jdbcType=VARCHAR} ) diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml index 463c29de8ac..51d24aaf0f5 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml @@ -17,8 +17,9 @@ </sql> <sql id="activeRuleKeyJoin"> - INNER JOIN rules_profiles qp ON qp.id=a.profile_id - INNER JOIN rules r ON r.id = a.rule_id + inner join rules_profiles qp on qp.id=a.profile_id + inner join org_qprofiles oqp on oqp.rules_profile_uuid = qp.kee + inner join rules r on r.id = a.rule_id </sql> <insert id="insert" parameterType="ActiveRule" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> @@ -88,7 +89,7 @@ FROM active_rules a <include refid="activeRuleKeyJoin"/> WHERE a.rule_id=#{ruleId, jdbcType=BIGINT} - AND qp.organization_uuid=#{organizationUuid, jdbcType=VARCHAR} + AND oqp.organization_uuid=#{organizationUuid, jdbcType=VARCHAR} </select> <select id="selectByRuleIdOfAllOrganizations" parameterType="Integer" resultType="ActiveRule"> @@ -109,7 +110,7 @@ <foreach collection="ruleIds" item="ruleId" separator="," open="(" close=")"> #{ruleId} </foreach> - AND qp.organization_uuid=#{organizationUuid, jdbcType=VARCHAR} + AND oqp.organization_uuid=#{organizationUuid, jdbcType=VARCHAR} </select> <!-- Parameters --> @@ -191,34 +192,37 @@ </select> <select id="countActiveRulesByProfileKey" resultType="KeyLongValue" parameterType="map"> - select p.kee as "key", count(ar.id) as "value" + select rp.kee as "key", count(ar.id) as "value" from active_rules ar - inner join rules_profiles p on p.id = ar.profile_id + inner join rules_profiles rp on rp.id = ar.profile_id + inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.kee inner join rules r on r.id = ar.rule_id - where p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + where oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} and r.status != 'REMOVED' - group by p.kee + group by rp.kee </select> <select id="countActiveRulesForRuleStatusByProfileKey" resultType="KeyLongValue" parameterType="map"> - select p.kee as "key", count(ar.id) as "value" + select rp.kee as "key", count(ar.id) as "value" from active_rules ar - inner join rules_profiles p on p.id = ar.profile_id + inner join rules_profiles rp on rp.id = ar.profile_id + inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.kee inner join rules r on r.id = ar.rule_id - where p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + where oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} and r.status = #{ruleStatus, jdbcType=VARCHAR} - group by p.kee + group by rp.kee </select> <select id="countActiveRulesForInheritanceByProfileKey" resultType="KeyLongValue" parameterType="map"> - select p.kee as "key", count(ar.id) as "value" + select rp.kee as "key", count(ar.id) as "value" from active_rules ar - inner join rules_profiles p on p.id = ar.profile_id + inner join rules_profiles rp on rp.id = ar.profile_id + inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.kee inner join rules r on r.id = ar.rule_id - where p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + where oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} and ar.inheritance = #{inheritance, jdbcType=VARCHAR} and r.status != 'REMOVED' - group by p.kee + group by rp.kee </select> </mapper> diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileChangeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileChangeMapper.xml index 728614064df..ddd345a3f32 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileChangeMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileChangeMapper.xml @@ -22,9 +22,9 @@ ) </insert> - <select id="countForProfileKey" resultType="int"> + <select id="countForProfileUuid" resultType="int"> select count(kee) from qprofile_changes - where qprofile_key = #{profileKey} + where qprofile_key = #{profileUuid, jdbcType=VARCHAR} </select> <select id="selectByQuery" resultType="org.sonar.db.qualityprofile.QProfileChangeDto"> @@ -61,7 +61,7 @@ <sql id="sqlSelectByQuery"> select <include refid="sqlColumns" /> from qprofile_changes - where qprofile_key = #{query.profileKey} + where qprofile_key = #{query.profileUuid, jdbcType=VARCHAR} <if test="query.fromIncluded != null"> and created_at >= #{query.fromIncluded} </if> @@ -71,11 +71,11 @@ order by created_at desc </sql> - <delete id="deleteByProfileKeys" parameterType="String"> + <delete id="deleteByProfileUuids" parameterType="String"> delete from qprofile_changes where qprofile_key in - <foreach collection="profileKeys" open="(" close=")" item="profileKey" separator=","> - #{profileKey, jdbcType=VARCHAR} + <foreach collection="profileUuids" open="(" close=")" item="profileUuid" separator=","> + #{profileUuid, jdbcType=VARCHAR} </foreach> </delete> </mapper> diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml index 3eeb5f676ff..fdd6f3224b1 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml @@ -3,201 +3,226 @@ <mapper namespace="org.sonar.db.qualityprofile.QualityProfileMapper"> - <sql id="profilesColumns"> - p.id as id, - p.organization_uuid as organizationUuid, - p.kee as kee, - p.name as name, - p.language as language, - p.parent_kee as parentKee, - p.rules_updated_at as rulesUpdatedAt, - p.last_used as lastUsed, - p.user_updated_at as userUpdatedAt, - p.is_built_in as isBuiltIn + <sql id="qProfileColumns"> + oqp.uuid as kee, + oqp.organization_uuid as organizationUuid, + oqp.parent_uuid as parentKee, + rp.id as id, + rp.name as name, + rp.language as language, + rp.rules_updated_at as rulesUpdatedAt, + rp.last_used as lastUsed, + rp.user_updated_at as userUpdatedAt, + rp.is_built_in as isBuiltIn </sql> - <insert id="insert" parameterType="map" keyColumn="id" useGeneratedKeys="true" keyProperty="dto.id"> - INSERT INTO rules_profiles (organization_uuid, kee, parent_kee, name, language, created_at, updated_at, rules_updated_at, last_used, user_updated_at, is_built_in) - VALUES ( - #{dto.organizationUuid, jdbcType=VARCHAR}, - #{dto.kee, jdbcType=VARCHAR}, - #{dto.parentKee, jdbcType=VARCHAR}, - #{dto.name, jdbcType=VARCHAR}, - #{dto.language, jdbcType=VARCHAR}, - #{now, jdbcType=TIMESTAMP}, - #{now, jdbcType=TIMESTAMP}, - #{dto.rulesUpdatedAt, jdbcType=VARCHAR}, - #{dto.lastUsed, jdbcType=BIGINT}, - #{dto.userUpdatedAt, jdbcType=BIGINT}, - #{dto.isBuiltIn, jdbcType=BOOLEAN} + <insert id="insertRulesProfile" parameterType="map" keyColumn="id" useGeneratedKeys="true" keyProperty="dto.id"> + insert into rules_profiles ( + kee, + name, + language, + created_at, + updated_at, + rules_updated_at, + last_used, + user_updated_at, + is_built_in + ) values ( + #{dto.kee, jdbcType=VARCHAR}, + #{dto.name, jdbcType=VARCHAR}, + #{dto.language, jdbcType=VARCHAR}, + #{now, jdbcType=TIMESTAMP}, + #{now, jdbcType=TIMESTAMP}, + #{dto.rulesUpdatedAt, jdbcType=VARCHAR}, + #{dto.lastUsed, jdbcType=BIGINT}, + #{dto.userUpdatedAt, jdbcType=BIGINT}, + #{dto.isBuiltIn, jdbcType=BOOLEAN} + ) + </insert> + + <insert id="insertOrgQProfile" parameterType="map" useGeneratedKeys="false"> + insert into org_qprofiles ( + uuid, + organization_uuid, + rules_profile_uuid, + parent_uuid, + created_at, + updated_at + ) values ( + #{dto.kee, jdbcType=VARCHAR}, + #{dto.organizationUuid, jdbcType=VARCHAR}, + #{dto.kee, jdbcType=VARCHAR}, + #{dto.parentKee, jdbcType=VARCHAR}, + #{now, jdbcType=BIGINT}, + #{now, jdbcType=BIGINT} ) </insert> - <update id="update" parameterType="map"> - UPDATE rules_profiles SET - name=#{dto.name, jdbcType=VARCHAR}, - language=#{dto.language, jdbcType=VARCHAR}, - parent_kee=#{dto.parentKee, jdbcType=VARCHAR}, - updated_at=#{now, jdbcType=TIMESTAMP}, - rules_updated_at=#{dto.rulesUpdatedAt, jdbcType=VARCHAR}, - last_used=#{dto.lastUsed, jdbcType=BIGINT}, - user_updated_at=#{dto.userUpdatedAt, jdbcType=BIGINT}, - is_built_in=#{dto.isBuiltIn, jdbcType=BOOLEAN} - WHERE id=#{dto.id} + <update id="updateRulesProfile" parameterType="map"> + update rules_profiles + set + name = #{dto.name, jdbcType=VARCHAR}, + language = #{dto.language, jdbcType=VARCHAR}, + updated_at = #{now, jdbcType=TIMESTAMP}, + rules_updated_at = #{dto.rulesUpdatedAt, jdbcType=VARCHAR}, + last_used = #{dto.lastUsed, jdbcType=BIGINT}, + user_updated_at = #{dto.userUpdatedAt, jdbcType=BIGINT}, + is_built_in = #{dto.isBuiltIn, jdbcType=BOOLEAN} + where + kee = #{dto.kee, jdbcType=VARCHAR} + </update> + + <update id="updateOrgQProfile" parameterType="map"> + update org_qprofiles + set + parent_uuid = #{dto.parentKee, jdbcType=VARCHAR}, + updated_at = #{now, jdbcType=BIGINT} + where + uuid = #{dto.kee, jdbcType=VARCHAR} </update> - <update id="deleteByKeys" parameterType="String"> + <delete id="deleteRulesProfilesByUuids" parameterType="String"> delete from rules_profiles where kee in - <foreach collection="profileKeys" open="(" close=")" item="profileKey" separator=",">#{profileKey, jdbcType=VARCHAR}</foreach> - </update> + <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach> + </delete> - <select id="selectAll" parameterType="map" resultType="RulesProfile"> - SELECT - <include refid="profilesColumns"/> - FROM rules_profiles p - WHERE p.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} - ORDER BY p.name, p.language - </select> - - <select id="selectByNameAndLanguage" parameterType="map" resultType="RulesProfile"> - SELECT - <include refid="profilesColumns"/> - FROM rules_profiles p - WHERE p.name=#{name, jdbcType=VARCHAR} - AND p.language=#{language, jdbcType=VARCHAR} - <if test="organizationUuid != null"> - AND p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} - </if> - </select> - - <select id="selectByNameAndLanguages" parameterType="map" resultType="RulesProfile"> - SELECT - <include refid="profilesColumns"/> - FROM rules_profiles p - WHERE p.name=#{name, jdbcType=VARCHAR} - AND p.language IN <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach> - AND p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} - </select> - - <select id="selectByKey" parameterType="string" resultType="RulesProfile"> - SELECT - <include refid="profilesColumns"/> - FROM rules_profiles p - WHERE p.kee=#{id} - </select> - - <select id="selectByKeys" parameterType="string" resultType="RulesProfile"> - SELECT - <include refid="profilesColumns"/> - FROM rules_profiles p - WHERE p.kee in - <foreach collection="keys" open="(" close=")" item="key" separator=","> - #{key} - </foreach> + <delete id="deleteOrgQProfilesByUuids" parameterType="String"> + delete from org_qprofiles + where uuid in + <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach> + </delete> + + <select id="selectAll" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto"> + select + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + where + oqp.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} + order by rp.name, rp.language </select> - <select id="selectByLanguage" parameterType="String" resultType="RulesProfile"> - SELECT - <include refid="profilesColumns"/> - FROM rules_profiles p - WHERE p.language=#{language} - AND p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} - ORDER BY p.name + <select id="selectDefaultProfile" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto"> + select + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + inner join default_qprofiles dp on dp.qprofile_uuid = oqp.uuid + where + dp.language = #{language, jdbcType=VARCHAR} + and dp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + and rp.language = dp.language + and oqp.organization_uuid = dp.organization_uuid </select> - <select id="selectChildren" parameterType="string" resultType="RulesProfile"> - SELECT - <include refid="profilesColumns"/> - FROM rules_profiles p - WHERE p.parent_kee=#{id} - ORDER BY p.name + <select id="selectDefaultProfiles" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto"> + select + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + inner join default_qprofiles dp on dp.qprofile_uuid = oqp.uuid + where + dp.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach> + and dp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + and rp.language = dp.language + and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} </select> - <select id="selectDefaultProfile" parameterType="map" resultType="RulesProfile"> - SELECT - <include refid="profilesColumns"/> - FROM rules_profiles p - INNER JOIN default_qprofiles dp on p.kee=dp.qprofile_uuid - WHERE - dp.language=#{language, jdbcType=VARCHAR} - AND dp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + <select id="selectByNameAndLanguage" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto"> + select + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + where + rp.name = #{name, jdbcType=VARCHAR} + and rp.language = #{language, jdbcType=VARCHAR} + and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + </select> + + <select id="selectByNameAndLanguages" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto"> + select + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + where + rp.name = #{name, jdbcType=VARCHAR} + and rp.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach> + and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} </select> - <select id="selectDefaultProfiles" parameterType="map" resultType="RulesProfile"> - SELECT - <include refid="profilesColumns"/> - FROM rules_profiles p - INNER JOIN default_qprofiles dp on p.kee=dp.qprofile_uuid - WHERE - dp.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach> - AND dp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + <select id="selectByUuid" parameterType="string" resultType="org.sonar.db.qualityprofile.QProfileDto"> + select + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + where + oqp.uuid = #{uuid, jdbcType=VARCHAR} </select> - <select id="selectSelectedProjects" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto"> - SELECT pp.id as id, pj.id as projectId, pj.uuid as projectUuid, pj.kee as projectKey, pj.name as projectName, pp.profile_key as profileKey - FROM projects pj - JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid - AND pp.profile_key = #{profileKey, jdbcType=VARCHAR} - WHERE pj.scope='PRJ' AND pj.qualifier='TRK' - AND UPPER(pj.name) LIKE #{nameQuery, jdbcType=VARCHAR} - AND pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} - ORDER BY pj.name ASC + <select id="selectByUuids" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto"> + select + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + where + oqp.uuid in <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid}</foreach> </select> - <select id="selectDeselectedProjects" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto"> - SELECT pp.id as id, pj.id as projectId, pj.uuid as projectUuid, pj.kee as projectKey, pj.name as projectName, pp.profile_key as profileKey - FROM projects pj - LEFT JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid - AND pp.profile_key = #{profileKey, jdbcType=VARCHAR} - WHERE pj.scope='PRJ' AND pj.qualifier='TRK' - AND UPPER(pj.name) LIKE #{nameQuery, jdbcType=VARCHAR} - AND pp.profile_key IS NULL - AND pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} - ORDER BY pj.name ASC + <select id="selectByLanguage" parameterType="String" resultType="org.sonar.db.qualityprofile.QProfileDto"> + select + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + where + rp.language = #{language, jdbcType=VARCHAR} + and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} </select> - <select id="selectProjectAssociations" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto"> - SELECT pp.id as id, pj.id as projectId, pj.uuid as projectUuid, pj.kee as projectKey, pj.name as projectName, pp.profile_key as profileKey - FROM projects pj - LEFT JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid - AND pp.profile_key = #{profileKey, jdbcType=VARCHAR} - WHERE pj.scope='PRJ' AND pj.qualifier='TRK' - AND UPPER(pj.name) LIKE #{nameQuery, jdbcType=VARCHAR} - AND pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} - ORDER BY pj.name ASC + <select id="selectChildren" parameterType="string" resultType="org.sonar.db.qualityprofile.QProfileDto"> + select + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + where + oqp.parent_uuid = #{uuid, jdbcType=VARCHAR} + ORDER BY rp.name </select> - <select id="countProjectsByProfileKey" resultType="KeyLongValue" parameterType="map"> - select pp.profile_key as "key", count(projects.id) as "value" - from projects projects - inner join project_qprofiles pp ON pp.project_uuid = projects.uuid - inner join rules_profiles prof ON pp.profile_key = prof.kee - where projects.enabled = ${_true} - and prof.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} - group by pp.profile_key + <select id="countProjectsByProfileUuid" resultType="KeyLongValue" parameterType="map"> + select pqp.profile_key as "key", count(pj.uuid) as "value" + from projects pj + inner join project_qprofiles pqp on pqp.project_uuid = pj.uuid + inner join org_qprofiles oqp on oqp.uuid = pqp.profile_key + where + pj.enabled = ${_true} + and pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + and oqp.organization_uuid = pj.organization_uuid + group by pqp.profile_key </select> - <select id="selectAssociatedToProjectUuidAndLanguage" parameterType="map" resultType="RulesProfile"> + <select id="selectAssociatedToProjectUuidAndLanguage" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto"> select - <include refid="profilesColumns"/> - from rules_profiles p - inner join project_qprofiles pp ON pp.profile_key = p.kee + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + inner join project_qprofiles pqp ON pqp.profile_key = oqp.uuid where - p.language = #{language, jdbcType=VARCHAR} - and pp.project_uuid = #{projectUuid, jdbcType=VARCHAR} - and p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + rp.language = #{language, jdbcType=VARCHAR} + and pqp.project_uuid = #{projectUuid, jdbcType=VARCHAR} + and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} </select> - <select id="selectAssociatedToProjectUuidAndLanguages" parameterType="map" resultType="RulesProfile"> + <select id="selectAssociatedToProjectUuidAndLanguages" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto"> select - <include refid="profilesColumns"/> - from rules_profiles p - inner join project_qprofiles pq ON pq.profile_key = p.kee + <include refid="qProfileColumns"/> + from org_qprofiles oqp + inner join rules_profiles rp on oqp.rules_profile_uuid = rp.kee + inner join project_qprofiles pqp ON pqp.profile_key = oqp.uuid where - p.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach> - and pq.project_uuid = #{projectUuid, jdbcType=VARCHAR} - and p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + rp.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach> + and pqp.project_uuid = #{projectUuid, jdbcType=VARCHAR} + and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} </select> <insert id="insertProjectProfileAssociation" useGeneratedKeys="false"> @@ -213,40 +238,86 @@ <update id="updateProjectProfileAssociation"> update project_qprofiles set - profile_key = #{profileUuid, jdbcType=VARCHAR} + profile_key = #{profileUuid, jdbcType=VARCHAR} where - project_uuid = #{projectUuid, jdbcType=VARCHAR} - and profile_key = #{oldProfileUuid, jdbcType=VARCHAR} + project_uuid = #{projectUuid, jdbcType=VARCHAR} + and profile_key = #{oldProfileUuid, jdbcType=VARCHAR} </update> - <update id="deleteProjectProfileAssociation"> - DELETE FROM project_qprofiles - WHERE project_uuid=#{projectUuid, jdbcType=VARCHAR} AND profile_key=#{profileKey, jdbcType=VARCHAR} - </update> + <delete id="deleteProjectProfileAssociation"> + delete from project_qprofiles + where + project_uuid = #{projectUuid, jdbcType=VARCHAR} + and profile_key=#{profileUuid, jdbcType=VARCHAR} + </delete> - <update id="deleteProjectAssociationByProfileKeys" parameterType="String"> + <delete id="deleteProjectAssociationByProfileUuids" parameterType="String"> delete from project_qprofiles where profile_key in - <foreach collection="profileKeys" open="(" close=")" item="profileKey" separator=","> - #{profileKey, jdbcType=VARCHAR} + <foreach collection="profileUuids" open="(" close=")" item="profileUuid" separator=","> + #{profileUuid, jdbcType=VARCHAR} </foreach> - </update> + </delete> + + <select id="selectSelectedProjects" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto"> + select + pp.id as id, + pj.id as projectId, + pj.uuid as projectUuid, + pj.kee as projectKey, + pj.name as projectName, + pp.profile_key as profileKey + from projects pj + inner join project_qprofiles pp ON pp.project_uuid = pj.uuid and pp.profile_key = #{profileUuid, jdbcType=VARCHAR} + where + pj.scope = 'PRJ' + and pj.qualifier = 'TRK' + and upper(pj.name) like #{nameQuery, jdbcType=VARCHAR} + and pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + order by pj.name ASC + </select> - <select id="selectOutdatedProfiles" parameterType="map" resultType="string"> - select rp.kee - from rules_profiles rp - inner join organizations o on o.uuid = rp.organization_uuid - where rp.language = #{language, jdbcType=VARCHAR} + <select id="selectDeselectedProjects" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto"> + SELECT pp.id as id, pj.id as projectId, pj.uuid as projectUuid, pj.kee as projectKey, pj.name as projectName, pp.profile_key as profileKey + FROM projects pj + LEFT JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid + AND pp.profile_key = #{profileUuid, jdbcType=VARCHAR} + WHERE pj.scope='PRJ' AND pj.qualifier='TRK' + AND UPPER(pj.name) LIKE #{nameQuery, jdbcType=VARCHAR} + AND pp.profile_key IS NULL + AND pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + ORDER BY pj.name ASC + </select> + + <select id="selectProjectAssociations" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto"> + SELECT pp.id as id, pj.id as projectId, pj.uuid as projectUuid, pj.kee as projectKey, pj.name as projectName, pp.profile_key as profileKey + FROM projects pj + LEFT JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid + AND pp.profile_key = #{profileUuid, jdbcType=VARCHAR} + WHERE pj.scope='PRJ' AND pj.qualifier='TRK' + AND UPPER(pj.name) LIKE #{nameQuery, jdbcType=VARCHAR} + AND pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + ORDER BY pj.name ASC + </select> + + <select id="selectUuidsOfCustomQProfiles" parameterType="map" resultType="string"> + select oqp.uuid + from org_qprofiles oqp + inner join organizations o on o.uuid = oqp.organization_uuid + inner join rules_profiles rp on rp.kee = oqp.rules_profile_uuid + where + rp.language = #{language, jdbcType=VARCHAR} and rp.name = #{name, jdbcType=VARCHAR} and rp.is_built_in = ${_false} </select> - <update id="rename" parameterType="map"> + <update id="renameRulesProfiles" parameterType="map"> update rules_profiles set name = #{newName, jdbcType=VARCHAR}, updated_at = #{updatedAt, jdbcType=TIMESTAMP} - where kee in <foreach collection="profileKeys" open="(" close=")" item="profileKey" separator=",">#{profileKey, jdbcType=VARCHAR}</foreach> + where + kee in <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach> </update> </mapper> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java index 30d00d121e2..242975bd404 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java @@ -41,7 +41,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.dialect.Dialect; import org.sonar.db.dialect.Oracle; -import org.sonar.db.qualityprofile.RulesProfileDto; +import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.GroupTesting; import org.sonar.db.user.UserDto; @@ -877,7 +877,7 @@ public class OrganizationDaoTest { public void selectWithoutQualityProfile_returns_() { OrganizationDto orgWithoutAnyProfiles = dbTester.organizations().insert(); OrganizationDto orgWithProfiles = dbTester.organizations().insert(); - RulesProfileDto profile = dbTester.qualityProfiles().insert(orgWithProfiles); + QProfileDto profile = dbTester.qualityProfiles().insert(orgWithProfiles); assertThat(underTest.selectWithoutQualityProfile(dbSession, "js", "foo")) .extracting(OrganizationDto::getUuid).containsExactlyInAnyOrder(orgWithoutAnyProfiles.getUuid(), orgWithProfiles.getUuid()); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java index 35530c4abd4..bf03320b40a 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java @@ -63,8 +63,8 @@ public class ActiveRuleDaoTest { private OrganizationDto organization = OrganizationTesting.newOrganizationDto(); - private RulesProfileDto profile1 = RulesProfileDto.createFor("qp1").setOrganizationUuid(organization.getUuid()).setName("QProfile1"); - private RulesProfileDto profile2 = RulesProfileDto.createFor("qp2").setOrganizationUuid(organization.getUuid()).setName("QProfile2"); + private QProfileDto profile1 = QProfileDto.createFor("qp1").setOrganizationUuid(organization.getUuid()).setName("QProfile1"); + private QProfileDto profile2 = QProfileDto.createFor("qp2").setOrganizationUuid(organization.getUuid()).setName("QProfile2"); private RuleDefinitionDto rule1 = RuleTesting.newRule(RuleTesting.XOO_X1); private RuleDefinitionDto rule2 = RuleTesting.newRule(RuleTesting.XOO_X2); @@ -334,7 +334,7 @@ public class ActiveRuleDaoTest { assertThat(dbTester.countRowsOfTable(dbSession, "active_rules")).isEqualTo(1); } - private static ActiveRuleDto newRow(RulesProfileDto profile, RuleDefinitionDto rule) { + private static ActiveRuleDto newRow(QProfileDto profile, RuleDefinitionDto rule) { return createFor(profile, rule).setSeverity(BLOCKER); } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/DefaultQProfileDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/DefaultQProfileDaoTest.java index f5800f52589..f317d828e08 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/DefaultQProfileDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/DefaultQProfileDaoTest.java @@ -20,6 +20,7 @@ package org.sonar.db.qualityprofile; import java.util.List; +import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; @@ -42,7 +43,7 @@ public class DefaultQProfileDaoTest { @Test public void insertOrUpdate_inserts_row_when_does_not_exist() { OrganizationDto org = dbTester.organizations().insert(); - RulesProfileDto profile = dbTester.qualityProfiles().insert(org); + QProfileDto profile = dbTester.qualityProfiles().insert(org); DefaultQProfileDto dto = DefaultQProfileDto.from(profile); underTest.insertOrUpdate(dbSession, dto); @@ -69,7 +70,7 @@ public class DefaultQProfileDaoTest { dbSession.commit(); assertThat(countRows()).isEqualTo(1); - assertThat(dbTester.qualityProfiles().selectUuidOfDefaultProfile(org, dto.getLanguage())).hasValue(newQProfileUuid); + assertThat(selectUuidOfDefaultProfile(org, dto.getLanguage())).hasValue(newQProfileUuid); } @Test @@ -85,18 +86,18 @@ public class DefaultQProfileDaoTest { dbSession.commit(); assertThat(countRows()).isEqualTo(2); - assertThat(dbTester.qualityProfiles().selectUuidOfDefaultProfile(org1, "java")).isEmpty(); - assertThat(dbTester.qualityProfiles().selectUuidOfDefaultProfile(org1, "js")).hasValue("u2"); - assertThat(dbTester.qualityProfiles().selectUuidOfDefaultProfile(org2, "java")).isEmpty(); - assertThat(dbTester.qualityProfiles().selectUuidOfDefaultProfile(org2, "js")).hasValue("u4"); + assertThat(selectUuidOfDefaultProfile(org1, "java")).isEmpty(); + assertThat(selectUuidOfDefaultProfile(org1, "js")).hasValue("u2"); + assertThat(selectUuidOfDefaultProfile(org2, "java")).isEmpty(); + assertThat(selectUuidOfDefaultProfile(org2, "js")).hasValue("u4"); } @Test public void selectExistingQProfileUuids_filters_defaults() { OrganizationDto org = dbTester.organizations().insert(); - RulesProfileDto profile1 = dbTester.qualityProfiles().insert(org); - RulesProfileDto profile2 = dbTester.qualityProfiles().insert(org); - dbTester.qualityProfiles().markAsDefault(profile1); + QProfileDto profile1 = dbTester.qualityProfiles().insert(org); + QProfileDto profile2 = dbTester.qualityProfiles().insert(org); + dbTester.qualityProfiles().setAsDefault(profile1); List<String> profileUuids = asList(profile1.getKee(), profile2.getKee(), "other"); assertThat(underTest.selectExistingQProfileUuids(dbSession, org.getUuid(), profileUuids)) @@ -106,21 +107,30 @@ public class DefaultQProfileDaoTest { @Test public void isDefault_returns_true_if_profile_is_marked_as_default() { OrganizationDto org = dbTester.organizations().insert(); - RulesProfileDto profile1 = dbTester.qualityProfiles().insert(org); - RulesProfileDto profile2 = dbTester.qualityProfiles().insert(org); - dbTester.qualityProfiles().markAsDefault(profile1); + QProfileDto profile1 = dbTester.qualityProfiles().insert(org); + QProfileDto profile2 = dbTester.qualityProfiles().insert(org); + dbTester.qualityProfiles().setAsDefault(profile1); assertThat(underTest.isDefault(dbSession, org.getUuid(), profile1.getKee())).isTrue(); assertThat(underTest.isDefault(dbSession, org.getUuid(), profile2.getKee())).isFalse(); assertThat(underTest.isDefault(dbSession, org.getUuid(), "does_not_exist")).isFalse(); } - private void assertThatIsDefault(OrganizationDto org, RulesProfileDto profile) { - assertThat(dbTester.qualityProfiles().selectUuidOfDefaultProfile(org, profile.getLanguage())).hasValue(profile.getKee()); + private void assertThatIsDefault(OrganizationDto org, QProfileDto profile) { + assertThat(selectUuidOfDefaultProfile(org, profile.getLanguage())).hasValue(profile.getKee()); assertThat(underTest.isDefault(dbSession, org.getUuid(), profile.getKee())).isTrue(); } private int countRows() { return dbTester.countRowsOfTable("default_qprofiles"); } + + private Optional<String> selectUuidOfDefaultProfile(OrganizationDto org, String language) { + return dbTester.select("select qprofile_uuid as \"profileUuid\" " + + " from default_qprofiles " + + " where organization_uuid='" + org.getUuid() + "' and language='" + language + "'") + .stream() + .findFirst() + .map(m -> (String) m.get("profileUuid")); + } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java index 6ecb82cebc4..be9bf0496de 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java @@ -89,14 +89,6 @@ public class QProfileChangeDaoTest { } @Test - public void insert_throws_ISE_if_key_is_already_set() { - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Key of QProfileChangeDto must be set by DAO only. Got C1."); - - underTest.insert(dbSession, new QProfileChangeDto().setKey("C1")); - } - - @Test public void insert_throws_ISE_if_date_is_already_set() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Date of QProfileChangeDto must be set by DAO only. Got 123."); @@ -211,8 +203,8 @@ public class QProfileChangeDaoTest { insertChange("P1", "ACTIVATED", null, null);// key: C1 insertChange("P1", "ACTIVATED", null, null);// key: C2 - assertThat(underTest.countForProfileKey(dbSession, "P1")).isEqualTo(2); - assertThat(underTest.countForProfileKey(dbSession, "P2")).isEqualTo(0); + assertThat(underTest.countForProfileUuid(dbSession, "P1")).isEqualTo(2); + assertThat(underTest.countForProfileUuid(dbSession, "P2")).isEqualTo(0); } @Test @@ -224,8 +216,8 @@ public class QProfileChangeDaoTest { underTest.deleteByProfileKeys(dbSession, asList("P1")); - assertThat(underTest.countForProfileKey(dbSession, "P1")).isEqualTo(0); - assertThat(underTest.countForProfileKey(dbSession, "P2")).isEqualTo(1); + assertThat(underTest.countForProfileUuid(dbSession, "P1")).isEqualTo(0); + assertThat(underTest.countForProfileUuid(dbSession, "P2")).isEqualTo(1); } @Test @@ -235,7 +227,7 @@ public class QProfileChangeDaoTest { underTest.deleteByProfileKeys(dbSession, asList("does_not_exist")); - assertThat(underTest.countForProfileKey(dbSession, "P1")).isEqualTo(1); + assertThat(underTest.countForProfileUuid(dbSession, "P1")).isEqualTo(1); } private void insertChange(String profileKey, String type, @Nullable String login, @Nullable String data) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java index c6332f53d86..f2f2e8f9d56 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java @@ -19,10 +19,12 @@ */ package org.sonar.db.qualityprofile; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.stream.IntStream; import org.assertj.core.data.MapEntry; import org.junit.After; import org.junit.Before; @@ -55,7 +57,6 @@ public class QualityProfileDaoTest { public DbTester dbTester = DbTester.create(system); private DbSession dbSession = dbTester.getSession(); - private QualityProfileDbTester qualityProfileDb = new QualityProfileDbTester(dbTester); private QualityProfileDao underTest = dbTester.getDbClient().qualityProfileDao(); private OrganizationDto organization; @@ -66,90 +67,109 @@ public class QualityProfileDaoTest { } @After - public void tearDown() { - // minor optimization, no need to commit pending operations + public void deleteData() { dbSession.rollback(); } @Test - public void insert() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); - - RulesProfileDto dto = RulesProfileDto.createFor("abcde") + public void test_insert() { + QProfileDto dto = QProfileDto.createFor("theUuid") .setOrganizationUuid(organization.getUuid()) - .setName("ABCDE") - .setLanguage("xoo") + .setName("theName") + .setLanguage("theLang") + .setLastUsed(1_000L) + .setParentKee("theParentUuid") + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") .setIsBuiltIn(true); - underTest.insert(dbTester.getSession(), dto); - dbTester.commit(); + underTest.insert(dbSession, dto); - dbTester.assertDbUnit(getClass(), "insert-result.xml", new String[] {"created_at", "updated_at", "rules_updated_at"}, "rules_profiles"); + QProfileDto reloaded = underTest.selectByUuid(dbSession, dto.getKee()); + assertThat(reloaded.getKee()).isEqualTo(dto.getKee()); + assertThat(reloaded.getId()).isNotNull().isNotZero(); + assertThat(reloaded.getLanguage()).isEqualTo(dto.getLanguage()); + assertThat(reloaded.getName()).isEqualTo(dto.getName()); + assertThat(reloaded.getLastUsed()).isEqualTo(dto.getLastUsed()); + assertThat(reloaded.getRulesUpdatedAt()).isEqualTo(dto.getRulesUpdatedAt()); + assertThat(reloaded.getParentKee()).isEqualTo(dto.getParentKee()); + assertThat(reloaded.getOrganizationUuid()).isEqualTo(dto.getOrganizationUuid()); + assertThat(reloaded.isBuiltIn()).isEqualTo(dto.isBuiltIn()); } @Test - public void update() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); - - RulesProfileDto dto = RulesProfileDto.createFor("key") - .setId(1) + public void test_update() { + QProfileDto initial = QProfileDto.createFor("theUuid") .setOrganizationUuid(organization.getUuid()) - .setName("New Name") - .setLanguage("js") - .setParentKee("fghij") - .setIsBuiltIn(false); - - underTest.update(dbSession, dto); - dbSession.commit(); - - dbTester.assertDbUnit(getClass(), "update-result.xml", new String[] {"created_at", "updated_at", "rules_updated_at"}, "rules_profiles"); + .setName("theName") + .setLanguage("theLang") + .setLastUsed(1_000L) + .setParentKee("theParentUuid") + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(true); + underTest.insert(dbSession, initial); + + QProfileDto update = QProfileDto.createFor(initial.getKee()) + .setName("theNewName") + .setLanguage("theNewLang") + .setLastUsed(11_000L) + .setParentKee("theNewParentUuid") + .setUserUpdatedAt(12_000L) + .setRulesUpdatedAt("2017-06-01") + .setIsBuiltIn(false) + + // field that cannot be changed + .setOrganizationUuid("theNewOrg"); + underTest.update(dbSession, update); + + QProfileDto reloaded = underTest.selectByUuid(dbSession, initial.getKee()); + assertThat(reloaded.getKee()).isEqualTo(initial.getKee()); + assertThat(reloaded.getOrganizationUuid()).isEqualTo(initial.getOrganizationUuid()); + + // updated fields + assertThat(reloaded.getLanguage()).isEqualTo(update.getLanguage()); + assertThat(reloaded.getName()).isEqualTo(update.getName()); + assertThat(reloaded.getLastUsed()).isEqualTo(update.getLastUsed()); + assertThat(reloaded.getRulesUpdatedAt()).isEqualTo(update.getRulesUpdatedAt()); + assertThat(reloaded.getParentKee()).isEqualTo(update.getParentKee()); + assertThat(reloaded.isBuiltIn()).isEqualTo(update.isBuiltIn()); } @Test - public void test_deleteByKeys() { - RulesProfileDto p1 = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization()); - RulesProfileDto p2 = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization()); - RulesProfileDto p3 = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization()); + public void test_deleteByUuids() { + QProfileDto p1 = dbTester.qualityProfiles().insert(organization); + QProfileDto p2 = dbTester.qualityProfiles().insert(organization); + QProfileDto p3 = dbTester.qualityProfiles().insert(organization); - underTest.deleteByKeys(dbSession, asList(p1.getKee(), p3.getKee(), "does_not_exist")); + underTest.deleteByUuids(dbSession, asList(p1.getKee(), p3.getKee(), "does_not_exist")); - List<Map<String, Object>> keysInDb = dbTester.select(dbSession, "select kee as \"key\" from rules_profiles"); - assertThat(keysInDb).hasSize(1); - assertThat(keysInDb.get(0).get("key")).isEqualTo(p2.getKee()); + List<QProfileDto> profiles = underTest.selectAll(dbSession, organization); + assertThat(profiles).extracting(QProfileDto::getKee).containsExactly(p2.getKee()); } @Test - public void deleteByKeys_does_nothing_if_empty_keys() { - RulesProfileDto p1 = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization()); + public void deleteByUuids_does_nothing_if_empty_uuids() { + dbTester.qualityProfiles().insert(organization); - underTest.deleteByKeys(dbSession, Collections.emptyList()); + underTest.deleteByUuids(dbSession, Collections.emptyList()); assertThat(dbTester.countRowsOfTable(dbSession, "rules_profiles")).isEqualTo(1); + assertThat(dbTester.countRowsOfTable(dbSession, "org_qprofiles")).isEqualTo(1); } @Test - public void deleteProjectAssociationsByProfileKeys_does_nothing_if_empty_keys() { - RulesProfileDto profile1 = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization()); - ComponentDto project1 = dbTester.components().insertPrivateProject(); - dbTester.qualityProfiles().associateProjectWithQualityProfile(project1, profile1); - - underTest.deleteProjectAssociationsByProfileKeys(dbSession, Collections.emptyList()); - - assertThat(dbTester.countRowsOfTable(dbSession, "project_qprofiles")).isEqualTo(1); - } + public void test_deleteProjectAssociationsByProfileUuids() { + QProfileDto profile1 = dbTester.qualityProfiles().insert(organization); + QProfileDto profile2 = dbTester.qualityProfiles().insert(organization); + ComponentDto project1 = dbTester.components().insertPrivateProject(organization); + ComponentDto project2 = dbTester.components().insertPrivateProject(organization); + ComponentDto project3 = dbTester.components().insertPrivateProject(organization); + dbTester.qualityProfiles().associateWithProject(project1, profile1); + dbTester.qualityProfiles().associateWithProject(project2, profile1); + dbTester.qualityProfiles().associateWithProject(project3, profile2); - @Test - public void deleteProjectAssociationsByProfileKeys_deletes_rows_from_table_project_profiles() { - RulesProfileDto profile1 = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization()); - RulesProfileDto profile2 = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization()); - ComponentDto project1 = dbTester.components().insertPrivateProject(); - ComponentDto project2 = dbTester.components().insertPrivateProject(); - ComponentDto project3 = dbTester.components().insertPrivateProject(); - dbTester.qualityProfiles().associateProjectWithQualityProfile(project1, profile1); - dbTester.qualityProfiles().associateProjectWithQualityProfile(project2, profile1); - dbTester.qualityProfiles().associateProjectWithQualityProfile(project3, profile2); - - underTest.deleteProjectAssociationsByProfileKeys(dbSession, asList(profile1.getKee(), "does_not_exist")); + underTest.deleteProjectAssociationsByProfileUuids(dbSession, asList(profile1.getKee(), "does_not_exist")); List<Map<String, Object>> rows = dbTester.select(dbSession, "select project_uuid as \"projectUuid\", profile_key as \"profileKey\" from project_qprofiles"); assertThat(rows).hasSize(1); @@ -158,103 +178,148 @@ public class QualityProfileDaoTest { } @Test - public void find_all() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); + public void deleteProjectAssociationsByProfileUuids_does_nothing_if_empty_uuids() { + QProfileDto profile = dbTester.qualityProfiles().insert(organization); + ComponentDto project = dbTester.components().insertPrivateProject(); + dbTester.qualityProfiles().associateWithProject(project, profile); - List<RulesProfileDto> dtos = underTest.selectAll(dbTester.getSession(), organization); + underTest.deleteProjectAssociationsByProfileUuids(dbSession, Collections.emptyList()); - assertThat(dtos).hasSize(2); + assertThat(dbTester.countRowsOfTable(dbSession, "project_qprofiles")).isEqualTo(1); + } - RulesProfileDto dto1 = dtos.get(0); - assertThat(dto1.getId()).isEqualTo(1); - assertThat(dto1.getName()).isEqualTo("Sonar Way"); - assertThat(dto1.getLanguage()).isEqualTo("java"); - assertThat(dto1.getParentKee()).isNull(); - assertThat(dto1.isBuiltIn()).isTrue(); - - RulesProfileDto dto2 = dtos.get(1); - assertThat(dto2.getId()).isEqualTo(2); - assertThat(dto2.getName()).isEqualTo("Sonar Way"); - assertThat(dto2.getLanguage()).isEqualTo("js"); - assertThat(dto2.getParentKee()).isNull(); - assertThat(dto2.isBuiltIn()).isFalse(); + @Test + public void test_selectAll() { + List<QProfileDto> sharedData = createSharedData(); + + List<QProfileDto> reloadeds = underTest.selectAll(dbSession, organization); + + assertThat(reloadeds).hasSize(sharedData.size()); + + IntStream.range(1, reloadeds.size()) + .forEach( + i -> { + QProfileDto reloaded = reloadeds.get(i - 1); + QProfileDto original = sharedData.get(i - 1); + + assertThat(reloaded.getId()).isEqualTo(original.getId()); + assertThat(reloaded.getName()).isEqualTo(original.getName()); + assertThat(reloaded.getKee()).isEqualTo(original.getKee()); + assertThat(reloaded.getOrganizationUuid()).isEqualTo(original.getOrganizationUuid()); + assertThat(reloaded.getLanguage()).isEqualTo(original.getLanguage()); + assertThat(reloaded.getParentKee()).isEqualTo(original.getParentKee()); + assertThat(reloaded.getRulesUpdatedAt()).isEqualTo(original.getRulesUpdatedAt()); + assertThat(reloaded.getLastUsed()).isEqualTo(original.getLastUsed()); + assertThat(reloaded.getUserUpdatedAt()).isEqualTo(original.getUserUpdatedAt()); + assertThat(reloaded.isBuiltIn()).isEqualTo(original.isBuiltIn()); + } + ); } @Test public void find_all_is_sorted_by_profile_name() { - dbTester.prepareDbUnit(getClass(), "select_all_is_sorted_by_profile_name.xml"); + QProfileDto dto1 = QProfileDto.createFor("js_first") + .setOrganizationUuid(organization.getUuid()) + .setName("First") + .setLanguage("js") + .setLastUsed(1_000L) + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(false); + underTest.insert(dbSession, dto1); + + QProfileDto dto2 = QProfileDto.createFor("js_second") + .setOrganizationUuid(organization.getUuid()) + .setName("Second") + .setLanguage("js") + .setLastUsed(1_000L) + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(false); + underTest.insert(dbSession, dto2); + + QProfileDto dto3 = QProfileDto.createFor("js_third") + .setOrganizationUuid(organization.getUuid()) + .setName("Third") + .setLanguage("js") + .setLastUsed(1_000L) + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(false); + underTest.insert(dbSession, dto3); - List<RulesProfileDto> dtos = underTest.selectAll(dbTester.getSession(), organization); + List<QProfileDto> dtos = underTest.selectAll(dbSession, organization); assertThat(dtos).hasSize(3); assertThat(dtos.get(0).getName()).isEqualTo("First"); assertThat(dtos.get(1).getName()).isEqualTo("Second"); assertThat(dtos.get(2).getName()).isEqualTo("Third"); + + underTest.deleteByUuids(dbSession, Arrays.asList("js_first", "js_second", "js_third")); } @Test public void get_default_profile() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); + List<QProfileDto> sharedData = createSharedData(); - RulesProfileDto java = underTest.selectDefaultProfile(dbTester.getSession(), organization, "java"); + QProfileDto java = underTest.selectDefaultProfile(dbSession, organization, "java"); assertThat(java).isNotNull(); assertThat(java.getKee()).isEqualTo("java_sonar_way"); - assertThat(underTest.selectDefaultProfile(dbTester.getSession(), dbTester.organizations().insert(), "java")).isNull(); - assertThat(underTest.selectDefaultProfile(dbTester.getSession(), organization, "js")).isNull(); + assertThat(underTest.selectDefaultProfile(dbSession, dbTester.organizations().insert(), "java")).isNull(); + assertThat(underTest.selectDefaultProfile(dbSession, organization, "js")).isNull(); } @Test public void get_default_profiles() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); + List<QProfileDto> sharedData = createSharedData(); - List<RulesProfileDto> java = underTest.selectDefaultProfiles(dbTester.getSession(), organization, singletonList("java")); - assertThat(java).extracting(RulesProfileDto::getKee).containsOnly("java_sonar_way"); + List<QProfileDto> java = underTest.selectDefaultProfiles(dbSession, organization, singletonList("java")); + assertThat(java).extracting(QProfileDto::getKee).containsOnly("java_sonar_way"); - assertThat(underTest.selectDefaultProfiles(dbTester.getSession(), organization, singletonList("js"))).isEmpty(); - assertThat(underTest.selectDefaultProfiles(dbTester.getSession(), organization, of("java", "js"))).extracting(RulesProfileDto::getKee).containsOnly("java_sonar_way"); - assertThat(underTest.selectDefaultProfiles(dbTester.getSession(), organization, of("js", "java"))).extracting(RulesProfileDto::getKee).containsOnly("java_sonar_way"); + assertThat(underTest.selectDefaultProfiles(dbSession, organization, singletonList("js"))).isEmpty(); + assertThat(underTest.selectDefaultProfiles(dbSession, organization, of("java", "js"))).extracting(QProfileDto::getKee).containsOnly("java_sonar_way"); + assertThat(underTest.selectDefaultProfiles(dbSession, organization, of("js", "java"))).extracting(QProfileDto::getKee).containsOnly("java_sonar_way"); } @Test public void get_by_name_and_language() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); + List<QProfileDto> sharedData = createSharedData(); - RulesProfileDto dto = underTest.selectByNameAndLanguage(organization, "Sonar Way", "java", dbTester.getSession()); - assertThat(dto.getId()).isEqualTo(1); + QProfileDto dto = underTest.selectByNameAndLanguage(dbSession, organization, "Sonar Way", "java"); assertThat(dto.getName()).isEqualTo("Sonar Way"); assertThat(dto.getLanguage()).isEqualTo("java"); assertThat(dto.getParentKee()).isNull(); - assertThat(underTest.selectByNameAndLanguage(organization, "Sonar Way", "java", dbTester.getSession())).isNotNull(); - assertThat(underTest.selectByNameAndLanguage(organization, "Sonar Way", "unknown", dbTester.getSession())).isNull(); + assertThat(underTest.selectByNameAndLanguage(dbSession, organization, "Sonar Way", "java")).isNotNull(); + assertThat(underTest.selectByNameAndLanguage(dbSession, organization, "Sonar Way", "unknown")).isNull(); } @Test public void get_by_name_and_languages() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); + createSharedData(); - List<RulesProfileDto> dtos = underTest.selectByNameAndLanguages(organization, "Sonar Way", singletonList("java"), dbTester.getSession()); + List<QProfileDto> dtos = underTest.selectByNameAndLanguages(dbSession, organization, "Sonar Way", singletonList("java")); assertThat(dtos).hasSize(1); - RulesProfileDto dto = dtos.iterator().next(); - assertThat(dto.getId()).isEqualTo(1); + QProfileDto dto = dtos.iterator().next(); assertThat(dto.getName()).isEqualTo("Sonar Way"); assertThat(dto.getLanguage()).isEqualTo("java"); assertThat(dto.getParentKee()).isNull(); - assertThat(underTest.selectByNameAndLanguages(organization, "Sonar Way", singletonList("unknown"), dbTester.getSession())).isEmpty(); - assertThat(underTest.selectByNameAndLanguages(organization, "Sonar Way", of("java", "unknown"), dbTester.getSession())).extracting("id").containsOnly(1); + assertThat(underTest.selectByNameAndLanguages(dbSession, organization, "Sonar Way", singletonList("unknown"))).isEmpty(); + assertThat(underTest.selectByNameAndLanguages(dbSession, organization, "Sonar Way", of("java", "unknown"))) + .extracting(QProfileDto::getKee).containsOnly(dto.getKee()); } @Test public void should_find_by_language() { - RulesProfileDto profile = QualityProfileTesting.newQualityProfileDto() + QProfileDto profile = QualityProfileTesting.newQualityProfileDto() .setOrganizationUuid(organization.getUuid()); underTest.insert(dbSession, profile); - List<RulesProfileDto> results = underTest.selectByLanguage(dbSession, organization, profile.getLanguage()); + List<QProfileDto> results = underTest.selectByLanguage(dbSession, organization, profile.getLanguage()); assertThat(results).hasSize(1); - RulesProfileDto result = results.get(0); + QProfileDto result = results.get(0); assertThat(result.getId()).isEqualTo(profile.getId()); assertThat(result.getName()).isEqualTo(profile.getName()); @@ -265,60 +330,123 @@ public class QualityProfileDaoTest { @Test public void should_not_find_by_language_in_wrong_organization() { - RulesProfileDto profile = QualityProfileTesting.newQualityProfileDto() + QProfileDto profile = QualityProfileTesting.newQualityProfileDto() .setOrganizationUuid(organization.getUuid()); underTest.insert(dbSession, profile); - List<RulesProfileDto> results = underTest.selectByLanguage(dbSession, OrganizationTesting.newOrganizationDto(), profile.getLanguage()); + List<QProfileDto> results = underTest.selectByLanguage(dbSession, OrganizationTesting.newOrganizationDto(), profile.getLanguage()); assertThat(results).isEmpty(); } @Test public void should_not_find_by_language_with_wrong_language() { - RulesProfileDto profile = QualityProfileTesting.newQualityProfileDto() + QProfileDto profile = QualityProfileTesting.newQualityProfileDto() .setOrganizationUuid(organization.getUuid()); underTest.insert(dbSession, profile); - List<RulesProfileDto> results = underTest.selectByLanguage(dbSession, organization, "another language"); + List<QProfileDto> results = underTest.selectByLanguage(dbSession, organization, "another language"); assertThat(results).isEmpty(); } + @Test public void find_children() { - dbTester.prepareDbUnit(getClass(), "inheritance.xml"); + QProfileDto original1 = QProfileDto.createFor("java_child1") + .setOrganizationUuid(organization.getUuid()) + .setName("Child1") + .setLanguage("java") + .setLastUsed(1_000L) + .setParentKee("java_parent") + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(false); + underTest.insert(dbSession, original1); + + QProfileDto original2 = QProfileDto.createFor("java_child2") + .setOrganizationUuid(organization.getUuid()) + .setName("Child2") + .setLanguage("java") + .setLastUsed(1_000L) + .setParentKee("java_parent") + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(false); + underTest.insert(dbSession, original2); + + QProfileDto original3 = QProfileDto.createFor("java_parent") + .setOrganizationUuid(organization.getUuid()) + .setName("Parent") + .setLanguage("java") + .setLastUsed(1_000L) + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(false); + underTest.insert(dbSession, original3); + + QProfileDto original4 = QProfileDto.createFor("js_child1") + .setOrganizationUuid(organization.getUuid()) + .setName("Child1") + .setLanguage("js") + .setLastUsed(1_000L) + .setParentKee("js_parent") + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(false); + underTest.insert(dbSession, original4); - List<RulesProfileDto> dtos = underTest.selectChildren(dbTester.getSession(), "java_parent"); + QProfileDto original5 = QProfileDto.createFor("js_child2") + .setOrganizationUuid(organization.getUuid()) + .setName("Child2") + .setLanguage("js") + .setLastUsed(1_000L) + .setParentKee("js_parent") + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(false); + underTest.insert(dbSession, original5); + + QProfileDto original6 = QProfileDto.createFor("js_parent") + .setOrganizationUuid(organization.getUuid()) + .setName("Parent") + .setLanguage("js") + .setLastUsed(1_000L) + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(false); + underTest.insert(dbSession, original6); + + List<QProfileDto> dtos = underTest.selectChildren(dbSession, "java_parent"); assertThat(dtos).hasSize(2); - RulesProfileDto dto1 = dtos.get(0); - assertThat(dto1.getId()).isEqualTo(1); + QProfileDto dto1 = dtos.get(0); assertThat(dto1.getName()).isEqualTo("Child1"); assertThat(dto1.getLanguage()).isEqualTo("java"); assertThat(dto1.getParentKee()).isEqualTo("java_parent"); - RulesProfileDto dto2 = dtos.get(1); - assertThat(dto2.getId()).isEqualTo(2); + QProfileDto dto2 = dtos.get(1); assertThat(dto2.getName()).isEqualTo("Child2"); assertThat(dto2.getLanguage()).isEqualTo("java"); assertThat(dto2.getParentKee()).isEqualTo("java_parent"); + + underTest.deleteByUuids(dbSession, Arrays.asList("java_parent", "java_child1", "java_child2", "js_parent", "js_child1", "js_child2")); } @Test public void countProjectsByProfileKey() { - RulesProfileDto profileWithoutProjects = dbTester.qualityProfiles().insert(organization); - RulesProfileDto profileWithProjects = dbTester.qualityProfiles().insert(organization); + QProfileDto profileWithoutProjects = dbTester.qualityProfiles().insert(organization); + QProfileDto profileWithProjects = dbTester.qualityProfiles().insert(organization); ComponentDto project1 = dbTester.components().insertPrivateProject(organization); ComponentDto project2 = dbTester.components().insertPrivateProject(organization); - dbTester.qualityProfiles().associateProjectWithQualityProfile(project1, profileWithProjects); - dbTester.qualityProfiles().associateProjectWithQualityProfile(project2, profileWithProjects); + dbTester.qualityProfiles().associateWithProject(project1, profileWithProjects); + dbTester.qualityProfiles().associateWithProject(project2, profileWithProjects); OrganizationDto otherOrg = dbTester.organizations().insert(); - RulesProfileDto profileInOtherOrg = dbTester.qualityProfiles().insert(otherOrg); + QProfileDto profileInOtherOrg = dbTester.qualityProfiles().insert(otherOrg); ComponentDto projectInOtherOrg = dbTester.components().insertPrivateProject(otherOrg); - dbTester.qualityProfiles().associateProjectWithQualityProfile(projectInOtherOrg, profileInOtherOrg); + dbTester.qualityProfiles().associateWithProject(projectInOtherOrg, profileInOtherOrg); - assertThat(underTest.countProjectsByProfileKey(dbTester.getSession(), organization)).containsOnly( + assertThat(underTest.countProjectsByProfileUuid(dbSession, organization)).containsOnly( MapEntry.entry(profileWithProjects.getKee(), 2L)); } @@ -327,17 +455,17 @@ public class QualityProfileDaoTest { OrganizationDto org = dbTester.organizations().insert(); ComponentDto project1 = dbTester.components().insertPublicProject(org); ComponentDto project2 = dbTester.components().insertPublicProject(org); - RulesProfileDto javaProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("java")); - RulesProfileDto jsProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("js")); - dbTester.qualityProfiles().associateProjectWithQualityProfile(project1, javaProfile, jsProfile); + QProfileDto javaProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("java")); + QProfileDto jsProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("js")); + dbTester.qualityProfiles().associateWithProject(project1, javaProfile, jsProfile); - assertThat(underTest.selectAssociatedToProjectAndLanguage(dbTester.getSession(), project1, "java").getKee()) + assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project1, "java").getKee()) .isEqualTo(javaProfile.getKee()); - assertThat(underTest.selectAssociatedToProjectAndLanguage(dbTester.getSession(), project1, "js").getKee()) + assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project1, "js").getKee()) .isEqualTo(jsProfile.getKee()); - assertThat(underTest.selectAssociatedToProjectAndLanguage(dbTester.getSession(), project1, "cobol")) + assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project1, "cobol")) .isNull(); - assertThat(underTest.selectAssociatedToProjectAndLanguage(dbTester.getSession(), project2, "java")) + assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project2, "java")) .isNull(); } @@ -346,19 +474,19 @@ public class QualityProfileDaoTest { OrganizationDto org = dbTester.organizations().insert(); ComponentDto project1 = dbTester.components().insertPublicProject(org); ComponentDto project2 = dbTester.components().insertPublicProject(org); - RulesProfileDto javaProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("java")); - RulesProfileDto jsProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("js")); - dbTester.qualityProfiles().associateProjectWithQualityProfile(project1, javaProfile, jsProfile); + QProfileDto javaProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("java")); + QProfileDto jsProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("js")); + dbTester.qualityProfiles().associateWithProject(project1, javaProfile, jsProfile); - assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbTester.getSession(), project1, singletonList("java"))) - .extracting(RulesProfileDto::getKee).containsOnly(javaProfile.getKee()); - assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbTester.getSession(), project1, singletonList("unknown"))) + assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbSession, project1, singletonList("java"))) + .extracting(QProfileDto::getKee).containsOnly(javaProfile.getKee()); + assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbSession, project1, singletonList("unknown"))) .isEmpty(); - assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbTester.getSession(), project1, of("java", "unknown"))) - .extracting(RulesProfileDto::getKee).containsExactly(javaProfile.getKee()); - assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbTester.getSession(), project1, of("java", "js"))) - .extracting(RulesProfileDto::getKee).containsExactlyInAnyOrder(javaProfile.getKee(), jsProfile.getKee()); - assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbTester.getSession(), project2, singletonList("java"))) + assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbSession, project1, of("java", "unknown"))) + .extracting(QProfileDto::getKee).containsExactly(javaProfile.getKee()); + assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbSession, project1, of("java", "js"))) + .extracting(QProfileDto::getKee).containsExactlyInAnyOrder(javaProfile.getKee(), jsProfile.getKee()); + assertThat(underTest.selectAssociatedToProjectUuidAndLanguages(dbSession, project2, singletonList("java"))) .isEmpty(); } @@ -366,10 +494,10 @@ public class QualityProfileDaoTest { public void test_updateProjectProfileAssociation() { OrganizationDto org = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(org); - RulesProfileDto javaProfile1 = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("java")); - RulesProfileDto jsProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("js")); - RulesProfileDto javaProfile2 = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("java")); - qualityProfileDb.associateProjectWithQualityProfile(project, javaProfile1, jsProfile); + QProfileDto javaProfile1 = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("java")); + QProfileDto jsProfile = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("js")); + QProfileDto javaProfile2 = dbTester.qualityProfiles().insert(org, p -> p.setLanguage("java")); + dbTester.qualityProfiles().associateWithProject(project, javaProfile1, jsProfile); underTest.updateProjectProfileAssociation(dbSession, project, javaProfile2.getKee(), javaProfile1.getKee()); @@ -379,15 +507,15 @@ public class QualityProfileDaoTest { @Test public void selectByKeys() { - qualityProfileDb.insertQualityProfiles(newQualityProfileDto().setKey("qp-key-1"), newQualityProfileDto().setKee("qp-key-2"), newQualityProfileDto().setKee("qp-key-3")); + dbTester.qualityProfiles().insert(newQualityProfileDto().setKey("qp-key-1"), newQualityProfileDto().setKee("qp-key-2"), newQualityProfileDto().setKee("qp-key-3")); - assertThat(underTest.selectOrFailByKey(dbSession, "qp-key-1")).isNotNull(); - assertThat(underTest.selectByKey(dbSession, "qp-key-1")).isNotNull(); - assertThat(underTest.selectByKey(dbSession, "qp-key-42")).isNull(); - assertThat(underTest.selectByKeys(dbSession, newArrayList("qp-key-1", "qp-key-3", "qp-key-42"))) + assertThat(underTest.selectOrFailByUuid(dbSession, "qp-key-1")).isNotNull(); + assertThat(underTest.selectByUuid(dbSession, "qp-key-1")).isNotNull(); + assertThat(underTest.selectByUuid(dbSession, "qp-key-42")).isNull(); + assertThat(underTest.selectByUuids(dbSession, newArrayList("qp-key-1", "qp-key-3", "qp-key-42"))) .hasSize(2) - .extracting(RulesProfileDto::getKee).containsOnlyOnce("qp-key-1", "qp-key-3"); - assertThat(underTest.selectByKeys(dbSession, emptyList())).isEmpty(); + .extracting(QProfileDto::getKee).containsOnlyOnce("qp-key-1", "qp-key-3"); + assertThat(underTest.selectByUuids(dbSession, emptyList())).isEmpty(); } @Test @@ -398,23 +526,24 @@ public class QualityProfileDaoTest { OrganizationDto organization2 = dbTester.organizations().insert(); ComponentDto project4 = dbTester.components().insertPrivateProject(t -> t.setName("Project4 name"), t -> t.setOrganizationUuid(organization2.getUuid())); - RulesProfileDto profile1 = newQualityProfileDto(); - qualityProfileDb.insertQualityProfiles(profile1); - qualityProfileDb.associateProjectWithQualityProfile(project1, profile1); - qualityProfileDb.associateProjectWithQualityProfile(project2, profile1); + QProfileDto profile1 = newQualityProfileDto(); + dbTester.qualityProfiles().insert(profile1); + dbTester.qualityProfiles().associateWithProject(project1, profile1); + dbTester.qualityProfiles().associateWithProject(project2, profile1); - RulesProfileDto profile2 = newQualityProfileDto(); - qualityProfileDb.insertQualityProfiles(profile2); - qualityProfileDb.associateProjectWithQualityProfile(project3, profile2); + QProfileDto profile2 = newQualityProfileDto(); + dbTester.qualityProfiles().insert(profile2); + dbTester.qualityProfiles().associateWithProject(project3, profile2); + QProfileDto profile3 = newQualityProfileDto(); - assertThat(underTest.selectSelectedProjects(organization, profile1.getKee(), null, dbSession)) + assertThat(underTest.selectSelectedProjects(dbSession, organization, profile1, null)) .extracting("projectId", "projectUuid", "projectKey", "projectName", "profileKey") .containsOnly( tuple(project1.getId(), project1.uuid(), project1.key(), project1.name(), profile1.getKee()), tuple(project2.getId(), project2.uuid(), project2.key(), project2.name(), profile1.getKee())); - assertThat(underTest.selectSelectedProjects(organization, profile1.getKee(), "ect1", dbSession)).hasSize(1); - assertThat(underTest.selectSelectedProjects(organization, "unknown", null, dbSession)).isEmpty(); + assertThat(underTest.selectSelectedProjects(dbSession, organization, profile1, "ect1")).hasSize(1); + assertThat(underTest.selectSelectedProjects(dbSession, organization, profile3, null)).isEmpty(); } @Test @@ -425,22 +554,23 @@ public class QualityProfileDaoTest { OrganizationDto organization2 = dbTester.organizations().insert(); ComponentDto project4 = dbTester.components().insertPrivateProject(t -> t.setName("Project4 name"), t -> t.setOrganizationUuid(organization2.getUuid())); - RulesProfileDto profile1 = newQualityProfileDto(); - qualityProfileDb.insertQualityProfiles(profile1); - qualityProfileDb.associateProjectWithQualityProfile(project1, profile1); + QProfileDto profile1 = newQualityProfileDto(); + dbTester.qualityProfiles().insert(profile1); + dbTester.qualityProfiles().associateWithProject(project1, profile1); - RulesProfileDto profile2 = newQualityProfileDto(); - qualityProfileDb.insertQualityProfiles(profile2); - qualityProfileDb.associateProjectWithQualityProfile(project2, profile2); + QProfileDto profile2 = newQualityProfileDto(); + dbTester.qualityProfiles().insert(profile2); + dbTester.qualityProfiles().associateWithProject(project2, profile2); + QProfileDto profile3 = newQualityProfileDto(); - assertThat(underTest.selectDeselectedProjects(organization, profile1.getKee(), null, dbSession)) + assertThat(underTest.selectDeselectedProjects(dbSession, organization, profile1, null)) .extracting("projectId", "projectUuid", "projectKey", "projectName", "profileKey") .containsExactly( tuple(project2.getId(), project2.uuid(), project2.key(), project2.name(), null), tuple(project3.getId(), project3.uuid(), project3.key(), project3.name(), null)); - assertThat(underTest.selectDeselectedProjects(organization, profile1.getKee(), "ect2", dbSession)).hasSize(1); - assertThat(underTest.selectDeselectedProjects(organization, "unknown", null, dbSession)).hasSize(3); + assertThat(underTest.selectDeselectedProjects(dbSession, organization, profile1, "ect2")).hasSize(1); + assertThat(underTest.selectDeselectedProjects(dbSession, organization, profile3, null)).hasSize(3); } @Test @@ -451,23 +581,24 @@ public class QualityProfileDaoTest { OrganizationDto organization2 = dbTester.organizations().insert(); ComponentDto project4 = dbTester.components().insertPrivateProject(t -> t.setName("Project4 name"), t -> t.setOrganizationUuid(organization2.getUuid())); - RulesProfileDto profile1 = newQualityProfileDto(); - qualityProfileDb.insertQualityProfiles(profile1); - qualityProfileDb.associateProjectWithQualityProfile(project1, profile1); + QProfileDto profile1 = newQualityProfileDto(); + dbTester.qualityProfiles().insert(profile1); + dbTester.qualityProfiles().associateWithProject(project1, profile1); - RulesProfileDto profile2 = newQualityProfileDto(); - qualityProfileDb.insertQualityProfiles(profile2); - qualityProfileDb.associateProjectWithQualityProfile(project2, profile2); + QProfileDto profile2 = newQualityProfileDto(); + dbTester.qualityProfiles().insert(profile2); + dbTester.qualityProfiles().associateWithProject(project2, profile2); + QProfileDto profile3 = newQualityProfileDto(); - assertThat(underTest.selectProjectAssociations(organization, profile1.getKee(), null, dbSession)) + assertThat(underTest.selectProjectAssociations(dbSession, organization, profile1, null)) .extracting("projectId", "projectUuid", "projectKey", "projectName", "profileKey") .containsOnly( tuple(project1.getId(), project1.uuid(), project1.key(), project1.name(), profile1.getKee()), tuple(project2.getId(), project2.uuid(), project2.key(), project2.name(), null), tuple(project3.getId(), project3.uuid(), project3.key(), project3.name(), null)); - assertThat(underTest.selectProjectAssociations(organization, profile1.getKee(), "ect2", dbSession)).hasSize(1); - assertThat(underTest.selectProjectAssociations(organization, "unknown", null, dbSession)).hasSize(3); + assertThat(underTest.selectProjectAssociations(dbSession, organization, profile1, "ect2")).hasSize(1); + assertThat(underTest.selectProjectAssociations(dbSession, organization, profile3, null)).hasSize(3); } @Test @@ -475,54 +606,75 @@ public class QualityProfileDaoTest { OrganizationDto org1 = dbTester.organizations().insert(); OrganizationDto org2 = dbTester.organizations().insert(); OrganizationDto org3 = dbTester.organizations().insert(); - RulesProfileDto outdatedProfile1 = dbTester.qualityProfiles().insert(org1, p -> p.setIsBuiltIn(false).setLanguage("java").setName("foo")); - RulesProfileDto outdatedProfile2 = dbTester.qualityProfiles().insert(org2, p -> p.setIsBuiltIn(false).setLanguage("java").setName("foo")); - RulesProfileDto builtInProfile = dbTester.qualityProfiles().insert(org3, p -> p.setIsBuiltIn(true).setLanguage("java").setName("foo")); - RulesProfileDto differentLanguage = dbTester.qualityProfiles().insert(org1, p -> p.setIsBuiltIn(false).setLanguage("cobol").setName("foo")); - RulesProfileDto differentName = dbTester.qualityProfiles().insert(org1, p -> p.setIsBuiltIn(false).setLanguage("java").setName("bar")); + QProfileDto outdatedProfile1 = dbTester.qualityProfiles().insert(org1, p -> p.setIsBuiltIn(false).setLanguage("java").setName("foo")); + QProfileDto outdatedProfile2 = dbTester.qualityProfiles().insert(org2, p -> p.setIsBuiltIn(false).setLanguage("java").setName("foo")); + QProfileDto builtInProfile = dbTester.qualityProfiles().insert(org3, p -> p.setIsBuiltIn(true).setLanguage("java").setName("foo")); + QProfileDto differentLanguage = dbTester.qualityProfiles().insert(org1, p -> p.setIsBuiltIn(false).setLanguage("cobol").setName("foo")); + QProfileDto differentName = dbTester.qualityProfiles().insert(org1, p -> p.setIsBuiltIn(false).setLanguage("java").setName("bar")); - Collection<String> keys = underTest.selectOutdatedProfiles(dbSession, "java", "foo"); + Collection<String> keys = underTest.selectUuidsOfCustomRulesProfiles(dbSession, "java", "foo"); assertThat(keys).containsExactlyInAnyOrder(outdatedProfile1.getKee(), outdatedProfile2.getKee()); } @Test public void selectOutdatedProfiles_returns_empty_list_if_no_match() { - assertThat(underTest.selectOutdatedProfiles(dbSession, "java", "foo")).isEmpty(); + assertThat(underTest.selectUuidsOfCustomRulesProfiles(dbSession, "java", "foo")).isEmpty(); } @Test public void renameAndCommit_updates_name_of_specified_profiles() { OrganizationDto org1 = dbTester.organizations().insert(); OrganizationDto org2 = dbTester.organizations().insert(); - RulesProfileDto fooInOrg1 = dbTester.qualityProfiles().insert(org1, p -> p.setName("foo")); - RulesProfileDto fooInOrg2 = dbTester.qualityProfiles().insert(org2, p -> p.setName("foo")); - RulesProfileDto bar = dbTester.qualityProfiles().insert(org1, p -> p.setName("bar")); + QProfileDto fooInOrg1 = dbTester.qualityProfiles().insert(org1, p -> p.setName("foo")); + QProfileDto fooInOrg2 = dbTester.qualityProfiles().insert(org2, p -> p.setName("foo")); + QProfileDto bar = dbTester.qualityProfiles().insert(org1, p -> p.setName("bar")); - underTest.renameAndCommit(dbSession, asList(fooInOrg1.getKee(), fooInOrg2.getKee()), "foo (copy)"); + underTest.renameRulesProfilesAndCommit(dbSession, asList(fooInOrg1.getKee(), fooInOrg2.getKee()), "foo (copy)"); - assertThat(underTest.selectOrFailByKey(dbSession, fooInOrg1.getKee()).getName()).isEqualTo("foo (copy)"); - assertThat(underTest.selectOrFailByKey(dbSession, fooInOrg2.getKee()).getName()).isEqualTo("foo (copy)"); - assertThat(underTest.selectOrFailByKey(dbSession, bar.getKee()).getName()).isEqualTo("bar"); + assertThat(underTest.selectOrFailByUuid(dbSession, fooInOrg1.getKee()).getName()).isEqualTo("foo (copy)"); + assertThat(underTest.selectOrFailByUuid(dbSession, fooInOrg2.getKee()).getName()).isEqualTo("foo (copy)"); + assertThat(underTest.selectOrFailByUuid(dbSession, bar.getKee()).getName()).isEqualTo("bar"); } @Test public void renameAndCommit_does_nothing_if_empty_keys() { OrganizationDto org = dbTester.organizations().insert(); - RulesProfileDto profile = dbTester.qualityProfiles().insert(org, p -> p.setName("foo")); + QProfileDto profile = dbTester.qualityProfiles().insert(org, p -> p.setName("foo")); - underTest.renameAndCommit(dbSession, Collections.emptyList(), "foo (copy)"); + underTest.renameRulesProfilesAndCommit(dbSession, Collections.emptyList(), "foo (copy)"); - assertThat(underTest.selectOrFailByKey(dbSession, profile.getKee()).getName()).isEqualTo("foo"); + assertThat(underTest.selectOrFailByUuid(dbSession, profile.getKee()).getName()).isEqualTo("foo"); } - private RulesProfileDto insertQualityProfileDto(String key, String name, String language) { - RulesProfileDto dto = RulesProfileDto.createFor(key) + private List<QProfileDto> createSharedData() { + QProfileDto dto1 = QProfileDto.createFor("java_sonar_way") .setOrganizationUuid(organization.getUuid()) - .setName(name) - .setLanguage(language); - underTest.insert(dbSession, dto); - return dto; + .setName("Sonar Way") + .setLanguage("java") + .setLastUsed(1_000L) + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(true); + underTest.insert(dbSession, dto1); + + QProfileDto dto2 = QProfileDto.createFor("js_sonar_way") + .setOrganizationUuid(organization.getUuid()) + .setName("Sonar Way") + .setLanguage("js") + .setLastUsed(1_000L) + .setUserUpdatedAt(2_000L) + .setRulesUpdatedAt("2017-05-31") + .setIsBuiltIn(true); + underTest.insert(dbSession, dto2); + + DefaultQProfileDto defaultQProfileDto = new DefaultQProfileDto() + .setQProfileUuid(dto1.getKee()) + .setLanguage(dto1.getLanguage()) + .setOrganizationUuid(organization.getUuid()); + dbTester.getDbClient().defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(dto1)); + + return Arrays.asList(dto1, dto2); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDbTester.java index 562f925b2ca..02c53fad43d 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDbTester.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDbTester.java @@ -22,6 +22,7 @@ package org.sonar.db.qualityprofile; import java.util.Arrays; import java.util.Optional; import java.util.function.Consumer; +import org.sonar.api.rule.Severity; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; @@ -29,93 +30,82 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.rule.RuleDefinitionDto; -import static org.sonar.api.rule.Severity.MAJOR; +import static org.apache.commons.lang.math.RandomUtils.nextInt; +import static org.apache.commons.lang.math.RandomUtils.nextLong; import static org.sonar.db.qualityprofile.ActiveRuleDto.createFor; public class QualityProfileDbTester { - private final DbTester dbTester; private final DbClient dbClient; private final DbSession dbSession; public QualityProfileDbTester(DbTester dbTester) { - this.dbTester = dbTester; this.dbClient = dbTester.getDbClient(); this.dbSession = dbTester.getSession(); } - public Optional<RulesProfileDto> selectByKey(String key) { - return Optional.ofNullable(dbClient.qualityProfileDao().selectByKey(dbSession, key)); + public Optional<QProfileDto> selectByUuid(String uuid) { + return Optional.ofNullable(dbClient.qualityProfileDao().selectByUuid(dbSession, uuid)); } /** * Create a profile with random field values on the specified organization. */ - @SafeVarargs - public final RulesProfileDto insert(OrganizationDto organization, Consumer<RulesProfileDto>... consumers) { - RulesProfileDto profile = QualityProfileTesting.newQualityProfileDto() + public QProfileDto insert(OrganizationDto organization) { + return insert(organization, c -> { + }); + } + + /** + * Create a profile with random field values on the specified organization. + */ + public QProfileDto insert(OrganizationDto organization, Consumer<QProfileDto> consumer) { + QProfileDto profile = QualityProfileTesting.newQualityProfileDto() .setOrganizationUuid(organization.getUuid()); - Arrays.stream(consumers).forEach(c -> c.accept(profile)); + consumer.accept(profile); dbClient.qualityProfileDao().insert(dbSession, profile); dbSession.commit(); return profile; } - public void insertQualityProfiles(RulesProfileDto qualityProfile, RulesProfileDto... qualityProfiles) { - dbClient.qualityProfileDao().insert(dbSession, qualityProfile, qualityProfiles); - dbSession.commit(); - } - - public RulesProfileDto insertQualityProfile(RulesProfileDto qualityProfile) { - dbClient.qualityProfileDao().insert(dbSession, qualityProfile); + public QualityProfileDbTester insert(QProfileDto profile, QProfileDto... others) { + dbClient.qualityProfileDao().insert(dbSession, profile); + Arrays.stream(others).forEach(p -> dbClient.qualityProfileDao().insert(dbSession, p)); dbSession.commit(); - return qualityProfile; + return this; } - public void insertProjectWithQualityProfileAssociations(ComponentDto project, RulesProfileDto... qualityProfiles) { - dbClient.componentDao().insert(dbSession, project); - for (RulesProfileDto qualityProfile : qualityProfiles) { - dbClient.qualityProfileDao().insertProjectProfileAssociation(dbSession, project, qualityProfile); + public QualityProfileDbTester associateWithProject(ComponentDto project, QProfileDto profile, QProfileDto... otherProfiles) { + dbClient.qualityProfileDao().insertProjectProfileAssociation(dbSession, project, profile); + for (QProfileDto p : otherProfiles) { + dbClient.qualityProfileDao().insertProjectProfileAssociation(dbSession, project, p); } dbSession.commit(); + return this; } - public void associateProjectWithQualityProfile(ComponentDto project, RulesProfileDto... qualityProfiles) { - for (RulesProfileDto qualityProfile : qualityProfiles) { - dbClient.qualityProfileDao().insertProjectProfileAssociation(dbSession, project, qualityProfile); - } - dbSession.commit(); + public ActiveRuleDto activateRule(QProfileDto profile, RuleDefinitionDto rule) { + return activateRule(profile, rule, ar -> { + }); } - @SafeVarargs - public final ActiveRuleDto activateRule(RulesProfileDto profile, RuleDefinitionDto rule, Consumer<ActiveRuleDto>... consumers) { - ActiveRuleDto activeRule = createFor(profile, rule).setSeverity(MAJOR); - for (Consumer<ActiveRuleDto> consumer : consumers) { - consumer.accept(activeRule); - } + public ActiveRuleDto activateRule(QProfileDto profile, RuleDefinitionDto rule, Consumer<ActiveRuleDto> consumer) { + ActiveRuleDto activeRule = createFor(profile, rule) + .setSeverity(Severity.ALL.get(nextInt(Severity.ALL.size()))) + .setCreatedAt(nextLong()) + .setUpdatedAt(nextLong()); + consumer.accept(activeRule); dbClient.activeRuleDao().insert(dbSession, activeRule); dbSession.commit(); return activeRule; } - public void markAsDefault(RulesProfileDto... profiles) { - for (RulesProfileDto profile : profiles) { - DefaultQProfileDto dto = new DefaultQProfileDto() - .setOrganizationUuid(profile.getOrganizationUuid()) - .setLanguage(profile.getLanguage()) - .setQProfileUuid(profile.getKee()); - dbClient.defaultQProfileDao().insertOrUpdate(dbSession, dto); + public QualityProfileDbTester setAsDefault(QProfileDto profile, QProfileDto... others) { + dbClient.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(profile)); + for (QProfileDto other : others) { + dbClient.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(other)); } - dbSession.commit(); - } - - public Optional<String> selectUuidOfDefaultProfile(OrganizationDto org, String language) { - return dbTester.select("select qprofile_uuid as \"profileUuid\" " + - " from default_qprofiles " + - " where organization_uuid='" + org.getUuid() + "' and language='" + language + "'") - .stream() - .findFirst() - .map(m -> (String)m.get("profileUuid")); + return this; } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileTesting.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileTesting.java index 3c1c9c1dec5..31625394af9 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileTesting.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileTesting.java @@ -20,25 +20,32 @@ package org.sonar.db.qualityprofile; import org.sonar.core.util.Uuids; -import org.sonar.db.DbSession; -import org.sonar.db.DbTester; -import static java.util.Arrays.stream; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.apache.commons.lang.math.RandomUtils.nextLong; public class QualityProfileTesting { - public static RulesProfileDto newQualityProfileDto() { + private QualityProfileTesting() { + // prevent instantiation + } + + /** + * Create an instance of {@link QProfileDto} with random field values. + */ + public static QProfileDto newQualityProfileDto() { String uuid = Uuids.createFast(); - RulesProfileDto dto = RulesProfileDto.createFor(uuid) + return QProfileDto.createFor(uuid) .setOrganizationUuid(randomAlphanumeric(40)) .setName(uuid) .setLanguage(randomAlphanumeric(20)) .setLastUsed(nextLong()); - return dto; } + /** + * Create an instance of {@link QProfileChangeDto} with random field values, + * except changeType which is always {@code "ACTIVATED"}. + */ public static QProfileChangeDto newQProfileChangeDto() { return new QProfileChangeDto() .setKey(randomAlphanumeric(40)) @@ -47,13 +54,4 @@ public class QualityProfileTesting { .setChangeType("ACTIVATED") .setLogin(randomAlphanumeric(10)); } - - public static void insert(DbTester dbTester, QProfileChangeDto... dtos) { - // do not use QProfileChangeDao so that generated fields key and creation date - // can be defined by tests - DbSession dbSession = dbTester.getSession(); - QProfileChangeMapper mapper = dbSession.getMapper(QProfileChangeMapper.class); - stream(dtos).forEach(mapper::insert); - dbSession.commit(); - } } diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/delete-result.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/delete-result.xml deleted file mode 100644 index 691735eb14d..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/delete-result.xml +++ /dev/null @@ -1,16 +0,0 @@ -<dataset> - - <rules_profiles id="2" - name="Sonar Way" - language="js" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="js_sonar_way" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]" - last_used="123456789" - user_updated_at="987654321"/> - -</dataset> diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/inheritance.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/inheritance.xml deleted file mode 100644 index 8f063f02d18..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/inheritance.xml +++ /dev/null @@ -1,71 +0,0 @@ -<dataset> - - <rules_profiles id="1" - name="Child1" - language="java" - organization_uuid="org-123" - parent_kee="java_parent" - kee="java_child1" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - <rules_profiles id="2" - name="Child2" - language="java" - organization_uuid="org-123" - parent_kee="java_parent" - kee="java_child2" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - <rules_profiles id="3" - name="Parent" - language="java" - organization_uuid="org-123" - parent_kee="[null]" - kee="java_parent" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - <!-- Same profile for another language --> - - <rules_profiles id="4" - name="Child1" - language="js" - organization_uuid="org-123" - parent_kee="js_parent" - kee="js_child1" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - <rules_profiles id="5" - name="Child2" - language="js" - organization_uuid="org-123" - parent_kee="js_parent" - kee="js_child2" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - <rules_profiles id="6" - name="Parent" - language="js" - organization_uuid="org-123" - parent_kee="[null]" - kee="js_parent" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - -</dataset> diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/insert-result.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/insert-result.xml deleted file mode 100644 index 1e4b2849681..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/insert-result.xml +++ /dev/null @@ -1,43 +0,0 @@ -<dataset> - - <rules_profiles id="1" - name="Sonar Way" - language="java" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="java_sonar_way" - is_built_in="[true]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]" - last_used="[null]" - user_updated_at="[null]"/> - - <rules_profiles id="2" - name="Sonar Way" - language="js" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="js_sonar_way" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]" - last_used="123456789" - user_updated_at="987654321"/> - - <rules_profiles id="3" - name="ABCDE" - language="xoo" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="abcde" - is_built_in="[true]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]" - last_used="[null]" - user_updated_at="[null]"/> - - -</dataset> diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/select_all_is_sorted_by_profile_name.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/select_all_is_sorted_by_profile_name.xml deleted file mode 100644 index 42c8a4f64ab..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/select_all_is_sorted_by_profile_name.xml +++ /dev/null @@ -1,37 +0,0 @@ -<dataset> - - <rules_profiles id="3" - name="Third" - language="js" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="js_third" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - <rules_profiles id="1" - name="First" - language="js" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="js_first" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - <rules_profiles id="2" - name="Second" - language="js" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="js_second" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - -</dataset> diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/select_by_language.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/select_by_language.xml deleted file mode 100644 index 3d5814908ca..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/select_by_language.xml +++ /dev/null @@ -1,36 +0,0 @@ -<dataset> - - <rules_profiles id="1" - name="Sonar Way 1" - language="java" - organization_uuid="org-123" - parent_kee="[null]" - kee="java_sonar_way" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - <rules_profiles id="2" - name="Sonar Way" - language="js" - organization_uuid="org-123" - parent_kee="[null]" - kee="js_sonar_way" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - - <rules_profiles id="3" - name="Sonar Way 2" - language="java" - organization_uuid="org-123" - parent_kee="[null]" - kee="java_sonar_way2" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]"/> - -</dataset> diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/shared.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/shared.xml deleted file mode 100644 index 7872802c029..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/shared.xml +++ /dev/null @@ -1,37 +0,0 @@ -<dataset> - - <rules_profiles id="1" - name="Sonar Way" - language="java" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="java_sonar_way" - is_built_in="[true]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]" - last_used="[null]" - user_updated_at="[null]"/> - - <rules_profiles id="2" - name="Sonar Way" - language="js" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="js_sonar_way" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]" - last_used="123456789" - user_updated_at="987654321"/> - - <default_qprofiles - organization_uuid="QualityProfileDaoTest-ORG" - language="java" - qprofile_uuid="java_sonar_way" - created_at="1000" - updated_at="2000" - /> - -</dataset> diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/update-result.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/update-result.xml deleted file mode 100644 index d37e24c2e62..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualityprofile/QualityProfileDaoTest/update-result.xml +++ /dev/null @@ -1,28 +0,0 @@ -<dataset> - - <rules_profiles id="1" - name="New Name" - language="js" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="fghij" - kee="java_sonar_way" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]" - last_used="[null]" - user_updated_at="[null]"/> - - <rules_profiles id="2" - name="Sonar Way" - language="js" - organization_uuid="QualityProfileDaoTest-ORG" - parent_kee="[null]" - kee="js_sonar_way" - is_built_in="[false]" - rules_updated_at="[null]" - created_at="[null]" - updated_at="[null]" - last_used="123456789" - user_updated_at="987654321"/> -</dataset> |