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.

QProfileTester.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.sonarqube.ws.tester;
  21. import java.util.List;
  22. import java.util.concurrent.atomic.AtomicInteger;
  23. import java.util.function.Consumer;
  24. import javax.annotation.Nullable;
  25. import org.sonarqube.ws.Projects.CreateWsResponse.Project;
  26. import org.sonarqube.ws.Qualityprofiles;
  27. import org.sonarqube.ws.Qualityprofiles.CreateWsResponse.QualityProfile;
  28. import org.sonarqube.ws.client.qualityprofiles.ActivateRuleRequest;
  29. import org.sonarqube.ws.client.qualityprofiles.AddProjectRequest;
  30. import org.sonarqube.ws.client.qualityprofiles.CreateRequest;
  31. import org.sonarqube.ws.client.qualityprofiles.DeactivateRuleRequest;
  32. import org.sonarqube.ws.client.qualityprofiles.DeleteRequest;
  33. import org.sonarqube.ws.client.qualityprofiles.QualityprofilesService;
  34. import org.sonarqube.ws.client.qualityprofiles.SearchRequest;
  35. import static java.util.Arrays.stream;
  36. public class QProfileTester {
  37. private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
  38. private final TesterSession session;
  39. QProfileTester(TesterSession session) {
  40. this.session = session;
  41. }
  42. public QualityprofilesService service() {
  43. return session.wsClient().qualityprofiles();
  44. }
  45. void deleteAll() {
  46. List<Qualityprofiles.SearchWsResponse.QualityProfile> qualityProfiles = session.wsClient().qualityprofiles().search(new SearchRequest()).getProfilesList().stream()
  47. .filter(qp -> !qp.getIsDefault())
  48. .filter(qp -> !qp.getIsBuiltIn())
  49. .filter(qp -> qp.getParentKey() == null || qp.getParentKey().equals(""))
  50. .toList();
  51. qualityProfiles.forEach(
  52. qp -> session.wsClient().qualityprofiles().delete(new DeleteRequest().setQualityProfile(qp.getName()).setLanguage(qp.getLanguage())));
  53. }
  54. @SafeVarargs
  55. public final QualityProfile createXooProfile(Consumer<CreateRequest>... populators) {
  56. CreateRequest request = new CreateRequest()
  57. .setLanguage("xoo")
  58. .setName(generateName());
  59. stream(populators).forEach(p -> p.accept(request));
  60. return service().create(request).getProfile();
  61. }
  62. public QProfileTester activateRule(QualityProfile profile, String ruleKey) {
  63. return activateRule(profile.getKey(), ruleKey);
  64. }
  65. public QProfileTester activateRule(String profileKey, String ruleKey) {
  66. ActivateRuleRequest request = new ActivateRuleRequest()
  67. .setKey(profileKey)
  68. .setRule(ruleKey);
  69. service().activateRule(request);
  70. return this;
  71. }
  72. public QProfileTester activateRule(QualityProfile profile, String ruleKey, String severity, List<String> params) {
  73. return activateRule(profile.getKey(), ruleKey, severity, params);
  74. }
  75. public QProfileTester activateRule(QualityProfile profile, String ruleKey, String severity) {
  76. return activateRule(profile.getKey(), ruleKey, severity, null);
  77. }
  78. public QProfileTester activateRule(String profileKey, String ruleKey, String severity) {
  79. return activateRule(profileKey, ruleKey, severity, null);
  80. }
  81. public QProfileTester activateRule(String profileKey, String ruleKey, String severity, @Nullable List<String> params) {
  82. service().activateRule(new ActivateRuleRequest()
  83. .setKey(profileKey)
  84. .setRule(ruleKey)
  85. .setSeverity(severity)
  86. .setParams(params));
  87. return this;
  88. }
  89. public QProfileTester deactivateRule(QualityProfile profile, String ruleKey) {
  90. service().deactivateRule(new DeactivateRuleRequest().setKey(profile.getKey()).setRule(ruleKey));
  91. return this;
  92. }
  93. public QProfileTester assignQProfileToProject(QualityProfile profile, Project project) {
  94. return assignQProfileToProject(profile, project.getKey());
  95. }
  96. public QProfileTester assignQProfileToProject(QualityProfile profile, String projectKey) {
  97. return assignQProfileToProject(profile.getName(), profile.getLanguage(), projectKey);
  98. }
  99. public QProfileTester assignQProfileToProject(String profileName, String profileLanguage, String projectKey) {
  100. service().addProject(new AddProjectRequest()
  101. .setProject(projectKey)
  102. .setQualityProfile(profileName)
  103. .setLanguage(profileLanguage));
  104. return this;
  105. }
  106. public String generateName() {
  107. int id = ID_GENERATOR.getAndIncrement();
  108. return "Profile" + id;
  109. }
  110. }