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.

SettingTester.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.sonarqube.ws.tester;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.Set;
  25. import java.util.stream.Collectors;
  26. import java.util.stream.Stream;
  27. import javax.annotation.Nullable;
  28. import org.sonarqube.ws.Settings;
  29. import org.sonarqube.ws.Settings.Setting;
  30. import org.sonarqube.ws.client.settings.ListDefinitionsRequest;
  31. import org.sonarqube.ws.client.settings.ResetRequest;
  32. import org.sonarqube.ws.client.settings.SetRequest;
  33. import org.sonarqube.ws.client.settings.SettingsService;
  34. import org.sonarqube.ws.client.settings.ValuesRequest;
  35. import static java.util.Arrays.asList;
  36. public class SettingTester {
  37. private static final Set<String> EMAIL_SETTINGS = ImmutableSet.of("email.smtp_host.secured", "email.smtp_port.secured", "email.smtp_secure_connection.secured",
  38. "email.smtp_username.secured", "email.smtp_password.secured", "email.from", "email.prefix");
  39. private final TesterSession session;
  40. SettingTester(TesterSession session) {
  41. this.session = session;
  42. }
  43. public SettingsService service() {
  44. return session.wsClient().settings();
  45. }
  46. public void deleteAll() {
  47. List<String> settingKeys = Stream.concat(
  48. session.wsClient().settings().listDefinitions(new ListDefinitionsRequest()).getDefinitionsList()
  49. .stream()
  50. .filter(def -> def.getType() != Settings.Type.LICENSE)
  51. .map(Settings.Definition::getKey)
  52. .filter(key -> !key.equals(Tester.FORCE_AUTHENTICATION_PROPERTY_NAME)),
  53. EMAIL_SETTINGS.stream())
  54. .collect(Collectors.toList());
  55. session.wsClient().settings().reset(new ResetRequest().setKeys(settingKeys));
  56. }
  57. public void resetSettings(String... keys) {
  58. session.wsClient().settings().reset(new ResetRequest().setKeys(asList(keys)));
  59. }
  60. public List<Setting> getGlobalSettings(String... keys) {
  61. return session.wsClient().settings().values(new ValuesRequest().setKeys(asList(keys)))
  62. .getSettingsList();
  63. }
  64. public void resetProjectSettings(String projectKey, String... keys) {
  65. session.wsClient().settings().reset(new ResetRequest().setComponent(projectKey).setKeys(asList(keys)));
  66. }
  67. public void setGlobalSetting(String key, @Nullable String value) {
  68. setSetting(null, key, value);
  69. }
  70. public void setMultiValuesGlobalSetting(String key, String... values) {
  71. session.wsClient().settings().set(new SetRequest()
  72. .setKey(key)
  73. .setValues(Arrays.asList(values))
  74. .setComponent(null));
  75. }
  76. public void setGlobalSettings(String... keyValues) {
  77. for (int i = 0; i < keyValues.length; i += 2) {
  78. setSetting(null, keyValues[i], keyValues[i + 1]);
  79. }
  80. }
  81. public void setProjectSetting(String componentKey, String key, @Nullable String value) {
  82. setSetting(componentKey, key, value);
  83. }
  84. public void setProjectSettings(String componentKey, String... properties) {
  85. for (int i = 0; i < properties.length; i += 2) {
  86. setSetting(componentKey, properties[i], properties[i + 1]);
  87. }
  88. }
  89. private void setSetting(@Nullable String componentKey, String key, @Nullable String value) {
  90. if (value == null) {
  91. session.wsClient().settings().reset(new ResetRequest().setKeys(asList(key)).setComponent(componentKey));
  92. } else {
  93. session.wsClient().settings().set(new SetRequest().setKey(key).setValue(value).setComponent(componentKey));
  94. }
  95. }
  96. }