import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY;
import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY;
+import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY;
import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY;
import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
.put(SQALE_RATING_KEY, "maintainability")
.put(ALERT_STATUS_KEY, "quality gate")
.put(RELIABILITY_RATING_KEY, "reliability")
+ .put(SECURITY_HOTSPOTS_KEY, "security hotspots")
.put(SECURITY_RATING_KEY, "security")
.put(TECHNICAL_DEBT_KEY, "technical debt")
.put(VULNERABILITIES_KEY, "vulnerabilities")
.build();
- private static final Map<Level, String> QUALITY_GATE_MESSAGE_BY_STATUS = new EnumMap<>(ImmutableMap.of(
+ private static final Map<Level, String> QUALITY_GATE_MESSAGE_BY_STATUS = new EnumMap<>(Map.of(
OK, "passed",
WARN, "warning",
ERROR, "failed"));
- private static final Map<Level, Color> COLOR_BY_QUALITY_GATE_STATUS = new EnumMap<>(ImmutableMap.of(
+ private static final Map<Level, Color> COLOR_BY_QUALITY_GATE_STATUS = new EnumMap<>(Map.of(
OK, Color.QUALITY_GATE_OK,
WARN, Color.QUALITY_GATE_WARN,
ERROR, Color.QUALITY_GATE_ERROR));
- private static final Map<Rating, Color> COLOR_BY_RATING = new EnumMap<>(ImmutableMap.of(
+ private static final Map<Rating, Color> COLOR_BY_RATING = new EnumMap<>(Map.of(
A, Color.RATING_A,
B, Color.RATING_B,
C, Color.RATING_C,
return svgGenerator.generateBadge(METRIC_NAME_BY_KEY.get(metric.getKey()), value, color);
}
- private static <PARAM> PARAM getNonNullValue(LiveMeasureDto measure, Function<LiveMeasureDto, PARAM> function) {
- PARAM value = function.apply(measure);
+ private static <P> P getNonNullValue(LiveMeasureDto measure, Function<LiveMeasureDto, P> function) {
+ P value = function.apply(measure);
checkState(value != null, "Measure has not been found");
return value;
}
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.measure.Rating;
import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
import static org.sonar.api.measures.CoreMetrics.COVERAGE_KEY;
+import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY;
import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
import static org.sonar.api.measures.Metric.Level.ERROR;
new SvgGenerator()));
@Before
- public void before(){
+ public void before() {
mapSettings.setProperty(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, false);
}
public void int_measure() {
ComponentDto project = db.components().insertPublicProject();
userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
- db.measures().insertLiveMeasure(project, metric, m -> m.setValue(10_000d));
+ MetricDto metric = createIntMetricAndMeasure(project, BUGS_KEY, 10_000);
TestResponse response = ws.newRequest()
.setParam("project", project.getKey())
checkWithIfNoneMatchHeader(project, metric, response);
}
+ @Test
+ public void security_hotspots() {
+ ComponentDto project = db.components().insertPublicProject();
+ userSession.registerComponents(project);
+ MetricDto metric = createIntMetricAndMeasure(project, SECURITY_HOTSPOTS_KEY, 42);
+
+ TestResponse response = ws.newRequest()
+ .setParam("project", project.getKey())
+ .setParam("metric", metric.getKey())
+ .execute();
+
+ checkSvg(response, "security hotspots", "42", DEFAULT);
+
+ // Second call with If-None-Match must return 304
+ checkWithIfNoneMatchHeader(project, metric, response);
+
+ }
+
@Test
public void display_deprecated_warning_quality_gate() {
ComponentDto project = db.components().insertPublicProject();
public void measure_on_non_main_branch() {
ComponentDto project = db.components().insertPublicProject(p -> p.setPrivate(false));
userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
- db.measures().insertLiveMeasure(project, metric, m -> m.setValue(5_000d));
+ MetricDto metric = createIntMetricAndMeasure(project, BUGS_KEY, 5_000);
+
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH));
db.measures().insertLiveMeasure(branch, metric, m -> m.setValue(10_000d));
public void measure_on_application() {
ComponentDto application = db.components().insertPublicApplication();
userSession.registerComponents(application);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
- db.measures().insertLiveMeasure(application, metric, m -> m.setValue(10_000d));
+ MetricDto metric = createIntMetricAndMeasure(application, BUGS_KEY, 10_000);
TestResponse response = ws.newRequest()
.setParam("project", application.getKey())
}
@DataProvider
- public static Object[][] publicProject_forceAuth_accessGranted(){
+ public static Object[][] publicProject_forceAuth_accessGranted() {
return new Object[][] {
// public project, force auth : works depending on token's validity
{true, true, true, true},
@Test
@UseDataProvider("publicProject_forceAuth_accessGranted")
public void badge_accessible_on_private_project_with_token(boolean publicProject, boolean forceAuth,
- boolean validToken, boolean accessGranted) throws ParseException {
+ boolean validToken, boolean accessGranted) throws ParseException {
ComponentDto projectAsComponent = publicProject ? db.components().insertPublicProject() : db.components().insertPrivateProject();
userSession.registerComponents(projectAsComponent);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
+ MetricDto metric = createIntMetricAndMeasure(projectAsComponent, BUGS_KEY, 10_000);
- db.measures().insertLiveMeasure(projectAsComponent, metric, m -> m.setValue(10_000d));
ProjectDto project = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), projectAsComponent.getKey())
.orElseThrow(() -> new IllegalStateException("project not found"));
.setParam("token", validToken ? token : "invalid-token")
.execute();
- if(accessGranted){
+ if (accessGranted) {
checkSvg(response, "bugs", "10k", DEFAULT);
- }else{
+ } else {
checkError(response, "Project has not been found");
}
}
MetricDto metric = createQualityGateMetric();
db.measures().insertLiveMeasure(project, metric, m -> m.setData("UNKNOWN"));
+ TestRequest request = ws.newRequest()
+ .setParam("project", project.getKey())
+ .setParam("metric", metric.getKey());
assertThatThrownBy(() -> {
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
+ request
.execute();
})
.isInstanceOf(IllegalArgumentException.class)
MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
db.measures().insertLiveMeasure(project, metric, m -> m.setValue(null));
+ TestRequest request = ws.newRequest()
+ .setParam("project", project.getKey())
+ .setParam("metric", metric.getKey());
+
assertThatThrownBy(() -> {
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
+ request
.execute();
})
.isInstanceOf(IllegalStateException.class)
ComponentDto project = db.components().insertPublicProject();
userSession.registerComponents(project);
+ TestRequest request = ws.newRequest()
+ .setParam("project", project.getKey())
+ .setParam("metric", BUGS_KEY);
+
assertThatThrownBy(() -> {
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", BUGS_KEY)
+ request
.execute();
})
.isInstanceOf(IllegalStateException.class)
private MetricDto createQualityGateMetric() {
return db.measures().insertMetric(m -> m.setKey(CoreMetrics.ALERT_STATUS_KEY).setValueType(LEVEL.name()));
}
+
+ private MetricDto createIntMetricAndMeasure(ComponentDto project, String key, Integer value) {
+ MetricDto metric = db.measures().insertMetric(m -> m.setKey(key).setValueType(INT.name()));
+ db.measures().insertLiveMeasure(project, metric, m -> m.setValue(value.doubleValue()));
+ return metric;
+ }
}