]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6522 Consume batch/users WS to populate JSON report 289/head
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 5 May 2015 15:11:35 +0000 (17:11 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 6 May 2015 08:04:18 +0000 (10:04 +0200)
sonar-batch/src/main/java/org/sonar/batch/repository/user/User.java [deleted file]
sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepository.java
sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java
sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java

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 (file)
index 5323e18..0000000
+++ /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();
-  }
-
-}
index e7185b98645131f82ba555b67bc3842a971d5ffb..099b947090313305314ed5d0225e6b61264421c3 100644 (file)
  */
 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;
   }
 
 }
index 19523999ab04ad9f4bc3652e6d646d2fb75b18a7..3025da02f0d286f36eca7e45c8639543c6b7497a 100644 (file)
@@ -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();
index ad8c954e98fb8fec4b772745b4033f7700d151c6..54d62bce5f84c8813dd4323bbed4b91f48e0255c 100644 (file)
  */
 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"));
   }
 }
index e1afdb05fcf9814b8d6c300d63aaadf1ba62b7b5..43b4131b00703795b4d8b679ff24e73ffae4c644 100644 (file)
@@ -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();