-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestBase.java
More file actions
107 lines (94 loc) · 4.05 KB
/
TestBase.java
File metadata and controls
107 lines (94 loc) · 4.05 KB
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
/*
* SonarSource Scala
* Copyright (C) 2018-2025 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 Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* 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 Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonarsource.slang;
import com.sonar.orchestrator.junit4.OrchestratorRule;
import com.sonar.orchestrator.build.SonarScanner;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.junit.ClassRule;
import org.sonarqube.ws.Issues;
import org.sonarqube.ws.Measures.ComponentWsResponse;
import org.sonarqube.ws.Measures.Measure;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import org.sonarqube.ws.client.issues.SearchRequest;
import org.sonarqube.ws.client.measures.ComponentRequest;
import static java.util.Collections.singletonList;
public abstract class TestBase {
@ClassRule
public static final OrchestratorRule ORCHESTRATOR = Tests.ORCHESTRATOR;
protected SonarScanner getSonarScanner(String projectKey, String directoryToScan, String languageKey) {
return getSonarScanner(projectKey, directoryToScan, languageKey, null);
}
protected SonarScanner getSonarScanner(String projectKey, String directoryToScan, String languageKey, @Nullable String profileName) {
ORCHESTRATOR.getServer().provisionProject(projectKey, projectKey);
if (profileName != null) {
ORCHESTRATOR.getServer().associateProjectToQualityProfile(projectKey, languageKey, profileName);
}
return SonarScanner.create()
.setProjectDir(new File(directoryToScan, languageKey))
.setProjectKey(projectKey)
.setProjectName(projectKey)
.setProjectVersion("1")
.setSourceDirs(".");
}
protected Measure getMeasure(String projectKey, String metricKey) {
return getMeasure(projectKey, null, metricKey);
}
protected Measure getMeasure(String projectKey, @Nullable String componentKey, String metricKey) {
String component;
if (componentKey != null) {
component = projectKey + ":" + componentKey;
} else {
component = projectKey;
}
ComponentWsResponse response = newWsClient().measures().component(new ComponentRequest()
.setComponent(component)
.setMetricKeys(singletonList(metricKey)));
List<Measure> measures = response.getComponent().getMeasuresList();
return measures.size() == 1 ? measures.get(0) : null;
}
protected Map<String, Measure> getMeasures(String projectKey, String... metricKeys) {
return newWsClient().measures().component(new ComponentRequest()
.setComponent(projectKey)
.setMetricKeys(Arrays.asList(metricKeys)))
.getComponent().getMeasuresList()
.stream()
.collect(Collectors.toMap(Measure::getMetric, Function.identity()));
}
protected List<Issues.Issue> getIssuesForRule(String componentKey, String rule) {
return newWsClient().issues().search(new SearchRequest()
.setRules(Collections.singletonList(rule))
.setComponentKeys(Collections.singletonList(componentKey))).getIssuesList();
}
protected Integer getMeasureAsInt(String componentKey, String metricKey) {
Measure measure = getMeasure(componentKey, metricKey);
return (measure == null) ? null : Integer.parseInt(measure.getValue());
}
protected static WsClient newWsClient() {
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
.url(ORCHESTRATOR.getServer().getUrl())
.build());
}
}