From: Julien Lancelot Date: Wed, 4 May 2016 16:18:05 +0000 (+0200) Subject: SONAR-7269 Split installing plugins into installing and updating in api/plugins/pending X-Git-Tag: 5.6-RC1~199 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F929%2Fhead;p=sonarqube.git SONAR-7269 Split installing plugins into installing and updating in api/plugins/pending --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PendingAction.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PendingAction.java index 5f459e1a43b..8a79908e9aa 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PendingAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PendingAction.java @@ -19,9 +19,14 @@ */ 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 compatiblePluginsByKey) { - Collection plugins = pluginDownloader.getDownloadedPlugins(); - pluginWSCommons.writePluginInfoList(json, plugins, compatiblePluginsByKey, ARRAY_INSTALLING); + private void writePlugins(JsonWriter json, Map compatiblePluginsByKey) { + Collection uninstalledPlugins = installer.getUninstalledPlugins(); + Collection downloadedPlugins = pluginDownloader.getDownloadedPlugins(); + Collection installedPlugins = installer.getPluginInfos(); + MatchPluginKeys matchPluginKeys = new MatchPluginKeys(from(installedPlugins).transform(PluginInfoToKey.INSTANCE).toSet()); + + Collection newPlugins = new ArrayList<>(); + Collection 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 compatiblePluginsByKey) { - Collection plugins = installer.getUninstalledPlugins(); - pluginWSCommons.writePluginInfoList(json, plugins, compatiblePluginsByKey, ARRAY_REMOVING); + private enum PluginInfoToKey implements Function { + INSTANCE; + + @Override + public String apply(@Nonnull PluginInfo input) { + return input.getKey(); + } + } + + private static class MatchPluginKeys implements Predicate { + private final Set pluginKeys; + + private MatchPluginKeys(Collection pluginKeys) { + this.pluginKeys = copyOf(pluginKeys); + } + + @Override + public boolean apply(@Nonnull PluginInfo input) { + return pluginKeys.contains(input.getKey()); + } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java index 43370f78fd5..7b82a95890a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java @@ -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 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); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginsWsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginsWsMediumTest.java index 30d91d60a3e..b0ba63bf886 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginsWsMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginsWsMediumTest.java @@ -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\"," +