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.

CreateActionTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.qualitygate.ws;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Collection;
  25. import java.util.Optional;
  26. import javax.annotation.Nullable;
  27. import org.junit.Before;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.sonar.api.measures.Metric;
  32. import org.sonar.api.server.ws.WebService;
  33. import org.sonar.api.utils.System2;
  34. import org.sonar.core.util.UuidFactoryFast;
  35. import org.sonar.db.DbClient;
  36. import org.sonar.db.DbSession;
  37. import org.sonar.db.DbTester;
  38. import org.sonar.db.metric.MetricDto;
  39. import org.sonar.db.qualitygate.QualityGateConditionDto;
  40. import org.sonar.db.qualitygate.QualityGateDto;
  41. import org.sonar.server.exceptions.ForbiddenException;
  42. import org.sonar.server.qualitygate.QualityGateConditionsUpdater;
  43. import org.sonar.server.qualitygate.QualityGateUpdater;
  44. import org.sonar.server.tester.UserSessionRule;
  45. import org.sonar.server.ws.TestRequest;
  46. import org.sonar.server.ws.WsActionTester;
  47. import org.sonarqube.ws.Qualitygates.CreateResponse;
  48. import static org.assertj.core.api.Assertions.assertThat;
  49. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  50. import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
  51. import static org.sonar.server.qualitygate.QualityGateCaycChecker.CAYC_METRICS;
  52. import static org.sonar.server.qualitygate.ws.CreateAction.DEFAULT_METRIC_VALUES;
  53. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
  54. @RunWith(DataProviderRunner.class)
  55. public class CreateActionTest {
  56. @Rule
  57. public UserSessionRule userSession = UserSessionRule.standalone();
  58. @Rule
  59. public DbTester db = DbTester.create(System2.INSTANCE);
  60. private final DbClient dbClient = db.getDbClient();
  61. private final DbSession dbSession = db.getSession();
  62. private final CreateAction underTest = new CreateAction(dbClient, userSession, new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance()),
  63. new QualityGateConditionsUpdater(dbClient));
  64. private final WsActionTester ws = new WsActionTester(underTest);
  65. @Before
  66. public void setup() {
  67. CAYC_METRICS.forEach(this::insertMetric);
  68. }
  69. @Test
  70. public void create_quality_gate_with_cayc_conditions() {
  71. logInAsQualityGateAdmin();
  72. String qgName = "Default";
  73. CreateResponse response = executeRequest(qgName);
  74. assertThat(response.getName()).isEqualTo(qgName);
  75. dbSession.commit();
  76. QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByName(dbSession, qgName);
  77. assertThat(qualityGateDto).isNotNull();
  78. var conditions = getConditions(dbSession, qualityGateDto);
  79. CAYC_METRICS.stream()
  80. .map(m -> dbClient.metricDao().selectByKey(dbSession, m.getKey()))
  81. .forEach(metricDto -> assertThat(conditions)
  82. .anyMatch(c -> metricDto.getUuid().equals(c.getMetricUuid()) && c.getErrorThreshold().equals(String.valueOf(getDefaultCaycValue(metricDto)))));
  83. }
  84. @Test
  85. public void test_ws_definition() {
  86. WebService.Action action = ws.getDef();
  87. assertThat(action).isNotNull();
  88. assertThat(action.isInternal()).isFalse();
  89. assertThat(action.isPost()).isTrue();
  90. assertThat(action.responseExampleAsString()).isNotEmpty();
  91. assertThat(action.params()).hasSize(1);
  92. }
  93. @Test
  94. public void throw_ForbiddenException_if_not_gate_administrator() {
  95. userSession.logIn();
  96. assertThatThrownBy(() -> executeRequest("Default"))
  97. .isInstanceOf(ForbiddenException.class)
  98. .hasMessageContaining("Insufficient privileges");
  99. }
  100. @Test
  101. public void throw_BadRequestException_if_name_is_already_used() {
  102. userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES);
  103. executeRequest("Default");
  104. assertThatThrownBy(() -> executeRequest("Default"))
  105. .isInstanceOf(IllegalArgumentException.class)
  106. .hasMessageContaining("Name has already been taken");
  107. }
  108. @Test
  109. @UseDataProvider("nullOrEmpty")
  110. public void fail_when_name_parameter_is_empty(@Nullable String nameParameter) {
  111. userSession.addPermission(ADMINISTER_QUALITY_GATES);
  112. TestRequest request = ws.newRequest();
  113. Optional.ofNullable(nameParameter).ifPresent(t -> request.setParam(PARAM_NAME, ""));
  114. assertThatThrownBy(() -> request.execute())
  115. .isInstanceOf(IllegalArgumentException.class)
  116. .hasMessageContaining("The 'name' parameter is missing");
  117. }
  118. @DataProvider
  119. public static Object[][] nullOrEmpty() {
  120. return new Object[][] {
  121. {null},
  122. {""},
  123. {" "}
  124. };
  125. }
  126. private Collection<QualityGateConditionDto> getConditions(DbSession dbSession, QualityGateDto qualityGate) {
  127. return dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGate.getUuid());
  128. }
  129. private CreateResponse executeRequest(String qualitGateName) {
  130. return ws.newRequest()
  131. .setParam("name", qualitGateName)
  132. .executeProtobuf(CreateResponse.class);
  133. }
  134. private void logInAsQualityGateAdmin() {
  135. userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES);
  136. }
  137. private void insertMetric(Metric metric) {
  138. db.measures().insertMetric(m -> m
  139. .setKey(metric.getKey())
  140. .setValueType(metric.getType().name())
  141. .setHidden(metric.isHidden())
  142. .setDirection(metric.getDirection()));
  143. }
  144. private Integer getDefaultCaycValue(MetricDto metricDto) {
  145. return DEFAULT_METRIC_VALUES.containsKey(metricDto.getKey())
  146. ? DEFAULT_METRIC_VALUES.get(metricDto.getKey())
  147. : CAYC_METRICS.stream()
  148. .filter(metric -> metricDto.getKey().equals(metric.getKey()))
  149. .findAny()
  150. .orElseThrow()
  151. .getBestValue().intValue();
  152. }
  153. }