+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.
- */
-
-package org.sonar.server.issue.ws;
-
-public interface AvatarFactory {
-
- /**
- * Creates an avatar hash to load a user's avatar (ex: Gravatar identified by an email hash)
- */
- String create(String email);
-}
-
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.
- */
-
-package org.sonar.server.issue.ws;
-
-import com.google.common.hash.Hashing;
-
-import static java.nio.charset.StandardCharsets.UTF_8;
-import static java.util.Locale.ENGLISH;
-import static java.util.Objects.requireNonNull;
-
-public class AvatarFactoryImpl implements AvatarFactory {
-
- @Override
- public String create(String email) {
- return hash(requireNonNull(email, "Email cannot be null"));
- }
-
- private static String hash(String text) {
- return Hashing.md5().hashString(text.toLowerCase(ENGLISH), UTF_8).toString();
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.
+ */
+
+package org.sonar.server.issue.ws;
+
+import org.sonar.db.user.UserDto;
+
+public interface AvatarResolver {
+
+ /**
+ * Creates an avatar ID to load a user's avatar (ex: Gravatar identified by an email hash)
+ */
+ String create(UserDto user);
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.
+ */
+
+package org.sonar.server.issue.ws;
+
+import com.google.common.hash.Hashing;
+import org.sonar.db.user.UserDto;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Locale.ENGLISH;
+import static java.util.Objects.requireNonNull;
+
+public class AvatarResolverImpl implements AvatarResolver {
+
+ @Override
+ public String create(UserDto user) {
+ UserDto userDto = requireNonNull(user, "User cannot be null");
+ return hash(requireNonNull(userDto.getEmail(), "Email cannot be null"));
+ }
+
+ private static String hash(String text) {
+ return Hashing.md5().hashString(text.toLowerCase(ENGLISH), UTF_8).toString();
+ }
+}
private final DbClient dbClient;
private final IssueFinder issueFinder;
- private final AvatarFactory avatarFactory;
+ private final AvatarResolver avatarFactory;
- public ChangelogAction(DbClient dbClient, IssueFinder issueFinder, AvatarFactory avatarFactory) {
+ public ChangelogAction(DbClient dbClient, IssueFinder issueFinder, AvatarResolver avatarFactory) {
this.dbClient = dbClient;
this.issueFinder = issueFinder;
this.avatarFactory = avatarFactory;
if (user != null) {
changelogBuilder.setUser(user.getLogin());
changelogBuilder.setUserName(user.getName());
- setNullable(emptyToNull(user.getEmail()), email -> changelogBuilder.setAvatar(avatarFactory.create(email)));
+ setNullable(emptyToNull(user.getEmail()), email -> changelogBuilder.setAvatar(avatarFactory.create(user)));
}
change.diffs().entrySet().stream()
.map(toWsDiff(results))
IssueService.class,
IssueQueryFactory.class,
IssuesWs.class,
- AvatarFactoryImpl.class,
+ AvatarResolverImpl.class,
SearchResponseLoader.class,
SearchResponseFormat.class,
OperationResponseWriter.class,
private final Durations durations;
private final WsResponseCommonFormat commonFormat;
private final Languages languages;
- private final AvatarFactory avatarFactory;
+ private final AvatarResolver avatarFactory;
- public SearchResponseFormat(Durations durations, WsResponseCommonFormat commonFormat, Languages languages, AvatarFactory avatarFactory) {
+ public SearchResponseFormat(Durations durations, WsResponseCommonFormat commonFormat, Languages languages, AvatarResolver avatarFactory) {
this.durations = durations;
this.commonFormat = commonFormat;
this.languages = languages;
.setLogin(user.getLogin())
.setName(nullToEmpty(user.getName()))
.setActive(user.isActive());
- setNullable(user.getEmail(), email -> builder.setAvatar(avatarFactory.create(email)));
+ setNullable(user.getEmail(), email -> builder.setAvatar(avatarFactory.create(user)));
return builder;
}
{
"user": "john.smith",
"userName": "John Smith",
- "avatar": "avatar",
+ "avatar": "b0d8c6e5ea589e6fc3d3e08afb1873bb",
"creationDate": "2014-03-04T23:03:44+0100",
"diffs": [
{
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.
- */
-
-package org.sonar.server.issue.ws;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class AvatarFactoryImplTest {
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- private AvatarFactoryImpl underTest = new AvatarFactoryImpl();
-
- @Test
- public void create() throws Exception {
- String avatar = underTest.create("john@doo.com");
-
- assertThat(avatar).isEqualTo("9297bfb538f650da6143b604e82a355d");
- }
-
- @Test
- public void create_is_case_insensitive() throws Exception {
- assertThat(underTest.create("john@doo.com")).isEqualTo(underTest.create("John@Doo.com"));
- }
-
- @Test
- public void fail_with_NP_when_email_is_null() throws Exception {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Email cannot be null");
-
- underTest.create(null);
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.
+ */
+
+package org.sonar.server.issue.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.user.UserTesting.newUserDto;
+
+public class AvatarResolverImplTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private AvatarResolverImpl underTest = new AvatarResolverImpl();
+
+ @Test
+ public void create() throws Exception {
+ String avatar = underTest.create(newUserDto("john", "John", "john@doo.com"));
+
+ assertThat(avatar).isEqualTo("9297bfb538f650da6143b604e82a355d");
+ }
+
+ @Test
+ public void create_is_case_insensitive() throws Exception {
+ assertThat(underTest.create(newUserDto("john", "John", "john@doo.com"))).isEqualTo(underTest.create(newUserDto("john", "John", "John@Doo.com")));
+ }
+
+ @Test
+ public void fail_with_NP_when_user_is_null() throws Exception {
+ expectedException.expect(NullPointerException.class);
+ expectedException.expectMessage("User cannot be null");
+
+ underTest.create(null);
+ }
+
+ @Test
+ public void fail_with_NP_when_email_is_null() throws Exception {
+ expectedException.expect(NullPointerException.class);
+ expectedException.expectMessage("Email cannot be null");
+
+ underTest.create(newUserDto("john", "John", null));
+ }
+}
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.api.web.UserRole.USER;
import static org.sonar.core.util.Protobuf.setNullable;
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
- private AvatarFactory avatarFactory = mock(AvatarFactory.class);
-
- private WsActionTester tester = new WsActionTester(new ChangelogAction(db.getDbClient(), new IssueFinder(db.getDbClient(), userSession), avatarFactory));
+ private WsActionTester tester = new WsActionTester(new ChangelogAction(db.getDbClient(), new IssueFinder(db.getDbClient(), userSession), new AvatarResolverImpl()));
@Test
public void return_changelog() throws Exception {
IssueDto issueDto = db.issues().insertIssue(newIssue());
userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER"));
- mockAvatar(user.getEmail(), "avatar");
ChangelogWsResponse result = call(issueDto.getKey());
assertThat(result.getChangelogList()).hasSize(1);
assertThat(result.getChangelogList().get(0).getUser()).isNotNull().isEqualTo(user.getLogin());
assertThat(result.getChangelogList().get(0).getUserName()).isNotNull().isEqualTo(user.getName());
- assertThat(result.getChangelogList().get(0).getAvatar()).isNotNull().isEqualTo("avatar");
+ assertThat(result.getChangelogList().get(0).getAvatar()).isNotNull().isEqualTo("93942e96f5acd83e2e047ad8fe03114d");
assertThat(result.getChangelogList().get(0).getCreationDate()).isNotEmpty();
assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::getOldValue, Diff::getNewValue).containsOnly(tuple("severity", "MAJOR", "BLOCKER"));
}
.setUserLogin(user.getLogin())
.setDiff("severity", "MAJOR", "BLOCKER")
.setCreationDate(DateUtils.parseDateTime("2014-03-04T23:03:44+0100")));
- mockAvatar(user.getEmail(), "avatar");
String result = tester.newRequest().setParam("issue", issueDto.getKey()).execute().getInput();
}
private UserDto insertUser() {
- UserDto user = db.users().insertUser();
- mockAvatar(user.getEmail(), "avatar");
- return user;
- }
-
- private void mockAvatar(String email, String avatar) {
- when(avatarFactory.create(email)).thenReturn(avatar);
+ return db.users().insertUser(user -> user.setEmail("test@email.com"));
}
}