From 9c8d12d3daa2702ab555ef8582fcd228a106690b Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Fri, 8 Dec 2017 17:58:33 +0100 Subject: [PATCH] SONAR-10136 Update tests with BuiltIn QG already created --- .../OrganizationCreationImplTest.java | 26 ++--------- .../qualitygate/RegisterQualityGatesTest.java | 46 ++++++++----------- 2 files changed, 22 insertions(+), 50 deletions(-) diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/OrganizationCreationImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/OrganizationCreationImplTest.java index fb2dd0a1c55..a9c8e40447a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/OrganizationCreationImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/OrganizationCreationImplTest.java @@ -53,6 +53,7 @@ import org.sonar.db.user.UserMembershipDto; import org.sonar.db.user.UserMembershipQuery; import org.sonar.server.es.EsTester; import org.sonar.server.es.SearchOptions; +import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.qualityprofile.BuiltInQProfile; import org.sonar.server.qualityprofile.BuiltInQProfileRepositoryRule; import org.sonar.server.qualityprofile.QProfileName; @@ -107,6 +108,7 @@ public class OrganizationCreationImplTest { private UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); private UserIndex userIndex = new UserIndex(es.client(), system2); private DefaultGroupCreator defaultGroupCreator = new DefaultGroupCreatorImpl(dbClient); + private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient); private OrganizationCreationImpl underTest = new OrganizationCreationImpl(dbClient, system2, uuidFactory, organizationValidation, settings.asConfig(), userIndexer, builtInQProfileRepositoryRule, defaultGroupCreator); @@ -166,7 +168,6 @@ public class OrganizationCreationImplTest { @Test public void create_fails_with_ISE_if_BuiltInQProfileRepository_has_not_been_initialized() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("initialize must be called first"); @@ -186,7 +187,6 @@ public class OrganizationCreationImplTest { @Test public void create_creates_unguarded_organization_with_properties_from_NewOrganization_arg() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); builtInQProfileRepositoryRule.initialize(); underTest.create(dbSession, someUser, FULL_POPULATED_NEW_ORGANIZATION); @@ -206,7 +206,6 @@ public class OrganizationCreationImplTest { @Test public void create_creates_owners_group_with_all_permissions_for_new_organization_and_add_current_user_to_it() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); UserDto user = db.users().insertUser(); builtInQProfileRepositoryRule.initialize(); @@ -217,7 +216,6 @@ public class OrganizationCreationImplTest { @Test public void create_creates_members_group_and_add_current_user_to_it() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); UserDto user = db.users().insertUser(); builtInQProfileRepositoryRule.initialize(); @@ -228,7 +226,6 @@ public class OrganizationCreationImplTest { @Test public void create_does_not_require_description_url_and_avatar_to_be_non_null() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); builtInQProfileRepositoryRule.initialize(); underTest.create(dbSession, someUser, newOrganizationBuilder() @@ -248,7 +245,6 @@ public class OrganizationCreationImplTest { @Test public void create_creates_default_template_for_new_organization() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); builtInQProfileRepositoryRule.initialize(); underTest.create(dbSession, someUser, FULL_POPULATED_NEW_ORGANIZATION); @@ -271,7 +267,6 @@ public class OrganizationCreationImplTest { @Test public void create_add_current_user_as_member_of_organization() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); UserDto user = db.users().insertUser(); builtInQProfileRepositoryRule.initialize(); userIndexer.commitAndIndex(db.getSession(), someUser); @@ -284,7 +279,6 @@ public class OrganizationCreationImplTest { @Test public void create_associates_to_built_in_quality_profiles() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); BuiltInQProfile builtIn1 = builtInQProfileRepositoryRule.add(newLanguage("foo"), "qp1", true); BuiltInQProfile builtIn2 = builtInQProfileRepositoryRule.add(newLanguage("foo"), "qp2"); builtInQProfileRepositoryRule.initialize(); @@ -313,7 +307,7 @@ public class OrganizationCreationImplTest { @Test public void create_associates_to_built_in_quality_gate() throws OrganizationCreation.KeyConflictException { - QualityGateDto builtInQualityGate = insertBuiltInQualityGate(); + QualityGateDto builtInQualityGate = qualityGateFinder.getBuiltInQualityGate(dbSession); builtInQProfileRepositoryRule.initialize(); underTest.create(dbSession, someUser, FULL_POPULATED_NEW_ORGANIZATION); @@ -353,7 +347,6 @@ public class OrganizationCreationImplTest { @Test public void createForUser_creates_guarded_organization_with_key_name_and_description_generated_from_user_login_and_name_and_associated_to_user() { - insertBuiltInQualityGate(); UserDto user = db.users().insertUser(A_LOGIN); when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN); enableCreatePersonalOrg(true); @@ -390,7 +383,6 @@ public class OrganizationCreationImplTest { @Test public void createForUser_gives_all_permissions_for_new_organization_to_current_user() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); UserDto user = db.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME)); when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN); enableCreatePersonalOrg(true); @@ -405,7 +397,6 @@ public class OrganizationCreationImplTest { @Test public void createForUser_creates_members_group_and_add_current_user_to_it() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); UserDto user = db.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME)); when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN); enableCreatePersonalOrg(true); @@ -418,7 +409,6 @@ public class OrganizationCreationImplTest { @Test public void createForUser_creates_default_template_for_new_organization() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); UserDto user = db.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME)); when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN); enableCreatePersonalOrg(true); @@ -446,7 +436,6 @@ public class OrganizationCreationImplTest { @Test public void createForUser_add_current_user_as_member_of_organization() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); UserDto user = db.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME)); when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN); enableCreatePersonalOrg(true); @@ -460,7 +449,6 @@ public class OrganizationCreationImplTest { @Test public void createForUser_does_not_fail_if_name_is_too_long_for_an_organization_name() { - insertBuiltInQualityGate(); String nameTooLong = STRING_64_CHARS + "b"; UserDto user = db.users().insertUser(dto -> dto.setName(nameTooLong).setLogin(A_LOGIN)); when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN); @@ -476,7 +464,6 @@ public class OrganizationCreationImplTest { @Test public void createForUser_does_not_fail_if_name_is_empty_and_login_is_too_long_for_an_organization_name() { - insertBuiltInQualityGate(); String login = STRING_64_CHARS + "b"; UserDto user = db.users().insertUser(dto -> dto.setName("").setLogin(login)); when(organizationValidation.generateKeyFrom(login)).thenReturn(SLUG_OF_A_LOGIN); @@ -492,7 +479,6 @@ public class OrganizationCreationImplTest { @Test public void createForUser_does_not_fail_if_name_is_null_and_login_is_too_long_for_an_organization_name() { - insertBuiltInQualityGate(); String login = STRING_64_CHARS + "b"; UserDto user = db.users().insertUser(dto -> dto.setName(null).setLogin(login)); when(organizationValidation.generateKeyFrom(login)).thenReturn(SLUG_OF_A_LOGIN); @@ -508,7 +494,6 @@ public class OrganizationCreationImplTest { @Test public void createForUser_associates_to_built_in_quality_profiles() throws OrganizationCreation.KeyConflictException { - insertBuiltInQualityGate(); UserDto user = db.users().insertUser(A_LOGIN); when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN); enableCreatePersonalOrg(true); @@ -529,7 +514,7 @@ public class OrganizationCreationImplTest { @Test public void createForUser_associates_to_built_in_quality_gate() { - QualityGateDto builtInQualityGate = insertBuiltInQualityGate(); + QualityGateDto builtInQualityGate = qualityGateFinder.getBuiltInQualityGate(dbSession); UserDto user = db.users().insertUser(A_LOGIN); when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN); enableCreatePersonalOrg(true); @@ -586,8 +571,5 @@ public class OrganizationCreationImplTest { .containsOnly(user.getLogin()); } - private QualityGateDto insertBuiltInQualityGate(){ - return db.qualityGates().insertBuiltInQualityGate(); - } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/RegisterQualityGatesTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/RegisterQualityGatesTest.java index a4976339c72..21f576a81a4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/RegisterQualityGatesTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/RegisterQualityGatesTest.java @@ -77,10 +77,12 @@ public class RegisterQualityGatesTest { private RegisterQualityGates underTest = new RegisterQualityGates(dbClient, qualityGateUpdater, qualityGateConditionsUpdater, qualityGateFinder, UuidFactoryFast.getInstance(), System2.INSTANCE); + private QualityGateDto builtInQG; @Before public void setup() { insertMetrics(); + builtInQG = qualityGateFinder.getBuiltInQualityGate(dbSession); } @After @@ -90,6 +92,9 @@ public class RegisterQualityGatesTest { @Test public void register_default_gate() { + dbClient.qualityGateDao().delete(builtInQG, dbSession); + dbSession.commit(); + underTest.start(); verifyCorrectBuiltInQualityGate(); @@ -102,8 +107,6 @@ public class RegisterQualityGatesTest { @Test public void upgrade_empty_quality_gate() { - QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast()); - qualityGateDao.insert(dbSession, builtin); dbSession.commit(); underTest.start(); @@ -116,13 +119,10 @@ public class RegisterQualityGatesTest { @Test public void upgrade_should_remove_deleted_condition() { - QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast()); - qualityGateDao.insert(dbSession, builtin); - - createBuiltInConditions(builtin); + createBuiltInConditions(builtInQG); // Add another condition - qualityGateConditionsUpdater.createCondition(dbSession, builtin, + qualityGateConditionsUpdater.createCondition(dbSession, builtInQG, NEW_SECURITY_REMEDIATION_EFFORT_KEY, OPERATOR_GREATER_THAN, null, "5", LEAK_PERIOD); dbSession.commit(); @@ -136,10 +136,7 @@ public class RegisterQualityGatesTest { @Test public void upgrade_should_add_missing_condition() { - QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast()); - qualityGateDao.insert(dbSession, builtin); - - List builtInConditions = createBuiltInConditions(builtin); + List builtInConditions = createBuiltInConditions(builtInQG); // Remove a condition QualityGateConditionDto conditionToBeDeleted = builtInConditions.get(new Random().nextInt(builtInConditions.size())); @@ -157,9 +154,9 @@ public class RegisterQualityGatesTest { @Test public void should_set_SonarWay_as_builtin_when_not_set() { + dbClient.qualityGateDao().delete(builtInQG, dbSession); QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(false).setUuid(Uuids.createFast()); qualityGateDao.insert(dbSession, builtin); - createBuiltInConditions(builtin); dbSession.commit(); @@ -173,10 +170,7 @@ public class RegisterQualityGatesTest { @Test public void should_not_update_builtin_quality_gate_if_already_uptodate() { - QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast()); - qualityGateDao.insert(dbSession, builtin); - - createBuiltInConditions(builtin); + createBuiltInConditions(builtInQG); dbSession.commit(); underTest.start(); @@ -194,6 +188,7 @@ public class RegisterQualityGatesTest { @Test public void ensure_only_one_built_in_quality_gate() { + dbClient.qualityGateDao().delete(builtInQG, dbSession); String qualityGateName = "IncorrectQualityGate"; QualityGateDto builtin = new QualityGateDto().setName(qualityGateName).setBuiltIn(true).setUuid(Uuids.createFast()); qualityGateDao.insert(dbSession, builtin); @@ -217,14 +212,11 @@ public class RegisterQualityGatesTest { @Test public void ensure_only_that_builtin_is_set_as_default_when_no_default_quality_gate() { - QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast()); - qualityGateDao.insert(dbSession, builtin); - dbSession.commit(); underTest.start(); assertThat(qualityGateFinder.getDefault(dbSession)).isPresent(); - assertThat(qualityGateFinder.getDefault(dbSession).get().getId()).isEqualTo(builtin.getId()); + assertThat(qualityGateFinder.getDefault(dbSession).get().getId()).isEqualTo(builtInQG.getId()); assertThat( logTester.logs(LoggerLevel.INFO).contains("Built-in quality gate [Sonar way] has been set as default")).isTrue(); @@ -232,8 +224,6 @@ public class RegisterQualityGatesTest { @Test public void builtin_quality_gate_with_incorrect_metricId_should_not_throw_an_exception() { - QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast()); - qualityGateDao.insert(dbSession, builtin); QualityGateConditionDto conditionDto = new QualityGateConditionDto() .setMetricId(-1) // This Id does not exist .setOperator(OPERATOR_GREATER_THAN) @@ -283,18 +273,18 @@ public class RegisterQualityGatesTest { tuple(newDuplication.getId().longValue(), OPERATOR_GREATER_THAN, null, "3", 1)); } - private List createBuiltInConditions(QualityGateDto builtin) { + private List createBuiltInConditions(QualityGateDto qg) { List conditions = new ArrayList<>(); - conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin, + conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg, NEW_SECURITY_RATING_KEY, OPERATOR_GREATER_THAN, null, "1", LEAK_PERIOD)); - conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin, + conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg, NEW_RELIABILITY_RATING_KEY, OPERATOR_GREATER_THAN, null, "1", LEAK_PERIOD)); - conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin, + conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg, NEW_MAINTAINABILITY_RATING_KEY, OPERATOR_GREATER_THAN, null, "1", LEAK_PERIOD)); - conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin, + conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg, NEW_COVERAGE_KEY, OPERATOR_LESS_THAN, null, "80", LEAK_PERIOD)); - conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin, + conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg, NEW_DUPLICATED_LINES_DENSITY_KEY, OPERATOR_GREATER_THAN, null, "3", LEAK_PERIOD)); return conditions; -- 2.39.5