*/
package org.sonar.server.computation.issue;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Collections2;
this(index, LoggerFactory.getLogger(ScmAccountCacheLoader.class));
}
- public ScmAccountCacheLoader(UserIndex index, Logger log) {
+ @VisibleForTesting
+ ScmAccountCacheLoader(UserIndex index, Logger log) {
this.log = log;
this.index = index;
}
@Override
public String load(String scmAccount) {
List<UserDoc> users = index.getAtMostThreeUsersForScmAccount(scmAccount);
- if (users.isEmpty()) {
- return null;
- }
if (users.size() == 1) {
return users.get(0).login();
}
- Collection<String> logins = Collections2.transform(users, new Function<UserDoc, String>() {
- @Override
- public String apply(UserDoc input) {
- return input.login();
- }
- });
- log.warn(String.format("Multiple users share the SCM account '%s': %s", scmAccount, Joiner.on(", ").join(logins)));
+ if (!users.isEmpty()) {
+ // multiple users are associated to the same SCM account, for example
+ // the same email
+ Collection<String> logins = Collections2.transform(users, new Function<UserDoc, String>() {
+ @Override
+ public String apply(UserDoc input) {
+ return input.login();
+ }
+ });
+ log.warn(String.format("Multiple users share the SCM account '%s': %s", scmAccount, Joiner.on(", ").join(logins)));
+ }
return null;
}
return null;
}
+ /**
+ * Returns the user associated with the given SCM account. If multiple users have the same
+ * SCM account, then result is null.
+ */
@CheckForNull
public UserDoc getNullableByScmAccount(String scmAccount) {
if (!StringUtils.isEmpty(scmAccount)) {
/**
* Returns the users (at most 3) who are associated to the given SCM account. This method can be used
- * to detect user conflicts. It never returns null.
+ * to detect user conflicts.
*/
public List<UserDoc> getAtMostThreeUsersForScmAccount(String scmAccount) {
List<UserDoc> result = new ArrayList<>();
assertThat(index.getAtMostThreeUsersForScmAccount("")).isEmpty();
assertThat(index.getAtMostThreeUsersForScmAccount("unknown")).isEmpty();
}
+
+ @Test
+ public void get_maximum_three_users_for_scm_account() throws Exception {
+ esTester.putDocuments(UserIndexDefinition.INDEX, UserIndexDefinition.TYPE_USER, this.getClass(), "user1.json",
+ "user2-with-same-email-as-user1.json", "user3-with-same-email-as-user1.json", "user4-with-same-email-as-user1.json");
+
+ // restrict results to 3 users
+ assertThat(index.getAtMostThreeUsersForScmAccount("user1@mail.com")).hasSize(3);
+ }
}