]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8752 persist id of user for whom personal org was created
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 10 Feb 2017 15:33:52 +0000 (16:33 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 10 Feb 2017 17:29:38 +0000 (18:29 +0100)
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 fc842cf492b5ce252dc352f6d8dbde1ce051953b..e695811667f34f2f35a92f7177a50e44993653c2 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.organization;
 
 import java.util.Date;
 import java.util.Optional;
+import java.util.function.Consumer;
 import javax.annotation.Nullable;
 import org.sonar.api.config.Settings;
 import org.sonar.api.utils.System2;
@@ -67,7 +68,8 @@ public class OrganizationCreationImpl implements OrganizationCreation {
       throw new KeyConflictException(format("Organization key '%s' is already used", key));
     }
 
-    OrganizationDto organization = insertOrganization(dbSession, newOrganization, false);
+    OrganizationDto organization = insertOrganization(dbSession, newOrganization, dto -> {
+    });
     GroupDto group = insertOwnersGroup(dbSession, organization);
     insertDefaultTemplate(dbSession, organization, group);
     addCurrentUserToGroup(dbSession, group, creatorUserId);
@@ -92,7 +94,8 @@ public class OrganizationCreationImpl implements OrganizationCreation {
       newOrganization.getKey(),
       newUser.getLogin());
 
-    OrganizationDto organization = insertOrganization(dbSession, newOrganization, true);
+    OrganizationDto organization = insertOrganization(dbSession, newOrganization,
+      dto -> dto.setGuarded(true).setUserId(newUser.getId()));
     GroupDto group = insertOwnersGroup(dbSession, organization);
     insertDefaultTemplate(dbSession, organization, group);
     addCurrentUserToGroup(dbSession, group, newUser.getId());
@@ -122,15 +125,15 @@ public class OrganizationCreationImpl implements OrganizationCreation {
     organizationValidation.checkAvatar(newOrganization.getAvatar());
   }
 
-  private OrganizationDto insertOrganization(DbSession dbSession, NewOrganization newOrganization, boolean guarded) {
+  private OrganizationDto insertOrganization(DbSession dbSession, NewOrganization newOrganization, Consumer<OrganizationDto> extendCreation) {
     OrganizationDto res = new OrganizationDto()
       .setUuid(uuidFactory.create())
       .setName(newOrganization.getName())
       .setKey(newOrganization.getKey())
       .setDescription(newOrganization.getDescription())
       .setUrl(newOrganization.getUrl())
-      .setAvatarUrl(newOrganization.getAvatar())
-      .setGuarded(guarded);
+      .setAvatarUrl(newOrganization.getAvatar());
+    extendCreation.accept(res);
     dbClient.organizationDao().insert(dbSession, res);
     return res;
   }
index 0933ae201ec7eaa34ec43f49f960ca06a7da3f99..be73210c9653c86c93e1cd71ddd54f47d385b77c 100644 (file)
@@ -151,7 +151,8 @@ public class OrganizationCreationImplTest {
     assertThat(organization.getDescription()).isEqualTo(FULL_POPULATED_NEW_ORGANIZATION.getDescription());
     assertThat(organization.getUrl()).isEqualTo(FULL_POPULATED_NEW_ORGANIZATION.getUrl());
     assertThat(organization.getAvatarUrl()).isEqualTo(FULL_POPULATED_NEW_ORGANIZATION.getAvatar());
-     assertThat(organization.isGuarded()).isFalse();
+    assertThat(organization.isGuarded()).isFalse();
+    assertThat(organization.getUserId()).isNull();
     assertThat(organization.getCreatedAt()).isEqualTo(SOME_DATE);
     assertThat(organization.getUpdatedAt()).isEqualTo(SOME_DATE);
   }
@@ -183,6 +184,7 @@ public class OrganizationCreationImplTest {
     assertThat(organization.getUrl()).isNull();
     assertThat(organization.getAvatarUrl()).isNull();
     assertThat(organization.isGuarded()).isFalse();
+    assertThat(organization.getUserId()).isNull();
   }
 
   @Test
@@ -224,7 +226,7 @@ public class OrganizationCreationImplTest {
   }
 
   @Test
-  public void createForUser_creates_guarded_organization_with_name_and_key_generated_from_login() {
+  public void createForUser_creates_guarded_organization_with_name_and_key_generated_from_login_and_associated_to_user() {
     UserDto user = dbTester.users().insertUser(A_LOGIN);
     when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
@@ -240,6 +242,7 @@ public class OrganizationCreationImplTest {
     assertThat(organization.getUrl()).isNull();
     assertThat(organization.getAvatarUrl()).isNull();
     assertThat(organization.isGuarded()).isTrue();
+    assertThat(organization.getUserId()).isEqualTo(user.getId());
     assertThat(organization.getCreatedAt()).isEqualTo(SOME_DATE);
     assertThat(organization.getUpdatedAt()).isEqualTo(SOME_DATE);
   }