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.

OrganizationQualityGateTest.java 4.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 com.sonar.orchestrator.build.SonarScanner;
  23. import java.util.Map;
  24. import org.junit.ClassRule;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonarqube.qa.util.Tester;
  28. import org.sonarqube.ws.Organizations.Organization;
  29. import org.sonarqube.ws.Projects.CreateWsResponse.Project;
  30. import org.sonarqube.ws.Qualitygates;
  31. import org.sonarqube.ws.Users;
  32. import org.sonarqube.ws.client.GetRequest;
  33. import org.sonarqube.ws.client.WsResponse;
  34. import org.sonarqube.ws.client.qualitygates.CreateConditionRequest;
  35. import util.ItUtils;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static util.ItUtils.projectDir;
  38. public class OrganizationQualityGateTest {
  39. @ClassRule
  40. public static Orchestrator orchestrator = OrganizationQualityGateSuite.ORCHESTRATOR;
  41. @Rule
  42. public Tester tester = new Tester(orchestrator);
  43. @Test
  44. public void always_display_current_quality_gate_in_effect() throws Exception {
  45. Organization organization = tester.organizations().generate();
  46. Project project = tester.projects().provision(organization);
  47. Qualitygates.CreateResponse qualityGate = tester.qGates().generate();
  48. tester.qGates().associateProject(qualityGate, project);
  49. tester.wsClient().qualitygates().createCondition(new CreateConditionRequest()
  50. .setGateId(String.valueOf(qualityGate.getId()))
  51. .setMetric("new_coverage")
  52. .setOp("LT")
  53. .setWarning("90")
  54. .setError("80")
  55. .setPeriod("1"));
  56. tester.settings().setProjectSetting(project.getKey(), "sonar.leak.period", "previous_version");
  57. String password = "password1";
  58. Users.CreateWsResponse.User user = tester.users().generateAdministrator(organization, u -> u.setPassword(password));
  59. WsResponse response = tester.wsClient().wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", project.getKey()));
  60. Map currentQualityGate = (Map) ItUtils.jsonToMap(response.content()).get("qualityGate");
  61. assertThat((long) (double) (Double) currentQualityGate.get("key")).isEqualTo(qualityGate.getId());
  62. orchestrator.executeBuild(
  63. SonarScanner.create(projectDir("shared/xoo-sample"))
  64. .setProperty("sonar.organization", organization.getKey())
  65. .setProjectKey(project.getKey())
  66. .setProjectName(project.getName())
  67. .setProperty("sonar.login", user.getLogin())
  68. .setProperty("sonar.password", password)
  69. .setDebugLogs(true));
  70. WsResponse response2 = tester.wsClient().wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", project.getKey()));
  71. Map currentQualityGate2 = (Map) ItUtils.jsonToMap(response2.content()).get("qualityGate");
  72. assertThat((long) (double) (Double) currentQualityGate2.get("key")).isEqualTo(qualityGate.getId());
  73. Qualitygates.CreateResponse qualityGate2 = tester.qGates().generate();
  74. tester.qGates().associateProject(qualityGate2, project);
  75. tester.wsClient().qualitygates().createCondition(new CreateConditionRequest()
  76. .setGateId(String.valueOf(qualityGate2.getId()))
  77. .setMetric("new_coverage")
  78. .setOp("LT")
  79. .setWarning("90")
  80. .setError("80")
  81. .setPeriod("1"));
  82. WsResponse response3 = tester.wsClient().wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", project.getKey()));
  83. Map currentQualityGate3 = (Map) ItUtils.jsonToMap(response3.content()).get("qualityGate");
  84. assertThat((long) (double) (Double) currentQualityGate3.get("key")).isEqualTo(qualityGate2.getId());
  85. }
  86. }