aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-03-31 11:38:34 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-03-31 11:51:48 +0200
commit71822a322c00108cce240ae00ae5dbf38f7016c6 (patch)
tree914031e6547a14ba8acbfb485324a085068c601b
parent7728eed708b0c6c69b9ba35fb4239e70b247de9a (diff)
downloadsonarqube-71822a322c00108cce240ae00ae5dbf38f7016c6.tar.gz
sonarqube-71822a322c00108cce240ae00ae5dbf38f7016c6.zip
SONAR-8969 Rename AvatarFactory to AvatarResolver and replace string input by UserDto
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolver.java (renamed from server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarFactory.java)9
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java (renamed from server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarFactoryImpl.java)8
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/issue/ws/ChangelogAction.java6
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueWsModule.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java6
-rw-r--r--server/sonar-server/src/main/resources/org/sonar/server/issue/ws/changelog-example.json2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java (renamed from server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarFactoryImplTest.java)19
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java18
8 files changed, 35 insertions, 35 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarFactory.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolver.java
index 46ef798a265..bfb470e7c3c 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarFactory.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolver.java
@@ -20,11 +20,12 @@
package org.sonar.server.issue.ws;
-public interface AvatarFactory {
+import org.sonar.db.user.UserDto;
+
+public interface AvatarResolver {
/**
- * Creates an avatar hash to load a user's avatar (ex: Gravatar identified by an email hash)
+ * Creates an avatar ID to load a user's avatar (ex: Gravatar identified by an email hash)
*/
- String create(String email);
+ String create(UserDto user);
}
-
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarFactoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java
index 31d2cee73be..427a6ef1d42 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarFactoryImpl.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java
@@ -21,16 +21,18 @@
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 AvatarFactoryImpl implements AvatarFactory {
+public class AvatarResolverImpl implements AvatarResolver {
@Override
- public String create(String email) {
- return hash(requireNonNull(email, "Email cannot be null"));
+ 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) {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/ChangelogAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/ChangelogAction.java
index d3fab4d6337..3809f366712 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/ChangelogAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/ChangelogAction.java
@@ -58,9 +58,9 @@ public class ChangelogAction implements IssuesWsAction {
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;
@@ -118,7 +118,7 @@ public class ChangelogAction implements IssuesWsAction {
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))
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueWsModule.java
index be764c053ed..4f694963b21 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueWsModule.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueWsModule.java
@@ -47,7 +47,7 @@ public class IssueWsModule extends Module {
IssueService.class,
IssueQueryFactory.class,
IssuesWs.class,
- AvatarFactoryImpl.class,
+ AvatarResolverImpl.class,
SearchResponseLoader.class,
SearchResponseFormat.class,
OperationResponseWriter.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java
index 607af98ab36..e309be3cdbb 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java
@@ -69,9 +69,9 @@ public class SearchResponseFormat {
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;
@@ -351,7 +351,7 @@ public class SearchResponseFormat {
.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;
}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/issue/ws/changelog-example.json b/server/sonar-server/src/main/resources/org/sonar/server/issue/ws/changelog-example.json
index 3b626dcdf33..3b70838b670 100644
--- a/server/sonar-server/src/main/resources/org/sonar/server/issue/ws/changelog-example.json
+++ b/server/sonar-server/src/main/resources/org/sonar/server/issue/ws/changelog-example.json
@@ -3,7 +3,7 @@
{
"user": "john.smith",
"userName": "John Smith",
- "avatar": "avatar",
+ "avatar": "b0d8c6e5ea589e6fc3d3e08afb1873bb",
"creationDate": "2014-03-04T23:03:44+0100",
"diffs": [
{
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarFactoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java
index f067a5a92ac..c2b932c50e8 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarFactoryImplTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java
@@ -25,24 +25,33 @@ 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 AvatarFactoryImplTest {
+public class AvatarResolverImplTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
- private AvatarFactoryImpl underTest = new AvatarFactoryImpl();
+ private AvatarResolverImpl underTest = new AvatarResolverImpl();
@Test
public void create() throws Exception {
- String avatar = underTest.create("john@doo.com");
+ 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("john@doo.com")).isEqualTo(underTest.create("John@Doo.com"));
+ 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
@@ -50,6 +59,6 @@ public class AvatarFactoryImplTest {
expectedException.expect(NullPointerException.class);
expectedException.expectMessage("Email cannot be null");
- underTest.create(null);
+ underTest.create(newUserDto("john", "John", null));
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java
index 79f3e873a8d..de8c750ae54 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java
@@ -46,8 +46,6 @@ import org.sonarqube.ws.MediaTypes;
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;
@@ -68,9 +66,7 @@ public class ChangelogActionTest {
@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 {
@@ -78,14 +74,13 @@ public class ChangelogActionTest {
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"));
}
@@ -262,7 +257,6 @@ public class ChangelogActionTest {
.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();
@@ -298,13 +292,7 @@ public class ChangelogActionTest {
}
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"));
}
}