You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerExtensionInstallerTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.plugins;
  21. import java.util.Collection;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.Plugin;
  28. import org.sonar.api.SonarEdition;
  29. import org.sonar.api.SonarQubeSide;
  30. import org.sonar.api.SonarRuntime;
  31. import org.sonar.api.internal.SonarRuntimeImpl;
  32. import org.sonar.api.server.ServerSide;
  33. import org.sonar.api.utils.MessageException;
  34. import org.sonar.api.utils.Version;
  35. import org.sonar.core.platform.ComponentContainer;
  36. import org.sonar.core.platform.PluginInfo;
  37. import org.sonar.core.platform.PluginRepository;
  38. import static java.util.Collections.singleton;
  39. import static org.assertj.core.api.Assertions.assertThat;
  40. import static org.mockito.Mockito.mock;
  41. import static org.mockito.Mockito.when;
  42. public class ServerExtensionInstallerTest {
  43. @Rule
  44. public ExpectedException expectedException = ExpectedException.none();
  45. private SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.parse("8.0"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY);
  46. private TestPluginRepository pluginRepository = new TestPluginRepository();
  47. private TestServerExtensionInstaller underTest = new TestServerExtensionInstaller(sonarRuntime, pluginRepository);
  48. @Test
  49. public void add_plugin_to_container() {
  50. PluginInfo fooPluginInfo = newPlugin("foo", "Foo");
  51. Plugin fooPlugin = mock(Plugin.class);
  52. pluginRepository.add(fooPluginInfo, fooPlugin);
  53. ComponentContainer componentContainer = new ComponentContainer();
  54. underTest.installExtensions(componentContainer);
  55. assertThat(componentContainer.getPicoContainer().getComponents()).contains(fooPlugin);
  56. }
  57. @Test
  58. public void fail_when_detecting_github_auth_plugin() {
  59. PluginInfo foo = newPlugin("authgithub", "GitHub Auth");
  60. pluginRepository.add(foo, mock(Plugin.class));
  61. ComponentContainer componentContainer = new ComponentContainer();
  62. expectedException.expect(MessageException.class);
  63. expectedException.expectMessage("Plugins 'GitHub Auth' are no more compatible with SonarQube");
  64. underTest.installExtensions(componentContainer);
  65. }
  66. @Test
  67. public void fail_when_detecting_auth_plugins() {
  68. pluginRepository.add(newPlugin("authgitlab", "GitLab Auth"), mock(Plugin.class));
  69. pluginRepository.add(newPlugin("authsaml", "SAML Auth"), mock(Plugin.class));
  70. pluginRepository.add(newPlugin("ldap", "LDAP"), mock(Plugin.class));
  71. ComponentContainer componentContainer = new ComponentContainer();
  72. expectedException.expect(MessageException.class);
  73. expectedException.expectMessage("Plugins 'GitLab Auth, LDAP, SAML Auth' are no more compatible with SonarQube");
  74. underTest.installExtensions(componentContainer);
  75. }
  76. @Test
  77. public void fail_when_detecting_saml_auth_plugin() {
  78. PluginInfo foo = newPlugin("authsaml", "SAML Auth");
  79. pluginRepository.add(foo, mock(Plugin.class));
  80. ComponentContainer componentContainer = new ComponentContainer();
  81. expectedException.expect(MessageException.class);
  82. expectedException.expectMessage("Plugins 'SAML Auth' are no more compatible with SonarQube");
  83. underTest.installExtensions(componentContainer);
  84. }
  85. @Test
  86. public void fail_when_detecting_ldap_auth_plugin() {
  87. PluginInfo foo = newPlugin("ldap", "LDAP");
  88. pluginRepository.add(foo, mock(Plugin.class));
  89. ComponentContainer componentContainer = new ComponentContainer();
  90. expectedException.expect(MessageException.class);
  91. expectedException.expectMessage("Plugins 'LDAP' are no more compatible with SonarQube");
  92. underTest.installExtensions(componentContainer);
  93. }
  94. private static PluginInfo newPlugin(String key, String name) {
  95. PluginInfo plugin = mock(PluginInfo.class);
  96. when(plugin.getKey()).thenReturn(key);
  97. when(plugin.getName()).thenReturn(name);
  98. return plugin;
  99. }
  100. private static class TestPluginRepository implements PluginRepository {
  101. private final Map<String, PluginInfo> pluginsInfoMap = new HashMap<>();
  102. private final Map<String, Plugin> pluginsMap = new HashMap<>();
  103. void add(PluginInfo pluginInfo, Plugin plugin) {
  104. pluginsInfoMap.put(pluginInfo.getKey(), pluginInfo);
  105. pluginsMap.put(pluginInfo.getKey(), plugin);
  106. }
  107. @Override
  108. public Collection<PluginInfo> getPluginInfos() {
  109. return pluginsInfoMap.values();
  110. }
  111. @Override
  112. public PluginInfo getPluginInfo(String key) {
  113. if (!pluginsMap.containsKey(key)) {
  114. throw new IllegalArgumentException();
  115. }
  116. return pluginsInfoMap.get(key);
  117. }
  118. @Override
  119. public Plugin getPluginInstance(String key) {
  120. if (!pluginsMap.containsKey(key)) {
  121. throw new IllegalArgumentException();
  122. }
  123. return pluginsMap.get(key);
  124. }
  125. @Override
  126. public boolean hasPlugin(String key) {
  127. return pluginsMap.containsKey(key);
  128. }
  129. }
  130. private static class TestServerExtensionInstaller extends ServerExtensionInstaller {
  131. protected TestServerExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository) {
  132. super(sonarRuntime, pluginRepository, singleton(ServerSide.class));
  133. }
  134. }
  135. }