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.

UpgradesActionTest.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.platform.ws;
  21. import java.util.Optional;
  22. import org.junit.jupiter.api.BeforeEach;
  23. import org.junit.jupiter.api.Test;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.api.utils.DateUtils;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.core.platform.SonarQubeVersion;
  28. import org.sonar.server.plugins.UpdateCenterMatrixFactory;
  29. import org.sonar.server.ws.TestResponse;
  30. import org.sonar.server.ws.WsActionTester;
  31. import org.sonar.updatecenter.common.Plugin;
  32. import org.sonar.updatecenter.common.Release;
  33. import org.sonar.updatecenter.common.Sonar;
  34. import org.sonar.updatecenter.common.SonarUpdate;
  35. import org.sonar.updatecenter.common.UpdateCenter;
  36. import org.sonar.updatecenter.common.Version;
  37. import static com.google.common.collect.ImmutableList.of;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.mockito.ArgumentMatchers.anyBoolean;
  40. import static org.mockito.Mockito.mock;
  41. import static org.mockito.Mockito.when;
  42. import static org.sonar.api.utils.Version.parse;
  43. import static org.sonar.server.platform.ws.ActiveVersionEvaluatorTest.getReleases;
  44. import static org.sonar.test.JsonAssert.assertJson;
  45. class UpgradesActionTest {
  46. private static final String JSON_EMPTY_UPGRADE_LIST = "{" +
  47. " \"upgrades\":" + "[]" +
  48. "}";
  49. private final UpdateCenterMatrixFactory updateCenterFactory = mock(UpdateCenterMatrixFactory.class);
  50. private final SonarQubeVersion sonarQubeVersion = mock(SonarQubeVersion.class);
  51. private final UpdateCenter updateCenter = mock(UpdateCenter.class);
  52. private final Sonar sonar = mock(Sonar.class);
  53. private final System2 system2 = mock(System2.class);
  54. private final ActiveVersionEvaluator activeVersionEvaluator = new ActiveVersionEvaluator(sonarQubeVersion, system2);
  55. private final UpgradesAction underTest = new UpgradesAction(updateCenterFactory, activeVersionEvaluator);
  56. private final WsActionTester tester = new WsActionTester(underTest);
  57. private static SonarUpdate createSonar_51_update() {
  58. Plugin brandingPlugin = Plugin.factory("branding")
  59. .setCategory("Integration")
  60. .setName("Branding")
  61. .setDescription("Allows to add your own logo to the SonarQube UI.")
  62. .setHomepageUrl("http://docs.codehaus.org/display/SONAR/Branding+Plugin")
  63. .setLicense("GNU LGPL 3")
  64. .setOrganization("SonarSource")
  65. .setOrganizationUrl("http://www.sonarsource.com")
  66. .setIssueTrackerUrl("http://jira.sonarsource.com/browse/SONARPLUGINS/component/14663")
  67. .setSourcesUrl("https://github.com/SonarCommunity/sonar-branding");
  68. Plugin viewsPlugin = Plugin.factory("views")
  69. .setName("Views")
  70. .setCategory("Governance")
  71. .setDescription("Create aggregation trees to group projects. Projects can for instance be grouped by applications, applications " +
  72. "by team, teams by department.")
  73. .setHomepageUrl("https://redirect.sonarsource.com/plugins/views.html")
  74. .setLicense("Commercial")
  75. .setOrganization("SonarSource")
  76. .setOrganizationUrl("http://www.sonarsource.com")
  77. .setTermsConditionsUrl("http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf")
  78. .setIssueTrackerUrl("http://jira.sonarsource.com/browse/VIEWS");
  79. Release release = new Release(new Sonar(), Version.create("5.1.0.5498"))
  80. .setDate(DateUtils.parseDate("2015-04-02"))
  81. .setDescription("New overall layout, merge Issues Drilldown [...]")
  82. .setDownloadUrl("http://dist.sonar.codehaus.org/sonarqube-5.1.zip")
  83. .setChangelogUrl("http://jira.sonarsource.com/secure/ReleaseNote.jspa?projectId=11694&version=20666");
  84. SonarUpdate sonarUpdate = new SonarUpdate(release);
  85. sonarUpdate.addIncompatiblePlugin(brandingPlugin);
  86. sonarUpdate.addPluginToUpgrade(new Release(viewsPlugin, Version.create("2.8.0.5498")).setDisplayVersion("2.8 (build 5498)"));
  87. return sonarUpdate;
  88. }
  89. @BeforeEach
  90. void setup() {
  91. when(updateCenterFactory.getUpdateCenter(anyBoolean())).thenReturn(Optional.of(updateCenter));
  92. when(updateCenter.getSonar()).thenReturn(sonar);
  93. when(updateCenter.getDate()).thenReturn(DateUtils.parseDateTime("2015-04-24T16:08:36+0200"));
  94. when(sonar.getLtaVersion()).thenReturn(new Release(sonar, Version.create("9.9.4")));
  95. when(sonar.getPastLtaVersion()).thenReturn(new Release(sonar, Version.create("8.9.10")));
  96. }
  97. @Test
  98. void action_updates_is_defined() {
  99. WebService.Action def = tester.getDef();
  100. assertThat(def.key()).isEqualTo("upgrades");
  101. assertThat(def.isPost()).isFalse();
  102. assertThat(def.description()).isNotEmpty();
  103. assertThat(def.responseExample()).isNotNull();
  104. assertThat(def.params()).isEmpty();
  105. }
  106. @Test
  107. void empty_array_is_returned_when_there_is_no_upgrade_available() {
  108. when(updateCenter.getSonar().getAllReleases()).thenReturn(getReleases());
  109. when(sonarQubeVersion.get()).thenReturn(parse("9.9.2"));
  110. TestResponse response = tester.newRequest().execute();
  111. assertJson(response.getInput()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_UPGRADE_LIST);
  112. }
  113. @Test
  114. void empty_array_is_returned_when_update_center_is_unavailable() {
  115. when(updateCenterFactory.getUpdateCenter(anyBoolean())).thenReturn(Optional.empty());
  116. TestResponse response = tester.newRequest().execute();
  117. assertJson(response.getInput()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_UPGRADE_LIST);
  118. }
  119. @Test
  120. void verify_JSON_response_against_example() {
  121. SonarUpdate sonarUpdate = createSonar_51_update();
  122. when(sonarQubeVersion.get()).thenReturn(parse("8.9.0"));
  123. when(sonar.getLtsRelease()).thenReturn(new Release(sonar, Version.create("8.9.2")));
  124. when(sonar.getLtaVersion()).thenReturn(new Release(sonar, Version.create("8.9.2")));
  125. when(updateCenter.findSonarUpdates()).thenReturn(of(sonarUpdate));
  126. when(updateCenter.getSonar().getAllReleases()).thenReturn(getReleases());
  127. TestResponse response = tester.newRequest().execute();
  128. assertJson(response.getInput()).withStrictArrayOrder()
  129. .isSimilarTo(tester.getDef().responseExampleAsString());
  130. }
  131. }