blob: e13f8bb07d555cf90ca78c29cc737a261fd8db85 (
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
* Copyright (C) 2009-2014 SonarSource SA
* All rights reserved
* mailto:contact AT sonarsource DOT com
*/
package qualitygate;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarRunner;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.wsclient.project.NewProject;
import org.sonar.wsclient.qualitygate.NewCondition;
import org.sonar.wsclient.qualitygate.QualityGate;
import org.sonar.wsclient.qualitygate.QualityGateClient;
import org.sonar.wsclient.services.Measure;
import org.sonar.wsclient.services.Resource;
import org.sonar.wsclient.services.ResourceQuery;
import util.ItUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.projectDir;
public class QualityGateTest {
private static final String PROJECT_KEY = "sample";
private long provisionnedProjectId = -1L;
@ClassRule
public static Orchestrator orchestrator = Orchestrator.builderEnv()
.addPlugin(ItUtils.xooPlugin())
// 1 second. Required for notification test.
.setServerProperty("sonar.notifications.delay", "1")
.build();
@Before
public void cleanUp() throws Exception {
orchestrator.resetData();
provisionnedProjectId = Long.parseLong(orchestrator.getServer().adminWsClient().projectClient().create(NewProject.create().key(PROJECT_KEY).name("Sample")).id());
}
@Test
public void do_not_compute_status_if_no_gate() {
SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
orchestrator.executeBuild(build);
assertThat(fetchResourceWithGateStatus()).isNull();
}
@Test
public void status_ok_if_empty_gate() {
QualityGate empty = qgClient().create("Empty");
qgClient().setDefault(empty.id());
try {
SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
orchestrator.executeBuild(build);
assertThat(fetchGateStatus().getData()).isEqualTo("OK");
} finally {
qgClient().unsetDefault();
qgClient().destroy(empty.id());
}
}
@Test
public void test_status_ok() {
QualityGate simple = qgClient().create("SimpleWithHighThreshold");
qgClient().setDefault(simple.id());
qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").warningThreshold("40"));
try {
SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
orchestrator.executeBuild(build);
assertThat(fetchGateStatus().getData()).isEqualTo("OK");
} finally {
qgClient().unsetDefault();
qgClient().destroy(simple.id());
}
}
@Test
public void test_status_warning() {
QualityGate simple = qgClient().create("SimpleWithLowThreshold");
qgClient().setDefault(simple.id());
qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").warningThreshold("10"));
try {
SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
orchestrator.executeBuild(build);
assertThat(fetchGateStatus().getData()).isEqualTo("WARN");
} finally {
qgClient().unsetDefault();
qgClient().destroy(simple.id());
}
}
@Test
public void test_status_error() {
QualityGate simple = qgClient().create("SimpleWithLowThreshold");
qgClient().setDefault(simple.id());
qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").errorThreshold("10"));
try {
SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
orchestrator.executeBuild(build);
assertThat(fetchGateStatus().getData()).isEqualTo("ERROR");
} finally {
qgClient().unsetDefault();
qgClient().destroy(simple.id());
}
}
@Test
public void use_server_settings_instead_of_default_gate() {
QualityGate alert = qgClient().create("AlertWithLowThreshold");
qgClient().createCondition(NewCondition.create(alert.id()).metricKey("ncloc").operator("GT").warningThreshold("10"));
QualityGate error = qgClient().create("ErrorWithLowThreshold");
qgClient().createCondition(NewCondition.create(error.id()).metricKey("ncloc").operator("GT").errorThreshold("10"));
qgClient().setDefault(alert.id());
qgClient().selectProject(error.id(), provisionnedProjectId);
try {
SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
orchestrator.executeBuild(build);
assertThat(fetchGateStatus().getData()).isEqualTo("ERROR");
} finally {
qgClient().unsetDefault();
qgClient().destroy(alert.id());
qgClient().destroy(error.id());
}
}
@Test
public void conditions_on_multiple_metric_types() {
QualityGate allTypes = qgClient().create("AllMetricTypes");
qgClient().createCondition(NewCondition.create(allTypes.id()).metricKey("ncloc").operator("GT").warningThreshold("10"));
qgClient().createCondition(NewCondition.create(allTypes.id()).metricKey("duplicated_lines_density").operator("GT").warningThreshold("20"));
qgClient().setDefault(allTypes.id());
try {
SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"))
.setProperty("sonar.cpd.xoo.minimumLines", "2")
.setProperty("sonar.cpd.xoo.minimumTokens", "5");
orchestrator.executeBuild(build);
Measure alertStatus = fetchGateStatus();
assertThat(alertStatus.getData()).isEqualTo("WARN");
assertThat(alertStatus.getAlertText())
.contains("Lines of code > 10")
.contains("Duplicated lines (%) > 20");
} finally {
qgClient().unsetDefault();
qgClient().destroy(allTypes.id());
}
}
private Measure fetchGateStatus() {
return fetchResourceWithGateStatus().getMeasure("alert_status");
}
private Resource fetchResourceWithGateStatus() {
return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT_KEY, "alert_status").setIncludeAlerts(true));
}
private static QualityGateClient qgClient() {
return orchestrator.getServer().adminWsClient().qualityGateClient();
}
}
|