private Loaded loadPlugins(Map<String, ScannerPlugin> result) { for (InstalledPlugin plugin : listInstalledPlugins()) { if (pluginPredicate.apply(plugin.key)) { Optional<File> jarFile = pluginFiles.get(plugin); if (!jarFile.isPresent()) { return new Loaded(false, plugin.key); } PluginInfo info = PluginInfo.create(jarFile.get()); result.put(info.getKey(), new ScannerPlugin(plugin.key, plugin.updatedAt, info)); } } return new Loaded(true, null); }
@Override public Map<String, ScannerPlugin> installRemotes() { Profiler profiler = Profiler.create(LOG).startInfo("Load/download plugins"); try { Map<String, ScannerPlugin> result = new HashMap<>(); Loaded loaded = loadPlugins(result); if (!loaded.ok) { // retry once, a plugin may have been uninstalled during downloads result.clear(); loaded = loadPlugins(result); if (!loaded.ok) { throw new IllegalStateException(format("Fail to download plugin [%s]. Not found.", loaded.notFoundPlugin)); } } return result; } finally { profiler.stopInfo(); } }
@Test public void installLocals_always_returns_empty() { // this method is used only by medium tests assertThat(underTest.installLocals()).isEmpty(); }
@Test public void reload_list_if_plugin_uninstalled_during_blue_green_switch() throws IOException { WsTestUtil.mockReader(wsClient, "api/plugins/installed", new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/blue-installed.json")), new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/green-installed.json"))); enqueueNotFoundDownload("scmgit", "abc"); enqueueDownload("java", "def"); enqueueDownload("cobol", "ghi"); when(pluginPredicate.apply(any())).thenReturn(true); Map<String, ScannerPlugin> result = underTest.installRemotes(); assertThat(result.keySet()).containsExactlyInAnyOrder("java", "cobol"); }
@Test public void filter_blacklisted_plugins() throws IOException { WsTestUtil.mockReader(wsClient, "api/plugins/installed", new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json"))); enqueueDownload("scmgit", "abc"); enqueueDownload("java", "def"); when(pluginPredicate.apply("scmgit")).thenReturn(true); when(pluginPredicate.apply("java")).thenReturn(false); Map<String, ScannerPlugin> result = underTest.installRemotes(); assertThat(result.keySet()).containsExactlyInAnyOrder("scmgit"); verify(pluginFiles, times(1)).get(any()); }
private Loaded loadPlugins(Map<String, ScannerPlugin> result) { for (InstalledPlugin plugin : listInstalledPlugins()) { if (pluginPredicate.apply(plugin.key)) { Optional<File> jarFile = pluginFiles.get(plugin); if (!jarFile.isPresent()) { return new Loaded(false, plugin.key); } PluginInfo info = PluginInfo.create(jarFile.get()); result.put(info.getKey(), new ScannerPlugin(plugin.key, plugin.updatedAt, info)); } } return new Loaded(true, null); }
@Override public Map<String, ScannerPlugin> installRemotes() { Profiler profiler = Profiler.create(LOG).startInfo("Load/download plugins"); try { Map<String, ScannerPlugin> result = new HashMap<>(); Loaded loaded = loadPlugins(result); if (!loaded.ok) { // retry once, a plugin may have been uninstalled during downloads result.clear(); loaded = loadPlugins(result); if (!loaded.ok) { throw new IllegalStateException(format("Fail to download plugin [%s]. Not found.", loaded.notFoundPlugin)); } } return result; } finally { profiler.stopInfo(); } }
@Test public void fail_if_plugin_not_found_two_times() throws IOException { WsTestUtil.mockReader(wsClient, "api/plugins/installed", new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/blue-installed.json")), new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/green-installed.json"))); enqueueDownload("scmgit", "abc"); enqueueDownload("cobol", "ghi"); enqueueNotFoundDownload("java", "def"); when(pluginPredicate.apply(any())).thenReturn(true); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to download plugin [java]. Not found."); underTest.installRemotes(); }
@Test public void fail_if_json_of_installed_plugins_is_not_valid() { WsTestUtil.mockReader(wsClient, "api/plugins/installed", new StringReader("not json")); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to parse response of api/plugins/installed"); underTest.installRemotes(); }
@Test public void download_installed_plugins() throws IOException { WsTestUtil.mockReader(wsClient, "api/plugins/installed", new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json"))); enqueueDownload("scmgit", "abc"); enqueueDownload("java", "def"); when(pluginPredicate.apply(any())).thenReturn(true); Map<String, ScannerPlugin> result = underTest.installRemotes(); assertThat(result.keySet()).containsExactlyInAnyOrder("scmgit", "java"); ScannerPlugin gitPlugin = result.get("scmgit"); assertThat(gitPlugin.getKey()).isEqualTo("scmgit"); assertThat(gitPlugin.getInfo().getNonNullJarFile()).exists().isFile(); assertThat(gitPlugin.getUpdatedAt()).isEqualTo(100L); ScannerPlugin javaPlugin = result.get("java"); assertThat(javaPlugin.getKey()).isEqualTo("java"); assertThat(javaPlugin.getInfo().getNonNullJarFile()).exists().isFile(); assertThat(javaPlugin.getUpdatedAt()).isEqualTo(200L); }