]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10136 Set built-in QG as default of new organization
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 7 Dec 2017 16:05:12 +0000 (17:05 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-db-dao/src/test/java/org/sonar/db/DbTester.java
server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java
server/sonar-server/src/test/java/org/sonar/server/organization/OrganizationCreationImplTest.java

index 7e7870e09ead53fc8ac7e01960395e19197724a3..e0d9e2322a281754ff2d3fcafb0c595ad410aada 100644 (file)
@@ -30,6 +30,7 @@ import org.apache.commons.lang.StringUtils;
 import org.picocontainer.containers.TransientPicoContainer;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.SequenceUuidFactory;
+import org.sonar.core.util.Uuids;
 import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.event.EventDbTester;
 import org.sonar.db.favorite.FavoriteDbTester;
@@ -156,7 +157,7 @@ public class DbTester extends AbstractDbTester<TestDb> {
     db.start();
     db.truncateTables();
     initDbClient();
-    // TODO : insertBuiltInQualityGateIfTableExists();
+    insertBuiltInQualityGateIfTableExists();
 
     if (!disableDefaultOrganization) {
       insertDefaultOrganization();
@@ -168,6 +169,7 @@ public class DbTester extends AbstractDbTester<TestDb> {
     try (DbSession dbSession = db.getMyBatis().openSession(false)) {
       if (DatabaseUtils.tableExists("quality_gates", dbSession.getConnection())) {
         builtInQualityGate = new QualityGateDto()
+          .setUuid(Uuids.createFast())
           .setName("Sonar way")
           .setBuiltIn(true)
           .setCreatedAt(new Date(system2.now()))
index fa5ff5a23e7ebad00976d644a30d2a6ee8d7a645..8a6464d685c34a1a4ef5983de5c58fdbbfe27b9b 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.db.permission.OrganizationPermission;
 import org.sonar.db.permission.UserPermissionDto;
 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
 import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.db.qualityprofile.DefaultQProfileDto;
 import org.sonar.db.qualityprofile.OrgQProfileDto;
 import org.sonar.db.user.GroupDto;
@@ -95,8 +96,10 @@ public class OrganizationCreationImpl implements OrganizationCreation {
       throw new KeyConflictException(format("Organization key '%s' is already used", key));
     }
 
-    OrganizationDto organization = insertOrganization(dbSession, newOrganization);
+    QualityGateDto builtInQualityGate = dbClient.qualityGateDao().selectBuiltIn(dbSession);
+    OrganizationDto organization = insertOrganization(dbSession, newOrganization, builtInQualityGate);
     insertOrganizationMember(dbSession, organization, userCreator.getId());
+    dbClient.qualityGateDao().associate(dbSession, uuidFactory.create(), organization, builtInQualityGate);
     GroupDto ownerGroup = insertOwnersGroup(dbSession, organization);
     GroupDto defaultGroup = defaultGroupCreator.create(dbSession, organization.getUuid());
     insertDefaultTemplateOnGroups(dbSession, organization, ownerGroup, defaultGroup);
@@ -131,10 +134,12 @@ public class OrganizationCreationImpl implements OrganizationCreation {
       newOrganization.getKey(),
       newUser.getLogin());
 
-    OrganizationDto organization = insertOrganization(dbSession, newOrganization,
+    QualityGateDto builtInQualityGate = dbClient.qualityGateDao().selectBuiltIn(dbSession);
+    OrganizationDto organization = insertOrganization(dbSession, newOrganization, builtInQualityGate,
       dto -> dto.setGuarded(true).setUserId(newUser.getId()));
     insertOrganizationMember(dbSession, organization, newUser.getId());
     GroupDto defaultGroup = defaultGroupCreator.create(dbSession, organization.getUuid());
+    dbClient.qualityGateDao().associate(dbSession, uuidFactory.create(), organization, builtInQualityGate);
     OrganizationPermission.all()
       .forEach(p -> insertUserPermissions(dbSession, newUser, organization, p));
     insertPersonalOrgDefaultTemplate(dbSession, organization, defaultGroup);
@@ -179,14 +184,14 @@ public class OrganizationCreationImpl implements OrganizationCreation {
     organizationValidation.checkAvatar(newOrganization.getAvatar());
   }
 
-  private OrganizationDto insertOrganization(DbSession dbSession, NewOrganization newOrganization, Consumer<OrganizationDto>... extendCreation) {
+  private OrganizationDto insertOrganization(DbSession dbSession, NewOrganization newOrganization, QualityGateDto builtInQualityGate, Consumer<OrganizationDto>... extendCreation) {
     OrganizationDto res = new OrganizationDto()
       .setUuid(uuidFactory.create())
       .setName(newOrganization.getName())
       .setKey(newOrganization.getKey())
       .setDescription(newOrganization.getDescription())
       .setUrl(newOrganization.getUrl())
-      // TODO .setDefaultQualityGateUuid("" + qualityGateFinder.getBuiltInQualityGate(dbSession).getId())
+      .setDefaultQualityGateUuid(builtInQualityGate.getUuid())
       .setAvatarUrl(newOrganization.getAvatar());
     Arrays.stream(extendCreation).forEach(c -> c.accept(res));
     dbClient.organizationDao().insert(dbSession, res, false);
index bb7c05a1df57d9d47b1818fe3129bcac90b1547e..fb2dd0a1c55b2763185ca0e4e2e081676d5ae6f1 100644 (file)
@@ -44,6 +44,7 @@ import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
 import org.sonar.db.permission.template.PermissionTemplateDto;
 import org.sonar.db.permission.template.PermissionTemplateGroupDto;
+import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.db.qualityprofile.QProfileDto;
 import org.sonar.db.qualityprofile.RulesProfileDto;
 import org.sonar.db.user.GroupDto;
@@ -52,7 +53,6 @@ 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;
@@ -166,6 +166,8 @@ 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");
 
@@ -184,6 +186,7 @@ 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);
@@ -203,6 +206,7 @@ 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();
 
@@ -213,6 +217,7 @@ 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();
 
@@ -223,6 +228,7 @@ 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()
@@ -242,6 +248,7 @@ 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);
@@ -264,6 +271,7 @@ 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);
@@ -276,6 +284,7 @@ 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();
@@ -302,6 +311,17 @@ public class OrganizationCreationImplTest {
     db.commit();
   }
 
+  @Test
+  public void create_associates_to_built_in_quality_gate() throws OrganizationCreation.KeyConflictException {
+    QualityGateDto builtInQualityGate = insertBuiltInQualityGate();
+    builtInQProfileRepositoryRule.initialize();
+
+    underTest.create(dbSession, someUser, FULL_POPULATED_NEW_ORGANIZATION);
+
+    OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, FULL_POPULATED_NEW_ORGANIZATION.getKey()).get();
+    assertThat(dbClient.qualityGateDao().selectDefault(dbSession, organization).getUuid()).isEqualTo(builtInQualityGate.getUuid());
+  }
+
   @Test
   public void createForUser_has_no_effect_if_setting_for_feature_is_not_set() {
     checkSizeOfTables();
@@ -333,6 +353,7 @@ 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);
@@ -369,6 +390,7 @@ 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);
@@ -383,6 +405,7 @@ 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);
@@ -395,6 +418,7 @@ 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);
@@ -422,6 +446,7 @@ 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);
@@ -435,6 +460,7 @@ 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);
@@ -450,6 +476,7 @@ 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);
@@ -465,6 +492,7 @@ 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);
@@ -480,6 +508,7 @@ 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);
@@ -498,6 +527,20 @@ public class OrganizationCreationImplTest {
       builtIn1.getQProfileName(), builtIn2.getQProfileName());
   }
 
+  @Test
+  public void createForUser_associates_to_built_in_quality_gate() {
+    QualityGateDto builtInQualityGate = insertBuiltInQualityGate();
+    UserDto user = db.users().insertUser(A_LOGIN);
+    when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
+    enableCreatePersonalOrg(true);
+    builtInQProfileRepositoryRule.initialize();
+
+    underTest.createForUser(dbSession, user);
+
+    OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, SLUG_OF_A_LOGIN).get();
+    assertThat(dbClient.qualityGateDao().selectDefault(dbSession, organization).getUuid()).isEqualTo(builtInQualityGate.getUuid());
+  }
+
   private void enableCreatePersonalOrg(boolean flag) {
     settings.setProperty(CorePropertyDefinitions.ORGANIZATIONS_CREATE_PERSONAL_ORG, flag);
   }
@@ -543,4 +586,8 @@ public class OrganizationCreationImplTest {
       .containsOnly(user.getLogin());
   }
 
+  private QualityGateDto insertBuiltInQualityGate(){
+    return db.qualityGates().insertBuiltInQualityGate();
+  }
+
 }