From 7404d8a4583948e6bf8e69f26211af8513b79068 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 16 Jun 2017 17:18:43 +0200 Subject: [PATCH] SONAR-9302 do not change current default profiles --- .../db/qualityprofile/DefaultQProfileDao.java | 7 ++++++- .../qualityprofile/DefaultQProfileMapper.java | 5 ++++- .../qualityprofile/DefaultQProfileMapper.xml | 6 ++++++ .../DefaultQProfileDaoTest.java | 14 ++++++++++++- .../BuiltInQProfileInsertImpl.java | 7 ++++--- .../BuiltInQProfileInsertImplTest.java | 20 +++++++++++++++++-- 6 files changed, 51 insertions(+), 8 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileDao.java index 0f62f0b2962..ee877765372 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileDao.java @@ -20,6 +20,7 @@ package org.sonar.db.qualityprofile; import java.util.Collection; +import java.util.HashSet; import java.util.Set; import org.sonar.api.utils.System2; import org.sonar.db.Dao; @@ -50,13 +51,17 @@ public class DefaultQProfileDao implements Dao { } public Set selectExistingQProfileUuids(DbSession dbSession, String organizationUuid, Collection qProfileUuids) { - return mapper(dbSession).selectExistingQProfileUuids(organizationUuid, qProfileUuids); + return new HashSet<>(DatabaseUtils.executeLargeInputs(qProfileUuids, uuids -> mapper(dbSession).selectExistingQProfileUuids(organizationUuid, uuids))); } public boolean isDefault(DbSession dbSession, String organizationUuid, String qProfileUuid) { return selectExistingQProfileUuids(dbSession, organizationUuid, singletonList(qProfileUuid)).contains(qProfileUuid); } + public Set selectUuidsOfOrganizationsWithoutDefaultProfile(DbSession dbSession, String language) { + return mapper(dbSession).selectUuidsOfOrganizationsWithoutDefaultProfile(language); + } + private static DefaultQProfileMapper mapper(DbSession dbSession) { return dbSession.getMapper(DefaultQProfileMapper.class); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileMapper.java index 6f69965c896..580ea4028e2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileMapper.java @@ -20,6 +20,7 @@ package org.sonar.db.qualityprofile; import java.util.Collection; +import java.util.List; import java.util.Set; import org.apache.ibatis.annotations.Param; @@ -30,7 +31,9 @@ public interface DefaultQProfileMapper { void deleteByQProfileUuids(@Param("qProfileUuids") Collection qProfileUuids); - Set selectExistingQProfileUuids( + List selectExistingQProfileUuids( @Param("organizationUuid") String organizationUuid, @Param("qProfileUuids") Collection qProfileUuids); + + Set selectUuidsOfOrganizationsWithoutDefaultProfile(@Param("language") String language); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/DefaultQProfileMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/DefaultQProfileMapper.xml index 71c6c32e225..60c6d8cc2ac 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/DefaultQProfileMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/DefaultQProfileMapper.xml @@ -48,5 +48,11 @@ #{qProfileUuid, jdbcType=VARCHAR} + + 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 f317d828e08..720487158de 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 @@ -35,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class DefaultQProfileDaoTest { @Rule - public DbTester dbTester = DbTester.create(System2.INSTANCE); + public DbTester dbTester = DbTester.create(System2.INSTANCE).setDisableDefaultOrganization(true); private DbSession dbSession = dbTester.getSession(); private DefaultQProfileDao underTest = dbTester.getDbClient().defaultQProfileDao(); @@ -116,6 +116,18 @@ public class DefaultQProfileDaoTest { assertThat(underTest.isDefault(dbSession, org.getUuid(), "does_not_exist")).isFalse(); } + @Test + public void selectUuidsOfOrganizationsWithoutDefaultProfile() { + OrganizationDto org1 = dbTester.organizations().insert(); + OrganizationDto org2 = dbTester.organizations().insert(); + QProfileDto profileInOrg1 = dbTester.qualityProfiles().insert(org1, p -> p.setLanguage("java")); + QProfileDto profileInOrg2 = dbTester.qualityProfiles().insert(org2, p -> p.setLanguage("java")); + dbTester.qualityProfiles().setAsDefault(profileInOrg1); + + //assertThat(underTest.selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, "java")).containsExactly(org2.getUuid()); + assertThat(underTest.selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, "js")).containsExactlyInAnyOrder(org1.getUuid(), org2.getUuid()); + } + private void assertThatIsDefault(OrganizationDto org, QProfileDto profile) { assertThat(selectUuidOfDefaultProfile(org, profile.getLanguage())).hasValue(profile.getKee()); assertThat(underTest.isDefault(dbSession, org.getUuid(), profile.getKee())).isTrue(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImpl.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImpl.java index f69f0877b09..4fc3efa1bda 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImpl.java @@ -96,8 +96,9 @@ public class BuiltInQProfileInsertImpl implements BuiltInQProfileInsert { activeRuleIndexer.indexRuleProfile(dbSession, ruleProfile); } - private void associateToOrganizations(DbSession dbSession, DbSession batchDbSession, BuiltInQProfile builtInQProfile, RulesProfileDto rulesProfileDto) { + private void associateToOrganizations(DbSession dbSession, DbSession batchDbSession, BuiltInQProfile builtIn, RulesProfileDto rulesProfileDto) { List orgUuids = dbClient.organizationDao().selectAllUuids(dbSession); + Set orgUuidsWithoutDefault = dbClient.defaultQProfileDao().selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, builtIn.getLanguage()); List defaults = new ArrayList<>(); orgUuids.forEach(orgUuid -> { @@ -106,13 +107,13 @@ public class BuiltInQProfileInsertImpl implements BuiltInQProfileInsert { .setRulesProfileUuid(rulesProfileDto.getKee()) .setUuid(uuidFactory.create()); - if (builtInQProfile.isDefault()) { + if (builtIn.isDefault() && orgUuidsWithoutDefault.contains(orgUuid)) { // rows of table default_qprofiles must be inserted after // in order to benefit from batch SQL inserts defaults.add(new DefaultQProfileDto() .setQProfileUuid(dto.getUuid()) .setOrganizationUuid(orgUuid) - .setLanguage(builtInQProfile.getLanguage())); + .setLanguage(builtIn.getLanguage())); } dbClient.qualityProfileDao().insert(batchDbSession, dto); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImplTest.java index 1398f9e4fb6..3d85ade5b77 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImplTest.java @@ -120,7 +120,7 @@ public class BuiltInQProfileInsertImplTest { } @Test - public void flag_profile_as_default_on_organizations_if_declared_as_default_by_api() { + public void flag_profile_as_default_on_organization_if_declared_as_default_by_api() { OrganizationDto org = db.organizations().insert(); RulesProfile apiProfile = RulesProfile.create("the name", "xoo"); apiProfile.setDefaultProfile(true); @@ -134,7 +134,23 @@ public class BuiltInQProfileInsertImplTest { } @Test - public void dont_flag_profile_as_default_on_organizations_if_not_declared_as_default_by_api() { + public void existing_default_profile_in_organization_must_not_be_changed() { + RulesProfile apiProfile = RulesProfile.create("the name", "xoo"); + apiProfile.setDefaultProfile(true); + BuiltInQProfile builtIn = builtInQProfileRepository.create(apiProfile); + + OrganizationDto org = db.organizations().insert(); + QProfileDto currentDefault = db.qualityProfiles().insert(org, p -> p.setLanguage(apiProfile.getLanguage())); + db.qualityProfiles().setAsDefault(currentDefault); + + call(builtIn); + + QProfileDto defaultProfile = db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, org, apiProfile.getLanguage()); + assertThat(defaultProfile.getKee()).isEqualTo(currentDefault.getKee()); + } + + @Test + public void dont_flag_profile_as_default_on_organization_if_not_declared_as_default_by_api() { OrganizationDto org = db.organizations().insert(); RulesProfile apiProfile = RulesProfile.create("the name", "xoo"); apiProfile.setDefaultProfile(false); -- 2.39.5