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

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