From 45b11acbe0116d6781f0d4d5a4e035837e2ba54a Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Wed, 3 Mar 2021 16:55:59 +0100 Subject: [PATCH] SONAR-14523 implemented splitting plugins into two --- .../plugins/ServerPluginRepository.java | 8 +++ .../plugins/ServerPluginRepositoryTest.java | 70 ++++++++++++++++--- .../platform/AbstractSystemInfoWriter.java | 2 +- .../platform/SystemInfoWriterModule.java | 4 +- .../platform/monitoring/BundledSection.java | 58 +++++++++++++++ .../platform/monitoring/PluginsSection.java | 16 +++-- .../platform/SystemInfoWriterModuleTest.java | 4 +- .../monitoring/BundledSectionTest.java | 63 +++++++++++++++++ .../monitoring/PluginsSectionTest.java | 7 +- 9 files changed, 212 insertions(+), 20 deletions(-) create mode 100644 server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/BundledSection.java create mode 100644 server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/BundledSectionTest.java diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/ServerPluginRepository.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/ServerPluginRepository.java index 91ec5940a75..ace04fd1a0a 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/ServerPluginRepository.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/ServerPluginRepository.java @@ -84,6 +84,14 @@ public class ServerPluginRepository implements PluginRepository { return Optional.ofNullable(pluginByKey.get(key)); } + public Collection getPluginsInfoByType(PluginType type){ + return pluginByKey.values() + .stream() + .filter(p -> p.getType() == type) + .map(ServerPlugin::getPluginInfo) + .collect(Collectors.toList()); + } + @Override public Plugin getPluginInstance(String key) { ServerPlugin plugin = pluginByKey.get(key); diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java index 18fecba6258..2eebec65185 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java @@ -20,7 +20,10 @@ package org.sonar.server.plugins; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; +import java.util.stream.Collectors; + import org.junit.Assert; import org.junit.Test; import org.sonar.api.Plugin; @@ -30,6 +33,7 @@ import org.sonar.server.plugins.PluginFilesAndMd5.FileAndMd5; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.server.plugins.PluginType.BUNDLED; import static org.sonar.server.plugins.PluginType.EXTERNAL; public class ServerPluginRepositoryTest { @@ -37,8 +41,8 @@ public class ServerPluginRepositoryTest { @Test public void get_plugin_data() { - ServerPlugin plugin1 = newPlugin("plugin1"); - ServerPlugin plugin2 = newPlugin("plugin2"); + ServerPlugin plugin1 = newPlugin("plugin1", EXTERNAL); + ServerPlugin plugin2 = newPlugin("plugin2", EXTERNAL); repository.addPlugins(Collections.singletonList(plugin1)); repository.addPlugin(plugin2); @@ -56,8 +60,8 @@ public class ServerPluginRepositoryTest { @Test public void fail_getPluginInstance_if_plugin_doesnt_exist() { - ServerPlugin plugin1 = newPlugin("plugin1"); - ServerPlugin plugin2 = newPlugin("plugin2"); + ServerPlugin plugin1 = newPlugin("plugin1", EXTERNAL); + ServerPlugin plugin2 = newPlugin("plugin2", EXTERNAL); repository.addPlugins(Arrays.asList(plugin1, plugin2)); Assert.assertThrows("asd", IllegalArgumentException.class, () -> repository.getPluginInstance("plugin3")); @@ -65,20 +69,70 @@ public class ServerPluginRepositoryTest { @Test public void fail_getPluginInfo_if_plugin_doesnt_exist() { - ServerPlugin plugin1 = newPlugin("plugin1"); - ServerPlugin plugin2 = newPlugin("plugin2"); + ServerPlugin plugin1 = newPlugin("plugin1", EXTERNAL); + ServerPlugin plugin2 = newPlugin("plugin2", EXTERNAL); repository.addPlugins(Arrays.asList(plugin1, plugin2)); Assert.assertThrows("asd", IllegalArgumentException.class, () -> repository.getPluginInfo("plugin3")); } + @Test + public void getPluginsInfoByTypeExternal_given1ExternalPlugin_return1ExternalPlugin(){ + //given + ServerPlugin externalPlugin = newPlugin("plugin1", EXTERNAL); + repository.addPlugin(externalPlugin); + + PluginInfo expectedPluginInfo = externalPlugin.getPluginInfo(); + + //when + Collection pluginInfos = repository.getPluginsInfoByType(EXTERNAL); + + //then + assertThat(pluginInfos).containsOnly(expectedPluginInfo); + } + + @Test + public void getPluginsInfoByTypeExternal_given1ExternalAnd1BundledPlugin_return1ExternalPlugin(){ + //given + ServerPlugin externalPlugin = newPlugin("plugin1", EXTERNAL); + ServerPlugin bundledPlugin = newPlugin("plugin2", BUNDLED); + repository.addPlugin(externalPlugin); + repository.addPlugin(bundledPlugin); + + PluginInfo expectedPluginInfo = externalPlugin.getPluginInfo(); + + //when + Collection pluginInfos = repository.getPluginsInfoByType(EXTERNAL); + + //then + assertThat(pluginInfos).containsOnly(expectedPluginInfo); + } + + @Test + public void getPluginsInfoByTypeBundled_given2BundledPlugins_return2BundledPlugins(){ + //given + ServerPlugin bundledPlugin = newPlugin("plugin1", BUNDLED); + ServerPlugin bundledPlugin2 = newPlugin("plugin2", BUNDLED); + repository.addPlugin(bundledPlugin); + repository.addPlugin(bundledPlugin2); + + PluginInfo expectedPluginInfo = bundledPlugin.getPluginInfo(); + PluginInfo expectedPluginInfo2 = bundledPlugin2.getPluginInfo(); + + //when + Collection pluginInfos = repository.getPluginsInfoByType(BUNDLED); + + //then + assertThat(pluginInfos).containsOnly(expectedPluginInfo, expectedPluginInfo2); + } + private PluginInfo newPluginInfo(String key) { PluginInfo pluginInfo = mock(PluginInfo.class); when(pluginInfo.getKey()).thenReturn(key); return pluginInfo; } - private ServerPlugin newPlugin(String key) { - return new ServerPlugin(newPluginInfo(key), EXTERNAL, mock(Plugin.class), mock(FileAndMd5.class), mock(FileAndMd5.class), mock(ClassLoader.class)); + private ServerPlugin newPlugin(String key, PluginType type) { + return new ServerPlugin(newPluginInfo(key), type, mock(Plugin.class), mock(FileAndMd5.class), mock(FileAndMd5.class), mock(ClassLoader.class)); } } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/AbstractSystemInfoWriter.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/AbstractSystemInfoWriter.java index 7708b7c858e..95f556a3c34 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/AbstractSystemInfoWriter.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/AbstractSystemInfoWriter.java @@ -30,7 +30,7 @@ import org.sonar.server.telemetry.TelemetryDataLoader; public abstract class AbstractSystemInfoWriter implements SystemInfoWriter { private static final String[] ORDERED_SECTION_NAMES = { // standalone - "System", "Database", "Plugins", + "System", "Database", "Bundled", "Plugins", // cluster "Web JVM State", "Web Database Connection", "Web Logging", "Web JVM Properties", diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java index ee41ff6c490..d41750a8fa2 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java @@ -23,6 +23,7 @@ import org.sonar.core.platform.Module; import org.sonar.process.systeminfo.JvmPropertiesSection; import org.sonar.process.systeminfo.JvmStateSection; import org.sonar.server.platform.monitoring.AlmConfigurationSection; +import org.sonar.server.platform.monitoring.BundledSection; import org.sonar.server.platform.monitoring.DbConnectionSection; import org.sonar.server.platform.monitoring.DbSection; import org.sonar.server.platform.monitoring.EsIndexesSection; @@ -60,7 +61,8 @@ public class SystemInfoWriterModule extends Module { LoggingSection.class, PluginsSection.class, SettingsSection.class, - AlmConfigurationSection.class + AlmConfigurationSection.class, + BundledSection.class ); if (standalone) { diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/BundledSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/BundledSection.java new file mode 100644 index 00000000000..d0404cfcec0 --- /dev/null +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/BundledSection.java @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.server.platform.monitoring; + +import org.sonar.api.server.ServerSide; +import org.sonar.core.platform.PluginInfo; +import org.sonar.process.systeminfo.SystemInfoSection; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import org.sonar.server.plugins.PluginType; +import org.sonar.server.plugins.ServerPluginRepository; +import org.sonar.updatecenter.common.Version; + +import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; + +@ServerSide +public class BundledSection implements SystemInfoSection { + private final ServerPluginRepository repository; + + public BundledSection(ServerPluginRepository repository) { + this.repository = repository; + } + + @Override + public ProtobufSystemInfo.Section toProtobuf() { + ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); + protobuf.setName("Bundled"); + + for (PluginInfo plugin : repository.getPluginsInfoByType(PluginType.BUNDLED)) { + String label = ""; + Version version = plugin.getVersion(); + if (version != null) { + label = version.getName() + " "; + } + label += String.format("[%s]", plugin.getName()); + + setAttribute(protobuf, plugin.getKey(), label); + } + return protobuf.build(); + } +} diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/PluginsSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/PluginsSection.java index 45b1ee14f96..95efad64ed4 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/PluginsSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/PluginsSection.java @@ -21,18 +21,19 @@ package org.sonar.server.platform.monitoring; import org.sonar.api.server.ServerSide; import org.sonar.core.platform.PluginInfo; -import org.sonar.core.platform.PluginRepository; import org.sonar.process.systeminfo.SystemInfoSection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import org.sonar.server.plugins.PluginType; +import org.sonar.server.plugins.ServerPluginRepository; import org.sonar.updatecenter.common.Version; import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; @ServerSide public class PluginsSection implements SystemInfoSection { - private final PluginRepository repository; + private final ServerPluginRepository repository; - public PluginsSection(PluginRepository repository) { + public PluginsSection(ServerPluginRepository repository) { this.repository = repository; } @@ -40,12 +41,15 @@ public class PluginsSection implements SystemInfoSection { public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("Plugins"); - for (PluginInfo plugin : repository.getPluginInfos()) { - String label = "[" + plugin.getName() + "]"; + + for (PluginInfo plugin : repository.getPluginsInfoByType(PluginType.EXTERNAL)) { + String label = ""; Version version = plugin.getVersion(); if (version != null) { - label = version.getName() + " " + label; + label = version.getName() + " "; } + label += String.format("[%s]", plugin.getName()); + setAttribute(protobuf, plugin.getKey(), label); } return protobuf.build(); diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java index 4c190bebb06..77f359e78ba 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java @@ -42,7 +42,7 @@ public class SystemInfoWriterModuleTest { Collection> adapters = container.getPicoContainer().getComponentAdapters(); assertThat(adapters) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 18); + .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 19); } @Test @@ -54,7 +54,7 @@ public class SystemInfoWriterModuleTest { Collection> adapters = container.getPicoContainer().getComponentAdapters(); assertThat(adapters) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 12); + .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 13); } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/BundledSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/BundledSectionTest.java new file mode 100644 index 00000000000..3d7926a9695 --- /dev/null +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/BundledSectionTest.java @@ -0,0 +1,63 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.server.platform.monitoring; + +import org.junit.Test; +import org.sonar.core.platform.PluginInfo; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import org.sonar.server.plugins.PluginType; +import org.sonar.server.plugins.ServerPluginRepository; +import org.sonar.updatecenter.common.Version; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs; +import java.util.Arrays; + +public class BundledSectionTest { + + private ServerPluginRepository repo = mock(ServerPluginRepository.class); + private BundledSection underTest = new BundledSection(repo); + + @Test + public void name() { + assertThat(underTest.toProtobuf().getName()).isEqualTo("Bundled"); + } + + @Test + public void toProtobuf_given3BundledPlugins_returnThree() { + when(repo.getPluginsInfoByType(PluginType.BUNDLED)).thenReturn(Arrays.asList( + new PluginInfo("java") + .setName("Java") + .setVersion(Version.create("20.0")), + new PluginInfo("c++") + .setName("C++") + .setVersion(Version.create("1.0.2")), + new PluginInfo("no-version") + .setName("No Version"))); + + ProtobufSystemInfo.Section section = underTest.toProtobuf(); + + assertThatAttributeIs(section, "java", "20.0 [Java]"); + assertThatAttributeIs(section, "c++", "1.0.2 [C++]"); + assertThatAttributeIs(section, "no-version", "[No Version]"); + } +} diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/PluginsSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/PluginsSectionTest.java index e53cd71e9f3..8fbf32462d8 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/PluginsSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/PluginsSectionTest.java @@ -24,16 +24,19 @@ import org.junit.Test; import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginRepository; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import org.sonar.server.plugins.PluginType; +import org.sonar.server.plugins.ServerPluginRepository; import org.sonar.updatecenter.common.Version; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs; public class PluginsSectionTest { - private PluginRepository repo = mock(PluginRepository.class); + private ServerPluginRepository repo = mock(ServerPluginRepository.class); private PluginsSection underTest = new PluginsSection(repo); @Test @@ -43,7 +46,7 @@ public class PluginsSectionTest { @Test public void plugin_name_and_version() { - when(repo.getPluginInfos()).thenReturn(Arrays.asList( + when(repo.getPluginsInfoByType(PluginType.EXTERNAL)).thenReturn(Arrays.asList( new PluginInfo("key-1") .setName("Plugin 1") .setVersion(Version.create("1.1")), -- 2.39.5