Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

QualityGateServiceImplTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.ce.task.projectanalysis.qualitygate;
  21. import com.google.common.collect.ImmutableList;
  22. import java.util.Collections;
  23. import java.util.Optional;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.sonar.ce.task.projectanalysis.analysis.Organization;
  27. import org.sonar.ce.task.projectanalysis.metric.Metric;
  28. import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.qualitygate.QGateWithOrgDto;
  31. import org.sonar.db.qualitygate.QualityGateConditionDao;
  32. import org.sonar.db.qualitygate.QualityGateConditionDto;
  33. import org.sonar.db.qualitygate.QualityGateDao;
  34. import org.sonar.db.qualitygate.QualityGateDto;
  35. import org.sonar.server.project.Project;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.mockito.ArgumentMatchers.any;
  38. import static org.mockito.ArgumentMatchers.eq;
  39. import static org.mockito.Mockito.mock;
  40. import static org.mockito.Mockito.when;
  41. public class QualityGateServiceImplTest {
  42. private static final long SOME_ID = 123;
  43. private static final String SOME_NAME = "some name";
  44. private static final QualityGateDto QUALITY_GATE_DTO = new QualityGateDto().setId(SOME_ID).setName(SOME_NAME);
  45. private static final long METRIC_ID_1 = 951;
  46. private static final long METRIC_ID_2 = 753;
  47. private static final Metric METRIC_1 = mock(Metric.class);
  48. private static final Metric METRIC_2 = mock(Metric.class);
  49. private static final QualityGateConditionDto CONDITION_1 = new QualityGateConditionDto().setId(321).setMetricId(METRIC_ID_1).setOperator("LT")
  50. .setErrorThreshold("error_th");
  51. private static final QualityGateConditionDto CONDITION_2 = new QualityGateConditionDto().setId(456).setMetricId(METRIC_ID_2).setOperator("GT").setErrorThreshold("error_th");
  52. private QualityGateDao qualityGateDao = mock(QualityGateDao.class);
  53. private QualityGateConditionDao qualityGateConditionDao = mock(QualityGateConditionDao.class);
  54. private MetricRepository metricRepository = mock(MetricRepository.class);
  55. private DbClient dbClient = mock(DbClient.class);
  56. private QualityGateServiceImpl underTest = new QualityGateServiceImpl(dbClient, metricRepository);
  57. @Before
  58. public void setUp() {
  59. when(dbClient.qualityGateDao()).thenReturn(qualityGateDao);
  60. when(dbClient.gateConditionDao()).thenReturn(qualityGateConditionDao);
  61. when(METRIC_1.getKey()).thenReturn("metric");
  62. when(METRIC_2.getKey()).thenReturn("new_metric");
  63. }
  64. @Test
  65. public void findById_returns_absent_when_QualityGateDto_does_not_exist() {
  66. assertThat(underTest.findById(SOME_ID)).isNotPresent();
  67. }
  68. @Test
  69. public void findById_returns_QualityGate_with_empty_set_of_conditions_when_there_is_none_in_DB() {
  70. when(qualityGateDao.selectById(any(), eq(SOME_ID))).thenReturn(QUALITY_GATE_DTO);
  71. when(qualityGateConditionDao.selectForQualityGate(any(), eq(SOME_ID))).thenReturn(Collections.emptyList());
  72. Optional<QualityGate> res = underTest.findById(SOME_ID);
  73. assertThat(res).isPresent();
  74. assertThat(res.get().getId()).isEqualTo(SOME_ID);
  75. assertThat(res.get().getName()).isEqualTo(SOME_NAME);
  76. assertThat(res.get().getConditions()).isEmpty();
  77. }
  78. @Test
  79. public void findById_returns_conditions_when_there_is_some_in_DB() {
  80. when(qualityGateDao.selectById(any(), eq(SOME_ID))).thenReturn(QUALITY_GATE_DTO);
  81. when(qualityGateConditionDao.selectForQualityGate(any(), eq(SOME_ID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2));
  82. // metrics are always supposed to be there
  83. when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(Optional.of(METRIC_1));
  84. when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(Optional.of(METRIC_2));
  85. Optional<QualityGate> res = underTest.findById(SOME_ID);
  86. assertThat(res).isPresent();
  87. assertThat(res.get().getId()).isEqualTo(SOME_ID);
  88. assertThat(res.get().getName()).isEqualTo(SOME_NAME);
  89. assertThat(res.get().getConditions()).containsOnly(
  90. new Condition(METRIC_1, CONDITION_1.getOperator(), CONDITION_1.getErrorThreshold()),
  91. new Condition(METRIC_2, CONDITION_2.getOperator(), CONDITION_2.getErrorThreshold()));
  92. }
  93. @Test
  94. public void findById_ignores_conditions_on_missing_metrics() {
  95. when(qualityGateDao.selectById(any(), eq(SOME_ID))).thenReturn(QUALITY_GATE_DTO);
  96. when(qualityGateConditionDao.selectForQualityGate(any(), eq(SOME_ID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2));
  97. // metrics are always supposed to be there
  98. when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(Optional.empty());
  99. when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(Optional.of(METRIC_2));
  100. Optional<QualityGate> res = underTest.findById(SOME_ID);
  101. assertThat(res).isPresent();
  102. assertThat(res.get().getId()).isEqualTo(SOME_ID);
  103. assertThat(res.get().getName()).isEqualTo(SOME_NAME);
  104. assertThat(res.get().getConditions()).containsOnly(
  105. new Condition(METRIC_2, CONDITION_2.getOperator(), CONDITION_2.getErrorThreshold()));
  106. }
  107. @Test(expected = IllegalStateException.class)
  108. public void findDefaultQualityGate_by_organization_not_found() {
  109. when(qualityGateDao.selectByOrganizationAndUuid(any(), any(), any())).thenReturn(null);
  110. underTest.findDefaultQualityGate(mock(Organization.class));
  111. }
  112. @Test
  113. public void findDefaultQualityGate_by_organization_found() {
  114. QGateWithOrgDto qGateWithOrgDto = new QGateWithOrgDto();
  115. qGateWithOrgDto.setId(QUALITY_GATE_DTO.getId());
  116. qGateWithOrgDto.setName(QUALITY_GATE_DTO.getName());
  117. when(qualityGateDao.selectByOrganizationAndUuid(any(), any(), any())).thenReturn(qGateWithOrgDto);
  118. when(qualityGateConditionDao.selectForQualityGate(any(), eq(SOME_ID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2));
  119. when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(Optional.empty());
  120. when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(Optional.of(METRIC_2));
  121. QualityGate result = underTest.findDefaultQualityGate(mock(Organization.class));
  122. assertThat(result).isNotNull();
  123. assertThat(result.getId()).isEqualTo(QUALITY_GATE_DTO.getId());
  124. assertThat(result.getName()).isNotBlank();
  125. assertThat(result.getName()).isEqualTo(QUALITY_GATE_DTO.getName());
  126. }
  127. @Test
  128. public void findQualityGate_by_project_not_found() {
  129. when(qualityGateDao.selectByProjectUuid(any(), any())).thenReturn(null);
  130. Optional<QualityGate> result = underTest.findQualityGate(mock(Project.class));
  131. assertThat(result).isEmpty();
  132. }
  133. @Test
  134. public void findQualityGate_by_project_found() {
  135. QGateWithOrgDto qGateWithOrgDto = new QGateWithOrgDto();
  136. qGateWithOrgDto.setId(QUALITY_GATE_DTO.getId());
  137. qGateWithOrgDto.setName(QUALITY_GATE_DTO.getName());
  138. when(qualityGateDao.selectByProjectUuid(any(), any())).thenReturn(qGateWithOrgDto);
  139. when(qualityGateConditionDao.selectForQualityGate(any(), eq(SOME_ID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2));
  140. when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(Optional.empty());
  141. when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(Optional.of(METRIC_2));
  142. Optional<QualityGate> result = underTest.findQualityGate(mock(Project.class));
  143. assertThat(result).isNotNull();
  144. assertThat(result).isNotEmpty();
  145. QualityGate resultData = result.get();
  146. assertThat(resultData.getId()).isEqualTo(QUALITY_GATE_DTO.getId());
  147. assertThat(resultData.getName()).isNotBlank();
  148. assertThat(resultData.getName()).isEqualTo(QUALITY_GATE_DTO.getName());
  149. }
  150. }