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.

ProjectBindingsServiceTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.common.projectbindings.service;
  21. import java.util.List;
  22. import java.util.Optional;
  23. import java.util.Set;
  24. import org.junit.jupiter.api.BeforeEach;
  25. import org.junit.jupiter.api.Test;
  26. import org.junit.jupiter.api.extension.ExtendWith;
  27. import org.mockito.Answers;
  28. import org.mockito.ArgumentCaptor;
  29. import org.mockito.Captor;
  30. import org.mockito.InjectMocks;
  31. import org.mockito.Mock;
  32. import org.mockito.junit.jupiter.MockitoExtension;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbSession;
  35. import org.sonar.db.alm.setting.ProjectAlmSettingDto;
  36. import org.sonar.db.alm.setting.ProjectAlmSettingQuery;
  37. import org.sonar.db.project.ProjectDto;
  38. import org.sonar.server.common.SearchResults;
  39. import static org.assertj.core.api.Assertions.assertThat;
  40. import static org.mockito.ArgumentMatchers.any;
  41. import static org.mockito.ArgumentMatchers.anyInt;
  42. import static org.mockito.ArgumentMatchers.eq;
  43. import static org.mockito.Mockito.mock;
  44. import static org.mockito.Mockito.never;
  45. import static org.mockito.Mockito.verify;
  46. import static org.mockito.Mockito.when;
  47. @ExtendWith(MockitoExtension.class)
  48. public class ProjectBindingsServiceTest {
  49. private static final String UUID = "uuid";
  50. public static final String REPO_QUERY = "repoQuery";
  51. public static final String ALM_SETTING_UUID_QUERY = "almSettingUuidQuery";
  52. @Mock
  53. private DbSession dbSession;
  54. @Mock(answer = Answers.RETURNS_DEEP_STUBS)
  55. private DbClient dbClient;
  56. @InjectMocks
  57. private ProjectBindingsService underTest;
  58. @Captor
  59. private ArgumentCaptor<ProjectAlmSettingQuery> daoQueryCaptor;
  60. @BeforeEach
  61. void setup() {
  62. when(dbClient.openSession(false)).thenReturn(dbSession);
  63. }
  64. @Test
  65. void findProjectBindingByUuid_whenNoResult_returnsOptionalEmpty() {
  66. when(dbClient.projectAlmSettingDao().selectByUuid(dbSession, UUID)).thenReturn(Optional.empty());
  67. assertThat(underTest.findProjectBindingByUuid(UUID)).isEmpty();
  68. }
  69. @Test
  70. void findProjectBindingByUuid_whenResult_returnsIt() {
  71. ProjectAlmSettingDto projectAlmSettingDto = mock(ProjectAlmSettingDto.class);
  72. when(dbClient.projectAlmSettingDao().selectByUuid(dbSession, UUID)).thenReturn(Optional.of(projectAlmSettingDto));
  73. assertThat(underTest.findProjectBindingByUuid(UUID)).contains(projectAlmSettingDto);
  74. }
  75. @Test
  76. void findProjectBindingsByRequest_whenResults_returnsThem() {
  77. ProjectAlmSettingDto projectAlmSettingDto1 = mockProjectAlmSettingDto("1");
  78. ProjectAlmSettingDto projectAlmSettingDto2 = mockProjectAlmSettingDto("2");
  79. List<ProjectAlmSettingDto> projectAlmSettings = List.of(projectAlmSettingDto1, projectAlmSettingDto2);
  80. when(dbClient.projectAlmSettingDao().selectProjectAlmSettings(eq(dbSession), daoQueryCaptor.capture(), eq(12), eq(42)))
  81. .thenReturn(projectAlmSettings);
  82. when(dbClient.projectAlmSettingDao().countProjectAlmSettings(eq(dbSession), any()))
  83. .thenReturn(projectAlmSettings.size());
  84. ProjectDto mockProjectDto1 = mockProjectDto("1");
  85. ProjectDto mockProjectDto2 = mockProjectDto("2");
  86. when(dbClient.projectDao().selectByUuids(dbSession, Set.of("projectUuid_1", "projectUuid_2")))
  87. .thenReturn(List.of(mockProjectDto1, mockProjectDto2));
  88. ProjectBindingsSearchRequest request = new ProjectBindingsSearchRequest(REPO_QUERY, ALM_SETTING_UUID_QUERY, 12, 42);
  89. List<ProjectBindingInformation> expectedResults = List.of(projectBindingInformation("1"), projectBindingInformation("2"));
  90. SearchResults<ProjectBindingInformation> actualResults = underTest.findProjectBindingsByRequest(request);
  91. assertThat(daoQueryCaptor.getValue().repository()).isEqualTo(REPO_QUERY);
  92. assertThat(daoQueryCaptor.getValue().almSettingUuid()).isEqualTo(ALM_SETTING_UUID_QUERY);
  93. assertThat(actualResults.total()).isEqualTo(projectAlmSettings.size());
  94. assertThat(actualResults.searchResults()).containsExactlyInAnyOrderElementsOf(expectedResults);
  95. }
  96. @Test
  97. void findProjectBindingsByRequest_whenPageSize0_returnsOnlyTotal() {
  98. when(dbClient.projectAlmSettingDao().countProjectAlmSettings(eq(dbSession), any()))
  99. .thenReturn(12);
  100. ProjectBindingsSearchRequest request = new ProjectBindingsSearchRequest(null, null, 42, 0);
  101. SearchResults<ProjectBindingInformation> actualResults = underTest.findProjectBindingsByRequest(request);
  102. assertThat(actualResults.total()).isEqualTo(12);
  103. assertThat(actualResults.searchResults()).isEmpty();
  104. verify(dbClient.projectAlmSettingDao(), never()).selectProjectAlmSettings(eq(dbSession), any(), anyInt(), anyInt());
  105. }
  106. private static ProjectAlmSettingDto mockProjectAlmSettingDto(String i) {
  107. ProjectAlmSettingDto dto = mock();
  108. when(dto.getUuid()).thenReturn("uuid_" + i);
  109. when(dto.getAlmSettingUuid()).thenReturn("almSettingUuid_" + i);
  110. when(dto.getProjectUuid()).thenReturn("projectUuid_" + i);
  111. when(dto.getAlmRepo()).thenReturn("almRepo_" + i);
  112. when(dto.getAlmSlug()).thenReturn("almSlug_" + i);
  113. return dto;
  114. }
  115. private static ProjectDto mockProjectDto(String i) {
  116. ProjectDto dto = mock();
  117. when(dto.getUuid()).thenReturn("projectUuid_" + i);
  118. when(dto.getKey()).thenReturn("projectKey_" + i);
  119. return dto;
  120. }
  121. private static ProjectBindingInformation projectBindingInformation(String i) {
  122. return new ProjectBindingInformation("uuid_" + i,
  123. "almSettingUuid_" + i,
  124. "projectUuid_" + i,
  125. "projectKey_" + i,
  126. "almRepo_" + i,
  127. "almSlug_" + i);
  128. }
  129. }