aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server/src
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-02-13 15:06:48 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-02-13 15:06:48 +0100
commit8820bf56ef9617140e4d7d2483584af7bb185779 (patch)
treec3e26b02aa2e6baf8dd5ac282c3f5b0dbf1788f5 /server/sonar-server/src
parentad57a55ea39e3c012edf6ea57db88c0b813f5ae9 (diff)
downloadsonarqube-8820bf56ef9617140e4d7d2483584af7bb185779.tar.gz
sonarqube-8820bf56ef9617140e4d7d2483584af7bb185779.zip
SONAR-8752 SONAR-8751 personal org name from username
and also populate description
Diffstat (limited to 'server/sonar-server/src')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreation.java7
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java13
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/organization/OrganizationCreationImplTest.java52
3 files changed, 59 insertions, 13 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreation.java b/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreation.java
index c1caec8c5e1..518fc0d3134 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreation.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreation.java
@@ -33,6 +33,7 @@ public interface OrganizationCreation {
String OWNERS_GROUP_NAME = "Owners";
String OWNERS_GROUP_DESCRIPTION_PATTERN = "Owners of organization %s";
String PERM_TEMPLATE_DESCRIPTION_PATTERN = "Default permission template of organization %s";
+ String PERSONAL_ORGANIZATION_DESCRIPTION_PATTERN = "%s's personal organization";
/**
* Create a new non guarded organization with the specified properties and of which the specified user will assign
@@ -71,8 +72,10 @@ public interface OrganizationCreation {
* user's login:
* <ul>
* <li>key: generated from the user's login</li>
- * <li>name: the user's login</li>
- * <li>description, url and avatar: null</li>
+ * <li>name: the user's name if set, otherwise the user's login</li>
+ * <li>description: {@link #PERSONAL_ORGANIZATION_DESCRIPTION_PATTERN "[name]'s personal organization"} where name
+ * is user name (when non null and non empty) or login</li>
+ * <li>url and avatar: null</li>
* </ul>
* </p>
*
diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java b/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java
index e695811667f..c5333acf0d0 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java
@@ -45,6 +45,7 @@ import static java.util.Objects.requireNonNull;
import static org.sonar.server.organization.OrganizationCreation.NewOrganization.newOrganizationBuilder;
public class OrganizationCreationImpl implements OrganizationCreation {
+
private final DbClient dbClient;
private final System2 system2;
private final UuidFactory uuidFactory;
@@ -85,9 +86,11 @@ public class OrganizationCreationImpl implements OrganizationCreation {
return Optional.empty();
}
+ String nameOrLogin = nameOrLogin(newUser);
NewOrganization newOrganization = newOrganizationBuilder()
.setKey(organizationValidation.generateKeyFrom(newUser.getLogin()))
- .setName(toName(newUser.getLogin()))
+ .setName(toName(nameOrLogin))
+ .setDescription(format(PERSONAL_ORGANIZATION_DESCRIPTION_PATTERN, nameOrLogin))
.build();
checkState(!organizationKeyIsUsed(dbSession, newOrganization.getKey()),
"Can't create organization with key '%s' for new user '%s' because an organization with this key already exists",
@@ -105,6 +108,14 @@ public class OrganizationCreationImpl implements OrganizationCreation {
return Optional.of(organization);
}
+ private static String nameOrLogin(UserDto newUser) {
+ String name = newUser.getName();
+ if (name == null || name.isEmpty()) {
+ return newUser.getLogin();
+ }
+ return name;
+ }
+
private String toName(String login) {
String name = login.substring(0, Math.min(login.length(), OrganizationValidation.NAME_MAX_LENGTH));
// should not happen has login can't be less than 2 chars, but we call it for safety
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 be73210c965..4ebeec9bb31 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
@@ -55,7 +55,8 @@ public class OrganizationCreationImplTest {
private static final long SOME_DATE = 12893434L;
private static final String A_LOGIN = "a-login";
private static final String SLUG_OF_A_LOGIN = "slug-of-a-login";
- public static final String STRING_64_CHARS = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+ private static final String STRING_64_CHARS = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+ private static final String A_NAME = "a name";
private OrganizationCreation.NewOrganization FULL_POPULATED_NEW_ORGANIZATION = newOrganizationBuilder()
.setName("a-name")
@@ -226,7 +227,7 @@ public class OrganizationCreationImplTest {
}
@Test
- public void createForUser_creates_guarded_organization_with_name_and_key_generated_from_login_and_associated_to_user() {
+ public void createForUser_creates_guarded_organization_with_key_name_and_description_generated_from_user_login_and_name_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);
@@ -237,8 +238,8 @@ public class OrganizationCreationImplTest {
OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, SLUG_OF_A_LOGIN).get();
assertThat(organization.getUuid()).isEqualTo(SOME_UUID);
assertThat(organization.getKey()).isEqualTo(SLUG_OF_A_LOGIN);
- assertThat(organization.getName()).isEqualTo(user.getLogin());
- assertThat(organization.getDescription()).isNull();
+ assertThat(organization.getName()).isEqualTo(user.getName());
+ assertThat(organization.getDescription()).isEqualTo(user.getName() + "'s personal organization");
assertThat(organization.getUrl()).isNull();
assertThat(organization.getAvatarUrl()).isNull();
assertThat(organization.isGuarded()).isTrue();
@@ -264,14 +265,14 @@ public class OrganizationCreationImplTest {
@Test
public void createForUser_creates_owners_group_with_all_permissions_for_new_organization_and_add_current_user_to_it() throws OrganizationCreation.KeyConflictException {
- UserDto user = dbTester.users().insertUser(A_LOGIN);
+ UserDto user = dbTester.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME));
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
enableCreatePersonalOrg(true);
underTest.createForUser(dbSession, user);
- verifyGroupOwners(user, SLUG_OF_A_LOGIN, A_LOGIN);
+ verifyGroupOwners(user, SLUG_OF_A_LOGIN, A_NAME);
}
private void verifyGroupOwners(UserDto user, String organizationKey, String organizationName) {
@@ -292,14 +293,14 @@ public class OrganizationCreationImplTest {
@Test
public void createForUser_creates_default_template_for_new_organization() throws OrganizationCreation.KeyConflictException {
- UserDto user = dbTester.users().insertUser(A_LOGIN);
+ UserDto user = dbTester.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME));
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
enableCreatePersonalOrg(true);
underTest.createForUser(dbSession, user);
- verifyDefaultTemplate(SLUG_OF_A_LOGIN, A_LOGIN);
+ verifyDefaultTemplate(SLUG_OF_A_LOGIN, A_NAME);
}
private void verifyDefaultTemplate(String organizationKey, String organizationName) {
@@ -319,9 +320,39 @@ public class OrganizationCreationImplTest {
}
@Test
- public void createForUser_does_not_fail_if_login_is_too_long_for_an_organization_name() {
+ public void createForUser_does_not_fail_if_name_is_too_long_for_an_organization_name() {
+ String nameTooLong = STRING_64_CHARS + "b";
+ UserDto user = dbTester.users().insertUser(dto -> dto.setName(nameTooLong).setLogin(A_LOGIN));
+ when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
+ mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
+ enableCreatePersonalOrg(true);
+
+ underTest.createForUser(dbSession, user);
+
+ OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, SLUG_OF_A_LOGIN).get();
+ assertThat(organization.getName()).isEqualTo(STRING_64_CHARS);
+ assertThat(organization.getDescription()).isEqualTo(nameTooLong + "'s personal organization");
+ }
+
+ @Test
+ public void createForUser_does_not_fail_if_name_is_empty_and_login_is_too_long_for_an_organization_name() {
+ String login = STRING_64_CHARS + "b";
+ UserDto user = dbTester.users().insertUser(dto -> dto.setName("").setLogin(login));
+ when(organizationValidation.generateKeyFrom(login)).thenReturn(SLUG_OF_A_LOGIN);
+ mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
+ enableCreatePersonalOrg(true);
+
+ underTest.createForUser(dbSession, user);
+
+ OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, SLUG_OF_A_LOGIN).get();
+ assertThat(organization.getName()).isEqualTo(STRING_64_CHARS);
+ assertThat(organization.getDescription()).isEqualTo(login + "'s personal organization");
+ }
+
+ @Test
+ public void createForUser_does_not_fail_if_name_is_null_and_login_is_too_long_for_an_organization_name() {
String login = STRING_64_CHARS + "b";
- UserDto user = dbTester.users().insertUser(login);
+ UserDto user = dbTester.users().insertUser(dto -> dto.setName(null).setLogin(login));
when(organizationValidation.generateKeyFrom(login)).thenReturn(SLUG_OF_A_LOGIN);
mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
enableCreatePersonalOrg(true);
@@ -330,6 +361,7 @@ public class OrganizationCreationImplTest {
OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, SLUG_OF_A_LOGIN).get();
assertThat(organization.getName()).isEqualTo(STRING_64_CHARS);
+ assertThat(organization.getDescription()).isEqualTo(login + "'s personal organization");
}
private void enableCreatePersonalOrg(boolean flag) {