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

QualityGateOnRatingMeasuresTest.java 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2017 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.sonarqube.tests.qualityGate;
  21. import com.sonar.orchestrator.Orchestrator;
  22. import org.junit.ClassRule;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonarqube.qa.util.Tester;
  26. import org.sonarqube.ws.Measures;
  27. import org.sonarqube.ws.Projects.CreateWsResponse.Project;
  28. import org.sonarqube.ws.Qualitygates;
  29. import org.sonarqube.ws.client.qualitygates.CreateConditionRequest;
  30. import util.ItUtils;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static util.ItUtils.getMeasure;
  33. import static util.ItUtils.runProjectAnalysis;
  34. public class QualityGateOnRatingMeasuresTest {
  35. @ClassRule
  36. public static Orchestrator orchestrator = QualityGateSuite.ORCHESTRATOR;
  37. @Rule
  38. public Tester tester = new Tester(orchestrator)
  39. // all the tests of QualityGateSuite must disable organizations
  40. .disableOrganizations();
  41. @Test
  42. public void generate_warning_qgate_on_rating_metric() throws Exception {
  43. Project project = tester.projects().provision();
  44. Qualitygates.CreateResponse qualityGate = tester.qGates().generate();
  45. tester.qGates().associateProject(qualityGate, project);
  46. tester.qGates().service().createCondition(new CreateConditionRequest()
  47. .setGateId(String.valueOf(qualityGate.getId()))
  48. .setMetric("security_rating")
  49. .setOp("GT")
  50. .setWarning("3"));
  51. ItUtils.restoreProfile(orchestrator, getClass().getResource("/qualityGate/QualityGateOnRatingMeasuresTest/with-many-rules.xml"));
  52. orchestrator.getServer().associateProjectToQualityProfile(project.getKey(), "xoo", "with-many-rules");
  53. runProjectAnalysis(orchestrator, "qualitygate/xoo-sample", "sonar.projectKey", project.getKey());
  54. assertThat(getGateStatusMeasure(project).getValue()).isEqualTo("WARN");
  55. }
  56. @Test
  57. public void generate_error_qgate_on_rating_metric_on_leak_period() throws Exception {
  58. Project project = tester.projects().provision();
  59. Qualitygates.CreateResponse qualityGate = tester.qGates().generate();
  60. tester.qGates().associateProject(qualityGate, project);
  61. tester.settings().setGlobalSetting("sonar.leak.period", "previous_version");
  62. tester.wsClient().qualitygates().createCondition(new CreateConditionRequest()
  63. .setGateId(String.valueOf(qualityGate.getId()))
  64. .setMetric("new_security_rating")
  65. .setOp("GT")
  66. .setError("3")
  67. .setPeriod("1"));
  68. // Run first analysis with empty quality gate -> quality gate is green
  69. orchestrator.getServer().associateProjectToQualityProfile(project.getKey(), "xoo", "empty");
  70. runProjectAnalysis(orchestrator, "qualitygate/xoo-sample", "sonar.projectKey", project.getKey());
  71. assertThat(getGateStatusMeasure(project).getValue()).isEqualTo("OK");
  72. // Run second analysis with some rules that makes Security Rating to E -> quality gate is red
  73. ItUtils.restoreProfile(orchestrator, getClass().getResource("/qualityGate/QualityGateOnRatingMeasuresTest/with-many-rules.xml"));
  74. orchestrator.getServer().associateProjectToQualityProfile(project.getKey(), "xoo", "with-many-rules");
  75. runProjectAnalysis(orchestrator, "qualitygate/xoo-sample", "sonar.projectKey", project.getKey());
  76. assertThat(getGateStatusMeasure(project).getValue()).isEqualTo("ERROR");
  77. }
  78. private Measures.Measure getGateStatusMeasure(Project project) {
  79. return getMeasure(orchestrator, project.getKey(), "alert_status");
  80. }
  81. }