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.

PluginWSCommonsTest.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 java.io.File;
  22. import org.junit.Test;
  23. import org.sonar.api.utils.text.JsonWriter;
  24. import org.sonar.core.platform.PluginInfo;
  25. import org.sonar.server.ws.WsTester;
  26. import org.sonar.updatecenter.common.Plugin;
  27. import org.sonar.updatecenter.common.PluginUpdate;
  28. import org.sonar.updatecenter.common.Release;
  29. import org.sonar.updatecenter.common.Version;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.sonar.api.utils.DateUtils.parseDate;
  32. import static org.sonar.server.plugins.ws.PluginWSCommons.toJSon;
  33. import static org.sonar.test.JsonAssert.assertJson;
  34. import static org.sonar.updatecenter.common.PluginUpdate.Status.COMPATIBLE;
  35. import static org.sonar.updatecenter.common.PluginUpdate.Status.DEPENDENCIES_REQUIRE_SONAR_UPGRADE;
  36. import static org.sonar.updatecenter.common.PluginUpdate.Status.INCOMPATIBLE;
  37. import static org.sonar.updatecenter.common.PluginUpdate.Status.REQUIRE_SONAR_UPGRADE;
  38. public class PluginWSCommonsTest {
  39. private static final PluginInfo GIT_PLUGIN_METADATA = new PluginInfo("scmgit")
  40. .setName("Git")
  41. .setDescription("Git SCM Provider.")
  42. .setVersion(Version.create("1.0"))
  43. .setLicense("GNU LGPL 3")
  44. .setOrganizationName("SonarSource")
  45. .setOrganizationUrl("http://www.sonarsource.com")
  46. .setHomepageUrl("http://redirect.sonarsource.com/plugins/scmgit.html")
  47. .setIssueTrackerUrl("http://jira.codehaus.org/browse/SONARSCGIT")
  48. .setFile(new File("/home/user/sonar-scm-git-plugin-1.0.jar"));
  49. private static final Plugin PLUGIN = new Plugin("p_key")
  50. .setName("p_name")
  51. .setCategory("p_category")
  52. .setDescription("p_description")
  53. .setLicense("p_license")
  54. .setOrganization("p_orga_name")
  55. .setOrganizationUrl("p_orga_url")
  56. .setTermsConditionsUrl("p_t_and_c_url");
  57. private static final Release RELEASE = new Release(PLUGIN, version("1.0")).setDate(parseDate("2015-04-16"))
  58. .setDownloadUrl("http://toto.com/file.jar")
  59. .setDescription("release description")
  60. .setChangelogUrl("http://change.org/plugin");
  61. private WsTester.TestResponse response = new WsTester.TestResponse();
  62. private JsonWriter jsonWriter = response.newJsonWriter();
  63. private PluginWSCommons underTest = new PluginWSCommons();
  64. @Test
  65. public void verify_properties_written_by_writePluginMetadata() {
  66. underTest.writePluginMetadata(jsonWriter, GIT_PLUGIN_METADATA);
  67. jsonWriter.close();
  68. assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" +
  69. " \"key\": \"scmgit\"," +
  70. " \"name\": \"Git\"," +
  71. " \"description\": \"Git SCM Provider.\"," +
  72. " \"version\": \"1.0\"," +
  73. " \"license\": \"GNU LGPL 3\"," +
  74. " \"organizationName\": \"SonarSource\"," +
  75. " \"organizationUrl\": \"http://www.sonarsource.com\"," +
  76. " \"homepage\": \"http://redirect.sonarsource.com/plugins/scmgit.html\"," +
  77. " \"issueTrackerUrl\": \"http://jira.codehaus.org/browse/SONARSCGIT\"" +
  78. "}");
  79. }
  80. @Test
  81. public void verify_properties_written_by_writeMetadata() {
  82. jsonWriter.beginObject();
  83. underTest.writeMetadata(jsonWriter, GIT_PLUGIN_METADATA);
  84. jsonWriter.endObject();
  85. jsonWriter.close();
  86. assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" +
  87. " \"key\": \"scmgit\"," +
  88. " \"name\": \"Git\"," +
  89. " \"description\": \"Git SCM Provider.\"," +
  90. " \"version\": \"1.0\"," +
  91. " \"license\": \"GNU LGPL 3\"," +
  92. " \"organizationName\": \"SonarSource\"," +
  93. " \"organizationUrl\": \"http://www.sonarsource.com\"," +
  94. " \"homepage\": \"http://redirect.sonarsource.com/plugins/scmgit.html\"," +
  95. " \"issueTrackerUrl\": \"http://jira.codehaus.org/browse/SONARSCGIT\"," +
  96. "}");
  97. }
  98. @Test
  99. public void verify_properties_written_by_writePluginUpdate() {
  100. underTest.writePluginUpdate(jsonWriter, PluginUpdate.createForPluginRelease(RELEASE, version("1.0")));
  101. jsonWriter.close();
  102. assertJson(response.outputAsString()).isSimilarTo("{" +
  103. " \"key\": \"p_key\"," +
  104. " \"name\": \"p_name\"," +
  105. " \"description\": \"p_description\"," +
  106. " \"category\": \"p_category\"," +
  107. " \"license\": \"p_license\"," +
  108. " \"organizationName\": \"p_orga_name\"," +
  109. " \"organizationUrl\": \"p_orga_url\"," +
  110. " \"termsAndConditionsUrl\": \"p_t_and_c_url\"" +
  111. " \"release\": {" +
  112. " \"version\": \"1.0\"," +
  113. " \"date\": \"2015-04-16\"" +
  114. " }" +
  115. "}");
  116. }
  117. @Test
  118. public void verify_properties_written_by_writeMetadata_from_plugin() {
  119. jsonWriter.beginObject();
  120. underTest.writeMetadata(jsonWriter, PLUGIN);
  121. jsonWriter.endObject();
  122. jsonWriter.close();
  123. assertJson(response.outputAsString()).isSimilarTo("{" +
  124. " \"key\": \"p_key\"," +
  125. " \"name\": \"p_name\"," +
  126. " \"description\": \"p_description\"," +
  127. " \"category\": \"p_category\"," +
  128. " \"license\": \"p_license\"," +
  129. " \"organizationName\": \"p_orga_name\"," +
  130. " \"organizationUrl\": \"p_orga_url\"," +
  131. " \"termsAndConditionsUrl\": \"p_t_and_c_url\"" +
  132. "}");
  133. }
  134. @Test
  135. public void writeRelease() {
  136. jsonWriter.beginObject();
  137. underTest.writeRelease(jsonWriter, RELEASE);
  138. jsonWriter.endObject();
  139. jsonWriter.close();
  140. assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" +
  141. " \"release\": {" +
  142. " \"version\": \"1.0\"," +
  143. " \"date\": \"2015-04-16\"," +
  144. " \"description\": \"release description\"," +
  145. " \"changeLogUrl\": \"http://change.org/plugin\"" +
  146. " }" +
  147. "}");
  148. }
  149. @Test
  150. public void writeArtifact_from_release_writes_artifact_object_and_file_name() {
  151. jsonWriter.beginObject();
  152. underTest.writeArtifact(jsonWriter, release("p_key").setDownloadUrl("http://toto.com/file.jar"));
  153. jsonWriter.endObject();
  154. jsonWriter.close();
  155. assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" +
  156. " \"artifact\": {" +
  157. " \"name\": \"file.jar\"," +
  158. " \"url\": \"http://toto.com/file.jar\"" +
  159. " }" +
  160. "}");
  161. }
  162. @Test
  163. public void status_COMPATIBLE_is_displayed_COMPATIBLE_in_JSON() {
  164. assertThat(toJSon(COMPATIBLE)).isEqualTo("COMPATIBLE");
  165. }
  166. @Test
  167. public void status_INCOMPATIBLE_is_displayed_INCOMPATIBLE_in_JSON() {
  168. assertThat(toJSon(INCOMPATIBLE)).isEqualTo("INCOMPATIBLE");
  169. }
  170. @Test
  171. public void status_REQUIRE_SONAR_UPGRADE_is_displayed_REQUIRES_UPGRADE_in_JSON() {
  172. assertThat(toJSon(REQUIRE_SONAR_UPGRADE)).isEqualTo("REQUIRES_SYSTEM_UPGRADE");
  173. }
  174. @Test
  175. public void status_DEPENDENCIES_REQUIRE_SONAR_UPGRADE_is_displayed_DEPS_REQUIRE_SYSTEM_UPGRADE_in_JSON() {
  176. assertThat(toJSon(DEPENDENCIES_REQUIRE_SONAR_UPGRADE)).isEqualTo("DEPS_REQUIRE_SYSTEM_UPGRADE");
  177. }
  178. @Test
  179. public void writeUpdate_renders_key_name_and_description_of_outgoing_dependencies() {
  180. PluginUpdate pluginUpdate = new PluginUpdate();
  181. pluginUpdate.setRelease(
  182. new Release(PLUGIN, version("1.0")).addOutgoingDependency(RELEASE)
  183. );
  184. jsonWriter.beginObject();
  185. underTest.writeUpdate(jsonWriter, pluginUpdate);
  186. jsonWriter.endObject();
  187. jsonWriter.close();
  188. assertJson(response.outputAsString()).isSimilarTo("{" +
  189. " \"update\": {" +
  190. " \"requires\": [" +
  191. " {" +
  192. " \"key\": \"p_key\"," +
  193. " \"name\": \"p_name\"," +
  194. " \"description\": \"p_description\"" +
  195. " }" +
  196. " ]" +
  197. " }" +
  198. "}");
  199. }
  200. private static Version version(String version) {
  201. return Version.create(version);
  202. }
  203. private static Release release(String key) {
  204. return new Release(new Plugin(key), version("1.0"));
  205. }
  206. }