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.

DefaultDopSettingsControllerTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.v2.api.dop.controller;
  21. import com.google.gson.Gson;
  22. import java.util.List;
  23. import org.junit.Rule;
  24. import org.junit.jupiter.api.BeforeEach;
  25. import org.junit.jupiter.api.Test;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.alm.setting.AlmSettingDto;
  29. import org.sonar.server.tester.UserSessionRule;
  30. import org.sonar.server.v2.api.ControllerTester;
  31. import org.sonar.server.v2.api.dop.response.DopSettingsResource;
  32. import org.sonar.server.v2.api.dop.response.DopSettingsRestResponse;
  33. import org.springframework.test.web.servlet.MockMvc;
  34. import org.springframework.test.web.servlet.MvcResult;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.when;
  39. import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
  40. import static org.sonar.server.v2.WebApiEndpoints.DOP_SETTINGS_ENDPOINT;
  41. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  42. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  43. class DefaultDopSettingsControllerTest {
  44. @Rule
  45. public UserSessionRule userSession = UserSessionRule.standalone();
  46. private final DbClient dbClient = mock(DbClient.class, RETURNS_DEEP_STUBS);
  47. private final DbSession dbSession = mock();
  48. private final MockMvc mockMvc = ControllerTester.getMockMvc(
  49. new DefaultDopSettingsController(userSession, dbClient));
  50. private static final Gson gson = new Gson();
  51. @BeforeEach
  52. void setup() {
  53. when(dbClient.openSession(false)).thenReturn(dbSession);
  54. }
  55. @Test
  56. void fetchAllDopSettings_whenUserDoesntHaveCreateProjectPermission_returnsForbidden() throws Exception {
  57. userSession.logIn();
  58. mockMvc
  59. .perform(get(DOP_SETTINGS_ENDPOINT))
  60. .andExpect(status().isForbidden());
  61. }
  62. @Test
  63. void fetchAllDopSettings_whenDbClientReturnsData_returnsResponse() throws Exception {
  64. List<AlmSettingDto> dopSettings = List.of(
  65. generateAlmSettingsDto("github"),
  66. generateAlmSettingsDto("azure"),
  67. generateAlmSettingsDto("bitbucket_cloud")
  68. );
  69. when(dbClient.almSettingDao().selectAll(dbSession)).thenReturn(dopSettings);
  70. userSession.logIn().addPermission(PROVISION_PROJECTS);;
  71. MvcResult mvcResult = mockMvc
  72. .perform(get(DOP_SETTINGS_ENDPOINT))
  73. .andExpect(status().isOk())
  74. .andReturn();
  75. DopSettingsRestResponse response = gson.fromJson(mvcResult.getResponse().getContentAsString(), DopSettingsRestResponse.class);
  76. assertThat(response.dopSettings())
  77. .containsExactlyInAnyOrderElementsOf(dopSettings.stream().map(DefaultDopSettingsControllerTest::toDopSettingsResource).toList());
  78. }
  79. private static DopSettingsResource toDopSettingsResource(AlmSettingDto almSettingDto) {
  80. return new DopSettingsResource(
  81. almSettingDto.getUuid(),
  82. almSettingDto.getRawAlm(),
  83. almSettingDto.getKey(),
  84. almSettingDto.getUrl(),
  85. almSettingDto.getAppId()
  86. );
  87. }
  88. private AlmSettingDto generateAlmSettingsDto(String dopType) {
  89. AlmSettingDto dto = mock();
  90. when(dto.getUuid()).thenReturn("uuid_" + dopType);
  91. when(dto.getRawAlm()).thenReturn(dopType);
  92. when(dto.getKey()).thenReturn("key_" + dopType);
  93. when(dto.getUrl()).thenReturn("url_" + dopType);
  94. when(dto.getAppId()).thenReturn("appId_" + dopType);
  95. return dto;
  96. }
  97. }