]> source.dussan.org Git - sonarqube.git/commitdiff
NO-JIRA Fix code smells
authorAntoine Vinot <antoine.vinot@sonarsource.com>
Fri, 14 Oct 2022 13:25:31 +0000 (15:25 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 14 Oct 2022 20:03:04 +0000 (20:03 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/DefaultBranchImpl.java
server/sonar-ce/src/test/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListenerTest.java
server/sonar-webserver-api/src/main/java/org/sonar/server/qualitygate/changeevent/ChangedIssueImpl.java
server/sonar-webserver-api/src/test/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImplTest.java
server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterCreateTest.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/duplication/ws/DuplicationsParser.java

index 80ce63ab8998ca0a58a89b2945598492fdee1514..b7d3c0d5808b8ccfc887ac7b49b6b67dfcfc2a82 100644 (file)
  */
 package org.sonar.ce.task.projectanalysis.component;
 
-import javax.annotation.Nullable;
 import org.sonar.ce.task.projectanalysis.analysis.Branch;
-import org.sonar.core.component.ComponentKeys;
 import org.sonar.db.component.BranchDto;
 import org.sonar.db.component.BranchType;
 
-import static org.apache.commons.lang.StringUtils.isEmpty;
-import static org.apache.commons.lang.StringUtils.trimToNull;
-
 /**
  * Implementation of {@link Branch} for default/main branch. It is used
  * when no branch is provided as a scanner parameter or if the branch plugin is not installed.
index da0c7e7cb7a096bdad461d8ceeb8ae6c1a43f6a1..e53ca3b76b7e8e135fa9d8fa986eeada39b60059 100644 (file)
@@ -134,7 +134,8 @@ public class ReportAnalysisFailureNotificationExecutionListenerTest {
     when(notificationService.hasProjectSubscribersForTypes(componentUuid, singleton(ReportAnalysisFailureNotification.class)))
       .thenReturn(true);
 
-    assertThatThrownBy(() -> underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock))
+    Duration randomDuration = randomDuration();
+    assertThatThrownBy(() -> underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration, ceTaskResultMock, throwableMock))
       .isInstanceOf(IllegalStateException.class)
       .hasMessage("Could not find project uuid " + componentUuid);
   }
@@ -150,7 +151,8 @@ public class ReportAnalysisFailureNotificationExecutionListenerTest {
     when(notificationService.hasProjectSubscribersForTypes(componentUuid, singleton(ReportAnalysisFailureNotification.class)))
       .thenReturn(true);
 
-    assertThatThrownBy(() -> underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock))
+    Duration randomDuration = randomDuration();
+    assertThatThrownBy(() -> underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration, ceTaskResultMock, throwableMock))
       .isInstanceOf(IllegalStateException.class)
       .hasMessage("Could not find a branch for project uuid " + componentUuid);
   }
@@ -169,12 +171,14 @@ public class ReportAnalysisFailureNotificationExecutionListenerTest {
 
     Arrays.asList(module, directory, file, view, subView, projectCopy, application)
       .forEach(component -> {
-        try {
-          when(ceTaskMock.getComponent()).thenReturn(Optional.of(new CeTask.Component(component.uuid(), null, null)));
-          when(notificationService.hasProjectSubscribersForTypes(component.uuid(), singleton(ReportAnalysisFailureNotification.class)))
-            .thenReturn(true);
 
-          underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock);
+      when(ceTaskMock.getComponent()).thenReturn(Optional.of(new CeTask.Component(component.uuid(), null, null)));
+      when(notificationService.hasProjectSubscribersForTypes(component.uuid(), singleton(ReportAnalysisFailureNotification.class)))
+        .thenReturn(true);
+
+        Duration randomDuration = randomDuration();
+        try {
+          underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration, ceTaskResultMock, throwableMock);
 
           fail("An IllegalArgumentException should have been thrown for component " + component);
         } catch (IllegalArgumentException e) {
@@ -196,7 +200,8 @@ public class ReportAnalysisFailureNotificationExecutionListenerTest {
       .thenReturn(true);
     dbTester.components().insertPrivateProject(s -> s.setUuid(componentUuid));
 
-    assertThatThrownBy(() -> underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock))
+    Duration randomDuration = randomDuration();
+    assertThatThrownBy(() -> underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration, ceTaskResultMock, throwableMock))
       .isInstanceOf(RowNotFoundException.class)
       .hasMessage("CeActivity with uuid '" + taskUuid + "' not found");
   }
index 4fb7264c4bfc9f89b69f07481da88f1d1f728c66..560bc6de3813db39a614469d1d9c2b10a51a10e5 100644 (file)
@@ -53,8 +53,6 @@ class ChangedIssueImpl implements QGChangeEventListener.ChangedIssue {
         return QGChangeEventListener.Status.REOPENED;
       case Issue.STATUS_TO_REVIEW:
         return QGChangeEventListener.Status.TO_REVIEW;
-      case Issue.STATUS_IN_REVIEW:
-        return QGChangeEventListener.Status.IN_REVIEW;
       case Issue.STATUS_REVIEWED:
         return QGChangeEventListener.Status.REVIEWED;
       case Issue.STATUS_RESOLVED:
index 4a25dc9339567262c2f9a35e27586955b1ea777a..b676f5cc1d29b9a67f62e60e5c5dd426694f3ed7 100644 (file)
@@ -343,8 +343,6 @@ public class QGChangeEventListenersImplTest {
   public void test_status_mapping_on_security_hotspots() {
     assertThat(ChangedIssueImpl.statusOf(new DefaultIssue().setType(RuleType.SECURITY_HOTSPOT).setStatus(Issue.STATUS_TO_REVIEW)))
       .isEqualTo(QGChangeEventListener.Status.TO_REVIEW);
-    assertThat(ChangedIssueImpl.statusOf(new DefaultIssue().setType(RuleType.SECURITY_HOTSPOT).setStatus(Issue.STATUS_IN_REVIEW)))
-      .isEqualTo(QGChangeEventListener.Status.IN_REVIEW);
     assertThat(ChangedIssueImpl.statusOf(new DefaultIssue().setType(RuleType.SECURITY_HOTSPOT).setStatus(Issue.STATUS_REVIEWED)))
       .isEqualTo(QGChangeEventListener.Status.REVIEWED);
   }
index 7763abaa469582c29c58159dfa0376318f86e095..d6fa0328087dcbf2b7c8c9cf6e545682956b1146 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.user;
 
-import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Multimap;
 import java.util.List;
@@ -46,6 +45,7 @@ import org.sonar.server.usergroups.DefaultGroupFinder;
 
 import static java.util.Arrays.asList;
 import static java.util.Collections.singletonList;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.assertj.core.data.MapEntry.entry;
@@ -80,7 +80,7 @@ public class UserUpdaterCreateTest {
   public void create_user() {
     createDefaultGroup();
 
-    UserDto dto = underTest.createAndCommit(db.getSession(), NewUser.builder()
+    UserDto dto = underTest.createAndCommit(session, NewUser.builder()
       .setLogin("user")
       .setName("User")
       .setEmail("user@mail.com")
@@ -118,7 +118,7 @@ public class UserUpdaterCreateTest {
   public void create_user_with_minimum_fields() {
     createDefaultGroup();
 
-    underTest.createAndCommit(db.getSession(), NewUser.builder()
+    underTest.createAndCommit(session, NewUser.builder()
       .setLogin("us")
       .setName("User")
       .build(), u -> {
@@ -137,7 +137,7 @@ public class UserUpdaterCreateTest {
   public void create_user_generates_unique_login_no_login_provided() {
     createDefaultGroup();
 
-    UserDto user = underTest.createAndCommit(db.getSession(), NewUser.builder()
+    UserDto user = underTest.createAndCommit(session, NewUser.builder()
       .setName("John Doe")
       .build(), u -> {
     });
@@ -151,7 +151,7 @@ public class UserUpdaterCreateTest {
   public void create_user_generates_unique_login_when_login_is_empty() {
     createDefaultGroup();
 
-    UserDto user = underTest.createAndCommit(db.getSession(), NewUser.builder()
+    UserDto user = underTest.createAndCommit(session, NewUser.builder()
       .setLogin("")
       .setName("John Doe")
       .build(), u -> {
@@ -166,7 +166,7 @@ public class UserUpdaterCreateTest {
   public void create_user_with_sq_authority_when_no_authority_set() {
     createDefaultGroup();
 
-    underTest.createAndCommit(db.getSession(), NewUser.builder()
+    underTest.createAndCommit(session, NewUser.builder()
       .setLogin("user")
       .setName("User")
       .setPassword("password")
@@ -183,7 +183,7 @@ public class UserUpdaterCreateTest {
   public void create_user_with_identity_provider() {
     createDefaultGroup();
 
-    underTest.createAndCommit(db.getSession(), NewUser.builder()
+    underTest.createAndCommit(session, NewUser.builder()
       .setLogin("user")
       .setName("User")
       .setExternalIdentity(new ExternalIdentity("github", "github-user", "ABCD"))
@@ -203,7 +203,7 @@ public class UserUpdaterCreateTest {
   public void create_user_with_sonarqube_external_identity() {
     createDefaultGroup();
 
-    underTest.createAndCommit(db.getSession(), NewUser.builder()
+    underTest.createAndCommit(session, NewUser.builder()
       .setLogin("user")
       .setName("User")
       .setExternalIdentity(new ExternalIdentity(SQ_AUTHORITY, "user", "user"))
@@ -211,6 +211,7 @@ public class UserUpdaterCreateTest {
     });
 
     UserDto dto = dbClient.userDao().selectByLogin(session, "user");
+    assertThat(dto).isNotNull();
     assertThat(dto.isLocal()).isFalse();
     assertThat(dto.getExternalId()).isEqualTo("user");
     assertThat(dto.getExternalLogin()).isEqualTo("user");
@@ -223,7 +224,7 @@ public class UserUpdaterCreateTest {
   public void create_user_with_scm_accounts_containing_blank_or_null_entries() {
     createDefaultGroup();
 
-    underTest.createAndCommit(db.getSession(), NewUser.builder()
+    underTest.createAndCommit(session, NewUser.builder()
       .setLogin("user")
       .setName("User")
       .setPassword("password")
@@ -238,11 +239,11 @@ public class UserUpdaterCreateTest {
   public void create_user_with_scm_accounts_containing_one_blank_entry() {
     createDefaultGroup();
 
-    underTest.createAndCommit(db.getSession(), NewUser.builder()
+    underTest.createAndCommit(session, NewUser.builder()
       .setLogin("user")
       .setName("User")
       .setPassword("password")
-      .setScmAccounts(asList(""))
+      .setScmAccounts(List.of(""))
       .build(), u -> {
     });
 
@@ -253,7 +254,7 @@ public class UserUpdaterCreateTest {
   public void create_user_with_scm_accounts_containing_duplications() {
     createDefaultGroup();
 
-    underTest.createAndCommit(db.getSession(), NewUser.builder()
+    underTest.createAndCommit(session, NewUser.builder()
       .setLogin("user")
       .setName("User")
       .setPassword("password")
@@ -269,7 +270,7 @@ public class UserUpdaterCreateTest {
     createDefaultGroup();
     UserDto otherUser = db.users().insertUser();
 
-    UserDto created = underTest.createAndCommit(db.getSession(), NewUser.builder()
+    UserDto created = underTest.createAndCommit(session, NewUser.builder()
       .setLogin("user")
       .setName("User")
       .setEmail("user@mail.com")
@@ -282,60 +283,32 @@ public class UserUpdaterCreateTest {
 
   @Test
   public void fail_to_create_user_with_invalid_login() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin("/marius/")
-        .setName("Marius")
-        .setEmail("marius@mail.com")
-        .setPassword("password")
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setLogin("/marius/").build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Login should contain only letters, numbers, and .-_@");
   }
 
   @Test
   public void fail_to_create_user_with_space_in_login() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin("mari us")
-        .setName("Marius")
-        .setEmail("marius@mail.com")
-        .setPassword("password")
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setLogin("mari us").build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Login should contain only letters, numbers, and .-_@");
   }
 
   @Test
   public void fail_to_create_user_with_too_short_login() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin("m")
-        .setName("Marius")
-        .setEmail("marius@mail.com")
-        .setPassword("password")
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setLogin("m").build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Login is too short (minimum is 2 characters)");
   }
 
   @Test
   public void fail_to_create_user_login_start_with_underscore() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin("_marbalous")
-        .setName("Marius")
-        .setEmail("marius@mail.com")
-        .setPassword("password")
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setLogin("_marbalous").build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Login should not start with .-_@");
   }
@@ -344,88 +317,60 @@ public class UserUpdaterCreateTest {
   public void create_user_login_contains_underscore() {
     createDefaultGroup();
     String login = "name_with_underscores";
-    NewUser newUser = NewUser.builder()
-      .setLogin(login)
-      .setName("Marius")
-      .setEmail("marius@mail.com")
-      .setPassword("password")
-      .build();
+    NewUser newUser = newUserBuilder().setLogin(login).build();
 
-    underTest.createAndCommit(db.getSession(), newUser, u -> {});
+    underTest.createAndCommit(session, newUser, u -> {});
 
     assertThat(dbClient.userDao().selectByLogin(session, login)).isNotNull();
   }
 
   @Test
   public void fail_to_create_user_with_too_long_login() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin(Strings.repeat("m", 256))
-        .setName("Marius")
-        .setEmail("marius@mail.com")
-        .setPassword("password")
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setLogin(randomAlphabetic(256)).build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Login is too long (maximum is 255 characters)");
   }
 
   @Test
   public void fail_to_create_user_with_missing_name() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin(DEFAULT_LOGIN)
-        .setName(null)
-        .setEmail("marius@mail.com")
-        .setPassword("password")
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = NewUser.builder()
+      .setLogin(DEFAULT_LOGIN)
+      .setEmail("marius@mail.com")
+      .setPassword("password")
+      .build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Name can't be empty");
   }
 
   @Test
   public void fail_to_create_user_with_too_long_name() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin(DEFAULT_LOGIN)
-        .setName(Strings.repeat("m", 201))
-        .setEmail("marius@mail.com")
-        .setPassword("password")
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setName(randomAlphabetic(201)).build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Name is too long (maximum is 200 characters)");
   }
 
   @Test
   public void fail_to_create_user_with_too_long_email() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin(DEFAULT_LOGIN)
-        .setName("Marius")
-        .setEmail(Strings.repeat("m", 101))
-        .setPassword("password")
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setEmail(randomAlphabetic(101)).build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Email is too long (maximum is 100 characters)");
   }
 
   @Test
   public void fail_to_create_user_with_many_errors() {
+    NewUser newUser = NewUser.builder()
+      .setLogin("")
+      .setName("")
+      .setEmail("marius@mail.com")
+      .setPassword("")
+      .build();
+
     try {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin("")
-        .setName("")
-        .setEmail("marius@mail.com")
-        .setPassword("")
-        .build(), u -> {
-      });
+      underTest.createAndCommit(session, newUser, u -> {});
       fail();
     } catch (BadRequestException e) {
       assertThat(e.errors()).containsExactlyInAnyOrder("Name can't be empty", "Password can't be empty");
@@ -435,17 +380,9 @@ public class UserUpdaterCreateTest {
   @Test
   public void fail_to_create_user_when_scm_account_is_already_used() {
     db.users().insertUser(newLocalUser("john", "John", null).setScmAccounts(singletonList("jo")));
+    NewUser newUser = newUserBuilder().setScmAccounts(singletonList("jo")).build();
 
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin(DEFAULT_LOGIN)
-        .setName("Marius")
-        .setEmail("marius@mail.com")
-        .setPassword("password")
-        .setScmAccounts(singletonList("jo"))
-        .build(), u -> {
-      });
-    })
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("The scm account 'jo' is already used by user(s) : 'John (john)'");
   }
@@ -454,49 +391,25 @@ public class UserUpdaterCreateTest {
   public void fail_to_create_user_when_scm_account_is_already_used_by_many_users() {
     db.users().insertUser(newLocalUser("john", "John", null).setScmAccounts(singletonList("john@email.com")));
     db.users().insertUser(newLocalUser("technical-account", "Technical account", null).setScmAccounts(singletonList("john@email.com")));
+    NewUser newUser = newUserBuilder().setScmAccounts(List.of("john@email.com")).build();
 
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin(DEFAULT_LOGIN)
-        .setName("Marius")
-        .setEmail("marius@mail.com")
-        .setPassword("password")
-        .setScmAccounts(asList("john@email.com"))
-        .build(), u -> {
-      });
-    })
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("The scm account 'john@email.com' is already used by user(s) : 'John (john), Technical account (technical-account)'");
   }
 
   @Test
   public void fail_to_create_user_when_scm_account_is_user_login() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin(DEFAULT_LOGIN)
-        .setName("Marius2")
-        .setEmail("marius2@mail.com")
-        .setPassword("password2")
-        .setScmAccounts(singletonList(DEFAULT_LOGIN))
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setLogin(DEFAULT_LOGIN).setScmAccounts(singletonList(DEFAULT_LOGIN)).build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Login and email are automatically considered as SCM accounts");
   }
 
   @Test
   public void fail_to_create_user_when_scm_account_is_user_email() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin(DEFAULT_LOGIN)
-        .setName("Marius2")
-        .setEmail("marius2@mail.com")
-        .setPassword("password2")
-        .setScmAccounts(singletonList("marius2@mail.com"))
-        .build(), u -> {
-      });
-    })
+    NewUser newUser =  newUserBuilder().setEmail("marius2@mail.com").setScmAccounts(singletonList("marius2@mail.com")).build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("Login and email are automatically considered as SCM accounts");
   }
@@ -506,14 +419,8 @@ public class UserUpdaterCreateTest {
     createDefaultGroup();
     UserDto existingUser = db.users().insertUser(u -> u.setLogin("existing_login"));
 
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin(existingUser.getLogin())
-        .setName("User")
-        .setPassword("PASSWORD")
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setLogin(existingUser.getLogin()).build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(IllegalArgumentException.class)
       .hasMessage("A user with login 'existing_login' already exists");
   }
@@ -523,14 +430,12 @@ public class UserUpdaterCreateTest {
     createDefaultGroup();
     UserDto existingUser = db.users().insertUser(u -> u.setExternalId("existing_external_id").setExternalIdentityProvider("existing_external_provider"));
 
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin("new_login")
-        .setName("User")
-        .setExternalIdentity(new ExternalIdentity(existingUser.getExternalIdentityProvider(), existingUser.getExternalLogin(), existingUser.getExternalId()))
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = NewUser.builder()
+      .setLogin("new_login")
+      .setName("User")
+      .setExternalIdentity(new ExternalIdentity(existingUser.getExternalIdentityProvider(), existingUser.getExternalLogin(), existingUser.getExternalId()))
+      .build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(IllegalArgumentException.class)
       .hasMessage("A user with provider id 'existing_external_id' and identity provider 'existing_external_provider' already exists");
   }
@@ -539,7 +444,7 @@ public class UserUpdaterCreateTest {
   public void notify_new_user() {
     createDefaultGroup();
 
-    underTest.createAndCommit(db.getSession(), NewUser.builder()
+    underTest.createAndCommit(session, NewUser.builder()
       .setLogin("user")
       .setName("User")
       .setEmail("user@mail.com")
@@ -558,30 +463,17 @@ public class UserUpdaterCreateTest {
   public void associate_default_group_when_creating_user() {
     GroupDto defaultGroup = createDefaultGroup();
 
-    underTest.createAndCommit(db.getSession(), NewUser.builder()
-      .setLogin("user")
-      .setName("User")
-      .setEmail("user@mail.com")
-      .setPassword("password")
-      .build(), u -> {
-    });
+    NewUser newUser = newUserBuilder().build();
+    underTest.createAndCommit(session, newUser, u -> {});
 
-    Multimap<String, String> groups = dbClient.groupMembershipDao().selectGroupsByLogins(session, singletonList("user"));
-    assertThat(groups.get("user")).containsOnly(defaultGroup.getName());
+    Multimap<String, String> groups = dbClient.groupMembershipDao().selectGroupsByLogins(session, singletonList(newUser.login()));
+    assertThat(groups.get(newUser.login())).containsOnly(defaultGroup.getName());
   }
 
   @Test
   public void fail_to_associate_default_group_when_default_group_does_not_exist() {
-    assertThatThrownBy(() -> {
-      underTest.createAndCommit(db.getSession(), NewUser.builder()
-        .setLogin("user")
-        .setName("User")
-        .setEmail("user@mail.com")
-        .setPassword("password")
-        .setScmAccounts(asList("u1", "u_1"))
-        .build(), u -> {
-      });
-    })
+    NewUser newUser = newUserBuilder().setScmAccounts(asList("u1", "u_1")).build();
+    assertThatThrownBy(() -> underTest.createAndCommit(session, newUser, u -> {}))
       .isInstanceOf(IllegalStateException.class)
       .hasMessage("Default group cannot be found");
   }
@@ -589,4 +481,12 @@ public class UserUpdaterCreateTest {
   private GroupDto createDefaultGroup() {
     return db.users().insertDefaultGroup();
   }
+
+  private NewUser.Builder newUserBuilder() {
+    return NewUser.builder()
+      .setLogin(DEFAULT_LOGIN)
+      .setName("Marius")
+      .setEmail("marius@mail.com")
+      .setPassword("password");
+  }
 }
index 57db08dae40f6eebe6d15d0e38676e79b9e61b55..8ae2da21bbfb8520a326ec0167509d2cc1afbd6d 100644 (file)
@@ -37,7 +37,6 @@ import org.codehaus.staxmate.in.SMHierarchicCursor;
 import org.codehaus.staxmate.in.SMInputCursor;
 import org.sonar.api.server.ServerSide;
 import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDao;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentFinder;