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.

InstalledActionTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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.ws;
  21. import com.google.common.base.Optional;
  22. import com.hazelcast.com.eclipsesource.json.Json;
  23. import com.hazelcast.com.eclipsesource.json.JsonObject;
  24. import com.tngtech.java.junit.dataprovider.DataProvider;
  25. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  26. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.util.Random;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.rules.ExpectedException;
  33. import org.junit.rules.TemporaryFolder;
  34. import org.junit.runner.RunWith;
  35. import org.sonar.api.server.ws.WebService;
  36. import org.sonar.api.server.ws.WebService.Action;
  37. import org.sonar.api.utils.System2;
  38. import org.sonar.core.platform.PluginInfo;
  39. import org.sonar.db.DbTester;
  40. import org.sonar.server.plugins.InstalledPlugin;
  41. import org.sonar.server.plugins.InstalledPlugin.FileAndMd5;
  42. import org.sonar.server.plugins.PluginFileSystem;
  43. import org.sonar.server.plugins.UpdateCenterMatrixFactory;
  44. import org.sonar.server.ws.WsActionTester;
  45. import org.sonar.updatecenter.common.Plugin;
  46. import org.sonar.updatecenter.common.UpdateCenter;
  47. import org.sonar.updatecenter.common.Version;
  48. import static java.util.Arrays.asList;
  49. import static java.util.Collections.singletonList;
  50. import static org.assertj.core.api.Assertions.assertThat;
  51. import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
  52. import static org.mockito.Mockito.mock;
  53. import static org.mockito.Mockito.verifyZeroInteractions;
  54. import static org.mockito.Mockito.when;
  55. import static org.sonar.test.JsonAssert.assertJson;
  56. @RunWith(DataProviderRunner.class)
  57. public class InstalledActionTest {
  58. private static final String JSON_EMPTY_PLUGIN_LIST = "{" +
  59. " \"plugins\":" + "[]" +
  60. "}";
  61. @Rule
  62. public ExpectedException expectedException = ExpectedException.none();
  63. @Rule
  64. public TemporaryFolder temp = new TemporaryFolder();
  65. @Rule
  66. public DbTester db = DbTester.create(System2.INSTANCE);
  67. private UpdateCenterMatrixFactory updateCenterMatrixFactory = mock(UpdateCenterMatrixFactory.class, RETURNS_DEEP_STUBS);
  68. private PluginFileSystem pluginFileSystem = mock(PluginFileSystem.class);
  69. private InstalledAction underTest = new InstalledAction(pluginFileSystem, updateCenterMatrixFactory, db.getDbClient());
  70. private WsActionTester tester = new WsActionTester(underTest);
  71. @Test
  72. public void action_installed_is_defined() {
  73. Action action = tester.getDef();
  74. assertThat(action.isPost()).isFalse();
  75. assertThat(action.description()).isNotEmpty();
  76. assertThat(action.responseExample()).isNotNull();
  77. }
  78. @Test
  79. public void empty_array_is_returned_when_there_is_not_plugin_installed() {
  80. String response = tester.newRequest().execute().getInput();
  81. assertJson(response).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST);
  82. }
  83. @Test
  84. public void empty_array_when_update_center_is_unavailable() {
  85. when(updateCenterMatrixFactory.getUpdateCenter(false)).thenReturn(Optional.absent());
  86. String response = tester.newRequest().execute().getInput();
  87. assertJson(response).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST);
  88. }
  89. @Test
  90. public void empty_fields_are_not_serialized_to_json() throws IOException {
  91. when(pluginFileSystem.getInstalledFiles()).thenReturn(
  92. singletonList(newInstalledPlugin(new PluginInfo("foo")
  93. .setName("")
  94. .setDescription("")
  95. .setLicense("")
  96. .setOrganizationName("")
  97. .setOrganizationUrl("")
  98. .setImplementationBuild("")
  99. .setHomepageUrl("")
  100. .setIssueTrackerUrl(""))));
  101. db.pluginDbTester().insertPlugin(
  102. p -> p.setKee("foo"),
  103. p -> p.setUpdatedAt(100L));
  104. String response = tester.newRequest().execute().getInput();
  105. JsonObject json = Json.parse(response).asObject().get("plugins").asArray().get(0).asObject();
  106. assertThat(json.get("key")).isNotNull();
  107. assertThat(json.get("name")).isNull();
  108. assertThat(json.get("description")).isNull();
  109. assertThat(json.get("license")).isNull();
  110. assertThat(json.get("organizationName")).isNull();
  111. assertThat(json.get("organizationUrl")).isNull();
  112. assertThat(json.get("homepageUrl")).isNull();
  113. assertThat(json.get("issueTrackerUrl")).isNull();
  114. }
  115. private InstalledPlugin newInstalledPlugin(PluginInfo plugin) throws IOException {
  116. FileAndMd5 jar = new FileAndMd5(temp.newFile());
  117. return new InstalledPlugin(plugin.setJarFile(jar.getFile()), jar, null);
  118. }
  119. private InstalledPlugin newInstalledPluginWithCompression(PluginInfo plugin) throws IOException {
  120. FileAndMd5 jar = new FileAndMd5(temp.newFile());
  121. FileAndMd5 compressedJar = new FileAndMd5(temp.newFile());
  122. return new InstalledPlugin(plugin.setJarFile(jar.getFile()), jar, compressedJar);
  123. }
  124. @Test
  125. public void return_default_fields() throws Exception {
  126. InstalledPlugin plugin = newInstalledPlugin(new PluginInfo("foo")
  127. .setName("plugName")
  128. .setDescription("desc_it")
  129. .setVersion(Version.create("1.0"))
  130. .setLicense("license_hey")
  131. .setOrganizationName("org_name")
  132. .setOrganizationUrl("org_url")
  133. .setHomepageUrl("homepage_url")
  134. .setIssueTrackerUrl("issueTracker_url")
  135. .setImplementationBuild("sou_rev_sha1")
  136. .setSonarLintSupported(true));
  137. when(pluginFileSystem.getInstalledFiles()).thenReturn(singletonList(plugin));
  138. db.pluginDbTester().insertPlugin(
  139. p -> p.setKee(plugin.getPluginInfo().getKey()),
  140. p -> p.setUpdatedAt(100L));
  141. String response = tester.newRequest().execute().getInput();
  142. verifyZeroInteractions(updateCenterMatrixFactory);
  143. assertJson(response).isSimilarTo(
  144. "{" +
  145. " \"plugins\":" +
  146. " [" +
  147. " {" +
  148. " \"key\": \"foo\"," +
  149. " \"name\": \"plugName\"," +
  150. " \"description\": \"desc_it\"," +
  151. " \"version\": \"1.0\"," +
  152. " \"license\": \"license_hey\"," +
  153. " \"organizationName\": \"org_name\"," +
  154. " \"organizationUrl\": \"org_url\",\n" +
  155. " \"editionBundled\": false," +
  156. " \"homepageUrl\": \"homepage_url\"," +
  157. " \"issueTrackerUrl\": \"issueTracker_url\"," +
  158. " \"implementationBuild\": \"sou_rev_sha1\"," +
  159. " \"sonarLintSupported\": true," +
  160. " \"filename\": \"" + plugin.getLoadedJar().getFile().getName() + "\"," +
  161. " \"hash\": \"" + plugin.getLoadedJar().getMd5() + "\"," +
  162. " \"updatedAt\": 100" +
  163. " }" +
  164. " ]" +
  165. "}");
  166. }
  167. @Test
  168. public void return_compression_fields_if_available() throws Exception {
  169. InstalledPlugin plugin = newInstalledPluginWithCompression(new PluginInfo("foo")
  170. .setName("plugName")
  171. .setDescription("desc_it")
  172. .setVersion(Version.create("1.0"))
  173. .setLicense("license_hey")
  174. .setOrganizationName("org_name")
  175. .setOrganizationUrl("org_url")
  176. .setHomepageUrl("homepage_url")
  177. .setIssueTrackerUrl("issueTracker_url")
  178. .setImplementationBuild("sou_rev_sha1")
  179. .setDocumentationPath("static/documentation.md")
  180. .setSonarLintSupported(true));
  181. when(pluginFileSystem.getInstalledFiles()).thenReturn(singletonList(plugin));
  182. db.pluginDbTester().insertPlugin(
  183. p -> p.setKee(plugin.getPluginInfo().getKey()),
  184. p -> p.setUpdatedAt(100L));
  185. String response = tester.newRequest().execute().getInput();
  186. verifyZeroInteractions(updateCenterMatrixFactory);
  187. assertJson(response).isSimilarTo(
  188. "{" +
  189. " \"plugins\":" +
  190. " [" +
  191. " {" +
  192. " \"key\": \"foo\"," +
  193. " \"name\": \"plugName\"," +
  194. " \"description\": \"desc_it\"," +
  195. " \"version\": \"1.0\"," +
  196. " \"license\": \"license_hey\"," +
  197. " \"organizationName\": \"org_name\"," +
  198. " \"organizationUrl\": \"org_url\",\n" +
  199. " \"editionBundled\": false," +
  200. " \"homepageUrl\": \"homepage_url\"," +
  201. " \"issueTrackerUrl\": \"issueTracker_url\"," +
  202. " \"implementationBuild\": \"sou_rev_sha1\"," +
  203. " \"sonarLintSupported\": true," +
  204. " \"documentationPath\": \"static/documentation.md\"," +
  205. " \"filename\": \"" + plugin.getLoadedJar().getFile().getName() + "\"," +
  206. " \"hash\": \"" + plugin.getLoadedJar().getMd5() + "\"," +
  207. " \"updatedAt\": 100" +
  208. " }" +
  209. " ]" +
  210. "}");
  211. }
  212. @Test
  213. public void category_is_returned_when_in_additional_fields() throws Exception {
  214. String jarFilename = getClass().getSimpleName() + "/" + "some.jar";
  215. File jar = new File(getClass().getResource(jarFilename).toURI());
  216. when(pluginFileSystem.getInstalledFiles()).thenReturn(asList(
  217. new InstalledPlugin(new PluginInfo("plugKey")
  218. .setName("plugName")
  219. .setDescription("desc_it")
  220. .setVersion(Version.create("1.0"))
  221. .setLicense("license_hey")
  222. .setOrganizationName("org_name")
  223. .setOrganizationUrl("org_url")
  224. .setHomepageUrl("homepage_url")
  225. .setIssueTrackerUrl("issueTracker_url")
  226. .setImplementationBuild("sou_rev_sha1")
  227. .setJarFile(jar), new FileAndMd5(jar), null)));
  228. UpdateCenter updateCenter = mock(UpdateCenter.class);
  229. when(updateCenterMatrixFactory.getUpdateCenter(false)).thenReturn(Optional.of(updateCenter));
  230. when(updateCenter.findAllCompatiblePlugins()).thenReturn(
  231. asList(
  232. Plugin.factory("plugKey")
  233. .setCategory("cat_1")));
  234. db.pluginDbTester().insertPlugin(
  235. p -> p.setKee("plugKey"),
  236. p -> p.setFileHash("abcdplugKey"),
  237. p -> p.setUpdatedAt(111111L));
  238. String response = tester.newRequest()
  239. .setParam(WebService.Param.FIELDS, "category")
  240. .execute().getInput();
  241. assertJson(response).isSimilarTo(
  242. "{" +
  243. " \"plugins\":" +
  244. " [" +
  245. " {" +
  246. " \"key\": \"plugKey\"," +
  247. " \"name\": \"plugName\"," +
  248. " \"description\": \"desc_it\"," +
  249. " \"version\": \"1.0\"," +
  250. " \"category\":\"cat_1\"," +
  251. " \"license\": \"license_hey\"," +
  252. " \"organizationName\": \"org_name\"," +
  253. " \"organizationUrl\": \"org_url\",\n" +
  254. " \"editionBundled\": false," +
  255. " \"homepageUrl\": \"homepage_url\"," +
  256. " \"issueTrackerUrl\": \"issueTracker_url\"," +
  257. " \"implementationBuild\": \"sou_rev_sha1\"" +
  258. " }" +
  259. " ]" +
  260. "}");
  261. }
  262. @Test
  263. public void plugins_are_sorted_by_name_then_key_and_only_one_plugin_can_have_a_specific_name() throws IOException {
  264. when(pluginFileSystem.getInstalledFiles()).thenReturn(
  265. asList(
  266. plugin("A", "name2"),
  267. plugin("B", "name1"),
  268. plugin("C", "name0"),
  269. plugin("D", "name0")));
  270. db.pluginDbTester().insertPlugin(
  271. p -> p.setKee("A"),
  272. p -> p.setFileHash("abcdA"),
  273. p -> p.setUpdatedAt(111111L));
  274. db.pluginDbTester().insertPlugin(
  275. p -> p.setKee("B"),
  276. p -> p.setFileHash("abcdB"),
  277. p -> p.setUpdatedAt(222222L));
  278. db.pluginDbTester().insertPlugin(
  279. p -> p.setKee("C"),
  280. p -> p.setFileHash("abcdC"),
  281. p -> p.setUpdatedAt(333333L));
  282. db.pluginDbTester().insertPlugin(
  283. p -> p.setKee("D"),
  284. p -> p.setFileHash("abcdD"),
  285. p -> p.setUpdatedAt(444444L));
  286. String resp = tester.newRequest().execute().getInput();
  287. assertJson(resp).withStrictArrayOrder().isSimilarTo(
  288. "{" +
  289. " \"plugins\":" +
  290. " [" +
  291. " {\"key\": \"C\"}" + "," +
  292. " {\"key\": \"D\"}" + "," +
  293. " {\"key\": \"B\"}" + "," +
  294. " {\"key\": \"A\"}" +
  295. " ]" +
  296. "}");
  297. }
  298. @Test
  299. @UseDataProvider("editionBundledLicenseValues")
  300. public void commercial_plugins_from_SonarSource_has_flag_editionBundled_true_based_on_jar_info(String license) throws Exception {
  301. String jarFilename = getClass().getSimpleName() + "/" + "some.jar";
  302. Random random = new Random();
  303. String organization = random.nextBoolean() ? "SonarSource" : "SONARSOURCE";
  304. String pluginKey = "plugKey";
  305. File jar = new File(getClass().getResource(jarFilename).toURI());
  306. when(pluginFileSystem.getInstalledFiles()).thenReturn(asList(
  307. new InstalledPlugin(new PluginInfo(pluginKey)
  308. .setName("plugName")
  309. .setVersion(Version.create("1.0"))
  310. .setLicense(license)
  311. .setOrganizationName(organization)
  312. .setImplementationBuild("sou_rev_sha1")
  313. .setJarFile(jar), new FileAndMd5(jar), null)));
  314. db.pluginDbTester().insertPlugin(
  315. p -> p.setKee(pluginKey),
  316. p -> p.setFileHash("abcdplugKey"),
  317. p -> p.setUpdatedAt(111111L));
  318. // ensure flag editionBundled is computed from jar info by enabling datacenter with other organization and license values
  319. UpdateCenter updateCenter = mock(UpdateCenter.class);
  320. when(updateCenterMatrixFactory.getUpdateCenter(false)).thenReturn(Optional.of(updateCenter));
  321. when(updateCenter.findAllCompatiblePlugins()).thenReturn(
  322. singletonList(
  323. Plugin.factory(pluginKey)
  324. .setOrganization("foo")
  325. .setLicense("bar")
  326. .setCategory("cat_1")));
  327. String response = tester.newRequest().execute().getInput();
  328. verifyZeroInteractions(updateCenterMatrixFactory);
  329. assertJson(response)
  330. .isSimilarTo("{" +
  331. " \"plugins\":" +
  332. " [" +
  333. " {" +
  334. " \"key\": \"plugKey\"," +
  335. " \"name\": \"plugName\"," +
  336. " \"license\": \"" + license + "\"," +
  337. " \"organizationName\": \"" + organization + "\"," +
  338. " \"editionBundled\": true" +
  339. " }" +
  340. " ]" +
  341. "}");
  342. }
  343. @DataProvider
  344. public static Object[][] editionBundledLicenseValues() {
  345. return new Object[][]{
  346. {"sonarsource"},
  347. {"SonarSource"},
  348. {"SonaRSOUrce"},
  349. {"SONARSOURCE"},
  350. {"commercial"},
  351. {"Commercial"},
  352. {"COMMERCIAL"},
  353. {"COmmERCiaL"},
  354. };
  355. }
  356. @Test
  357. public void only_one_plugin_can_have_a_specific_name_and_key() throws IOException {
  358. when(pluginFileSystem.getInstalledFiles()).thenReturn(
  359. asList(
  360. plugin("A", "name2"),
  361. plugin("A", "name2")));
  362. db.pluginDbTester().insertPlugin(
  363. p -> p.setKee("A"),
  364. p -> p.setFileHash("abcdA"),
  365. p -> p.setUpdatedAt(111111L));
  366. String response = tester.newRequest().execute().getInput();
  367. assertJson(response).withStrictArrayOrder().isSimilarTo(
  368. "{" +
  369. " \"plugins\":" +
  370. " [" +
  371. " {\"key\": \"A\"}" +
  372. " ]" +
  373. "}");
  374. assertThat(response).containsOnlyOnce("name2");
  375. }
  376. private InstalledPlugin plugin(String key, String name) throws IOException {
  377. File file = temp.newFile();
  378. PluginInfo info = new PluginInfo(key)
  379. .setName(name)
  380. .setVersion(Version.create("1.0"));
  381. info.setJarFile(file);
  382. return new InstalledPlugin(info, new FileAndMd5(file), null);
  383. }
  384. }