Skip to content

Commit 04640a9

Browse files
julienlancelotvilchik-elena
authored andcommitted
Remove usage of internal api/rules/app WS (SonarSource#85)
Replace usage of api/rules/app by api/qualityprofiles/search Replace of old ws client by latest one
1 parent ea07b04 commit 04640a9

3 files changed

Lines changed: 51 additions & 35 deletions

File tree

its/plugin/src/test/java/com/sonar/python/it/plugin/PylintReportTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
import com.sonar.orchestrator.Orchestrator;
2323
import com.sonar.orchestrator.build.BuildResult;
2424
import com.sonar.orchestrator.build.SonarScanner;
25-
import org.junit.ClassRule;
26-
import org.junit.Test;
27-
import org.sonar.wsclient.SonarClient;
28-
import org.sonar.wsclient.issue.Issue;
29-
import org.sonar.wsclient.issue.IssueQuery;
30-
3125
import java.io.File;
3226
import java.util.List;
27+
import org.junit.ClassRule;
28+
import org.junit.Test;
29+
import org.sonarqube.ws.Issues.Issue;
30+
import org.sonarqube.ws.client.issue.SearchWsRequest;
3331

32+
import static com.sonar.python.it.plugin.Tests.newWsClient;
33+
import static java.util.Collections.singletonList;
3434
import static org.assertj.core.api.Assertions.assertThat;
3535

3636
public class PylintReportTest {
@@ -40,8 +40,6 @@ public class PylintReportTest {
4040
@ClassRule
4141
public static Orchestrator orchestrator = Tests.ORCHESTRATOR;
4242

43-
private SonarClient wsClient = orchestrator.getServer().wsClient();
44-
4543
@Test
4644
public void import_report() throws Exception {
4745
analyseProjectWithReport("pylint-report.txt");
@@ -69,7 +67,7 @@ public void unknown_rule() throws Exception {
6967
}
7068

7169
private List<Issue> issues() {
72-
return wsClient.issueClient().find(IssueQuery.create().componentRoots(PROJECT)).list();
70+
return newWsClient().issues().search(new SearchWsRequest().setProjects(singletonList(PROJECT))).getIssuesList();
7371
}
7472

7573
private BuildResult analyseProjectWithReport(String reportPath) {

its/plugin/src/test/java/com/sonar/python/it/plugin/Tests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.function.Function;
3030
import java.util.stream.Collectors;
3131
import javax.annotation.CheckForNull;
32+
import javax.annotation.Nullable;
3233
import org.junit.ClassRule;
3334
import org.junit.runner.RunWith;
3435
import org.junit.runners.Suite;
@@ -39,6 +40,8 @@
3940
import org.sonarqube.ws.client.WsClientFactories;
4041
import org.sonarqube.ws.client.measure.ComponentWsRequest;
4142

43+
import static com.sonar.orchestrator.container.Server.ADMIN_LOGIN;
44+
import static com.sonar.orchestrator.container.Server.ADMIN_PASSWORD;
4245
import static java.lang.Double.parseDouble;
4346
import static java.util.Collections.singletonList;
4447
import static org.assertj.core.api.Assertions.assertThat;
@@ -108,9 +111,18 @@ static Double getMeasureAsDouble(String componentKey, String metricKey) {
108111
return (measure == null) ? null : parseDouble(measure.getValue());
109112
}
110113

111-
private static WsClient newWsClient() {
114+
protected static WsClient newWsClient() {
115+
return newWsClient(null, null);
116+
}
117+
118+
protected static WsClient newAdminWsClient() {
119+
return newWsClient(ADMIN_LOGIN, ADMIN_PASSWORD);
120+
}
121+
122+
protected static WsClient newWsClient(@Nullable String login, @Nullable String password) {
112123
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
113124
.url(ORCHESTRATOR.getServer().getUrl())
125+
.credentials(login, password)
114126
.build());
115127
}
116128

its/plugin/src/test/java/com/sonar/python/it/plugin/XPathRuleTest.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@
1919
*/
2020
package com.sonar.python.it.plugin;
2121

22-
import com.google.common.collect.ImmutableMap;
2322
import com.sonar.orchestrator.Orchestrator;
2423
import com.sonar.orchestrator.build.SonarScanner;
2524
import java.io.File;
2625
import java.util.List;
27-
import java.util.regex.Matcher;
28-
import java.util.regex.Pattern;
2926
import org.junit.Before;
3027
import org.junit.ClassRule;
3128
import org.junit.Test;
32-
import org.sonar.wsclient.SonarClient;
3329
import org.sonar.wsclient.issue.Issue;
3430
import org.sonar.wsclient.issue.IssueQuery;
31+
import org.sonarqube.ws.QualityProfiles;
32+
import org.sonarqube.ws.client.PostRequest;
33+
import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
3534

35+
import static com.sonar.python.it.plugin.Tests.newAdminWsClient;
36+
import static com.sonar.python.it.plugin.Tests.newWsClient;
37+
import static java.lang.String.format;
3638
import static org.assertj.core.api.Assertions.assertThat;
3739

3840
public class XPathRuleTest {
@@ -55,27 +57,7 @@ public void resetData() throws Exception {
5557

5658
@Test
5759
public void testXPathRule() {
58-
SonarClient sonarClient = orchestrator.getServer().adminWsClient();
59-
sonarClient.post("/api/rules/create", ImmutableMap.<String, Object>builder()
60-
.put("name", "XPathTestRule")
61-
.put("markdown_description", "XPath test rule")
62-
.put("severity", "INFO")
63-
.put("status", "READY")
64-
.put("template_key", "python:XPath")
65-
.put("custom_key", RULE_KEY)
66-
.put("prevent_reactivation", "true")
67-
.put("params", "message=\"Do something fantastic!\";xpathQuery=\"//FILE_INPUT\"")
68-
.build());
69-
String profiles = sonarClient.get("api/rules/app");
70-
Pattern pattern = Pattern.compile("py-" + PROFILE_NAME + "-\\d+");
71-
Matcher matcher = pattern.matcher(profiles);
72-
assertThat(matcher.find()).isTrue();
73-
String profilekey = matcher.group();
74-
sonarClient.post("api/qualityprofiles/activate_rule", ImmutableMap.<String, Object>of(
75-
"profile_key", profilekey,
76-
"rule_key", RULE_KEY_WITH_PREFIX,
77-
"severity", "INFO",
78-
"params", ""));
60+
createAndActivateRuleFromTemplate();
7961

8062
orchestrator.getServer().provisionProject(PROJECT, PROJECT);
8163
orchestrator.getServer().associateProjectToQualityProfile(PROJECT, "py", PROFILE_NAME);
@@ -100,4 +82,28 @@ private List<Issue> getIssues(String ruleKey) {
10082
return orchestrator.getServer().wsClient().issueClient().find(query).list();
10183
}
10284

85+
private void createAndActivateRuleFromTemplate() {
86+
String language = "py";
87+
newAdminWsClient().wsConnector().call(new PostRequest("api/rules/create")
88+
.setParam("name", "XPathTestRule")
89+
.setParam("markdown_description", "XPath test rule")
90+
.setParam("severity", "INFO")
91+
.setParam("status", "READY")
92+
.setParam("template_key", "python:XPath")
93+
.setParam("custom_key", RULE_KEY)
94+
.setParam("prevent_reactivation", "true")
95+
.setParam("params", "message=\"Do something fantastic!\";xpathQuery=\"//FILE_INPUT\"")).failIfNotSuccessful();
96+
97+
QualityProfiles.SearchWsResponse.QualityProfile qualityProfile = newWsClient().qualityProfiles().search(new SearchWsRequest()).getProfilesList().stream()
98+
.filter(qp -> qp.getLanguage().equals(language))
99+
.filter(qp -> qp.getName().equals(PROFILE_NAME))
100+
.findFirst().orElseThrow(() -> new IllegalStateException(format("Could not find quality profile '%s' for language '%s' ", PROFILE_NAME, language)));
101+
String profileKey = qualityProfile.getKey();
102+
103+
newAdminWsClient().wsConnector().call(new PostRequest("api/qualityprofiles/activate_rule")
104+
.setParam("profile_key", profileKey)
105+
.setParam("rule_key", RULE_KEY_WITH_PREFIX)
106+
.setParam("severity", "INFO")).failIfNotSuccessful();
107+
}
108+
103109
}

0 commit comments

Comments
 (0)