]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8969 Rename AvatarFactory to AvatarResolver and replace string input by UserDto
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 31 Mar 2017 09:38:34 +0000 (11:38 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 31 Mar 2017 09:51:48 +0000 (11:51 +0200)
server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarFactory.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarFactoryImpl.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolver.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/issue/ws/ChangelogAction.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueWsModule.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java
server/sonar-server/src/main/resources/org/sonar/server/issue/ws/changelog-example.json
server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarFactoryImplTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java

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/AvatarFactory.java
deleted file mode 100644 (file)
index 46ef798..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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);
-}
-
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/AvatarFactoryImpl.java
deleted file mode 100644 (file)
index 31d2cee..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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();
-  }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolver.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolver.java
new file mode 100644 (file)
index 0000000..bfb470e
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * 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);
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java
new file mode 100644 (file)
index 0000000..427a6ef
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * 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();
+  }
+}
index d3fab4d633758ba4c92eec947d7b6892c7fdc5e8..3809f366712664162bd6304e1d6c7fb016d33757 100644 (file)
@@ -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))
index be764c053ed5471288b9a50cff22f9cc6fdf9e37..4f694963b21b5bc0fbfa3f2112e8f196fe48646f 100644 (file)
@@ -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,
index 607af98ab365aa2503ef2a91365fa103fec4d6c8..e309be3cdbb4828ed6087e242dc8b4f11c2b1cd9 100644 (file)
@@ -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;
   }
 
index 3b626dcdf33e60603c65d0d21cf8d635b1a58200..3b70838b6709707b0924570c38657a26c1107af6 100644 (file)
@@ -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/AvatarFactoryImplTest.java
deleted file mode 100644 (file)
index f067a5a..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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);
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java
new file mode 100644 (file)
index 0000000..c2b932c
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * 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));
+  }
+}
index 79f3e873a8ddd439fb60f7c8ab2a1895f9d2e302..de8c750ae546debe865ecc172422e93797a71d33 100644 (file)
@@ -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"));
   }
 
 }