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.

UpdatesActionTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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 org.junit.Test;
  22. import org.sonar.api.server.ws.WebService;
  23. import org.sonar.api.utils.DateUtils;
  24. import org.sonar.server.ws.WsTester;
  25. import org.sonar.updatecenter.common.Plugin;
  26. import org.sonar.updatecenter.common.Release;
  27. import static com.google.common.collect.ImmutableList.of;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.mockito.Mockito.when;
  30. import static org.sonar.test.JsonAssert.assertJson;
  31. import static org.sonar.updatecenter.common.PluginUpdate.Status.COMPATIBLE;
  32. import static org.sonar.updatecenter.common.PluginUpdate.Status.INCOMPATIBLE;
  33. public class UpdatesActionTest extends AbstractUpdateCenterBasedPluginsWsActionTest {
  34. private static final Plugin JAVA_PLUGIN = new Plugin("java")
  35. .setName("Java")
  36. .setDescription("SonarQube rule engine.");
  37. private static final Plugin ABAP_PLUGIN = new Plugin("abap")
  38. .setName("ABAP")
  39. .setCategory("Languages")
  40. .setDescription("Enable analysis and reporting on ABAP projects")
  41. .setLicense("Commercial")
  42. .setOrganization("SonarSource")
  43. .setOrganizationUrl("http://www.sonarsource.com")
  44. .setTermsConditionsUrl("http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf");
  45. private static final Release ABAP_31 = release(ABAP_PLUGIN, "3.1")
  46. .setDate(DateUtils.parseDate("2014-12-21"))
  47. .setDescription("New rules, several improvements")
  48. .setDownloadUrl("http://dist.sonarsource.com/abap/download/sonar-abap-plugin-3.1.jar")
  49. .setChangelogUrl("http://jira.sonarsource.com/secure/ReleaseNote.jspa?projectId=10054&version=10552");
  50. private static final Release ABAP_32 = release(ABAP_PLUGIN, "3.2")
  51. .setDate(DateUtils.parseDate("2015-03-10"))
  52. .setDescription("14 new rules, most of them designed to detect potential performance hotspots.")
  53. .setDownloadUrl("http://dist.sonarsource.com/abap/download/sonar-abap-plugin-3.2.jar")
  54. .setChangelogUrl("http://jira.sonarsource.com/secure/ReleaseNote.jspa?projectId=10054&version=10575");
  55. private static final Plugin ANDROID_PLUGIN = new Plugin("android")
  56. .setName("Android")
  57. .setCategory("Languages")
  58. .setDescription("Import Android Lint reports.")
  59. .setLicense("GNU LGPL 3")
  60. .setOrganization("SonarSource and Jerome Van Der Linden, Stephane Nicolas, Florian Roncari, Thomas Bores")
  61. .setOrganizationUrl("http://www.sonarsource.com");
  62. private static final Release ANDROID_10 = release(ANDROID_PLUGIN, "1.0")
  63. .setDate(DateUtils.parseDate("2014-03-31"))
  64. .setDescription("Makes the plugin compatible with multi-language analysis introduced in SonarQube 4.2 and adds support of Emma 2.0 reports")
  65. .setDownloadUrl("http://repository.codehaus.org/org/codehaus/sonar-plugins/android/sonar-android-plugin/1.0/sonar-android-plugin-1.0.jar")
  66. .setChangelogUrl("http://jira.sonarsource.com/secure/ReleaseNote.jspa?projectId=13235&version=20187")
  67. .addOutgoingDependency(release(JAVA_PLUGIN, "1.0"));
  68. private UpdatesAction underTest = new UpdatesAction(updateCenterFactory,
  69. new PluginWSCommons(), new PluginUpdateAggregator()
  70. );
  71. @Test
  72. public void action_updatable_is_defined() {
  73. WsTester wsTester = new WsTester();
  74. WebService.NewController newController = wsTester.context().createController(DUMMY_CONTROLLER_KEY);
  75. underTest.define(newController);
  76. newController.done();
  77. WebService.Controller controller = wsTester.controller(DUMMY_CONTROLLER_KEY);
  78. assertThat(controller.actions()).extracting("key").containsExactly("updates");
  79. WebService.Action action = controller.actions().iterator().next();
  80. assertThat(action.isPost()).isFalse();
  81. assertThat(action.description()).isNotEmpty();
  82. assertThat(action.responseExample()).isNotNull();
  83. }
  84. @Test
  85. public void empty_array_is_returned_when_there_is_no_plugin_available() throws Exception {
  86. underTest.handle(request, response);
  87. assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST);
  88. }
  89. @Test
  90. public void verify_response_against_example() throws Exception {
  91. when(updateCenter.findPluginUpdates()).thenReturn(of(
  92. pluginUpdate(ABAP_32, COMPATIBLE),
  93. pluginUpdate(ABAP_31, INCOMPATIBLE),
  94. pluginUpdate(ANDROID_10, COMPATIBLE)
  95. ));
  96. underTest.handle(request, response);
  97. assertJson(response.outputAsString()).isSimilarTo(getClass().getResource("example-updates_plugins.json"));
  98. }
  99. @Test
  100. public void status_COMPATIBLE_is_displayed_COMPATIBLE_in_JSON() throws Exception {
  101. when(updateCenter.findPluginUpdates()).thenReturn(of(
  102. pluginUpdate(release(PLUGIN_1, "1.0.0"), COMPATIBLE)
  103. ));
  104. underTest.handle(request, response);
  105. assertJson(response.outputAsString()).isSimilarTo(
  106. "{" +
  107. " \"plugins\": [" +
  108. " {" +
  109. " \"updates\": [" +
  110. " {" +
  111. " \"status\": \"COMPATIBLE\"" +
  112. " }" +
  113. " ]" +
  114. " }" +
  115. " ]" +
  116. "}"
  117. );
  118. }
  119. @Test
  120. public void plugins_are_sorted_by_name_and_made_unique() throws Exception {
  121. when(updateCenter.findPluginUpdates()).thenReturn(of(
  122. pluginUpdate("key2", "name2"),
  123. pluginUpdate("key2", "name2"),
  124. pluginUpdate("key0", "name0"),
  125. pluginUpdate("key1", "name1")
  126. ));
  127. underTest.handle(request, response);
  128. assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(
  129. "{" +
  130. " \"plugins\": [" +
  131. " {" +
  132. " \"key\": \"key0\"," +
  133. " \"name\": \"name0\"" +
  134. " }," +
  135. " {" +
  136. " \"key\": \"key1\"," +
  137. " \"name\": \"name1\"" +
  138. " }," +
  139. " {" +
  140. " \"key\": \"key2\"," +
  141. " \"name\": \"name2\"" +
  142. " }," +
  143. " ]" +
  144. "}"
  145. );
  146. }
  147. }