aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-05-05 17:11:35 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2015-05-06 10:04:18 +0200
commit3e2f029ae23606da92ce0d13c9fd97b49d1556e1 (patch)
tree4cdfe7dc61d188aa4b17312f7be2ff16bbdf0b3f
parentf6c58cf67c43d5058f41cbdba0f3ca332c67dd19 (diff)
downloadsonarqube-3e2f029ae23606da92ce0d13c9fd97b49d1556e1.tar.gz
sonarqube-3e2f029ae23606da92ce0d13c9fd97b49d1556e1.zip
SONAR-6522 Consume batch/users WS to populate JSON report
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/repository/user/User.java61
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepository.java42
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java12
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java23
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java6
5 files changed, 54 insertions, 90 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/user/User.java b/sonar-batch/src/main/java/org/sonar/batch/repository/user/User.java
deleted file mode 100644
index 5323e18b943..00000000000
--- a/sonar-batch/src/main/java/org/sonar/batch/repository/user/User.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 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.
- */
-package org.sonar.batch.repository.user;
-
-import org.apache.commons.lang.builder.EqualsBuilder;
-
-public class User {
-
- private final String login;
- private final String name;
-
- public User(String login, String name) {
- this.login = login;
- this.name = name;
- }
-
- public String login() {
- return login;
- }
-
- public String name() {
- return name;
- }
-
- // For testing
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (obj == this) {
- return true;
- }
- if (obj.getClass() != getClass()) {
- return false;
- }
- User rhs = (User) obj;
- return new EqualsBuilder()
- .append(login, rhs.login)
- .append(name, rhs.name)
- .isEquals();
- }
-
-}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepository.java b/sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepository.java
index e7185b98645..099b9470903 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepository.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepository.java
@@ -19,10 +19,16 @@
*/
package org.sonar.batch.repository.user;
+import com.google.common.base.Function;
import com.google.common.base.Joiner;
+import com.google.common.collect.Lists;
+import com.google.common.io.InputSupplier;
+import org.sonar.api.utils.HttpDownloader;
import org.sonar.batch.bootstrap.ServerClient;
-import org.sonar.batch.protocol.GsonHelper;
+import org.sonar.batch.protocol.input.BatchInput;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -36,23 +42,29 @@ public class UserRepository {
this.serverClient = serverClient;
}
- private static class Users {
-
- private List<User> users = new ArrayList<>();
-
- public List<User> getUsers() {
- return users;
- }
- }
-
- public Collection<User> loadFromWs(List<String> userLogins) {
+ public Collection<BatchInput.User> loadFromWs(List<String> userLogins) {
if (userLogins.isEmpty()) {
return Collections.emptyList();
}
- String url = "/api/users/search?format=json&includeDeactivated=true&logins=" + Joiner.on(',').join(userLogins);
- String json = serverClient.request(url);
- Users users = GsonHelper.create().fromJson(json, Users.class);
- return users.getUsers();
+ InputSupplier<InputStream> request = serverClient.doRequest("/batch/users?logins=" + Joiner.on(',').join(Lists.transform(userLogins, new Function<String, String>() {
+ @Override
+ public String apply(String input) {
+ return ServerClient.encodeForUrl(input);
+ }
+ })), "GET", null);
+ List<BatchInput.User> users = new ArrayList<>();
+ try (InputStream is = request.getInput()) {
+ BatchInput.User user = BatchInput.User.parseDelimitedFrom(is);
+ while (user != null) {
+ users.add(user);
+ user = BatchInput.User.parseDelimitedFrom(is);
+ }
+ } catch (HttpDownloader.HttpException e) {
+ throw serverClient.handleHttpException(e);
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to get user details from server", e);
+ }
+ return users;
}
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java
index 19523999ab0..3025da02f0d 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java
@@ -42,7 +42,7 @@ import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.SonarException;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.batch.issue.IssueCache;
-import org.sonar.batch.repository.user.User;
+import org.sonar.batch.protocol.input.BatchInput;
import org.sonar.batch.repository.user.UserRepository;
import org.sonar.batch.scan.filesystem.InputPathCache;
@@ -122,7 +122,7 @@ public class JSONReport implements Reporter {
writeJsonIssues(json, ruleKeys, userLogins);
writeJsonComponents(json);
writeJsonRules(json, ruleKeys);
- Collection<User> users = userRepository.loadFromWs(new ArrayList<String>(userLogins));
+ Collection<BatchInput.User> users = userRepository.loadFromWs(new ArrayList<String>(userLogins));
writeUsers(json, users);
json.endObject().close();
@@ -214,13 +214,13 @@ public class JSONReport implements Reporter {
json.endArray();
}
- private void writeUsers(JsonWriter json, Collection<User> users) throws IOException {
+ private void writeUsers(JsonWriter json, Collection<BatchInput.User> users) throws IOException {
json.name("users").beginArray();
- for (User user : users) {
+ for (BatchInput.User user : users) {
json
.beginObject()
- .prop("login", user.login())
- .prop("name", user.name())
+ .prop("login", user.getLogin())
+ .prop("name", user.getName())
.endObject();
}
json.endArray();
diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java
index ad8c954e98f..54d62bce5f8 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java
@@ -19,26 +19,39 @@
*/
package org.sonar.batch.repository.user;
+import com.google.common.io.InputSupplier;
import org.junit.Test;
import org.sonar.batch.bootstrap.ServerClient;
+import org.sonar.batch.protocol.input.BatchInput;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class UserRepositoryTest {
@Test
- public void testLoad() {
+ public void testLoad() throws IOException {
ServerClient serverClient = mock(ServerClient.class);
UserRepository userRepo = new UserRepository(serverClient);
- when(serverClient.request("/api/users/search?format=json&includeDeactivated=true&logins=fmallet,sbrandhof"))
- .thenReturn(
- "{ \"users\": [ { \"login\": \"fmallet\", \"name\": \"Freddy Mallet\", \"active\": true, \"email\": \"f@m.com\" }, { \"login\": \"sbrandhof\", \"name\": \"Simon\", \"active\": true } ] }");
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ BatchInput.User.Builder builder = BatchInput.User.newBuilder();
+ builder.setLogin("fmallet").setName("Freddy Mallet").build().writeDelimitedTo(out);
+ builder.setLogin("sbrandhof").setName("Simon").build().writeDelimitedTo(out);
- assertThat(userRepo.loadFromWs(Arrays.asList("fmallet", "sbrandhof"))).containsOnly(new User("fmallet", "Freddy Mallet"), new User("sbrandhof", "Simon"));
+ InputSupplier<InputStream> is = mock(InputSupplier.class);
+ when(serverClient.doRequest("/batch/users?logins=fmallet,sbrandhof", "GET", null))
+ .thenReturn(is);
+ when(is.getInput()).thenReturn(new ByteArrayInputStream(out.toByteArray()));
+
+ assertThat(userRepo.loadFromWs(Arrays.asList("fmallet", "sbrandhof"))).extracting("login", "name").containsOnly(tuple("fmallet", "Freddy Mallet"), tuple("sbrandhof", "Simon"));
}
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java
index e1afdb05fcf..43b4131b007 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java
@@ -39,7 +39,7 @@ import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.rule.RuleKey;
import org.sonar.batch.issue.IssueCache;
-import org.sonar.batch.repository.user.User;
+import org.sonar.batch.protocol.input.BatchInput;
import org.sonar.batch.repository.user.UserRepository;
import org.sonar.batch.scan.filesystem.InputPathCache;
import org.sonar.test.JsonAssert;
@@ -115,8 +115,8 @@ public class JSONReportTest {
.setUpdateDate(SIMPLE_DATE_FORMAT.parse("2013-04-25"))
.setNew(false);
when(jsonReport.getIssues()).thenReturn(Lists.newArrayList(issue));
- User user1 = new User("julien", "Julien");
- User user2 = new User("simon", "Simon");
+ BatchInput.User user1 = BatchInput.User.newBuilder().setLogin("julien").setName("Julien").build();
+ BatchInput.User user2 = BatchInput.User.newBuilder().setLogin("simon").setName("Simon").build();
when(userRepository.loadFromWs(anyListOf(String.class))).thenReturn(Lists.newArrayList(user1, user2));
StringWriter writer = new StringWriter();