import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
-import org.apache.commons.lang.StringUtils;
import org.sonar.api.user.RubyUserService;
import org.sonar.api.user.User;
import org.sonar.api.user.UserFinder;
import org.sonar.api.user.UserQuery;
-import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.user.index.UserDoc;
-import org.sonar.server.user.index.UserIndex;
import org.sonar.server.util.RubyUtils;
-import static org.sonar.server.util.Validation.checkMandatoryParameter;
-
public class DefaultUserService implements RubyUserService {
- private final UserIndex userIndex;
- private final UserUpdater userUpdater;
private final UserFinder finder;
- public DefaultUserService(UserIndex userIndex, UserUpdater userUpdater, UserFinder finder) {
- this.userIndex = userIndex;
- this.userUpdater = userUpdater;
+ public DefaultUserService(UserFinder finder) {
this.finder = finder;
}
return finder.find(query);
}
- private UserQuery parseQuery(Map<String, Object> params) {
+ private static UserQuery parseQuery(Map<String, Object> params) {
UserQuery.Builder builder = UserQuery.builder();
if (RubyUtils.toBoolean(params.get("includeDeactivated")) == Boolean.TRUE) {
builder.includeDeactivated();
builder.searchText((String) params.get("s"));
return builder.build();
}
-
- @CheckForNull
- public UserDoc getByLogin(String login) {
- return userIndex.getNullableByLogin(login);
- }
-
- public boolean create(Map<String, Object> params) {
- String password = (String) params.get("password");
- String passwordConfirmation = (String) params.get("password_confirmation");
- checkMandatoryParameter(password, "Password");
- checkMandatoryParameter(passwordConfirmation, "Password confirmation");
- if (!StringUtils.equals(password, passwordConfirmation)) {
- throw new BadRequestException("user.password_doesnt_match_confirmation");
- }
-
- NewUser newUser = NewUser.create()
- .setLogin((String) params.get("login"))
- .setName((String) params.get("name"))
- .setEmail((String) params.get("email"))
- .setScmAccounts(RubyUtils.toStrings(params.get("scm_accounts")))
- .setPassword(password);
- return userUpdater.create(newUser);
- }
-
- public void index() {
- userUpdater.index();
- }
}
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
-import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.sonar.api.user.UserFinder;
import org.sonar.api.user.UserQuery;
-import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.exceptions.Message;
-import org.sonar.server.user.index.UserIndex;
-import org.sonar.server.util.Validation;
-import static com.google.common.collect.Lists.newArrayList;
-import static com.google.common.collect.Maps.newHashMap;
-import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
public class DefaultUserServiceTest {
- private UserIndex userIndex = mock(UserIndex.class);
private UserFinder finder = mock(UserFinder.class);
- private UserUpdater userUpdater = mock(UserUpdater.class);
- private DefaultUserService service = new DefaultUserService(userIndex, userUpdater, finder);
+ private DefaultUserService service = new DefaultUserService(finder);
@Rule
public ExpectedException thrown = ExpectedException.none();
}
}));
}
-
- @Test
- public void create_user() {
- Map<String, Object> params = newHashMap();
- params.put("login", "john");
- params.put("name", "John");
- params.put("email", "john@email.com");
- params.put("password", "123456");
- params.put("password_confirmation", "123456");
- params.put("scm_accounts", newArrayList("jn"));
- service.create(params);
-
- ArgumentCaptor<NewUser> newUserCaptor = ArgumentCaptor.forClass(NewUser.class);
- verify(userUpdater).create(newUserCaptor.capture());
- assertThat(newUserCaptor.getValue().login()).isEqualTo("john");
- assertThat(newUserCaptor.getValue().name()).isEqualTo("John");
- assertThat(newUserCaptor.getValue().email()).isEqualTo("john@email.com");
- assertThat(newUserCaptor.getValue().password()).isEqualTo("123456");
- assertThat(newUserCaptor.getValue().scmAccounts()).containsOnly("jn");
- }
-
- @Test
- public void fail_to_create_user_when_password_is_empty() throws Exception {
- Map<String, Object> params = newHashMap();
- params.put("login", "john");
- params.put("name", "John");
- params.put("email", "john@email.com");
- params.put("password", "");
- params.put("password_confirmation", "123456");
-
- try {
- service.create(params);
- } catch (BadRequestException e) {
- assertThat(e.errors().messages()).containsOnly(Message.of(Validation.CANT_BE_EMPTY_MESSAGE, "Password"));
- }
- }
-
- @Test
- public void fail_to_create_user_when_password_confirmation_is_empty() throws Exception {
- Map<String, Object> params = newHashMap();
- params.put("login", "john");
- params.put("name", "John");
- params.put("email", "john@email.com");
- params.put("password", "123456");
- params.put("password_confirmation", "");
-
- try {
- service.create(params);
- } catch (BadRequestException e) {
- assertThat(e.errors().messages()).containsOnly(Message.of(Validation.CANT_BE_EMPTY_MESSAGE, "Password confirmation"));
- }
- }
-
- @Test
- public void fail_to_create_user_when_password_does_not_match_confirmation() throws Exception {
- Map<String, Object> params = newHashMap();
- params.put("login", "john");
- params.put("name", "John");
- params.put("email", "john@email.com");
- params.put("password", "123456");
- params.put("password_confirmation", "654321");
-
- try {
- service.create(params);
- } catch (BadRequestException e) {
- assertThat(e.errors().messages()).containsOnly(Message.of("user.password_doesnt_match_confirmation"));
- }
- }
-
- @Test
- public void get_by_login() {
- service.getByLogin("john");
- verify(userIndex).getNullableByLogin("john");
- }
-
- @Test
- public void index() {
- service.index();
- verify(userUpdater).index();
- }
}
+++ /dev/null
-#
-# SonarQube, open source software quality management tool.
-# Copyright (C) 2008-2016 SonarSource
-# mailto:contact AT sonarsource DOT com
-#
-# SonarQube is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 3 of the License, or (at your option) any later version.
-#
-# SonarQube is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public License
-# along with this program; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-
-# Entry points to Java API. All other Ruby classes are not considered
-# as an API and can evolve through time.
-class Api
-
- # See the javadoc of org.sonar.api.user.RubyUserService
- # Since 3.6
- def self.users
- Internal.users_api
- end
-end