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.

MarketplaceActionTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.ui.ws;
  21. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.runner.RunWith;
  25. import org.sonar.api.platform.Server;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbTester;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.server.exceptions.ForbiddenException;
  31. import org.sonar.server.exceptions.UnauthorizedException;
  32. import org.sonar.server.tester.UserSessionRule;
  33. import org.sonar.server.ws.TestRequest;
  34. import org.sonar.server.ws.WsActionTester;
  35. import org.sonarqube.ws.Navigation;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  38. import static org.mockito.Mockito.mock;
  39. import static org.mockito.Mockito.when;
  40. import static org.sonar.test.JsonAssert.assertJson;
  41. @RunWith(DataProviderRunner.class)
  42. public class MarketplaceActionTest {
  43. @Rule
  44. public UserSessionRule userSessionRule = UserSessionRule.standalone();
  45. @Rule
  46. public DbTester db = DbTester.create();
  47. private final Server server = mock(Server.class);
  48. private final DbClient dbClient = db.getDbClient();
  49. private final MarketplaceAction underTest = new MarketplaceAction(userSessionRule, server, dbClient);
  50. private final WsActionTester ws = new WsActionTester(underTest);
  51. @Test
  52. public void definition() {
  53. WebService.Action def = ws.getDef();
  54. assertThat(def.key()).isEqualTo("marketplace");
  55. assertThat(def.since()).isEqualTo("7.2");
  56. assertThat(def.isPost()).isFalse();
  57. assertThat(def.isInternal()).isTrue();
  58. assertThat(def.description()).isNotEmpty();
  59. assertThat(def.params()).isEmpty();
  60. }
  61. @Test
  62. public void request_fails_if_user_not_logged_in() {
  63. userSessionRule.anonymous();
  64. TestRequest request = ws.newRequest();
  65. assertThatThrownBy(() -> request.execute())
  66. .isInstanceOf(UnauthorizedException.class)
  67. .hasMessageContaining("Authentication is required");
  68. }
  69. @Test
  70. public void request_fails_if_user_is_not_system_administer() {
  71. userSessionRule.logIn();
  72. TestRequest request = ws.newRequest();
  73. assertThatThrownBy(() -> request.execute())
  74. .isInstanceOf(ForbiddenException.class)
  75. .hasMessageContaining("Insufficient privileges");
  76. }
  77. @Test
  78. public void json_example() {
  79. userSessionRule.logIn().setSystemAdministrator();
  80. when(server.getId()).thenReturn("AU-Tpxb--iU5OvuD2FLy");
  81. setNcloc(12345L);
  82. String result = ws.newRequest().execute().getInput();
  83. assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
  84. }
  85. @Test
  86. public void returns_server_id_and_nloc() {
  87. userSessionRule.logIn().setSystemAdministrator();
  88. when(server.getId()).thenReturn("myserver");
  89. long ncloc = 256L;
  90. setNcloc(ncloc);
  91. Navigation.MarketplaceResponse expectedResponse = Navigation.MarketplaceResponse.newBuilder()
  92. .setServerId("myserver")
  93. .setNcloc(ncloc)
  94. .build();
  95. Navigation.MarketplaceResponse result = ws.newRequest().executeProtobuf(Navigation.MarketplaceResponse.class);
  96. assertThat(result).isEqualTo(expectedResponse);
  97. }
  98. private void setNcloc(double ncloc) {
  99. ComponentDto project = db.components().insertPublicProject();
  100. db.getDbClient().projectDao().updateNcloc(db.getSession(), project.uuid(), (long) ncloc);
  101. db.commit();
  102. }
  103. }