aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws/src/testFixtures/java/org/sonarqube/ws/tester/SettingTester.java
blob: 0ddc9f57e8feaf6c9ff6957f7058f1014c4b2651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonarqube.ws.tester;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.sonarqube.ws.Settings;
import org.sonarqube.ws.Settings.Setting;
import org.sonarqube.ws.client.settings.ListDefinitionsRequest;
import org.sonarqube.ws.client.settings.ResetRequest;
import org.sonarqube.ws.client.settings.SetRequest;
import org.sonarqube.ws.client.settings.SettingsService;
import org.sonarqube.ws.client.settings.ValuesRequest;

import static java.util.Arrays.asList;

public class SettingTester {

  private static final Set<String> EMAIL_SETTINGS = Set.of("email.smtp_host.secured", "email.smtp_port.secured", "email.smtp_secure_connection.secured",
    "email.smtp_username.secured", "email.smtp_password.secured", "email.from", "email.prefix");

  private final TesterSession session;

  SettingTester(TesterSession session) {
    this.session = session;
  }

  public SettingsService service() {
    return session.wsClient().settings();
  }

  public void deleteAll() {
    List<String> settingKeys = Stream.concat(
      session.wsClient().settings().listDefinitions(new ListDefinitionsRequest()).getDefinitionsList()
        .stream()
        .filter(def -> def.getType() != Settings.Type.LICENSE)
        .map(Settings.Definition::getKey)
        .filter(key -> !key.equals(Tester.FORCE_AUTHENTICATION_PROPERTY_NAME)),
      EMAIL_SETTINGS.stream())
      .toList();
    session.wsClient().settings().reset(new ResetRequest().setKeys(settingKeys));
  }

  public void resetSettings(String... keys) {
    session.wsClient().settings().reset(new ResetRequest().setKeys(asList(keys)));
  }

  public List<Setting> getGlobalSettings(String... keys) {
    return session.wsClient().settings().values(new ValuesRequest().setKeys(asList(keys)))
        .getSettingsList();
  }

  public void resetProjectSettings(String projectKey, String... keys) {
    session.wsClient().settings().reset(new ResetRequest().setComponent(projectKey).setKeys(asList(keys)));
  }

  public void setGlobalSetting(String key, @Nullable String value) {
    setSetting(null, key, value);
  }

  public void setMultiValuesGlobalSetting(String key, String... values) {
    session.wsClient().settings().set(new SetRequest()
      .setKey(key)
      .setValues(Arrays.asList(values))
      .setComponent(null));
  }

  public void setGlobalSettings(String... keyValues) {
    for (int i = 0; i < keyValues.length; i += 2) {
      setSetting(null, keyValues[i], keyValues[i + 1]);
    }
  }

  public void setProjectSetting(String componentKey, String key, @Nullable String value) {
    setSetting(componentKey, key, value);
  }

  public void setProjectSettings(String componentKey, String... properties) {
    for (int i = 0; i < properties.length; i += 2) {
      setSetting(componentKey, properties[i], properties[i + 1]);
    }
  }

  private void setSetting(@Nullable String componentKey, String key, @Nullable String value) {
    if (value == null) {
      session.wsClient().settings().reset(new ResetRequest().setKeys(asList(key)).setComponent(componentKey));
    } else {
      session.wsClient().settings().set(new SetRequest().setKey(key).setValue(value).setComponent(componentKey));
    }
  }

}