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.

ImportHelperIT.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.almintegration.ws;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.api.impl.ws.SimpleGetRequest;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.db.DbTester;
  27. import org.sonar.db.alm.setting.ALM;
  28. import org.sonar.db.alm.setting.AlmSettingDto;
  29. import org.sonar.db.component.ProjectTesting;
  30. import org.sonar.db.project.ProjectDto;
  31. import org.sonar.server.exceptions.NotFoundException;
  32. import org.sonar.server.exceptions.UnauthorizedException;
  33. import org.sonar.server.tester.UserSessionRule;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  36. import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
  37. import static org.sonarqube.ws.Projects.CreateWsResponse;
  38. public class ImportHelperIT {
  39. // We use a GitHub ALM just because we have to use one. Tests are not specific to GitHub.
  40. private static final ALM GITHUB_ALM = ALM.GITHUB;
  41. private final System2 system2 = System2.INSTANCE;
  42. private final ProjectDto projectDto = ProjectTesting.newPublicProjectDto();
  43. private final Request emptyRequest = new SimpleGetRequest();
  44. @Rule
  45. public final DbTester db = DbTester.create(system2);
  46. @Rule
  47. public UserSessionRule userSession = UserSessionRule.standalone();
  48. private final ImportHelper underTest = new ImportHelper(db.getDbClient(), userSession);
  49. @Test
  50. public void checkProvisionProjectPermission_whenNoPermissions_shouldThrow() {
  51. assertThatThrownBy(underTest::checkProvisionProjectPermission)
  52. .isInstanceOf(UnauthorizedException.class)
  53. .hasMessage("Authentication is required");
  54. }
  55. @Test
  56. public void getAlmConfig_whenNoAlmSettingKeyAndNoConfig_shouldThrow() {
  57. assertThatThrownBy(() -> underTest.getAlmSettingDtoForAlm(emptyRequest, GITHUB_ALM))
  58. .isInstanceOf(NotFoundException.class)
  59. .hasMessage("There is no GITHUB configuration for DevOps Platform. Please add one.");
  60. }
  61. @Test
  62. public void getAlmConfig_whenNoAlmSettingKeyAndOnlyOneConfig_shouldReturnConfig() {
  63. AlmSettingDto githubAlmSettingDto = db.almSettings().insertGitHubAlmSetting();
  64. AlmSettingDto almSettingDto = underTest.getAlmSettingDtoForAlm(emptyRequest, GITHUB_ALM);
  65. assertThat(almSettingDto).usingRecursiveComparison().isEqualTo(githubAlmSettingDto);
  66. }
  67. @Test
  68. public void getAlmConfig_whenNoAlmSettingKeyAndMultipleConfigs_shouldThrow() {
  69. db.almSettings().insertGitHubAlmSetting();
  70. db.almSettings().insertGitHubAlmSetting();
  71. assertThatThrownBy(() -> underTest.getAlmSettingDtoForAlm(emptyRequest, GITHUB_ALM))
  72. .isInstanceOf(IllegalArgumentException.class)
  73. .hasMessage("Parameter almSetting is required as there are multiple DevOps Platform configurations.");
  74. }
  75. @Test
  76. public void getAlmConfig_whenAlmSettingKeyProvidedButDoesNotExist_shouldThrow() {
  77. Request request = new SimpleGetRequest()
  78. .setParam(PARAM_ALM_SETTING, "key");
  79. assertThatThrownBy(() -> underTest.getAlmSettingDtoForAlm(request, GITHUB_ALM))
  80. .isInstanceOf(NotFoundException.class)
  81. .hasMessage("DevOps Platform configuration 'key' not found.");
  82. }
  83. @Test
  84. public void getAlmConfig_whenConfigExists_shouldReturnConfig(){
  85. AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
  86. Request request = new SimpleGetRequest()
  87. .setParam(PARAM_ALM_SETTING, almSettingDto.getKey());
  88. AlmSettingDto result = underTest.getAlmSettingDtoForAlm(request, GITHUB_ALM);
  89. assertThat(result.getUuid()).isEqualTo(almSettingDto.getUuid());
  90. }
  91. @Test
  92. public void getUserUuid_whenUserUuidNull_shouldThrow() {
  93. assertThatThrownBy(underTest::getUserUuid)
  94. .isInstanceOf(NullPointerException.class)
  95. .hasMessage("User UUID cannot be null.");
  96. }
  97. @Test
  98. public void toCreateResponse_shouldReturnProjectResponse() {
  99. CreateWsResponse response = ImportHelper.toCreateResponse(projectDto);
  100. CreateWsResponse.Project project = response.getProject();
  101. assertThat(project).extracting(CreateWsResponse.Project::getKey, CreateWsResponse.Project::getName,
  102. CreateWsResponse.Project::getQualifier)
  103. .containsExactly(projectDto.getKey(), projectDto.getName(), projectDto.getQualifier());
  104. }
  105. }