]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7269 Split installing plugins into installing and updating in api/plugins/pending 929/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 4 May 2016 16:18:05 +0000 (18:18 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 9 May 2016 13:53:06 +0000 (15:53 +0200)
server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PendingAction.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginsWsMediumTest.java

index 5f459e1a43b8bbfa693491be0d1ed1d4d571ec5e..8a79908e9aaadbc8cc2729415380c63f3477bf69 100644 (file)
  */
 package org.sonar.server.plugins.ws;
 
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableMap;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nonnull;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -32,6 +37,8 @@ import org.sonar.server.plugins.ServerPluginRepository;
 import org.sonar.server.plugins.UpdateCenterMatrixFactory;
 import org.sonar.updatecenter.common.Plugin;
 
+import static com.google.common.collect.FluentIterable.from;
+import static com.google.common.collect.ImmutableSet.copyOf;
 import static com.google.common.io.Resources.getResource;
 import static org.sonar.server.plugins.ws.PluginWSCommons.compatiblePluginsByKey;
 
@@ -42,6 +49,7 @@ public class PendingAction implements PluginsWsAction {
 
   private static final String ARRAY_INSTALLING = "installing";
   private static final String ARRAY_REMOVING = "removing";
+  private static final String ARRAY_UPDATING = "updating";
 
   private final PluginDownloader pluginDownloader;
   private final ServerPluginRepository installer;
@@ -73,19 +81,51 @@ public class PendingAction implements PluginsWsAction {
     JsonWriter jsonWriter = response.newJsonWriter();
 
     jsonWriter.beginObject();
-    writeInstalling(jsonWriter, compatiblePluginsByKey);
-    writeRemoving(jsonWriter, compatiblePluginsByKey);
+    writePlugins(jsonWriter, compatiblePluginsByKey);
     jsonWriter.endObject();
     jsonWriter.close();
   }
 
-  private void writeInstalling(JsonWriter json, Map<String, Plugin> compatiblePluginsByKey) {
-    Collection<PluginInfo> plugins = pluginDownloader.getDownloadedPlugins();
-    pluginWSCommons.writePluginInfoList(json, plugins, compatiblePluginsByKey, ARRAY_INSTALLING);
+  private void writePlugins(JsonWriter json, Map<String, Plugin> compatiblePluginsByKey) {
+    Collection<PluginInfo> uninstalledPlugins = installer.getUninstalledPlugins();
+    Collection<PluginInfo> downloadedPlugins = pluginDownloader.getDownloadedPlugins();
+    Collection<PluginInfo> installedPlugins = installer.getPluginInfos();
+    MatchPluginKeys matchPluginKeys = new MatchPluginKeys(from(installedPlugins).transform(PluginInfoToKey.INSTANCE).toSet());
+
+    Collection<PluginInfo> newPlugins = new ArrayList<>();
+    Collection<PluginInfo> updatedPlugins = new ArrayList<>();
+    for (PluginInfo pluginInfo : downloadedPlugins) {
+      if (matchPluginKeys.apply(pluginInfo)) {
+        updatedPlugins.add(pluginInfo);
+      } else {
+        newPlugins.add(pluginInfo);
+      }
+    }
+
+    pluginWSCommons.writePluginInfoList(json, newPlugins, compatiblePluginsByKey, ARRAY_INSTALLING);
+    pluginWSCommons.writePluginInfoList(json, updatedPlugins, compatiblePluginsByKey, ARRAY_UPDATING);
+    pluginWSCommons.writePluginInfoList(json, uninstalledPlugins, compatiblePluginsByKey, ARRAY_REMOVING);
   }
 
-  private void writeRemoving(JsonWriter json, Map<String, Plugin> compatiblePluginsByKey) {
-    Collection<PluginInfo> plugins = installer.getUninstalledPlugins();
-    pluginWSCommons.writePluginInfoList(json, plugins, compatiblePluginsByKey, ARRAY_REMOVING);
+  private enum PluginInfoToKey implements Function<PluginInfo, String> {
+    INSTANCE;
+
+    @Override
+    public String apply(@Nonnull PluginInfo input) {
+      return input.getKey();
+    }
+  }
+
+  private static class MatchPluginKeys implements Predicate<PluginInfo> {
+    private final Set<String> pluginKeys;
+
+    private MatchPluginKeys(Collection<String> pluginKeys) {
+      this.pluginKeys = copyOf(pluginKeys);
+    }
+
+    @Override
+    public boolean apply(@Nonnull PluginInfo input) {
+      return pluginKeys.contains(input.getKey());
+    }
   }
 }
index 43370f78fd5a9d48b16f5bb1b97ff206120ee5cb..7b82a95890a2ad05839380be6f0df0545d6201e1 100644 (file)
@@ -20,7 +20,8 @@
 package org.sonar.server.plugins.ws;
 
 import com.google.common.base.Optional;
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
 import org.junit.Test;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.WebService;
@@ -75,7 +76,8 @@ public class PendingActionTest {
     assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(
       "{" +
         "  \"installing\": []," +
-        "  \"removing\": []" +
+        "  \"removing\": []," +
+        "  \"updating\": []" +
         "}"
       );
   }
@@ -89,22 +91,16 @@ public class PendingActionTest {
     assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(
       "{" +
         "  \"installing\": []," +
-        "  \"removing\": []" +
+        "  \"removing\": []," +
+        "  \"updating\": []" +
         "}"
       );
   }
 
   @Test
   public void verify_properties_displayed_in_json_per_installing_plugin() throws Exception {
-    when(pluginDownloader.getDownloadedPlugins()).thenReturn(of(gitPluginInfo()));
-    UpdateCenter updateCenter = mock(UpdateCenter.class);
-    when(updateCenterMatrixFactory.getUpdateCenter(false)).thenReturn(Optional.of(updateCenter));
-    when(updateCenter.findAllCompatiblePlugins()).thenReturn(
-      Arrays.asList(
-        new Plugin("scmgit")
-          .setCategory("cat_1")
-        )
-      );
+    newUpdateCenter("scmgit");
+    when(pluginDownloader.getDownloadedPlugins()).thenReturn(of(newScmGitPluginInfo()));
 
     underTest.handle(request, response);
 
@@ -126,20 +122,22 @@ public class PendingActionTest {
         "      \"implementationBuild\": \"9ce9d330c313c296fab051317cc5ad4b26319e07\"" +
         "    }" +
         "  ]," +
-        "  \"removing\": []" +
+        "  \"removing\": []," +
+        "  \"updating\": []" +
         "}"
       );
   }
 
   @Test
   public void verify_properties_displayed_in_json_per_removing_plugin() throws Exception {
-    when(serverPluginRepository.getUninstalledPlugins()).thenReturn(of(gitPluginInfo()));
+    when(serverPluginRepository.getUninstalledPlugins()).thenReturn(of(newScmGitPluginInfo()));
 
     underTest.handle(request, response);
 
     assertJson(response.outputAsString()).isSimilarTo(
       "{" +
         "  \"installing\": []," +
+        "  \"updating\": []," +
         "  \"removing\": " +
         "  [" +
         "    {" +
@@ -159,6 +157,65 @@ public class PendingActionTest {
       );
   }
 
+  @Test
+  public void verify_properties_displayed_in_json_per_updating_plugin() throws Exception {
+    newUpdateCenter("scmgit");
+    when(serverPluginRepository.getPluginInfos()).thenReturn(of(newScmGitPluginInfo()));
+    when(pluginDownloader.getDownloadedPlugins()).thenReturn(of(newScmGitPluginInfo()));
+
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(
+      "{" +
+        "  \"installing\": []," +
+        "  \"removing\": []," +
+        "  \"updating\": " +
+        "  [" +
+        "    {" +
+        "      \"key\": \"scmgit\"" +
+        "    }" +
+        "  ]" +
+        "}"
+    );
+  }
+
+  @Test
+  public void verify_properties_displayed_in_json_per_installing_removing_and_updating_plugins() throws Exception {
+    PluginInfo installed = newPluginInfo("java");
+    PluginInfo removedPlugin = newPluginInfo("js");
+    PluginInfo newPlugin = newPluginInfo("php");
+
+    newUpdateCenter("scmgit");
+    when(serverPluginRepository.getPluginInfos()).thenReturn(of(installed));
+    when(serverPluginRepository.getUninstalledPlugins()).thenReturn(of(removedPlugin));
+    when(pluginDownloader.getDownloadedPlugins()).thenReturn(of(newPlugin, installed));
+
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(
+      "{" +
+        "  \"installing\":" +
+        "  [" +
+        "    {" +
+        "      \"key\": \"php\"" +
+        "    }" +
+        "  ]," +
+        "  \"removing\":" +
+        "  [" +
+        "    {" +
+        "      \"key\": \"js\"" +
+        "    }" +
+        "  ]," +
+        "  \"updating\": " +
+        "  [" +
+        "    {" +
+        "      \"key\": \"java\"" +
+        "    }" +
+        "  ]" +
+        "}"
+    );
+  }
+
   @Test
   public void installing_plugins_are_sorted_by_name_then_key_and_are_unique() throws Exception {
     when(pluginDownloader.getDownloadedPlugins()).thenReturn(of(
@@ -186,7 +243,8 @@ public class PendingActionTest {
         "      \"name\": \"Foo\"," +
         "    }" +
         "  ]," +
-        "  \"removing\": []" +
+        "  \"removing\": []," +
+        "  \"updating\": []" +
         "}"
       );
   }
@@ -204,6 +262,7 @@ public class PendingActionTest {
     assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(
       "{" +
         "  \"installing\": []," +
+        "  \"updating\": []," +
         "  \"removing\": " +
         "  [" +
         "    {" +
@@ -223,7 +282,7 @@ public class PendingActionTest {
       );
   }
 
-  public PluginInfo gitPluginInfo() {
+  public PluginInfo newScmGitPluginInfo() {
     return new PluginInfo("scmgit")
       .setName("Git")
       .setDescription("Git SCM Provider.")
@@ -236,6 +295,21 @@ public class PendingActionTest {
       .setImplementationBuild("9ce9d330c313c296fab051317cc5ad4b26319e07");
   }
 
+  public PluginInfo newPluginInfo(String key){
+    return new PluginInfo(key);
+  }
+
+  private UpdateCenter newUpdateCenter(String... pluginKeys){
+    UpdateCenter updateCenter = mock(UpdateCenter.class);
+    when(updateCenterMatrixFactory.getUpdateCenter(false)).thenReturn(Optional.of(updateCenter));
+    List<Plugin> plugins = new ArrayList<>();
+    for (String pluginKey : pluginKeys) {
+      plugins.add(new Plugin(pluginKey).setCategory("cat_1"));
+    }
+    when(updateCenter.findAllCompatiblePlugins()).thenReturn(plugins);
+    return updateCenter;
+  }
+
   public PluginInfo newPluginInfo(int id) {
     return new PluginInfo("key" + id).setName("name" + id);
   }
index 30d91d60a3e06df88efbf156f1a5c06c2eebb5d3..b0ba63bf886ae4599c7e1044444f4a745e57b47d 100644 (file)
@@ -77,19 +77,20 @@ public class PluginsWsMediumTest {
       "  ]" +
       "}");
 
-    wsTester.newGetRequest("api/plugins", "pending").execute().assertJson("{\"installing\":[],\"removing\":[]}");
+    wsTester.newGetRequest("api/plugins", "pending").execute().assertJson("{\"installing\":[],\"removing\":[],\"updating\":[]}");
 
     // 2 - login as admin and install one plugin, update another, verify pending status in the process
     userSessionRule.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
     wsTester.newPostRequest("api/plugins", "update").setParam("key", "decoy").execute().assertNoContent();
 
     wsTester.newGetRequest("api/plugins", "pending").execute().assertJson("{" +
-      "  \"installing\": [" +
+      "  \"updating\": [" +
       "    {" +
       "      \"key\": \"decoy\"," +
       "      \"version\": \"1.1\"" +
       "    }" +
       "  ]," +
+      "  \"installing\": []," +
       "  \"removing\": []" +
       "}");
 
@@ -98,14 +99,16 @@ public class PluginsWsMediumTest {
     wsTester.newGetRequest("api/plugins", "pending").execute().assertJson("{" +
       "  \"installing\": [" +
       "    {" +
-      "      \"key\": \"decoy\"," +
-      "      \"version\": \"1.1\"" +
-      "    }," +
-      "    {" +
       "      \"key\": \"foo\"," +
       "      \"version\": \"1.0\"" +
       "    }" +
       "  ]," +
+      "  \"updating\": [" +
+      "    {" +
+      "      \"key\": \"decoy\"," +
+      "      \"version\": \"1.1\"" +
+      "    }" +
+      "  ]," +
       "  \"removing\": []" +
       "}");
 
@@ -133,6 +136,7 @@ public class PluginsWsMediumTest {
 
     wsTester.newGetRequest("api/plugins", "pending").execute().assertJson("{" +
       "  \"installing\": []," +
+      "  \"updating\": []," +
       "  \"removing\": [" +
       "    {" +
       "      \"key\": \"foo\"," +