import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.lang.StringUtils;
import org.sonar.core.user.DefaultUser;
import static java.util.Objects.requireNonNull;
return this;
}
- public UserDto setScmAccounts(@Nullable List list) {
+ public UserDto setScmAccounts(@Nullable List<String> list) {
this.scmAccounts = encodeScmAccounts(list);
return this;
}
@CheckForNull
public static String encodeScmAccounts(@Nullable List<String> scmAccounts) {
if (scmAccounts != null && !scmAccounts.isEmpty()) {
- return String.format("%s%s%s", SCM_ACCOUNTS_SEPARATOR, StringUtils.join(scmAccounts, SCM_ACCOUNTS_SEPARATOR), SCM_ACCOUNTS_SEPARATOR);
+ return String.format("%s%s%s", SCM_ACCOUNTS_SEPARATOR, String.join(String.valueOf(SCM_ACCOUNTS_SEPARATOR), scmAccounts), SCM_ACCOUNTS_SEPARATOR);
}
return null;
}
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import org.sonar.api.config.Configuration;
import org.sonar.api.platform.NewUserHandler;
import org.sonar.api.server.ServerSide;
-import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationMemberDto;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.String.format;
import static org.sonar.core.config.CorePropertyDefinitions.ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS;
+import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.db.user.UserDto.encryptPassword;
import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkRequest;
private static List<String> sanitizeScmAccounts(@Nullable List<String> scmAccounts) {
if (scmAccounts != null) {
- return scmAccounts.stream().filter(s -> !Strings.isNullOrEmpty(s)).collect(MoreCollectors.toList());
+ return new HashSet<>(scmAccounts).stream()
+ .map(Strings::emptyToNull)
+ .filter(Objects::nonNull)
+ .sorted(String::compareToIgnoreCase)
+ .collect(toList(scmAccounts.size()));
}
return Collections.emptyList();
}
*/
package org.sonar.server.user.ws;
+import java.util.Arrays;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
assertThat(user.getScmAccountsAsList()).containsOnly("jon,snow");
}
+ @Test
+ public void update_scm_account_ignores_duplicates() throws Exception {
+ createUser();
+
+ ws.newRequest()
+ .setParam("login", "john")
+ .setMultiParam("scmAccount", Arrays.asList("jon.snow", "jon.snow", "jon.jon", "jon.snow"))
+ .execute();
+
+ UserDto user = dbClient.userDao().selectByLogin(dbSession, "john");
+ assertThat(user.getScmAccountsAsList()).containsExactlyInAnyOrder("jon.jon", "jon.snow");
+ }
+
+ @Test
+ public void update_scm_account_ordered_case_insensitive() throws Exception {
+ createUser();
+
+ ws.newRequest()
+ .setParam("login", "john")
+ .setMultiParam("scmAccount", Arrays.asList("jon.3", "Jon.1", "JON.2"))
+ .execute();
+
+ UserDto user = dbClient.userDao().selectByLogin(dbSession, "john");
+ assertThat(user.getScmAccountsAsList()).containsExactly("Jon.1", "JON.2", "jon.3");
+ }
+
@Test
public void update_only_scm_accounts_with_deprecated_scmAccounts_parameter() throws Exception {
createUser();